Example_cel_Time showcases timezones, timestamp components, and epoch conversions as documented in https://celbyexample.com/time/
()
| 65 | // Example_cel_Time showcases timezones, timestamp components, and epoch conversions |
| 66 | // as documented in https://celbyexample.com/time/ |
| 67 | func 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 | } |