Improve date formatting
All checks were successful
the build was successful

This commit is contained in:
Peter Stuifzand 2019-03-02 14:38:35 +01:00
parent 66719c58fc
commit e80383911d
2 changed files with 23 additions and 7 deletions

25
main.go
View File

@ -41,10 +41,14 @@ type Revision struct {
}
type Change struct {
Page string
Date time.Time
Body string
Count int
Page string
Date time.Time
EndDate time.Time
Body string
Count int
DateSummary string
TimeSummary string
}
type PagesRepository interface {
@ -429,10 +433,11 @@ func (h *recentHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
i := f
for {
if changes[f].Page == changes[i].Page {
if changes[f].Page == changes[i].Page && changes[f].Date.Truncate(24*time.Hour) == changes[i].Date.Truncate(24*time.Hour) {
changes[f].Count++
i--
} else {
changes[f].EndDate = changes[i+1].Date
f--
changes[f] = changes[i]
}
@ -445,6 +450,16 @@ func (h *recentHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
changes = changes[f:]
}
for i, c := range changes {
if c.Count == 1 {
changes[i].DateSummary = c.Date.Format("Mon, 2 Jan 2006")
changes[i].TimeSummary = c.Date.Format("15:04")
} else {
changes[i].DateSummary = c.Date.Format("Mon, 2 Jan 2006")
changes[i].TimeSummary = fmt.Sprintf("%s - %s", c.Date.Format("15:04"), c.EndDate.Format("15:04"))
}
}
t, err := template.New("layout.html").ParseFiles("templates/layout.html", "templates/recent.html")
if err != nil {
http.Error(w, err.Error(), 500)

View File

@ -7,9 +7,10 @@
<table class="table">
{{ range $index, $change := .Recent }}
<tr>
<td>{{ $change.Date }}</td>
<td><a href="/{{ $change.Page }}">{{ $change.Page }}</a></td>
<td>{{ $change.DateSummary }}</td>
<td>{{ $change.TimeSummary }}</td>
<td>{{ $change.Count }}</td>
<td><a href="/{{ $change.Page }}">{{ $change.Page }}</a></td>
<td>{{ $change.Body }}</td>
</tr>
{{ end }}