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

Add Optional Timezone to now() #635

Closed
wants to merge 2 commits into from
Closed
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
13 changes: 12 additions & 1 deletion builtin/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -472,9 +472,20 @@ var Builtins = []*Function{
{
Name: "now",
Func: func(args ...any) (any, error) {
if len(args) == 1 {
timeZone := args[0].(string)
tz, err := time.LoadLocation(timeZone)
if err != nil {
return nil, err
}
return time.Now().In(tz), nil
}
return time.Now(), nil
},
Types: types(new(func() time.Time)),
Types: types(
new(func() time.Time),
new(func(string) time.Time),
),
},
{
Name: "duration",
Expand Down
15 changes: 15 additions & 0 deletions builtin/builtin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ func TestBuiltin(t *testing.T) {
"PtrArrayWithNil": &ArrayWithNil,
}

locations := map[string]*time.Location{
"America/Chicago": mustLoadLocation("America/Chicago"),
"Europe/Bratislava": mustLoadLocation("Europe/Bratislava"),
}

var tests = []struct {
input string
want any
Expand Down Expand Up @@ -110,6 +115,8 @@ func TestBuiltin(t *testing.T) {
{`toBase64("hello")`, "aGVsbG8="},
{`fromBase64("aGVsbG8=")`, "hello"},
{`now().Format("2006-01-02T15:04Z")`, time.Now().Format("2006-01-02T15:04Z")},
{`now("America/Chicago").Format("2006-01-02T15:04Z")`, time.Now().In(locations["America/Chicago"]).Format("2006-01-02T15:04Z")},
{`now("Europe/Bratislava").Format("2006-01-02T15:04Z")`, time.Now().In(locations["Europe/Bratislava"]).Format("2006-01-02T15:04Z")},
{`duration("1h")`, time.Hour},
{`date("2006-01-02T15:04:05Z")`, time.Date(2006, 1, 2, 15, 4, 5, 0, time.UTC)},
{`date("2006.01.02", "2006.01.02")`, time.Date(2006, 1, 2, 0, 0, 0, 0, time.UTC)},
Expand Down Expand Up @@ -625,3 +632,11 @@ func Test_int_unwraps_underlying_value(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, true, out)
}

func mustLoadLocation(location string) *time.Location {
loc, err := time.LoadLocation(location)
if err != nil {
panic(err.Error())
}
return loc
}
Loading