Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Update for latest SQL timezone behavior #5657

Merged
merged 2 commits into from
Oct 23, 2024

Conversation

jeffreyssmith2nd
Copy link
Contributor

Closes #

Describe your proposed changes here.

@jeffreyssmith2nd jeffreyssmith2nd marked this pull request as ready for review October 22, 2024 15:41
Copy link
Collaborator

@sanderson sanderson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jeffreyssmith2nd Thanks for this! I added some suggestions.

Converts a timestamp to a provided timezone. If the second argument is not provided, it defaults to UTC.

```sql
tz(time[, timezone])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
tz(time[, timezone])
tz(time_expression[, timezone])


##### Arguments

- **expression**: time to operate on.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- **expression**: time to operate on.
- **time_expression**: Time to operate on.

Comment on lines 1090 to 1092
- **timezone**: Optional [timezone db string](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones) into which the value should be cast. Defaults to "UTC" if not provided
The function returns the timestamp cast to the correct timezone.
If an incorrect timezone string is passed, or the wrong datatype is provided, the function returns an error.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- **timezone**: Optional [timezone db string](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones) into which the value should be cast. Defaults to "UTC" if not provided
The function returns the timestamp cast to the correct timezone.
If an incorrect timezone string is passed, or the wrong datatype is provided, the function returns an error.
- **timezone**: [Timezone string](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones)
to cast the value into. Default is `'UTC'`.
The function returns the timestamp cast to the specified timezone.
If an incorrect timezone string is passed or the wrong datatype is provided, the function returns an error.

Comment on lines 1121 to 1142
##### What is the difference between tz and AT TIME ZONE?
`tz` and [AT TIME ZONE](../operators/other/#at-time-zone) have similar behavior that differs when the input timestamp **does not** have a timezone associated with it.

When a timestamp does not have a timezone associated with it (the default behavior in InfluxDB 3.0), `AT TIME ZONE` will cast a timestamp to the "wall clock" time in a particular time zone. This is the moment in time something would be experienced in a given time zone. This is a good way to define bounds on a query when you want them relative to some known range in a timezone (for example your local time).
If a timezone is associated with the timestamp, then `AT TIME ZONE` will behave the same as `tz`.

`tz` will **always** cast a timestamp to the "absolute" time in a particular time zone. This is a time relative to the unix epoch that has then be converted to that point in time in a given time zone. This is useful when you have data that was written as UTC or epoch timestamps, and you would like to view them in a specific timezone.

{{< expand-wrapper >}}
{{% expand "View `tz` and `::timestamp` comparison" %}}
```sql
SELECT
'2024-04-01T00:00:20Z'::timestamp AT TIME ZONE 'Europe/Brussels' as time_timestamp,
tz('2024-04-01T00:00:20', 'Europe/Brussels') as time_tz;
```

| time_timestamp | time_tz |
| :--------------------------- | :------------------------- |
| 2024-04-01T00:00:20+02:00 | 2024-04-01T02:00:20+02:00 |

{{% /expand %}}
{{< /expand-wrapper >}}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
##### What is the difference between tz and AT TIME ZONE?
`tz` and [AT TIME ZONE](../operators/other/#at-time-zone) have similar behavior that differs when the input timestamp **does not** have a timezone associated with it.
When a timestamp does not have a timezone associated with it (the default behavior in InfluxDB 3.0), `AT TIME ZONE` will cast a timestamp to the "wall clock" time in a particular time zone. This is the moment in time something would be experienced in a given time zone. This is a good way to define bounds on a query when you want them relative to some known range in a timezone (for example your local time).
If a timezone is associated with the timestamp, then `AT TIME ZONE` will behave the same as `tz`.
`tz` will **always** cast a timestamp to the "absolute" time in a particular time zone. This is a time relative to the unix epoch that has then be converted to that point in time in a given time zone. This is useful when you have data that was written as UTC or epoch timestamps, and you would like to view them in a specific timezone.
{{< expand-wrapper >}}
{{% expand "View `tz` and `::timestamp` comparison" %}}
```sql
SELECT
'2024-04-01T00:00:20Z'::timestamp AT TIME ZONE 'Europe/Brussels' as time_timestamp,
tz('2024-04-01T00:00:20', 'Europe/Brussels') as time_tz;
```
| time_timestamp | time_tz |
| :--------------------------- | :------------------------- |
| 2024-04-01T00:00:20+02:00 | 2024-04-01T02:00:20+02:00 |
{{% /expand %}}
{{< /expand-wrapper >}}
##### Differences between tz and AT TIME ZONE
`tz` and [`AT TIME ZONE`](/influxdb/cloud-dedicated/reference/sql/operators/other/#at-time-zone)
differ when the input timestamp **does not** have a timezone.
- When using an input timestamp that does not have a timezone (the default behavior in InfluxDB) with the
`AT TIME ZONE` operator, the operator returns the the same timestamp, but with a timezone offset
(also known as the "wall clock" time)--for example:
```sql
'2024-01-01 00:00:00'::TIMESTAMP AT TIME ZONE 'America/Los_Angeles'
-- Returns
2024-01-01T00:00:00-08:00
```
- When using an input timestamp with a timezone, both the `tz()` function and the `AT TIME ZONE`
operator return the timestamp converted to the time in the specified timezone--for example:
```sql
'2024-01-01T00:00:00-00:00' AT TIME ZONE 'America/Los_Angeles'
tz('2024-01-01T00:00:00-00:00', 'America/Los_Angeles')
-- Both return
2023-12-31T16:00:00-08:00
```
- `tz()` always converts the input timestamp to the specified time zone.
If the input timestamp does not have a timezone, the function assumes it is a UTC timestamp--for example:
```sql
tz('2024-01-01 00:00:00'::TIMESTAMP, 'America/Los_Angeles')
-- Returns
2023-12-31T16:00:00-08:00
```
```sql
tz('2024-01-01T00:00:00+1:00', 'America/Los_Angeles')
-- Returns
2023-12-31T15:00:00-08:00
```
{{< expand-wrapper >}}
{{% expand "View `tz` and `::timestamp` comparison" %}}
```sql
SELECT
'2024-04-01T00:00:20Z'::timestamp AT TIME ZONE 'Europe/Brussels' as time_timestamp,
tz('2024-04-01T00:00:20', 'Europe/Brussels') as time_tz;
```
| time_timestamp | time_tz |
| :--------------------------- | :------------------------- |
| 2024-04-01T00:00:20+02:00 | 2024-04-01T02:00:20+02:00 |
{{% /expand %}}
{{< /expand-wrapper >}}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I like this way of describing it, much clearer!

Converts a timestamp to a provided timezone. If the second argument is not provided, it defaults to UTC.

```sql
tz(time[, timezone])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
tz(time[, timezone])
tz(time_expression[, timezone])

