Kontext Kontext

Spark SQL - Conversion between UTC and Timestamp with Time Zone

event 2022-06-04 visibility 18,789 comment 0 insights toc
more_vert
insights Stats
toc Table of contents

In Spark SQL, function from_utc_timestamp(timestamp, timezone) converts UTC timestamp to a timestamp in the given time zone; function to_utc_timestamp(timestamp, timezone) converts timestamp in a given time zone to UTC timestamp.

Code snippets

The following are some examples to convert UTC to the time with time zone information and vice versa.

spark-sql> select from_utc_timestamp('2022-06-04 21:34:29.538','Australia/Melbourne');
from_utc_timestamp(CAST(2022-06-04 21:34:29.538 AS TIMESTAMP), Australia/Melbourne)
2022-06-05 07:34:29.538

spark-sql> select to_utc_timestamp('2022-06-04 21:34:29.538','UTC+10');
to_utc_timestamp(CAST(2022-06-04 21:34:29.538 AS TIMESTAMP), UTC+10)
2022-06-04 11:34:29.538

spark-sql> select to_utc_timestamp('2022-06-04 21:34:29.538','Australia/Melbourne');
to_utc_timestamp(CAST(2022-06-04 21:34:29.538 AS TIMESTAMP), Australia/Melbourne)
2022-06-04 11:34:29.538

Time zone ID list

You can find the complete list of time zone ID here:

List of tz database time zones - Wikipedia

You can also use GMT and UTC timezone string, for example, 'GMT+1', 'UTC+10', etc. 

More from Kontext
comment Comments
No comments yet.

Please log in or register to comment.

account_circle Log in person_add Register

Log in with external accounts