Skip to content

Commit b8e2717

Browse files
committed
Add parseLocalTime function
1 parent 5029507 commit b8e2717

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

docs/custom-api.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,7 @@ The following helper functions provided by Glance are available:
378378
- `offsetNow(offset string) time.Time`: Returns the current time with an offset. The offset can be positive or negative and must be in the format "3h" "-1h" or "2h30m10s".
379379
- `duration(str string) time.Duration`: Parses a string such as `1h`, `24h`, `5h30m`, etc into a `time.Duration`.
380380
- `parseTime(layout string, s string) time.Time`: Parses a string into time.Time. The layout must be provided in Go's [date format](https://pkg.go.dev/time#pkg-constants). You can alternatively use these values instead of the literal format: "unix", "RFC3339", "RFC3339Nano", "DateTime", "DateOnly".
381+
- `parseLocalTime(layout string, s string) time.Time`: Same as the above, except in the absence of a timezone, it will use the local timezone instead of UTC.
381382
- `parseRelativeTime(layout string, s string) time.Time`: A shorthand for `{{ .String "date" | parseTime "rfc3339" | toRelativeTime }}`.
382383
- `add(a, b float) float`: Adds two numbers.
383384
- `sub(a, b float) float`: Subtracts two numbers.

internal/glance/widget-custom-api.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -450,11 +450,16 @@ var customAPITemplateFuncs = func() template.FuncMap {
450450

451451
return d
452452
},
453-
"parseTime": customAPIFuncParseTime,
453+
"parseTime": func(layout, value string) time.Time {
454+
return customAPIFuncParseTimeInLocation(layout, value, time.UTC)
455+
},
456+
"parseLocalTime": func(layout, value string) time.Time {
457+
return customAPIFuncParseTimeInLocation(layout, value, time.Local)
458+
},
454459
"toRelativeTime": dynamicRelativeTimeAttrs,
455460
"parseRelativeTime": func(layout, value string) template.HTMLAttr {
456461
// Shorthand to do both of the above with a single function call
457-
return dynamicRelativeTimeAttrs(customAPIFuncParseTime(layout, value))
462+
return dynamicRelativeTimeAttrs(customAPIFuncParseTimeInLocation(layout, value, time.UTC))
458463
},
459464
// The reason we flip the parameter order is so that you can chain multiple calls together like this:
460465
// {{ .JSON.String "foo" | trimPrefix "bar" | doSomethingElse }}
@@ -528,8 +533,8 @@ var customAPITemplateFuncs = func() template.FuncMap {
528533
},
529534
"sortByTime": func(key, layout, order string, results []decoratedGJSONResult) []decoratedGJSONResult {
530535
sort.Slice(results, func(a, b int) bool {
531-
timeA := customAPIFuncParseTime(layout, results[a].String(key))
532-
timeB := customAPIFuncParseTime(layout, results[b].String(key))
536+
timeA := customAPIFuncParseTimeInLocation(layout, results[a].String(key), time.UTC)
537+
timeB := customAPIFuncParseTimeInLocation(layout, results[b].String(key), time.UTC)
533538

534539
if order == "asc" {
535540
return timeA.Before(timeB)
@@ -566,7 +571,7 @@ var customAPITemplateFuncs = func() template.FuncMap {
566571
return funcs
567572
}()
568573

569-
func customAPIFuncParseTime(layout, value string) time.Time {
574+
func customAPIFuncParseTimeInLocation(layout, value string, loc *time.Location) time.Time {
570575
switch strings.ToLower(layout) {
571576
case "unix":
572577
asInt, err := strconv.ParseInt(value, 10, 64)
@@ -585,7 +590,7 @@ func customAPIFuncParseTime(layout, value string) time.Time {
585590
layout = time.DateOnly
586591
}
587592

588-
parsed, err := time.Parse(layout, value)
593+
parsed, err := time.ParseInLocation(layout, value, loc)
589594
if err != nil {
590595
return time.Unix(0, 0)
591596
}

0 commit comments

Comments
 (0)