Skip to content
This repository has been archived by the owner on Apr 7, 2022. It is now read-only.

Commit

Permalink
add relative timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
finzzz committed May 24, 2021
1 parent c7f2adf commit 8f7f00f
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Feature-rich HTTP File Server
- [x] SHA1 checksum
- [x] Command line cheatsheet (curl, wget, PS)
- [ ] Hot reload
- [ ] Relative last modified
- [x] Relative timestamp
- [ ] Regex filtering
- Functionality
- [ ] Send message to backend
Expand Down
85 changes: 85 additions & 0 deletions internal/utils/time.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package utils

// original project : https://github.com/andanhm/go-prettytime

import (
"strconv"
"strings"
"time"
)

// Unix epoch (or Unix time or POSIX time or Unix timestamp) 1 year (365.24 days)
const infinity float64 = 31556926 * 1000

// Handler function which determines the time difference based on defined time spams
func handler(timeIntervalThreshold float64, timePeriod, message string) func(float64) string {
return func(difference float64) string {
var str strings.Builder
n := difference / timeIntervalThreshold
nStr := strconv.FormatFloat(n, 'f', 0, 64)
str.WriteString(nStr)
str.WriteString(" ")
str.WriteString(timePeriod)
if int(n) > 1 {
str.WriteString("s ")
str.WriteString(message)
return str.String()
}
str.WriteString(" ")
str.WriteString(message)
return str.String()
}
}

// timeLapse condition struct
type timeLapse struct {
// Time stamp threshold to handle the time lap condition
Threshold float64
// Handler function which determines the time lapse based on the condition
Handler func(float64) string
}

var timeLapses = []timeLapse{
{Threshold: -31535999, Handler: handler(-31536000, "year", "from now")},
{Threshold: -2591999, Handler: handler(-2592000, "month", "from now")},
{Threshold: -604799, Handler: handler(-604800, "week", "from now")},
{Threshold: -172799, Handler: handler(-86400, "day", "from now")},
{Threshold: -86399, Handler: func(diff float64) string {
return "tomorrow"
}},
{Threshold: -3599, Handler: handler(-3600, "hour", "from now")},
{Threshold: -59, Handler: handler(-60, "minute", "from now")},
{Threshold: -0.9999, Handler: handler(-1, "second", "from now")},
{Threshold: 1, Handler: func(diff float64) string {
return "just now"
}},
{Threshold: 60, Handler: handler(1, "second", "ago")},
{Threshold: 3600, Handler: handler(60, "minute", "ago")},
{Threshold: 86400, Handler: handler(3600, "hour", "ago")},
{Threshold: 172800, Handler: func(diff float64) string {
return "yesterday"
}},
{Threshold: 604800, Handler: handler(86400, "day", "ago")},
{Threshold: 2592000, Handler: handler(604800, "week", "ago")},
{Threshold: 31536000, Handler: handler(2592000, "month", "ago")},
{Threshold: infinity, Handler: handler(31536000, "year", "ago")},
}

//Format returns a string describing how long it has been since the time argument passed int
func RelativeTimeDiff(t time.Time) (timeSince string) {
timestamp := t.Unix()
now := time.Now().Unix()

if timestamp > now || timestamp <= 0 {
timeSince = ""
}

timeElapsed := float64(now - timestamp)
for _, formatter := range timeLapses {
if timeElapsed < formatter.Threshold {
timeSince = formatter.Handler(timeElapsed)
break
}
}
return timeSince
}
7 changes: 4 additions & 3 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ test:
go run cmd/gohfs/main.go

compress:
upx releases/gohfs-linux-amd64
upx releases/gohfs-macos-amd64
upx releases/gohfs-amd64.exe
upx --ultra-brute releases/gohfs-linux-amd64
upx --ultra-brute releases/gohfs-macos-amd64
cp releases/gohfs-amd64.exe releases/gohfs-amd64-packed.exe
upx --ultra-brute releases/gohfs-amd64-packed.exe
6 changes: 3 additions & 3 deletions web/static/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ a:hover {

.footer {
width: 100%;
padding-top: 5px;
padding-top: 15px;
text-align: center;
color: #107d8e;
font-weight: bold;
Expand Down Expand Up @@ -230,7 +230,7 @@ td {

/* UPLOAD MODAL */
.upload {
width: fit-content;
display: table;
margin: auto;
}

Expand All @@ -250,7 +250,7 @@ td {

.qrcode {
margin: 0 auto;
width: fit-content;
display: table;
}

.qrcaption {
Expand Down
2 changes: 1 addition & 1 deletion web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func Embed(cfg *config.Config){
func ParseItem(info fs.FileInfo) Item {
tmp := Item{
Name: url.PathEscape(info.Name()),
ModTime: info.ModTime().Format(time.RFC1123),
ModTime: info.ModTime().Format(time.RFC822) + " (" + utils.RelativeTimeDiff(info.ModTime()) + ")",
RawModTime: info.ModTime().Format(time.RFC3339),
}

Expand Down

0 comments on commit 8f7f00f

Please sign in to comment.