Skip to content

Commit

Permalink
Adding time.ZoneOffset function
Browse files Browse the repository at this point in the history
Signed-off-by: Dave Henderson <dhenderson@gmail.com>
  • Loading branch information
hairyhenderson committed Nov 1, 2017
1 parent 491dee1 commit eef471a
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 0 deletions.
16 changes: 16 additions & 0 deletions docs/content/functions/time.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,19 @@ time.ZoneName
$ gomplate -i '{{time.ZoneName}}'
EDT
```

## `time.ZoneOffset`

Return the local system's time zone offset, in seconds east of UTC.

### Usage
```go
time.ZoneOffset
```

### Example

```console
$ gomplate -i '{{time.ZoneOffset}}'
-14400
```
5 changes: 5 additions & 0 deletions funcs/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ func (f *TimeFuncs) ZoneName() string {
return time.ZoneName()
}

// ZoneOffset - return the local system's time zone's name
func (f *TimeFuncs) ZoneOffset() int {
return time.ZoneOffset()
}

// Parse -
func (f *TimeFuncs) Parse(layout, value string) (gotime.Time, error) {
return gotime.Parse(layout, value)
Expand Down
6 changes: 6 additions & 0 deletions test/integration/time.bats
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ load helper
[[ "${output}" == `date +"%Z"` ]]
}

@test "'time.ZoneOffset'" {
TZ=UTC gomplate -i '{{ time.ZoneOffset }}'
[ "$status" -eq 0 ]
[[ "${output}" == "0" ]]
}

@test "'(time.Now).Format'" {
gomplate -i '{{ (time.Now).Format "2006-01-02 15 -0700" }}'
[ "$status" -eq 0 ]
Expand Down
6 changes: 6 additions & 0 deletions time/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,9 @@ func ZoneName() string {
n, _ := time.Now().Zone()
return n
}

// ZoneOffset - determine the current timezone's offset, in seconds east of UTC
func ZoneOffset() int {
_, o := time.Now().Zone()
return o
}
14 changes: 14 additions & 0 deletions time/time_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package time

import (
"testing"
"time"

"github.com/stretchr/testify/assert"
)

func TestZoneFuncs(t *testing.T) {
name, offset := time.Now().Zone()
assert.Equal(t, name, ZoneName())
assert.Equal(t, offset, ZoneOffset())
}

0 comments on commit eef471a

Please sign in to comment.