Example_cel_TimestampsAndDurations showcases timestamps, durations, arithmetic, and field access as documented in https://celbyexample.com/timestamps-and-durations/
()
| 24 | // Example_cel_TimestampsAndDurations showcases timestamps, durations, arithmetic, and field access |
| 25 | // as documented in https://celbyexample.com/timestamps-and-durations/ |
| 26 | func 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/ |