Converts a timestamp to a provided timezone. If the second argument is not provided, it defaults to UTC.

```sql
tz(time[, timezone])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
tz(time[, timezone])
tz(time_expression[, timezone])


##### Arguments

- **expression**: time to operate on.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- **expression**: time to operate on.
- **time_expression**: Time to operate on.

Comment on lines 1090 to 1092
- **timezone**: Optional [timezone db string](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones) into which the value should be cast. Defaults to "UTC" if not provided
The function returns the timestamp cast to the correct timezone.
If an incorrect timezone string is passed, or the wrong datatype is provided, the function returns an error.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- **timezone**: Optional [timezone db string](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones) into which the value should be cast. Defaults to "UTC" if not provided
The function returns the timestamp cast to the correct timezone.
If an incorrect timezone string is passed, or the wrong datatype is provided, the function returns an error.
- **timezone**: [Timezone string](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones)
to cast the value into. Default is `'UTC'`.
The function returns the timestamp cast to the specified timezone.
If an incorrect timezone string is passed or the wrong datatype is provided, the function returns an error.

Comment on lines 1090 to 1092
- **timezone**: Optional [timezone db string](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones) into which the value should be cast. Defaults to "UTC" if not provided
The function returns the timestamp cast to the correct timezone.
If an incorrect timezone string is passed, or the wrong datatype is provided, the function returns an error.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- **timezone**: Optional [timezone db string](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones) into which the value should be cast. Defaults to "UTC" if not provided
The function returns the timestamp cast to the correct timezone.
If an incorrect timezone string is passed, or the wrong datatype is provided, the function returns an error.
- **timezone**: [Timezone string](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones)
to cast the value into. Default is `'UTC'`.
The function returns the timestamp cast to the specified timezone.
If an incorrect timezone string is passed or the wrong datatype is provided, the function returns an error.

Comment on lines 1121 to 1142
##### What is the difference between tz and AT TIME ZONE?
`tz` and [AT TIME ZONE](../operators/other/#at-time-zone) have similar behavior that differs when the input timestamp **does not** have a timezone associated with it.

When a timestamp does not have a timezone associated with it (the default behavior in InfluxDB 3.0), `AT TIME ZONE` will cast a timestamp to the "wall clock" time in a particular time zone. This is the moment in time something would be experienced in a given time zone. This is a good way to define bounds on a query when you want them relative to some known range in a timezone (for example your local time).
If a timezone is associated with the timestamp, then `AT TIME ZONE` will behave the same as `tz`.

`tz` will **always** cast a timestamp to the "absolute" time in a particular time zone. This is a time relative to the unix epoch that has then be converted to that point in time in a given time zone. This is useful when you have data that was written as UTC or epoch timestamps, and you would like to view them in a specific timezone.

{{< expand-wrapper >}}
{{% expand "View `tz` and `::timestamp` comparison" %}}
```sql
SELECT
'2024-04-01T00:00:20Z'::timestamp AT TIME ZONE 'Europe/Brussels' as time_timestamp,
tz('2024-04-01T00:00:20', 'Europe/Brussels') as time_tz;
```

| time_timestamp | time_tz |
| :--------------------------- | :------------------------- |
| 2024-04-01T00:00:20+02:00 | 2024-04-01T02:00:20+02:00 |

{{% /expand %}}
{{< /expand-wrapper >}}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
##### What is the difference between tz and AT TIME ZONE?
`tz` and [AT TIME ZONE](../operators/other/#at-time-zone) have similar behavior that differs when the input timestamp **does not** have a timezone associated with it.
When a timestamp does not have a timezone associated with it (the default behavior in InfluxDB 3.0), `AT TIME ZONE` will cast a timestamp to the "wall clock" time in a particular time zone. This is the moment in time something would be experienced in a given time zone. This is a good way to define bounds on a query when you want them relative to some known range in a timezone (for example your local time).
If a timezone is associated with the timestamp, then `AT TIME ZONE` will behave the same as `tz`.
`tz` will **always** cast a timestamp to the "absolute" time in a particular time zone. This is a time relative to the unix epoch that has then be converted to that point in time in a given time zone. This is useful when you have data that was written as UTC or epoch timestamps, and you would like to view them in a specific timezone.
{{< expand-wrapper >}}
{{% expand "View `tz` and `::timestamp` comparison" %}}
```sql
SELECT
'2024-04-01T00:00:20Z'::timestamp AT TIME ZONE 'Europe/Brussels' as time_timestamp,
tz('2024-04-01T00:00:20', 'Europe/Brussels') as time_tz;
```
| time_timestamp | time_tz |
| :--------------------------- | :------------------------- |
| 2024-04-01T00:00:20+02:00 | 2024-04-01T02:00:20+02:00 |
{{% /expand %}}
{{< /expand-wrapper >}}
##### Differences between tz and AT TIME ZONE
`tz` and [`AT TIME ZONE`](/influxdb/clustered/reference/sql/operators/other/#at-time-zone)
differ when the input timestamp **does not** have a timezone.
- When using an input timestamp that does not have a timezone (the default behavior in InfluxDB) with the
`AT TIME ZONE` operator, the operator returns the the same timestamp, but with a timezone offset
(also known as the "wall clock" time)--for example:
```sql
'2024-01-01 00:00:00'::TIMESTAMP AT TIME ZONE 'America/Los_Angeles'
-- Returns
2024-01-01T00:00:00-08:00
```
- When using an input timestamp with a timezone, both the `tz()` function and the `AT TIME ZONE`
operator return the timestamp converted to the time in the specified timezone--for example:
```sql
'2024-01-01T00:00:00-00:00' AT TIME ZONE 'America/Los_Angeles'
tz('2024-01-01T00:00:00-00:00', 'America/Los_Angeles')
-- Both return
2023-12-31T16:00:00-08:00
```
- `tz()` always converts the input timestamp to the specified time zone.
If the input timestamp does not have a timezone, the function assumes it is a UTC timestamp--for example:
```sql
tz('2024-01-01 00:00:00'::TIMESTAMP, 'America/Los_Angeles')
-- Returns
2023-12-31T16:00:00-08:00
```
```sql
tz('2024-01-01T00:00:00+1:00', 'America/Los_Angeles')
-- Returns
2023-12-31T15:00:00-08:00
```
{{< expand-wrapper >}}
{{% expand "View `tz` and `::timestamp` comparison" %}}
```sql
SELECT
'2024-04-01T00:00:20Z'::timestamp AT TIME ZONE 'Europe/Brussels' as time_timestamp,
tz('2024-04-01T00:00:20', 'Europe/Brussels') as time_tz;
```
| time_timestamp | time_tz |
| :--------------------------- | :------------------------- |
| 2024-04-01T00:00:20+02:00 | 2024-04-01T02:00:20+02:00 |
{{% /expand %}}
{{< /expand-wrapper >}}

Copy link
Collaborator

@sanderson sanderson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀 Looks great!

@jeffreyssmith2nd jeffreyssmith2nd merged commit 4ef4aa5 into v3/timezone-support Oct 23, 2024
1 check passed
@jeffreyssmith2nd jeffreyssmith2nd deleted the smith/updated-timezone-docs branch October 23, 2024 17:18
sanderson added a commit that referenced this pull request Oct 30, 2024
* updated timezone-related functions and operators

* updated deps

* Apply suggestions from code review

Co-authored-by: Jason Stirnaman <[email protected]>

* updates to address PR feedback

* chore: remove unnecessary ::timestamp casts (#5596)

* feat: Update for latest SQL timezone behavior (#5657)

* feat: Update for latest SQL timezone behavior

* fix: cleanup and improve wording

Co-authored-by: Scott Anderson <[email protected]>

---------

Co-authored-by: Scott Anderson <[email protected]>

---------

Co-authored-by: Jason Stirnaman <[email protected]>
Co-authored-by: Jeffrey Smith II <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants