github.com/hairyhenderson/gomplate/v4@v4.0.0-pre-2.0.20240520121557-362f058f0c93/docs/content/functions/time.md (about)

     1  ---
     2  title: time functions
     3  menu:
     4    main:
     5      parent: functions
     6  ---
     7  
     8  This namespace wraps Go's [`time` package](https://golang.org/pkg/time/), and a
     9  few of the functions return a `time.Time` value. All of the
    10  [`time.Time` functions](https://golang.org/pkg/time/#Time) can then be used to
    11  convert, adjust, or format the time in your template.
    12  
    13  ### Reference time
    14  
    15  An important difference between this and many other time/date utilities is how
    16  parsing and formatting is accomplished. Instead of relying solely on pre-defined
    17  formats, or having a complex system of variables, formatting is accomplished by
    18  declaring an example of the layout you wish to display.
    19  
    20  This uses a _reference time_, which is:
    21  
    22  ```
    23  Mon Jan 2 15:04:05 -0700 MST 2006
    24  ```
    25  
    26  ### Constants
    27  
    28  #### format layouts
    29  
    30  Some pre-defined layouts have been provided for convenience:
    31  
    32  | layout name | value |
    33  |-------------|-------|
    34  | `time.ANSIC`       | `"Mon Jan _2 15:04:05 2006"` |
    35  | `time.UnixDate`    | `"Mon Jan _2 15:04:05 MST 2006"` |
    36  | `time.RubyDate`    | `"Mon Jan 02 15:04:05 -0700 2006"` |
    37  | `time.RFC822`      | `"02 Jan 06 15:04 MST"` |
    38  | `time.RFC822Z`     | `"02 Jan 06 15:04 -0700"` // RFC822 with numeric zone |
    39  | `time.RFC850`      | `"Monday, 02-Jan-06 15:04:05 MST"` |
    40  | `time.RFC1123`     | `"Mon, 02 Jan 2006 15:04:05 MST"` |
    41  | `time.RFC1123Z`    | `"Mon, 02 Jan 2006 15:04:05 -0700"` // RFC1123 with numeric zone |
    42  | `time.RFC3339`     | `"2006-01-02T15:04:05Z07:00"` |
    43  | `time.RFC3339Nano` | `"2006-01-02T15:04:05.999999999Z07:00"` |
    44  | `time.Kitchen`     | `"3:04PM"` |
    45  | `time.Stamp`      | `"Jan _2 15:04:05"` |
    46  | `time.StampMilli` | `"Jan _2 15:04:05.000" `|
    47  | `time.StampMicro` | `"Jan _2 15:04:05.000000"` |
    48  | `time.StampNano`  | `"Jan _2 15:04:05.000000000"` |
    49  
    50  See below for examples of how these layouts can be used.
    51  
    52  #### durations
    53  
    54  Some operations (such as [`Time.Add`](https://golang.org/pkg/time/#Time.Add) and
    55  [`Time.Round`](https://golang.org/pkg/time/#Time.Round)) require a
    56  [`Duration`](https://golang.org/pkg/time/#Duration) value. These can be created
    57  conveniently with the following functions:
    58  
    59  - `time.Nanosecond`
    60  - `time.Microsecond`
    61  - `time.Millisecond`
    62  - `time.Second`
    63  - `time.Minute`
    64  - `time.Hour`
    65  
    66  For example:
    67  
    68  ```console
    69  $ gomplate -i '{{ (time.Now).Format time.Kitchen }}
    70  {{ ((time.Now).Add (time.Hour 2)).Format time.Kitchen }}'
    71  9:05AM
    72  11:05AM
    73  ```
    74  
    75  For other durations, such as `2h10m`, [`time.ParseDuration`](#time-parseduration) can be used.
    76  
    77  ## `time.Now`
    78  
    79  Returns the current local time, as a `time.Time`. This wraps [`time.Now`](https://golang.org/pkg/time/#Now).
    80  
    81  Usually, further functions are called using the value returned by `Now`.
    82  
    83  _Added in gomplate [v2.1.0](https://github.com/hairyhenderson/gomplate/releases/tag/v2.1.0)_
    84  ### Usage
    85  
    86  ```
    87  time.Now
    88  ```
    89  
    90  
    91  ### Examples
    92  
    93  Usage with [`UTC`](https://golang.org/pkg/time/#Time.UTC) and [`Format`](https://golang.org/pkg/time/#Time.Format):
    94  ```console
    95  $ gomplate -i '{{ (time.Now).UTC.Format "Day 2 of month 1 in year 2006 (timezone MST)" }}'
    96  Day 14 of month 10 in year 2017 (timezone UTC)
    97  ```
    98  Usage with [`AddDate`](https://golang.org/pkg/time/#Time.AddDate):
    99  ```console
   100  $ date
   101  Sat Oct 14 09:57:02 EDT 2017
   102  $ gomplate -i '{{ ((time.Now).AddDate 0 1 0).Format "Mon Jan 2 15:04:05 MST 2006" }}'
   103  Tue Nov 14 09:57:02 EST 2017
   104  ```
   105  
   106  _(notice how the TZ adjusted for daylight savings!)_
   107  Usage with [`IsDST`](https://golang.org/pkg/time/#Time.IsDST):
   108  ```console
   109  $ gomplate -i '{{ $t := time.Now }}At the tone, the time will be {{ ($t.Round (time.Minute 1)).Add (time.Minute 1) }}.
   110    It is{{ if not $t.IsDST }} not{{ end }} daylight savings time.
   111    ... ... BEEP'
   112  At the tone, the time will be 2022-02-10 09:01:00 -0500 EST.
   113  It is not daylight savings time.
   114  ... ... BEEP
   115  ```
   116  
   117  ## `time.Parse`
   118  
   119  Parses a timestamp defined by the given layout. This wraps [`time.Parse`](https://golang.org/pkg/time/#Parse).
   120  
   121  A number of pre-defined layouts are provided as constants, defined
   122  [here](https://golang.org/pkg/time/#pkg-constants).
   123  
   124  Just like [`time.Now`](#time-now), this is usually used in conjunction with
   125  other functions.
   126  
   127  _Note: In the absence of a time zone indicator, `time.Parse` returns a time in UTC._
   128  
   129  _Added in gomplate [v2.1.0](https://github.com/hairyhenderson/gomplate/releases/tag/v2.1.0)_
   130  ### Usage
   131  
   132  ```
   133  time.Parse layout timestamp
   134  ```
   135  ```
   136  timestamp | time.Parse layout
   137  ```
   138  
   139  ### Arguments
   140  
   141  | name | description |
   142  |------|-------------|
   143  | `layout` | _(required)_ The layout string to parse with |
   144  | `timestamp` | _(required)_ The timestamp to parse |
   145  
   146  ### Examples
   147  
   148  Usage with [`Format`](https://golang.org/pkg/time/#Time.Format):
   149  ```console
   150  $ gomplate -i '{{ (time.Parse "2006-01-02" "1993-10-23").Format "Monday January 2, 2006 MST" }}'
   151  Saturday October 23, 1993 UTC
   152  ```
   153  
   154  ## `time.ParseDuration`
   155  
   156  Parses a duration string. This wraps [`time.ParseDuration`](https://golang.org/pkg/time/#ParseDuration).
   157  
   158  A duration string is a possibly signed sequence of decimal numbers, each with
   159  optional fraction and a unit suffix, such as `300ms`, `-1.5h` or `2h45m`. Valid
   160  time units are `ns`, `us` (or `µs`), `ms`, `s`, `m`, `h`.
   161  
   162  _Added in gomplate [v2.1.0](https://github.com/hairyhenderson/gomplate/releases/tag/v2.1.0)_
   163  ### Usage
   164  
   165  ```
   166  time.ParseDuration duration
   167  ```
   168  ```
   169  duration | time.ParseDuration
   170  ```
   171  
   172  ### Arguments
   173  
   174  | name | description |
   175  |------|-------------|
   176  | `duration` | _(required)_ The duration string to parse |
   177  
   178  ### Examples
   179  
   180  ```console
   181  $ gomplate -i '{{ (time.Now).Format time.Kitchen }}
   182  {{ ((time.Now).Add (time.ParseDuration "2h30m")).Format time.Kitchen }}'
   183  12:43AM
   184  3:13AM
   185  ```
   186  
   187  ## `time.ParseLocal`
   188  
   189  Same as [`time.Parse`](#time-parse), except that in the absence of a time zone
   190  indicator, the timestamp wil be parsed in the local timezone.
   191  
   192  _Added in gomplate [v2.2.0](https://github.com/hairyhenderson/gomplate/releases/tag/v2.2.0)_
   193  ### Usage
   194  
   195  ```
   196  time.ParseLocal layout timestamp
   197  ```
   198  ```
   199  timestamp | time.ParseLocal layout
   200  ```
   201  
   202  ### Arguments
   203  
   204  | name | description |
   205  |------|-------------|
   206  | `layout` | _(required)_ The layout string to parse with |
   207  | `timestamp` | _(required)_ The timestamp to parse |
   208  
   209  ### Examples
   210  
   211  Usage with [`Format`](https://golang.org/pkg/time/#Time.Format):
   212  ```console
   213  $ bin/gomplate -i '{{ (time.ParseLocal time.Kitchen "6:00AM").Format "15:04 MST" }}'
   214  06:00 EST
   215  ```
   216  
   217  ## `time.ParseInLocation`
   218  
   219  Same as [`time.Parse`](#time-parse), except that the time is parsed in the given location's time zone.
   220  
   221  This wraps [`time.ParseInLocation`](https://golang.org/pkg/time/#ParseInLocation).
   222  
   223  _Added in gomplate [v2.2.0](https://github.com/hairyhenderson/gomplate/releases/tag/v2.2.0)_
   224  ### Usage
   225  
   226  ```
   227  time.ParseInLocation layout location timestamp
   228  ```
   229  ```
   230  timestamp | time.ParseInLocation layout location
   231  ```
   232  
   233  ### Arguments
   234  
   235  | name | description |
   236  |------|-------------|
   237  | `layout` | _(required)_ The layout string to parse with |
   238  | `location` | _(required)_ The location to parse in |
   239  | `timestamp` | _(required)_ The timestamp to parse |
   240  
   241  ### Examples
   242  
   243  Usage with [`Format`](https://golang.org/pkg/time/#Time.Format):
   244  ```console
   245  $ gomplate -i '{{ (time.ParseInLocation time.Kitchen "Africa/Luanda" "6:00AM").Format "15:04 MST" }}'
   246  06:00 LMT
   247  ```
   248  
   249  ## `time.Since`
   250  
   251  Returns the time elapsed since a given time. This wraps [`time.Since`](https://golang.org/pkg/time/#Since).
   252  
   253  It is shorthand for `time.Now.Sub t`.
   254  
   255  _Added in gomplate [v2.5.0](https://github.com/hairyhenderson/gomplate/releases/tag/v2.5.0)_
   256  ### Usage
   257  
   258  ```
   259  time.Since t
   260  ```
   261  ```
   262  t | time.Since
   263  ```
   264  
   265  ### Arguments
   266  
   267  | name | description |
   268  |------|-------------|
   269  | `t` | _(required)_ the `Time` to calculate since |
   270  
   271  ### Examples
   272  
   273  ```console
   274  $ gomplate -i '{{ $t := time.Parse time.RFC3339 "1970-01-01T00:00:00Z" }}time since the epoch:{{ time.Since $t }}'
   275  time since the epoch:423365h0m24.353828924s
   276  ```
   277  
   278  ## `time.Unix`
   279  
   280  Returns the local `Time` corresponding to the given Unix time, in seconds since
   281  January 1, 1970 UTC. Note that fractional seconds can be used to denote
   282  milliseconds, but must be specified as a string, not a floating point number.
   283  
   284  _Added in gomplate [v2.1.0](https://github.com/hairyhenderson/gomplate/releases/tag/v2.1.0)_
   285  ### Usage
   286  
   287  ```
   288  time.Unix time
   289  ```
   290  ```
   291  time | time.Unix
   292  ```
   293  
   294  ### Arguments
   295  
   296  | name | description |
   297  |------|-------------|
   298  | `time` | _(required)_ the time to parse |
   299  
   300  ### Examples
   301  
   302  _with whole seconds:_
   303  ```console
   304  $ gomplate -i '{{ (time.Unix 42).UTC.Format time.Stamp}}'
   305  Jan  1, 00:00:42
   306  ```
   307  
   308  _with fractional seconds:_
   309  ```console
   310  $ gomplate -i '{{ (time.Unix "123456.789").UTC.Format time.StampMilli}}'
   311  Jan  2 10:17:36.789
   312  ```
   313  
   314  ## `time.Until`
   315  
   316  Returns the duration until a given time. This wraps [`time.Until`](https://golang.org/pkg/time/#Until).
   317  
   318  It is shorthand for `$t.Sub time.Now`.
   319  
   320  _Added in gomplate [v2.5.0](https://github.com/hairyhenderson/gomplate/releases/tag/v2.5.0)_
   321  ### Usage
   322  
   323  ```
   324  time.Until t
   325  ```
   326  ```
   327  t | time.Until
   328  ```
   329  
   330  ### Arguments
   331  
   332  | name | description |
   333  |------|-------------|
   334  | `t` | _(required)_ the `Time` to calculate until |
   335  
   336  ### Examples
   337  
   338  ```console
   339  $ gomplate -i '{{ $t := time.Parse time.RFC3339 "2020-01-01T00:00:00Z" }}only {{ time.Until $t }} to go...'
   340  only 14922h56m46.578625891s to go...
   341  ```
   342  
   343  Or, less precise:
   344  ```console
   345  $ bin/gomplate -i '{{ $t := time.Parse time.RFC3339 "2020-01-01T00:00:00Z" }}only {{ (time.Until $t).Round (time.Hour 1) }} to go...'
   346  only 14923h0m0s to go...
   347  ```
   348  
   349  ## `time.ZoneName`
   350  
   351  Return the local system's time zone's name.
   352  
   353  _Added in gomplate [v2.1.0](https://github.com/hairyhenderson/gomplate/releases/tag/v2.1.0)_
   354  ### Usage
   355  
   356  ```
   357  time.ZoneName
   358  ```
   359  
   360  
   361  ### Examples
   362  
   363  ```console
   364  $ gomplate -i '{{time.ZoneName}}'
   365  EDT
   366  ```
   367  
   368  ## `time.ZoneOffset`
   369  
   370  Return the local system's time zone offset, in seconds east of UTC.
   371  
   372  _Added in gomplate [v2.2.0](https://github.com/hairyhenderson/gomplate/releases/tag/v2.2.0)_
   373  ### Usage
   374  
   375  ```
   376  time.ZoneOffset
   377  ```
   378  
   379  
   380  ### Examples
   381  
   382  ```console
   383  $ gomplate -i '{{time.ZoneOffset}}'
   384  -14400
   385  ```