MCPcopy Create free account
hub / github.com/cel-expr/cel-go / Example_cel_Time

Function Example_cel_Time

examples/example_cel_time_test.go:67–94  ·  view source on GitHub ↗

Example_cel_Time showcases timezones, timestamp components, and epoch conversions as documented in https://celbyexample.com/time/

()

Source from the content-addressed store, hash-verified

65// Example_cel_Time showcases timezones, timestamp components, and epoch conversions
66// as documented in https://celbyexample.com/time/
67func Example_cel_Time() {
68 exprs := []string{
69 `timestamp("2024-01-15T10:30:45Z").getHours("America/New_York")`,
70 `timestamp("2024-01-15T10:30:45Z").getDayOfWeek("UTC")`,
71 `duration("3600s")`,
72 `duration("-500ms")`,
73 `timestamp(1705314645)`,
74 }
75
76 for _, expr := range exprs {
77 prg, err := cel.Compile(expr)
78 if err != nil {
79 log.Fatalf("cel.Compile() error for %q: %v", expr, err)
80 }
81 out, _, err := prg.Eval(cel.NoVars())
82 if err != nil {
83 log.Fatalf("prg.Eval() error for %q: %v", expr, err)
84 }
85 fmt.Printf("%s -> %v\n", expr, out)
86 }
87
88 // Output:
89 // timestamp("2024-01-15T10:30:45Z").getHours("America/New_York") -> 5
90 // timestamp("2024-01-15T10:30:45Z").getDayOfWeek("UTC") -> 1
91 // duration("3600s") -> 1h0m0s
92 // duration("-500ms") -> -500ms
93 // timestamp(1705314645) -> 2024-01-15 10:30:45 +0000 UTC
94}

Callers

nothing calls this directly

Calls 3

CompileFunction · 0.92
NoVarsFunction · 0.92
EvalMethod · 0.65

Tested by

no test coverage detected