Skip to content

Commit 964744a

Browse files
committed
Add offsetNow function
1 parent 779304d commit 964744a

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

docs/custom-api.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,7 @@ The following helper functions provided by Glance are available:
375375
- `toInt(f float) int`: Converts a float to an integer.
376376
- `toRelativeTime(t time.Time) template.HTMLAttr`: Converts Time to a relative time such as 2h, 1d, etc which dynamically updates. **NOTE:** the value of this function should be used as an attribute in an HTML tag, e.g. `<span {{ toRelativeTime .Time }}></span>`.
377377
- `now() time.Time`: Returns the current time.
378+
- `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".
378379
- `duration(str string) time.Duration`: Parses a string such as `1h`, `1d`, `5h30m`, etc into a `time.Duration`.
379380
- `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".
380381
- `parseRelativeTime(layout string, s string) time.Time`: A shorthand for `{{ .String "date" | parseTime "rfc3339" | toRelativeTime }}`.

internal/glance/widget-custom-api.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,13 @@ var customAPITemplateFuncs = func() template.FuncMap {
435435
"now": func() time.Time {
436436
return time.Now()
437437
},
438+
"offsetNow": func(offset string) time.Time {
439+
d, err := time.ParseDuration(offset)
440+
if err != nil {
441+
return time.Now()
442+
}
443+
return time.Now().Add(d)
444+
},
438445
"duration": func(str string) time.Duration {
439446
d, err := time.ParseDuration(str)
440447
if err != nil {

0 commit comments

Comments
 (0)