View a markdown version of this page

Time zone functions - Amazon Athena

Time zone functions

Use these functions to convert between an instant and the local time in a named time zone, or to read the UTC offset of a value. You can also list the time zone names that DuckDB recognizes.

pg_timezone_names function

Returns a table with one row for each time zone name that DuckDB recognizes. Use it to find a valid name to pass to AT TIME ZONE, the timezone function, or make_timestamptz. The table includes both current names and older names that refer to the same zone, such as Asia/Kolkata and Asia/Calcutta.

The utc_offset and is_dst columns describe the zone at the time that the query runs. For a zone that observes daylight saving time, such as America/New_York, these values vary during the year. The abbrev column holds a short name only for some zones, such as IST for Asia/Kolkata. For most zones, including America/New_York, it repeats the name.

Syntax

pg_timezone_names()

Return type

A table with the columns name (VARCHAR), abbrev (VARCHAR), utc_offset (INTERVAL), and is_dst (BOOLEAN).

Examples

The following SQL query returns the abbreviation and UTC offset of two time zones that do not observe daylight saving time.

SELECT name, abbrev, utc_offset, is_dst FROM pg_timezone_names() WHERE name IN ('Asia/Kolkata', 'Asia/Tokyo') ORDER BY name

The query returns the following results.

name abbrev utc_offset is_dst

Asia/Kolkata

IST

05:30:00

false

Asia/Tokyo

JST

09:00:00

false

The following SQL query finds the names that contain Kolkata or Calcutta. Both names refer to the time zone of Kolkata, India.

SELECT name FROM pg_timezone_names() WHERE name ILIKE '%kolkata%' OR name ILIKE '%calcutta%' ORDER BY name

The query returns the following results.

name

Asia/Calcutta

Asia/Kolkata

Considerations

The name column holds each name in its usual mix of uppercase and lowercase letters, such as Asia/Tokyo. An equality test with different case, such as name = 'asia/tokyo', finds no rows. To search without regard to case, use ILIKE.

timezone function

With a time zone name, timezone(zone, value) converts between an instant and the local time in zone. For a TIMESTAMP WITH TIME ZONE value, it returns the local date and time in zone as a TIMESTAMP. For a TIMESTAMP value, it reads the value as a local time in zone and returns that instant as a TIMESTAMP WITH TIME ZONE. This is the same conversion as value AT TIME ZONE zone. For a DATE value, it reads midnight at the start of that date as a local time in zone and returns that instant as a TIMESTAMP WITH TIME ZONE.

For a TIME WITH TIME ZONE value, the function keeps the same UTC time and changes the offset. The new offset is the offset of zone, or offset when you pass an INTERVAL.

With one argument, timezone(value) returns the UTC offset of value in seconds, the same as extract(timezone FROM value). For a TIME WITH TIME ZONE value, this is the offset stored in the value. For a TIMESTAMP WITH TIME ZONE value, it is the offset of the session time zone, which is UTC, so the result is 0. For TIME and TIMESTAMP values, the result is 0.

If any argument is NULL, the result is NULL.

Syntax

timezone(zone, value) timezone(offset, time_tz) timezone(value)

Arguments

The timezone function takes the following arguments:

zone

The time zone, as a VARCHAR time zone name, such as 'America/New_York' or 'UTC'. To list the names that you can use, query pg_timezone_names().

value

The value to convert with zone, as a DATE, TIMESTAMP, TIMESTAMP WITH TIME ZONE, or TIME WITH TIME ZONE value. In the one-argument form, it is the value whose UTC offset to return, as a TIME, TIME WITH TIME ZONE, TIMESTAMP, or TIMESTAMP WITH TIME ZONE value.

offset

The new UTC offset, as an INTERVAL, such as INTERVAL '2 hours'.

time_tz

The time to give the new offset, as a TIME WITH TIME ZONE value.

Return type

