Skip to content

Commit 1f31cc5

Browse files
committed
Add timezone() builtin
1 parent 9207e71 commit 1f31cc5

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

builtin/builtin.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,17 @@ var Builtins = []*Function{
529529
new(func(string, string, string) time.Time),
530530
),
531531
},
532+
{
533+
Name: "timezone",
534+
Func: func(args ...any) (any, error) {
535+
tz, err := time.LoadLocation(args[0].(string))
536+
if err != nil {
537+
return nil, err
538+
}
539+
return tz, nil
540+
},
541+
Types: types(time.LoadLocation),
542+
},
532543
{
533544
Name: "first",
534545
Func: func(args ...any) (any, error) {

builtin/builtin_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ func TestBuiltin(t *testing.T) {
116116
{`date("2023-04-23T00:30:00.000+0100", "2006-01-02T15:04:05-0700", "America/Chicago").Format("2006-01-02")`, "2023-04-22"},
117117
{`date("2023-04-23T00:30:00", "2006-01-02T15:04:05", "America/Chicago").Format("2006-01-02")`, "2023-04-23"},
118118
{`date("2023-04-23", "2006-01-02", "America/Chicago").Format("2006-01-02")`, "2023-04-23"},
119+
{`timezone("UTC").String()`, "UTC"},
120+
{`timezone("Europe/Moscow").String()`, "Europe/Moscow"},
119121
{`first(ArrayOfString)`, "foo"},
120122
{`first(ArrayOfInt)`, 1},
121123
{`first(ArrayOfAny)`, 1},

docs/language-definition.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,38 @@ date("2023-08-14T00:00:00Z")
484484
date("2023-08-14 00:00:00", "2006-01-02 15:04:05", "Europe/Zurich")
485485
```
486486

487+
Available methods on the date:
488+
489+
- `Year()` - returns the year
490+
- `Month()` - returns the month (starting from 1)
491+
- `Day()` - returns the day of the month
492+
- `Hour()` - returns the hour
493+
- `Minute()` - returns the minute
494+
- `Second()` - returns the second
495+
- `Weekday()` - returns the day of the week
496+
- `YearDay()` - returns the day of the year
497+
- and [more](https://pkg.go.dev/time#Time).
498+
499+
```expr
500+
date("2023-08-14").Year() == 2023
501+
```
502+
503+
### timezone(str) {#timezone}
504+
505+
Returns the timezone of the given string `str`. List of available timezones can be
506+
found [here](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones).
507+
508+
```expr
509+
timezone("Europe/Zurich")
510+
timezone("UTC")
511+
```
512+
513+
To convert a date to a different timezone, use the [`In()`](https://pkg.go.dev/time#Time.In) method:
514+
515+
```expr
516+
date("2023-08-14 00:00:00").In(timezone("Europe/Zurich"))
517+
```
518+
487519
## Number Functions
488520

489521
### max(n1, n2) {#max}

0 commit comments

Comments
 (0)