Print times in human-readable format

main
Jasper Bok 2023-08-13 15:01:17 +02:00
parent c2ad110b7b
commit aa958b77b1
3 changed files with 10 additions and 3 deletions

2
TODO
View File

@ -2,7 +2,7 @@
## 1.0.0 ## 1.0.0
[ ] *.. Print times in human-readable format. [x] *.. Print times in human-readable format.
[ ] *.. Show max two departures per line per stop. [ ] *.. Show max two departures per line per stop.
[ ] **. Add Connexxion departures. [ ] **. Add Connexxion departures.
[ ] **. Make the page look nice (also on mobile). [ ] **. Make the page look nice (also on mobile).

View File

@ -17,7 +17,7 @@
{{ range .Departures }} {{ range .Departures }}
<dl> <dl>
<dt>{{ .RouteShortName }} | {{ .StopHeadsign }}</dt> <dt>{{ .RouteShortName }} | {{ .StopHeadsign }}</dt>
<dd><time>{{ .DepartureTime }}</time></dd> <dd><time>{{ formatTime .DepartureTime }}</time></dd>
</dl> </dl>
{{ end }} {{ end }}
</article> </article>

View File

@ -4,6 +4,7 @@ import (
"html/template" "html/template"
"log" "log"
"net/http" "net/http"
"time"
) )
type TemplateQuay struct { type TemplateQuay struct {
@ -16,6 +17,12 @@ type TemplateContext struct {
Quays []TemplateQuay Quays []TemplateQuay
} }
func formatTime(t time.Time) string {
return t.Format("15:04")
}
var tplFunctions = template.FuncMap{"formatTime": formatTime}
func serveHttp() { func serveHttp() {
http.HandleFunc("/", handleIndex) 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}) 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, _ = tpl.ParseFiles("index.html")
_ = tpl.Execute(w, ctxt) _ = tpl.Execute(w, ctxt)