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

Function Example_cel_TimestampsAndDurations

examples/example_cel_time_test.go:26–63  ·  view source on GitHub ↗

Example_cel_TimestampsAndDurations showcases timestamps, durations, arithmetic, and field access as documented in https://celbyexample.com/timestamps-and-durations/

()

Source from the content-addressed store, hash-verified

24// Example_cel_TimestampsAndDurations showcases timestamps, durations, arithmetic, and field access
25// as documented in https://celbyexample.com/timestamps-and-durations/
26func Example_cel_TimestampsAndDurations() {
27 exprs := []string{
28 `timestamp("2024-01-15T10:30:45Z")`,
29 `duration("1h30m")`,
30 `timestamp("2024-01-15T10:30:45Z") + duration("1h")`,
31 `timestamp("2024-01-15T10:30:45Z") - timestamp("2024-01-15T09:30:45Z")`,
32 `timestamp("2024-01-15T10:30:45Z").getFullYear()`,
33 `timestamp("2024-01-15T10:30:45Z").getMonth()`,
34 `timestamp("2024-01-15T10:30:45Z").getDayOfMonth()`,
35 `duration("1h").getHours()`,
36 `duration("90m").getMinutes()`,
37 `timestamp("2024-01-15T10:30:45Z") > timestamp("2024-01-01T00:00:00Z")`,
38 }
39
40 for _, expr := range exprs {
41 prg, err := cel.Compile(expr)
42 if err != nil {
43 log.Fatalf("cel.Compile() error for %q: %v", expr, err)
44 }
45 out, _, err := prg.Eval(cel.NoVars())
46 if err != nil {
47 log.Fatalf("prg.Eval() error for %q: %v", expr, err)
48 }
49 fmt.Printf("%s -> %v\n", expr, out)
50 }
51
52 // Output:
53 // timestamp("2024-01-15T10:30:45Z") -> 2024-01-15 10:30:45 +0000 UTC
54 // duration("1h30m") -> 1h30m0s
55 // timestamp("2024-01-15T10:30:45Z") + duration("1h") -> 2024-01-15 11:30:45 +0000 UTC
56 // timestamp("2024-01-15T10:30:45Z") - timestamp("2024-01-15T09:30:45Z") -> 1h0m0s
57 // timestamp("2024-01-15T10:30:45Z").getFullYear() -> 2024
58 // timestamp("2024-01-15T10:30:45Z").getMonth() -> 0
59 // timestamp("2024-01-15T10:30:45Z").getDayOfMonth() -> 14
60 // duration("1h").getHours() -> 1
61 // duration("90m").getMinutes() -> 90
62 // timestamp("2024-01-15T10:30:45Z") > timestamp("2024-01-01T00:00:00Z") -> true
63}
64
65// Example_cel_Time showcases timezones, timestamp components, and epoch conversions
66// as documented in https://celbyexample.com/time/

Callers

nothing calls this directly

Calls 3

CompileFunction · 0.92
NoVarsFunction · 0.92
EvalMethod · 0.65

Tested by

no test coverage detected