Local timestamp issue with Synpase notebook

Lotus88 136 Reputation points
2025-04-29T10:04:43.4766667+00:00

Hi,

I am using Synapse notebook to write a record with system update timestamp.

I wrote the following code in the beginning of the notebook code to adjust timestamp to UTC+8.

# Adjust timestamps to Asia/Singapore time
spark.conf.set("spark.sql.session.timeZone", "Asia/Singapore")

Then after all the db connection and query data to dataframes, I added a new system update timestamp field to the result_df.

result_df = select_df.withColumn("sys_update_dt", current_timestamp())

Then I write the records to a destination table. But when I checked the "sys_update_dt", it is populated with UTC timestamp. I expect UTC+8 timestamp. Does anyone have any idea what went wrong? Thanks in advance.

result_df.write.option("truncate", "true").jdbc(
    url=jdbc_url_db2,
    table="dest_stage",
    mode="overwrite",
    properties=conn_properties_db2
)
Azure Synapse Analytics
Azure Synapse Analytics
An Azure analytics service that brings together data integration, enterprise data warehousing, and big data analytics. Previously known as Azure SQL Data Warehouse.
5,318 questions
0 comments No comments
{count} votes

Accepted answer
  1. Smaran Thoomu 22,840 Reputation points Microsoft External Staff
    2025-04-29T11:14:32.1966667+00:00

    Hi @Lotus88
    Thanks for reaching out and for sharing the details of your notebook setup.

    You have correctly set the session time zone using:

    spark.conf.set("spark.sql.session.timeZone", "Asia/Singapore")
    

    However, the function current_timestamp() still returns the timestamp in UTC regardless of this setting, especially in PySpark contexts. This setting mainly affects Spark SQL queries, not necessarily PySpark API functions.

    To ensure the timestamp reflects the correct local time (UTC+8), you can explicitly convert it using:

    from pyspark.sql.functions import current_timestamp, from_utc_timestamp
    result_df = select_df.withColumn(
        "sys_update_dt", from_utc_timestamp(current_timestamp(), "Asia/Singapore")
    )
    

    User's image

    This ensures that the timestamp column reflects UTC+8 accurately when written to your destination.

    Let me know if you would like help validating this with your specific destination system. I’ve also attached screenshots showing the behavior and the fix in action for reference.

    Hope this helps! Please feel free to reach out if you have any more questions.


    If this answers your query, do click Accept Answer and Yes for was this answer helpful. And, if you have any further query do let us know.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.