summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDave Henderson <dhenderson@gmail.com>2018-04-19 00:48:53 -0400
committerGitHub <noreply@github.com>2018-04-19 00:48:53 -0400
commit02c8a39fd450efe61e79870a88f47ddeea787eac (patch)
tree471c4d8f303786f7725281a4ae72120fe0e4997d
parent7ff832bf5764912b12ad328fa618d2c4848ce1f2 (diff)
parent2285105a158822008bf9441b9b5a7e794eb3b70a (diff)
Merge pull request #294 from hairyhenderson/add-parseduration-func
Adding time.ParseDuration function
-rw-r--r--docs/content/functions/time.md27
-rw-r--r--funcs/time.go5
2 files changed, 32 insertions, 0 deletions
diff --git a/docs/content/functions/time.md b/docs/content/functions/time.md
index 2bd342a2..902f3d78 100644
--- a/docs/content/functions/time.md
+++ b/docs/content/functions/time.md
@@ -72,6 +72,8 @@ $ gomplate -i '{{ (time.Now).Format time.Kitchen }}
11:05AM
```
+For other durations, such as `2h10m`, [`time.ParseDuration`](#time-parseduration) can be used.
+
## `time.Now`
Returns the current local time, as a `time.Time`. This wraps [`time.Now`](https://golang.org/pkg/time/#Now).
@@ -133,6 +135,31 @@ $ gomplate -i '{{ (time.Parse "2006-01-02" "1993-10-23").Format "Monday January
Saturday October 23, 1993 UTC
```
+## `time.ParseDuration`
+
+Parses a duration string. This wraps [`time.ParseDuration`](https://golang.org/pkg/time/#ParseDuration).
+
+A duration string is a possibly signed sequence of decimal numbers, each with
+optional fraction and a unit suffix, such as `300ms`, `-1.5h` or `2h45m`. Valid
+time units are `ns`, `us` (or `µs`), `ms`, `s`, `m`, `h`.
+
+### Usage
+```go
+time.ParseDuration duration
+```
+```go
+duration | time.ParseDuration
+```
+
+### Examples
+
+```console
+$ gomplate -i '{{ (time.Now).Format time.Kitchen }}
+{{ ((time.Now).Add (time.ParseDuration "2h30m")).Format time.Kitchen }}'
+12:43AM
+3:13AM
+```
+
## `time.ParseLocal`
Same as [`time.Parse`](#time-parse), except that in the absence of a time zone
diff --git a/funcs/time.go b/funcs/time.go
index a1940584..111b0b23 100644
--- a/funcs/time.go
+++ b/funcs/time.go
@@ -138,6 +138,11 @@ func (f *TimeFuncs) Hour(n interface{}) gotime.Duration {
return gotime.Hour * gotime.Duration(conv.ToInt64(n))
}
+// ParseDuration -
+func (f *TimeFuncs) ParseDuration(n interface{}) (gotime.Duration, error) {
+ return gotime.ParseDuration(conv.ToString(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)