Refactored timeparsing from HTML input

This commit is contained in:
Magnus Åhall 2024-06-27 13:45:01 +02:00
parent ab87da256c
commit 530204c1a5
6 changed files with 45 additions and 22 deletions

View File

@ -1,6 +1,9 @@
package main
import (
// External
werr "git.gibonuddevalla.se/go/wrappederror"
// Standard
"time"
)
@ -9,3 +12,18 @@ func stringToTime(strTime string) (t time.Time, err error) {// {{{
t, err = time.Parse(time.RFC3339, strTime)
return
}// }}}
func parseHTMLDateTime(str string, dflt time.Time) (t time.Time, err error) {
// Browser sending 2024-06-27T10:43 (16 characters) when seconds is 00.
if len(str) == 16 {
str += ":00"
}
if str == "" {
return dflt, nil
} else {
t, err = time.ParseInLocation("2006-01-02T15:04:05", str, smonConfig.Timezone())
if err != nil {
err = werr.Wrap(err)
}
}
return
}

30
main.go
View File

@ -369,7 +369,7 @@ func getPage(layout, page string) (tmpl *template.Template, err error) { // {{{
funcMap := template.FuncMap{
"format_time": func(t time.Time) template.HTML {
return template.HTML(
t.In(smonConfig.Timezone()).Format(`<span class="date">2006-01-02</span> <span class="time">15:04<span class="seconds">:05</span></span>`),
t.In(smonConfig.Timezone()).Format(`<span class="date">2006-01-02</span> <span class="time">15:04:05<span class="seconds">:05</span></span>`),
)
},
}
@ -709,26 +709,18 @@ func pageDatapointValues(w http.ResponseWriter, r *http.Request, _ *session.T) {
}
var timeFrom, timeTo time.Time
timeFromParam := r.URL.Query().Get("f")
if timeFromParam == "" {
timeFrom = time.Now().Add(time.Duration(-(24 * 7) * time.Hour))
} else {
timeFrom, err = time.Parse("2006-01-02T15:04:05", timeFromParam)
if err != nil {
httpError(w, werr.Wrap(err).Log())
return
}
yesterday := time.Now().Add(time.Duration(-24 * time.Hour))
timeFrom, err = parseHTMLDateTime(r.URL.Query().Get("f"), yesterday)
if err != nil {
httpError(w, werr.Wrap(err).Log())
return
}
timeToParam := r.URL.Query().Get("t")
if timeToParam == "" {
timeTo = time.Now()
} else {
timeTo, err = time.Parse("2006-01-02T15:04:05", timeToParam)
if err != nil {
httpError(w, werr.Wrap(err).Log())
return
}
timeTo, err = parseHTMLDateTime(r.URL.Query().Get("t"), time.Now())
if err != nil {
httpError(w, werr.Wrap(err).Log())
return
}
// Fetch data point values according to the times.

View File

@ -42,6 +42,9 @@
gap: 16px;
white-space: nowrap;
}
#values .header {
font-weight: bold;
}
.widgets {
display: grid;
grid-template-columns: min-content 1fr;

View File

@ -42,6 +42,9 @@
gap: 16px;
white-space: nowrap;
}
#values .header {
font-weight: bold;
}
.widgets {
display: grid;
grid-template-columns: min-content 1fr;

View File

@ -51,6 +51,10 @@
grid-template-columns: repeat(2, min-content);
gap: 16px;
white-space: nowrap;
.header {
font-weight: bold;
}
}
.widgets {

View File

@ -14,8 +14,8 @@
</div>
{{ end }}
<input name="f" type="datetime-local" value="{{ .Data.TimeFrom }}">
<input name="t" type="datetime-local" value="{{ .Data.TimeTo }}">
<input name="f" type="datetime-local" step="1" value="{{ .Data.TimeFrom }}">
<input name="t" type="datetime-local" step="1" value="{{ .Data.TimeTo }}">
<button>OK</button>
</form>
<br><br>
@ -43,9 +43,12 @@
</script>
{{ else }}
<div id="values">
<div class="header">Value added</div>
<div class="header">Value</div>
<div class="line"></div>
{{ range .Data.Values }}
<div class="value">{{ format_time .Ts }}</div>
<div class="value">{{ .Value }}</div>
<div class="value">{{ format_time .Value }}</div>
{{ end }}
</div>
{{ end }}