mirror of
https://github.com/riwiwa/muzi.git
synced 2026-02-28 11:56:57 -08:00
format time locally for profile history
This commit is contained in:
@@ -26,7 +26,7 @@
|
|||||||
<tr>
|
<tr>
|
||||||
<td>{{index $artists $index}}</td>
|
<td>{{index $artists $index}}</td>
|
||||||
<td>{{$title}}</td>
|
<td>{{$title}}</td>
|
||||||
<td>{{formatTimestamp (index $times $index)}}</td>
|
<td title="{{formatTimestampFull (index $times $index)}}">{{formatTimestamp (index $times $index)}}</td>
|
||||||
</tr>
|
</tr>
|
||||||
{{end}}
|
{{end}}
|
||||||
</table>
|
</table>
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"time"
|
||||||
|
|
||||||
"muzi/db"
|
"muzi/db"
|
||||||
|
|
||||||
@@ -23,7 +24,7 @@ type ProfileData struct {
|
|||||||
ArtistCount int
|
ArtistCount int
|
||||||
Artists []string
|
Artists []string
|
||||||
Titles []string
|
Titles []string
|
||||||
Times []string
|
Times []time.Time
|
||||||
Page int
|
Page int
|
||||||
Title string
|
Title string
|
||||||
LoggedInUsername string
|
LoggedInUsername string
|
||||||
@@ -103,7 +104,7 @@ func profilePageHandler() http.HandlerFunc {
|
|||||||
}
|
}
|
||||||
profileData.Artists = append(profileData.Artists, artist)
|
profileData.Artists = append(profileData.Artists, artist)
|
||||||
profileData.Titles = append(profileData.Titles, title)
|
profileData.Titles = append(profileData.Titles, title)
|
||||||
profileData.Times = append(profileData.Times, time.Time.String())
|
profileData.Times = append(profileData.Times, time.Time)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = templates.ExecuteTemplate(w, "base", profileData)
|
err = templates.ExecuteTemplate(w, "base", profileData)
|
||||||
|
|||||||
32
web/utils.go
32
web/utils.go
@@ -4,6 +4,7 @@ package web
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Subtracts two integers
|
// Subtracts two integers
|
||||||
@@ -24,3 +25,34 @@ func formatInt(n int) string {
|
|||||||
return formatInt(n/1000) + "," + fmt.Sprintf("%03d", n%1000)
|
return formatInt(n/1000) + "," + fmt.Sprintf("%03d", n%1000)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Formats timestamps compared to local time
|
||||||
|
func formatTimestamp(timestamp time.Time) string {
|
||||||
|
now := time.Now()
|
||||||
|
duration := now.Sub(timestamp)
|
||||||
|
|
||||||
|
if duration < 24*time.Hour {
|
||||||
|
seconds := int(duration.Seconds())
|
||||||
|
if seconds < 60 {
|
||||||
|
return fmt.Sprintf("%d seconds ago", seconds)
|
||||||
|
}
|
||||||
|
minutes := seconds / 60
|
||||||
|
if minutes < 60 {
|
||||||
|
return fmt.Sprintf("%d minutes ago", minutes)
|
||||||
|
}
|
||||||
|
hours := minutes / 60
|
||||||
|
return fmt.Sprintf("%d hours ago", hours)
|
||||||
|
}
|
||||||
|
|
||||||
|
year := now.Year()
|
||||||
|
if timestamp.Year() == year {
|
||||||
|
return timestamp.Format("2 Jan 3:04pm")
|
||||||
|
}
|
||||||
|
|
||||||
|
return timestamp.Format("2 Jan 2006 3:04pm")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Full timestamp format for browser hover
|
||||||
|
func formatTimestampFull(timestamp time.Time) string {
|
||||||
|
return timestamp.Format("Monday 2 Jan 2006, 3:04pm")
|
||||||
|
}
|
||||||
|
|||||||
@@ -27,6 +27,8 @@ func init() {
|
|||||||
"sub": sub,
|
"sub": sub,
|
||||||
"add": add,
|
"add": add,
|
||||||
"formatInt": formatInt,
|
"formatInt": formatInt,
|
||||||
|
"formatTimestamp": formatTimestamp,
|
||||||
|
"formatTimestampFull": formatTimestampFull,
|
||||||
}
|
}
|
||||||
templates = template.Must(template.New("").Funcs(funcMap).ParseGlob("./templates/*.gohtml"))
|
templates = template.Must(template.New("").Funcs(funcMap).ParseGlob("./templates/*.gohtml"))
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user