time functions
This namespace wraps Go’s time package, and a
few of the functions return a time.Time value. All of the
time.Time functions can then be used to
convert, adjust, or format the time in your template.
Reference time
An important difference between this and many other time/date utilities is how parsing and formatting is accomplished. Instead of relying solely on pre-defined formats, or having a complex system of variables, formatting is accomplished by declaring an example of the layout you wish to display.
This uses a reference time, which is:
Constants
format layouts
Some pre-defined layouts have been provided for convenience:
| layout name | value |
|---|---|
time.ANSIC |
"Mon Jan _2 15:04:05 2006" |
time.UnixDate |
"Mon Jan _2 15:04:05 MST 2006" |
time.RubyDate |
"Mon Jan 02 15:04:05 -0700 2006" |
time.RFC822 |
"02 Jan 06 15:04 MST" |
time.RFC822Z |
"02 Jan 06 15:04 -0700" // RFC822 with numeric zone |
time.RFC850 |
"Monday, 02-Jan-06 15:04:05 MST" |
time.RFC1123 |
"Mon, 02 Jan 2006 15:04:05 MST" |
time.RFC1123Z |
"Mon, 02 Jan 2006 15:04:05 -0700" // RFC1123 with numeric zone |
time.RFC3339 |
"2006-01-02T15:04:05Z07:00" |
time.RFC3339Nano |
"2006-01-02T15:04:05.999999999Z07:00" |
time.Kitchen |
"3:04PM" |
time.Stamp |
"Jan _2 15:04:05" |
time.StampMilli |
"Jan _2 15:04:05.000" |
time.StampMicro |
"Jan _2 15:04:05.000000" |
time.StampNano |
"Jan _2 15:04:05.000000000" |
See below for examples of how these layouts can be used.
durations
Some operations (such as Time.Add and
Time.Round) require a
Duration value. These can be created
conveniently with the following functions:
time.Nanosecondtime.Microsecondtime.Millisecondtime.Secondtime.Minutetime.Hour
For example:
For other durations, such as 2h10m, time.ParseDuration can be used.
time.Now
Returns the current local time, as a time.Time. This wraps time.Now.
Usually, further functions are called using the value returned by Now.
Added in gomplate v2.1.0
Usage
Examples
Usage with AddDate:
(notice how the TZ adjusted for daylight savings!)
Usage with IsDST:
time.Parse
Parses a timestamp defined by the given layout. This wraps time.Parse.
A number of pre-defined layouts are provided as constants, defined here.
Just like time.Now, this is usually used in conjunction with
other functions.
Note: In the absence of a time zone indicator, time.Parse returns a time in UTC.
Added in gomplate v2.1.0
Usage
Arguments
| name | description |
|---|---|
layout |
(required) The layout string to parse with |
timestamp |
(required) The timestamp to parse |
Examples
Usage with Format:
time.ParseDuration
Parses a duration string. This wraps 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.
Added in gomplate v2.1.0
Usage
Arguments
| name | description |
|---|---|
duration |
(required) The duration string to parse |
Examples
time.ParseLocal
Same as time.Parse, except that in the absence of a time zone
indicator, the timestamp will be parsed in the local timezone.
Added in gomplate v2.2.0
Usage
Arguments
| name | description |
|---|---|
layout |
(required) The layout string to parse with |
timestamp |
(required) The timestamp to parse |
Examples
Usage with Format:
time.ParseInLocation
Same as time.Parse, except that the time is parsed in the given location’s time zone.
This wraps time.ParseInLocation.
Added in gomplate v2.2.0
Usage
Arguments
| name | description |
|---|---|
layout |
(required) The layout string to parse with |
location |
(required) The location to parse in |
timestamp |
(required) The timestamp to parse |
Examples
Usage with Format:
time.Since
Returns the time elapsed since a given time. This wraps time.Since.
It is shorthand for time.Now.Sub t.
Added in gomplate v2.5.0
Usage
Arguments
| name | description |
|---|---|
t |
(required) the Time to calculate since |
Examples
time.Unix
Returns the local Time corresponding to the given Unix time, in seconds since
January 1, 1970 UTC. Note that fractional seconds can be used to denote
milliseconds, but must be specified as a string, not a floating point number.
Added in gomplate v2.1.0
Usage
Arguments
| name | description |
|---|---|
time |
(required) the time to parse |
Examples
with whole seconds:
with fractional seconds:
time.Until
Returns the duration until a given time. This wraps time.Until.
It is shorthand for $t.Sub time.Now.
Added in gomplate v2.5.0
Usage
Arguments
| name | description |
|---|---|
t |
(required) the Time to calculate until |
Examples
Or, less precise:
time.ZoneName
Return the local system’s time zone’s name.
Added in gomplate v2.1.0
Usage
Examples
time.ZoneOffset
Return the local system’s time zone offset, in seconds east of UTC.
Added in gomplate v2.2.0