From 441edd4c01e158275039db882bf6b18d6f3fba8d Mon Sep 17 00:00:00 2001 From: Dave Henderson Date: Thu, 19 Apr 2018 01:06:33 -0400 Subject: Adding time.Since and time.Until wrappers Signed-off-by: Dave Henderson --- docs/content/functions/time.md | 54 ++++++++++++++++++++++++++++++++++++++++++ funcs/time.go | 10 ++++++++ 2 files changed, 64 insertions(+) diff --git a/docs/content/functions/time.md b/docs/content/functions/time.md index 902f3d78..54af2f47 100644 --- a/docs/content/functions/time.md +++ b/docs/content/functions/time.md @@ -212,6 +212,30 @@ $ gomplate -i '{{ (time.ParseInLocation time.Kitchen "Africa/Luanda" "6:00AM").F 06:00 LMT ``` +## `time.Since` + +Returns the time elapsed since a given time. This wraps [`time.Since`](https://golang.org/pkg/time/#Since). + +It is shorthand for `time.Now.Sub t`. + +### Usage +```go +time.Since t +``` + +### Arguments + +| name | description | +|--------|-------------| +| `t` | the `Time` to calculate since | + +### Examples + +```console +$ gomplate -i '{{ $t := time.Parse time.RFC3339 "1970-01-01T00:00:00Z" }}time since the epoch: {{ time.Since $t }}' +time since the epoch: 423365h0m24.353828924s +``` + ## `time.Unix` Returns the local `Time` corresponding to the given Unix time, in seconds since @@ -237,6 +261,36 @@ $ gomplate -i '{{ (time.Unix "123456.789").UTC.Format time.StampMilli}}' Jan 2 10:17:36.789 ``` +## `time.Until` + +Returns the duration until a given time. This wraps [`time.Until`](https://golang.org/pkg/time/#Until). + +It is shorthand for `$t.Sub time.Now`. + +### Usage +```go +time.Until t +``` + +### Arguments + +| name | description | +|--------|-------------| +| `t` | the `Time` to calculate until | + +### Examples + +```console +$ gomplate -i '{{ $t := time.Parse time.RFC3339 "2020-01-01T00:00:00Z" }}only {{ time.Until $t }} to go...' +only 14922h56m46.578625891s to go... +``` + +Or, less precise: +```console +$ bin/gomplate -i '{{ $t := time.Parse time.RFC3339 "2020-01-01T00:00:00Z" }}only {{ (time.Until $t).Round (time.Hour 1) }} to go...' +only 14923h0m0s to go... +``` + ## `time.ZoneName` Return the local system's time zone's name. diff --git a/funcs/time.go b/funcs/time.go index 111b0b23..d1030cab 100644 --- a/funcs/time.go +++ b/funcs/time.go @@ -143,6 +143,16 @@ func (f *TimeFuncs) ParseDuration(n interface{}) (gotime.Duration, error) { return gotime.ParseDuration(conv.ToString(n)) } +// Since - +func (f *TimeFuncs) Since(n gotime.Time) gotime.Duration { + return gotime.Since(n) +} + +// Until - +func (f *TimeFuncs) Until(n gotime.Time) gotime.Duration { + return gotime.Until(n) +} + // convert a number input to a pair of int64s, representing the integer portion and the decimal remainder // this can handle a string as well as any integer or float type // precision is at the "nano" level (i.e. 1e+9) -- cgit v1.2.3