Skip to content

Commit 9ba706f

Browse files
committed
Add Optional Timezone to now()
Add support for an optional timezone argument to now(). Formatting the returned time will use the specified timezone.
1 parent e6dfb71 commit 9ba706f

File tree

4 files changed

+36
-2
lines changed

4 files changed

+36
-2
lines changed

builtin/builtin.go

+12-1
Original file line numberDiff line numberDiff line change
@@ -472,9 +472,20 @@ var Builtins = []*Function{
472472
{
473473
Name: "now",
474474
Func: func(args ...any) (any, error) {
475+
if len(args) == 1 {
476+
timeZone := args[0].(string)
477+
tz, err := time.LoadLocation(timeZone)
478+
if err != nil {
479+
return nil, err
480+
}
481+
return time.Now().In(tz), nil
482+
}
475483
return time.Now(), nil
476484
},
477-
Types: types(new(func() time.Time)),
485+
Types: types(
486+
new(func() time.Time),
487+
new(func(string) time.Time),
488+
),
478489
},
479490
{
480491
Name: "duration",

builtin/builtin_test.go

+9
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"testing"
88
"time"
99

10+
"github.com/agiledragon/gomonkey/v2"
1011
"github.com/stretchr/testify/assert"
1112
"github.com/stretchr/testify/require"
1213

@@ -28,6 +29,12 @@ func TestBuiltin(t *testing.T) {
2829
"PtrArrayWithNil": &ArrayWithNil,
2930
}
3031

32+
// Monkey patch time.Now so we get a consistent timestamp
33+
patches := gomonkey.ApplyFunc(time.Now, func() time.Time {
34+
return time.Unix(946706400, 0)
35+
})
36+
defer patches.Reset()
37+
3138
var tests = []struct {
3239
input string
3340
want any
@@ -110,6 +117,8 @@ func TestBuiltin(t *testing.T) {
110117
{`toBase64("hello")`, "aGVsbG8="},
111118
{`fromBase64("aGVsbG8=")`, "hello"},
112119
{`now().Format("2006-01-02T15:04Z")`, time.Now().Format("2006-01-02T15:04Z")},
120+
{`now("America/Chicago").Format("2006-01-02T15:04Z")`, "2000-01-01T00:00Z"},
121+
{`now("Europe/Bratislava").Format("2006-01-02T15:04Z")`, "2000-01-01T07:00Z"},
113122
{`duration("1h")`, time.Hour},
114123
{`date("2006-01-02T15:04:05Z")`, time.Date(2006, 1, 2, 15, 4, 5, 0, time.UTC)},
115124
{`date("2006.01.02", "2006.01.02")`, time.Date(2006, 1, 2, 0, 0, 0, 0, time.UTC)},

go.mod

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ module github.com/expr-lang/expr
22

33
go 1.18
44

5-
require github.com/stretchr/testify v1.8.4
5+
require (
6+
github.com/agiledragon/gomonkey/v2 v2.11.0
7+
github.com/stretchr/testify v1.8.4
8+
)
69

710
require (
811
github.com/davecgh/go-spew v1.1.1 // indirect

go.sum

+11
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
1+
github.com/agiledragon/gomonkey/v2 v2.11.0 h1:5oxSgA+tC1xuGsrIorR+sYiziYltmJyEZ9qA25b6l5U=
2+
github.com/agiledragon/gomonkey/v2 v2.11.0/go.mod h1:ap1AmDzcVOAz1YpeJ3TCzIgstoaWLA6jbbgxfB4w2iY=
13
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
24
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
5+
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
6+
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
37
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
48
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
9+
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
10+
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
511
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
612
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
13+
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
14+
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
15+
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
16+
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
17+
golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
718
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
819
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
920
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

0 commit comments

Comments
 (0)