Skip to content

Commit 1fdd3f9

Browse files
add some docs for timestamptz (#257)
1 parent 3f3c3e5 commit 1fdd3f9

2 files changed

Lines changed: 39 additions & 5 deletions

File tree

src/documentation/language/datatypes.malloynb

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,21 +67,27 @@ For expressions which return a `boolean`, e.g. `might_be_null > 0`:
6767

6868
This is true for all the comparison operators (`>`, `=`, etc.) as well as functions which return a boolean (e.g. `starts_with`, `is_inf`, etc.).
6969

70-
## Timestamp
70+
## Timestamp and Timestamptz
7171

72-
A `timestamp` represents an instant in time.
72+
A `timestamp` and `timestamptz` both represent an instant in time. They work interchangeably in Malloy and have the same semantics. The distinction exists to support different database column storage formats.
7373

74-
Malloy's approach to timezone related computations relating to timestamp data is explained in the [Timezones](timezones.malloynb) section.
74+
Malloy's approach to timezone related computations is explained in the [Timezones](timezones.malloynb) section.
7575

7676
### Timestamp literals
7777

78-
Timestamp literals are specified in Malloy with the <code>@</code> character. Seconds, subsecond resolution, and locale are optional.
78+
Timestamp literals are specified in Malloy with the <code>@</code> character. Seconds and subsecond resolution are optional.
7979

80-
* `@2001-02-03 04:05:06.001[America/Mexico_City]`
8180
* `@2001-02-03 04:05:06.001`
8281
* `@2001-02-03 04:05:06`
8382
* `@2001-02-03 04:05`
8483

84+
A timezone can optionally be specified in square brackets:
85+
86+
* `@2001-02-03 04:05:06[America/Mexico_City]`
87+
* `@2001-02-03 04:05:06.001[UTC]`
88+
89+
On databases that support `timestamptz`, embedding a timezone in a literal produces a `timestamptz` literal.
90+
8591
In addition, in any of the above, a `T` can be used instead of a space between the date and time portion of the timestamp string, as in
8692

8793
* `@2001-02-03T04:05:06.001`

src/documentation/language/timestamp-operations.malloynb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,31 @@ expression | meaning | result
3535
`second(expr)` | second of minute 0-59 | 5
3636

3737
* See [Timezones](timezones.malloynb) for information on how extraction interacts with timezones.
38+
39+
## Casting
40+
41+
Timestamps and dates can be cast between types using the `::` operator.
42+
43+
### Casting to and from Date
44+
45+
When casting between `date` and timestamp-like types (`timestamp` or `timestamptz`), the query timezone is used to determine the conversion:
46+
47+
expression | meaning
48+
---- | ----
49+
`date_value::timestamp` | Interprets the date as midnight in the query timezone, returns a timestamp
50+
`date_value::timestamptz` | Interprets the date as midnight in the query timezone, returns a timestamptz
51+
`timestamp_value::date` | Converts the timestamp to a date in the query timezone
52+
`timestamptz_value::date` | Converts the timestamptz to a date in the query timezone
53+
54+
For example, with `timezone: 'America/Mexico_City'`:
55+
- `@2020-02-20::timestamptz` creates a timestamptz representing midnight on Feb 20 in Mexico City
56+
- `@2020-02-20 00:00:00[UTC]::date` produces `@2020-02-19` (because midnight UTC is Feb 19 in Mexico City)
57+
58+
### Casting between Timestamp and Timestamptz
59+
60+
expression | meaning
61+
---- | ----
62+
`timestamp_value::timestamptz` | Converts timestamp to timestamptz (when no query timezone, interprets as UTC)
63+
`timestamptz_value::timestamp` | Converts timestamptz to timestamp in the query timezone
64+
65+
* See [Timezones](timezones.malloynb) for more information on how timezones interact with casting.

0 commit comments

Comments
 (0)