From aa958b77b16d0f0891e65bb0d3eea24ee0e773ea Mon Sep 17 00:00:00 2001 From: Jasper Bok Date: Sun, 13 Aug 2023 15:01:17 +0200 Subject: [PATCH] Print times in human-readable format --- TODO | 2 +- index.html | 2 +- server.go | 9 ++++++++- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/TODO b/TODO index c1c0ac3..483308c 100644 --- a/TODO +++ b/TODO @@ -2,7 +2,7 @@ ## 1.0.0 -[ ] *.. Print times in human-readable format. +[x] *.. Print times in human-readable format. [ ] *.. Show max two departures per line per stop. [ ] **. Add Connexxion departures. [ ] **. Make the page look nice (also on mobile). diff --git a/index.html b/index.html index f7820dd..7007fbc 100644 --- a/index.html +++ b/index.html @@ -17,7 +17,7 @@ {{ range .Departures }}
{{ .RouteShortName }} | {{ .StopHeadsign }}
-
+
{{ end }} diff --git a/server.go b/server.go index b36d4c0..42a1b22 100644 --- a/server.go +++ b/server.go @@ -4,6 +4,7 @@ import ( "html/template" "log" "net/http" + "time" ) type TemplateQuay struct { @@ -16,6 +17,12 @@ type TemplateContext struct { Quays []TemplateQuay } +func formatTime(t time.Time) string { + return t.Format("15:04") +} + +var tplFunctions = template.FuncMap{"formatTime": formatTime} + func serveHttp() { http.HandleFunc("/", handleIndex) @@ -40,7 +47,7 @@ func handleIndex(w http.ResponseWriter, r *http.Request) { ctxt.Quays = append(ctxt.Quays, TemplateQuay{quay.Name, quay.Town, departures}) } - tpl := template.New("index.html") + tpl := template.New("index.html").Funcs(tplFunctions) tpl, _ = tpl.ParseFiles("index.html") _ = tpl.Execute(w, ctxt)