With zone or offset, timezone returns a TIMESTAMP for a TIMESTAMP WITH TIME ZONE value, and a TIMESTAMP WITH TIME ZONE for a TIMESTAMP or DATE value. It returns a TIME WITH TIME ZONE for a TIME WITH TIME ZONE value. With zone, timezone returns the nanosecond form for a nanosecond value. It returns a TIMESTAMP_NS for a TIMESTAMPTZ_NS value, and a TIMESTAMPTZ_NS for a TIMESTAMP_NS value. With one argument, timezone returns a BIGINT.

Examples

The following SQL query returns the local time in New York at midnight UTC on January 1, 2021. The result is a TIMESTAMP without a time zone.

SELECT timezone( 'America/New_York', TIMESTAMPTZ '2021-01-01 00:00:00+00' ) AS result

The query returns the following result.

result

2020-12-31 19:00:00

The following SQL query reads midnight on January 1, 2021, as a local time in New York, and returns that instant. It is shown in UTC, the session time zone.

SELECT timezone( 'America/New_York', TIMESTAMP '2021-01-01 00:00:00' ) AS result

The query returns the following result.

result

2021-01-01 05:00:00+00

The following SQL query reads the date January 1, 2021, as midnight in New York, and returns that instant. The result is a TIMESTAMP WITH TIME ZONE, shown in UTC.

SELECT timezone( 'America/New_York', DATE '2021-01-01' ) AS result

The query returns the following result.

result

2021-01-01 05:00:00+00

The following SQL query returns the UTC offset in seconds of a time with an offset of -07:00, and of a timestamp without a time zone.

SELECT timezone(TIMETZ '18:18:16-07:00') AS timetz_offset, timezone( TIMESTAMP '2021-08-03 11:59:44.123456' ) AS timestamp_offset

The query returns the following result.

timetz_offset timestamp_offset

-25200

0

The following SQL query gives a TIME WITH TIME ZONE value a UTC offset of +02:00. The UTC time stays the same, so the local time moves forward by two hours.

SELECT timezone( INTERVAL '2 hours', TIMETZ '10:00:00+00' ) AS result

The query returns the following result.

result

12:00:00+02

Considerations

An unknown time zone name makes the query fail.

The conversion follows daylight saving time. For example, midnight in New York is 05:00 in UTC in January and 04:00 in UTC in July.

The one-argument form does not accept DATE or INTERVAL values. The query fails.

Differences from Athena engine version 3

In Athena engine version 3, value AT TIME ZONE zone reads a timestamp without a time zone as UTC and returns the local time in zone. In DuckDB, timezone(zone, value) and AT TIME ZONE do the opposite conversion: they read value as a local time in zone. To get the Athena engine version 3 result, cast the value first, as in timezone(zone, CAST(value AS TIMESTAMPTZ)).

Athena engine version 3 has at_timezone(value, zone) and with_timezone(value, zone), which DuckDB does not have. Use timezone(zone, value), which takes the zone as the first argument. For a TIMESTAMP WITH TIME ZONE value, it returns the local time as a TIMESTAMP. For a TIMESTAMP value, it returns the instant as a TIMESTAMP WITH TIME ZONE.

timezone_hour function

Returns the whole hours of the UTC offset of value, with the sign of the offset. For a TIME WITH TIME ZONE value, this is the offset stored in the value, so an offset of +05:45 returns 5 and an offset of -03:30 returns -3. For a TIMESTAMP WITH TIME ZONE value, it is the offset of the session time zone, which is UTC, so the result is 0. For TIME and TIMESTAMP values, the result is 0. If value is NULL, the result is NULL.

timezone_hour(value) returns the same result as extract(timezone_hour FROM value). To get the minutes of the offset, use the timezone_minute function, and to get the whole offset in seconds, use the timezone function.

Syntax

timezone_hour(value)

Arguments

The timezone_hour function takes the following argument:

value

The value whose UTC offset to read, as a TIME, TIME WITH TIME ZONE, TIMESTAMP, or TIMESTAMP WITH TIME ZONE value.

Return type

BIGINT

Examples

The following SQL query returns the hours of a UTC offset of +05:45.

SELECT timezone_hour(TIMETZ '10:00:00+05:45') AS result

The query returns the following result.

result

5

The following SQL query returns both parts of a negative offset. Both parts have the sign of the offset.

SELECT timezone_hour( TIMETZ '10:00:00-03:30' ) AS offset_hours, timezone_minute( TIMETZ '10:00:00-03:30' ) AS offset_minutes

The query returns the following result.

offset_hours offset_minutes

-3

-30

The following SQL query passes a timestamp without a time zone and a timestamp with an offset of +05. Both calls return 0, because the second value does not keep the +05 offset, and the session time zone is UTC.

SELECT timezone_hour( TIMESTAMP '2021-08-03 11:59:44.123456' ) AS timestamp_hours, timezone_hour( TIMESTAMPTZ '2021-08-03 11:59:44+05' ) AS timestamptz_hours

The query returns the following result.

timestamp_hours timestamptz_hours

0

0

Considerations

A TIMESTAMP WITH TIME ZONE value does not keep the offset that you wrote in it. timezone_hour(TIMESTAMPTZ '2021-08-03 11:59:44+05') returns 0, not 5.

DATE and INTERVAL values make the query fail.

Differences from Athena engine version 3

In Athena engine version 3, a timestamp with a time zone keeps its zone, so timezone_hour returns the hours of the offset written in the value, such as 5 for +05:00. In DuckDB, a TIMESTAMP WITH TIME ZONE value is shown in the session time zone, UTC, and timezone_hour returns 0. Only TIME WITH TIME ZONE values keep their offset.

timezone_minute function

Returns the minutes of the UTC offset of value that remain after whole hours are removed, with the sign of the offset. For a TIME WITH TIME ZONE value, this is the offset stored in the value, so an offset of +05:45 returns 45 and an offset of -03:30 returns -30. For a TIMESTAMP WITH TIME ZONE value, it is the offset of the session time zone, which is UTC, so the result is 0. For TIME and TIMESTAMP values, the result is 0. If value is NULL, the result is NULL.

timezone_minute(value) returns the same result as extract(timezone_minute FROM value). To get the hours of the offset, use the timezone_hour function, and to get the whole offset in seconds, use the timezone function.

Syntax

timezone_minute(value)

Arguments

The timezone_minute function takes the following argument:

value

The value whose UTC offset to read, as a TIME, TIME WITH TIME ZONE, TIMESTAMP, or TIMESTAMP WITH TIME ZONE value.

Return type

BIGINT

Examples

The following SQL query returns the minutes of a UTC offset of +05:45.

SELECT timezone_minute(TIMETZ '10:00:00+05:45') AS result

The query returns the following result.

result

45

The following SQL query returns the minutes of a negative offset. The result has the sign of the offset.

SELECT timezone_minute(TIMETZ '10:00:00-03:30') AS result

The query returns the following result.

result

-30

The following SQL query passes a timestamp with an offset of +05:30. The result is 0, because the value does not keep the +05:30 offset, and the session time zone is UTC.

SELECT timezone_minute( TIMESTAMPTZ '2021-08-03 11:59:44+05:30' ) AS result

The query returns the following result.

result

0

Considerations

A TIMESTAMP WITH TIME ZONE value does not keep the offset that you wrote in it. timezone_minute(TIMESTAMPTZ '2021-08-03 11:59:44+05:30') returns 0, not 30.

DATE and INTERVAL values make the query fail.

Differences from Athena engine version 3

In Athena engine version 3, a timestamp with a time zone keeps its zone, so timezone_minute returns the minutes of the offset written in the value, such as 30 for +05:30. In DuckDB, a TIMESTAMP WITH TIME ZONE value is shown in the session time zone, UTC, and timezone_minute returns 0. Only TIME WITH TIME ZONE values keep their offset.