Add timezone convert
Any plans to add the option to convert to a different timezone. I think currently you can only specify a timezone for the input as part of the date() function.
Could be an extra function or an optional parameter of the date function.
date("2023-08-14T00:00:00Z").toTimezone('America/Los_Angeles')
I think it would make sense since its also part of the time package and already used to specify the input date.
Go already have In() function https://pkg.go.dev/time#Time.In
Expr only need a way to create a Location from a string. Right now I found this way 🫣
let utc = date("2023-08-14 00:00:00", "2006-01-02 15:04:05", "UTC").Location();
now().In(utc)
What about adding timezone()?
now().In(timezone('Europe/Moscow'))
Hmm, since the date function also accepts the timezone as a string (date("2023-08-14 00:00:00", "2006-01-02 15:04:05", "Europe/Zurich")) it would be cleaner to don't have a separate timezone() function to convert.
now().In('Europe/Moscow')
Not the biggest fan of the In() wording (could be a bit confusing for users that don't know the go time functions).
Here a couple of ideas:
now().ToTimezone('Europe/Moscow')
now().Timezone('Europe/Moscow')
now().Format('Jan 2006', 'Europe/Moscow') (second optional parameter of the format function)
I do not really want to wrap time.Time package into my own. ¯\_(ツ)_/¯
Do as you like :-)
It's a little convoluted when using now() because you have to format it then re-parse it, but it's possible with #633:
❯ date(now().Format("2006-01-02T15:04:05-0700"), "2006-01-02T15:04:05-0700", "America/Los_Angeles").Format("2006-01-02T15:04:05-0700")
2024-04-24T04:37:13-0700
❯ date(now().Format("2006-01-02T15:04:05-0700"), "2006-01-02T15:04:05-0700", "Europe/Moscow").Format("2006-01-02T15:04:05-0700")
2024-04-24T14:37:25+0300
What about adding TZ param to now()?
now("Europe/Moscow")
What about adding TZ param to now()?
now("Europe/Moscow")
There's an implementation in #635.
Another idea: lets overload in operator for timezone convert? This very clean and readable syntax:
now() in 'UTC'
And also let's add a expr.Timezone() option to configure default timezone for date() and now() functions.
expr.Compile(code, expr.Timezone("Europe/Zurich"))
I've implemented a default timezone option:
program, err := expr.Compile(`now().Location().String()`, expr.Timezone("Asia/Kamchatka"))
if err != nil {
fmt.Printf("%v", err)
return
}
output, err := expr.Run(program, nil)
if err != nil {
fmt.Printf("%v", err)
return
}
fmt.Printf("%v", output)
// Output: Asia/Kamchatka
And a new builtin timezone():
now().In(timezone("Asia/Kamchatka"))