-
-
Notifications
You must be signed in to change notification settings - Fork 420
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add default timezone option expr.Timezone()
- Loading branch information
Showing
8 changed files
with
141 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package patcher | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/expr-lang/expr/ast" | ||
) | ||
|
||
// WithTimezone passes Location to date() and now() functions. | ||
type WithTimezone struct { | ||
Location *time.Location | ||
} | ||
|
||
func (t WithTimezone) Visit(node *ast.Node) { | ||
if btin, ok := (*node).(*ast.BuiltinNode); ok { | ||
switch btin.Name { | ||
case "date", "now": | ||
loc := &ast.ConstantNode{Value: t.Location} | ||
ast.Patch(node, &ast.BuiltinNode{ | ||
Name: btin.Name, | ||
Arguments: append([]ast.Node{loc}, btin.Arguments...), | ||
}) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package patcher_test | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/expr-lang/expr" | ||
) | ||
|
||
func TestWithTimezone_date(t *testing.T) { | ||
program, err := expr.Compile(`date("2024-05-07 23:00:00")`, expr.Timezone("Europe/Zurich")) | ||
require.NoError(t, err) | ||
|
||
out, err := expr.Run(program, nil) | ||
require.NoError(t, err) | ||
require.Equal(t, "2024-05-07T23:00:00+02:00", out.(time.Time).Format(time.RFC3339)) | ||
} | ||
|
||
func TestWithTimezone_now(t *testing.T) { | ||
program, err := expr.Compile(`now()`, expr.Timezone("Asia/Kamchatka")) | ||
require.NoError(t, err) | ||
|
||
out, err := expr.Run(program, nil) | ||
require.NoError(t, err) | ||
require.Equal(t, "Asia/Kamchatka", out.(time.Time).Location().String()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters