cancel
Showing results forย 
Search instead forย 
Did you mean:ย 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

from_utc_timestamp silently double-shifts time when session timezone isn't UTC โ€” docs should call th

rikkyvai
New Contributor II

Summary:
from_utc_timestamp(current_timestamp(), '<tz>') produces an incorrect (future-shifted) timestamp whenever the Spark session timezone is already set to something other than UTC. This is a very common pattern for teams stamping dp_load_ts/created_at/audit columns, and the current docs don't warn against it.

Root cause:
current_timestamp() returns the current time in the session's configured timezone (spark.sql.session.timeZone / cluster timezone setting), not UTC. from_utc_timestamp(ts, tz) always treats its input ts as if it were UTC, regardless of what it actually is, and converts it into tz.

So if a cluster's session timezone is set to Asia/Bangkok (UTC+7):

SELECT current_timezone();
-- Asia/Bangkok

SELECT
  current_timestamp()                                    AS raw_ts,       -- correct Bangkok wall-clock time
  from_utc_timestamp(current_timestamp(), 'Asia/Bangkok') AS double_shifted -- Bangkok time + 7 more hours

 

 

double_shifted ends up 7 hours ahead of the actual current time, because current_timestamp() is already local, and from_utc_timestamp adds the offset a second time on top of it.

Why this matters:
from_utc_timestamp(current_timestamp(), '<local tz>') is a very natural-looking pattern to write when a team wants a "load timestamp in our local timezone," and it's silently wrong whenever the session timezone isn't UTC. It produces no error โ€” just a plausible-looking but incorrect future timestamp โ€” so it can go unnoticed in production pipelines for a long time.

Suggested fix:
Add an explicit warning/example on the from_utc_timestamp (and ideally current_timestamp()) doc pages, something like:

โš ๏ธ from_utc_timestamp always assumes its input is UTC. If your session timezone (current_timezone()) is not UTC, do not pass current_timestamp() directly into from_utc_timestamp() โ€” this will double-apply the timezone offset. If the session timezone already matches your target timezone, use current_timestamp() directly instead.




1 REPLY 1

Jen_Evangelista
New Contributor III

Hi rikkyvai, the audit-timestamp warning is useful. One nuance: with TIMESTAMP_LTZ, current_timestamp() represents an instant; the session timezone changes its display. from_utc_timestamp() shifts the value, rather than just formatting it.

A useful check is to compare epoch values in both UTC and Asia/Bangkok sessions:

SELECT unix_micros(from_utc_timestamp(current_timestamp(), 'Asia/Bangkok')) - unix_micros(current_timestamp()) AS difference_us;

The expected difference for this timezone is 25200000000 microseconds, or seven hours, in either session timezone. The shift therefore is not limited to non-UTC sessions. This was checked on local Spark 3.5.3 in the earlier lab; please verify the exact timestamp type and DBR behavior in your workspace.

For an audit instant, store current_timestamp() directly and choose the display timezone when reading it. Treat TIMESTAMP_NTZ and strings with timezone offsets as separate cases.

Reference: https://spark.apache.org/docs/latest/api/python/reference/pyspark.sql/api/pyspark.sql.functions.from...