trinodb / trino

Official repository of Trino, the distributed SQL query engine for big data, formerly known as PrestoSQL (https://trino.io)
https://trino.io
Apache License 2.0
10.3k stars 2.96k forks source link

Add a `timezone(timestamp(p) with time zone) -> varchar` extraction function to get IANA timezone ID #20893

Open stephen-zhao opened 7 months ago

stephen-zhao commented 7 months ago

Request

Similar to the current_timezone() function which returns the timezone of the current session, it would be nice to generalize this to allow extraction of timezone info from any provided timestamp(p) with time zone. Currently there are already functions to get timezone offsets (extract(TIMEZONE_HOUR from x) and extract(TIMEZONE_MINUTE from x)), but this loses information about the actual timezone. A lossless extraction function that would return the IANA timezone ID would be ideal.

Workaround

You can cast the timestamp(p) with time zone to a string, and manually extract the suffix.

hashhar commented 7 months ago

cc: @martint

ebyhr commented 7 months ago

The argument for EXTRACT is defined in SQL standard:

<time zone field> ::=
    TIMEZONE_HOUR
  | TIMEZONE_MINUTE

Is format_datetime function sufficient in your use case?

SELECT format_datetime(TIMESTAMP '2001-01-09 13:04:05.321 Asia/Tokyo', 'ZZZ');
   _col0
------------
 Asia/Tokyo
martint commented 3 months ago

Apologies for the delay. This is a reasonable request. To keep alignment with existing extraction functions (https://trino.io/docs/current/functions/datetime.html#convenience-extraction-functions), we should define it as:

timezone(<timestamp(p) with time zone>) -> varchar
timezone(<time(p) with time zone>) -> varchar
PhNorberg commented 3 weeks ago

Can I try this out?:)

hashhar commented 3 weeks ago

@PhNorberg Go ahead. Feel free to link your PR to this issue once you have it. Or ask on Slack for help.