From 3cd2f57f9e617f3a3f6caec243d5ecf90c944cc7 Mon Sep 17 00:00:00 2001 From: Ted Unangst Date: Thu, 3 Oct 2019 00:11:02 -0400 Subject: [PATCH] handle durations for times --- activity.go | 9 +++++++++ honk.go | 16 +++++++++++++++- views/honk.html | 1 + web.go | 9 +++++++-- 4 files changed, 32 insertions(+), 3 deletions(-) diff --git a/activity.go b/activity.go index 3b810fb..2998566 100644 --- a/activity.go +++ b/activity.go @@ -717,6 +717,12 @@ func xonksaver(user *WhatAbout, item junk.Junk, origin string) *Honk { if err == nil { t := new(Time) t.StartTime = start + dura, _ := obj.GetString("duration") + if strings.HasPrefix(dura, "PT") { + dura = strings.ToLower(dura[2:]) + d, _ := time.ParseDuration(dura) + t.Duration = Duration(d) + } xonk.Time = t } } @@ -1002,6 +1008,9 @@ func jonkjonk(user *WhatAbout, h *Honk) (junk.Junk, junk.Junk) { } if t := h.Time; t != nil { jo["startTime"] = t.StartTime.Format(time.RFC3339) + if t.Duration != 0 { + jo["duration"] = "PT" + strings.ToUpper(t.Duration.String()) + } } var atts []junk.Junk for _, d := range h.Donks { diff --git a/honk.go b/honk.go index aaa7294..ad9824d 100644 --- a/honk.go +++ b/honk.go @@ -20,6 +20,7 @@ import ( "html/template" "log" "os" + "strings" "time" ) @@ -101,10 +102,23 @@ type Place struct { Url string } +type Duration int64 + +func (d Duration) String() string { + s := time.Duration(d).String() + if strings.HasSuffix(s, "m0s") { + s = s[:len(s)-2] + } + if strings.HasSuffix(s, "h0m") { + s = s[:len(s)-2] + } + return s +} + type Time struct { StartTime time.Time EndTime time.Time - Duration time.Duration + Duration Duration } type Honker struct { diff --git a/views/honk.html b/views/honk.html index 755e7fb..241b30e 100644 --- a/views/honk.html +++ b/views/honk.html @@ -44,6 +44,7 @@ in reply to: {{ .RID }}

{{ .HTML }} {{ with .Time }}

Time: {{ .StartTime.Local.Format "03:04PM EDT Mon Jan 02"}} +{{ if .Duration }}
Duration: {{ .Duration }}{{ end }} {{ end }} {{ with .Place }}

Location: {{ with .Url }}{{ end }}{{ .Name }}{{ if .Url }}{{ end }}{{ if or .Latitude .Longitude }} {{ .Latitude }} {{ .Longitude }}{{ end }} diff --git a/web.go b/web.go index 5881281..9df5666 100644 --- a/web.go +++ b/web.go @@ -1054,8 +1054,8 @@ func submithonk(w http.ResponseWriter, r *http.Request) { if timestart != "" { t := new(Time) now := time.Now().Local() - for _, layout := range []string{"3:04pm", "15:04"} { - start, err := time.Parse(layout, timestart) + for _, layout := range []string{"2006-01-02 3:04pm", "2006-01-02 15:04", "3:04pm", "15:04"} { + start, err := time.ParseInLocation(layout, timestart, now.Location()) if err == nil { if start.Year() == 0 { start = time.Date(now.Year(), now.Month(), now.Day(), start.Hour(), start.Minute(), 0, 0, now.Location()) @@ -1064,6 +1064,11 @@ func submithonk(w http.ResponseWriter, r *http.Request) { break } } + timeend := r.FormValue("timeend") + dur, err := time.ParseDuration(timeend) + if err == nil { + t.Duration = Duration(dur) + } if !t.StartTime.IsZero() { honk.What = "event" honk.Time = t