Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding time.ZoneOffset function #222

Merged
merged 1 commit into from
Nov 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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())
}