You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Document time arithmetic and fix typos in timestamp operations (#297)
Add Arithmetic section to timestamp-operations.malloynb documenting
the `expr + N unit` / `expr - N unit` syntax for date and timestamp
values. Add corresponding entry in the expressions.malloynb summary
table. Also fix pre-existing typos: "funtion" → "function", wrong
day-of-month result, and incorrect week truncation comment. Rename
page title to "Timestamp and Date Operations".
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
Copy file name to clipboardExpand all lines: src/documentation/language/timestamp-operations.malloynb
+30-4Lines changed: 30 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,5 @@
1
1
>>>markdown
2
-
# Timestamp Operations
2
+
# Timestamp and Date Operations
3
3
4
4
## Truncation
5
5
@@ -10,7 +10,7 @@ For truncation to a time unit (timeframe), use the period (`.`) operator followe
10
10
`expr.minute` | 2021-08-06 00:36
11
11
`expr.hour` | 2021-08-06 00:00
12
12
`expr.day` | 2021-08-06 00:00
13
-
`expr.week` | 2021-08-01 00:00 _(the week containing the 10th)_
13
+
`expr.week` | 2021-08-01 00:00 _(the week containing the 6th)_
14
14
`expr.month` | 2021-08-01 00:00
15
15
`expr.quarter` | 2021-06-01 00:00
16
16
`expr.year` | 2021-01-01 00:00
@@ -19,14 +19,14 @@ For truncation to a time unit (timeframe), use the period (`.`) operator followe
19
19
20
20
## Extraction
21
21
22
-
To extract an integer from a component of a timestamp, malloy uses the time unit (timeframe) in the form of a funtion call.
22
+
To extract an integer from a component of a timestamp, malloy uses the time unit (timeframe) in the form of a function call.
23
23
24
24
The "Result" column uses a value of `@2021-08-06 00:55:05` for `expr`.
25
25
26
26
expression | meaning | result
27
27
---- | ---- | ----
28
28
`day_of_year(expr)` | day of year, 1-365 | 218
29
-
`day(expr)` | day of month 1-31 | 5
29
+
`day(expr)` | day of month 1-31 | 6
30
30
`day_of_week(expr)` | day of week 1-7 | 6 _(Note: 1 represents Sunday)_
31
31
`week(expr)` | week in year, 1-53 | 31
32
32
`quarter(expr)` | quarter in year 1-4 | 3
@@ -63,3 +63,29 @@ expression | meaning
63
63
`timestamptz_value::timestamp` | Converts timestamptz to timestamp in the query timezone
64
64
65
65
* See [Timezones](timezones.malloynb) for more information on how timezones interact with casting.
66
+
67
+
## Arithmetic
68
+
69
+
Add or subtract a duration from a timestamp or date using `+` or `-` with a number and time unit.
70
+
71
+
expression | result (expr = @2021-02-24 03:05:06)
72
+
---- | ----
73
+
`expr + 10 seconds` | 2021-02-24 03:05:16
74
+
`expr - 6 seconds` | 2021-02-24 03:05:00
75
+
`expr + 10 minutes` | 2021-02-24 03:15:06
76
+
`expr + 10 hours` | 2021-02-24 13:05:06
77
+
`expr + 3 days` | 2021-02-27 03:05:06
78
+
`expr - 2 weeks` | 2021-02-10 03:05:06
79
+
`expr + 9 months` | 2021-11-24 03:05:06
80
+
`expr + 2 quarters` | 2021-08-24 03:05:06
81
+
`expr + 10 years` | 2031-02-24 03:05:06
82
+
83
+
The supported time units are `seconds`, `minutes`, `hours`, `days`, `weeks`, `months`, `quarters`, and `years`. Both singular and plural forms are accepted (e.g. `1 day` or `3 days`).
84
+
85
+
Date arithmetic works on both timestamps and dates. The units `seconds`, `minutes`, and `hours` can only be used with timestamps — using them with a date value will produce a compile error.
86
+
87
+
This is commonly used with `now` for relative time filters:
0 commit comments