snowflake.snowpark.functions.convert_timezone¶

snowflake.snowpark.functions.convert_timezone(target_timezone: Union[Column, str], source_time: Union[Column, str], source_timezone: Optional[Union[Column, str]] = None) → Column[source]¶

Converts the given source_time to the target timezone.

For timezone information, refer to the Snowflake SQL convert_timezone notes

Args:

target_timezone: The time zone to which the input timestamp should be converted.= source_time: The timestamp to convert. When it’s a TIMESTAMP_LTZ, use None for source_timezone. source_timezone: The time zone for the source_time. Required for timestamps with no time zone (i.e. TIMESTAMP_NTZ). Use None if the timestamps have a time zone (i.e. TIMESTAMP_LTZ). Default is None.

Note:

The sequence of the 3 params is different from the SQL function, which two overloads:

  • CONVERT_TIMEZONE( <source_tz> , <target_tz> , <source_timestamp_ntz> )

  • CONVERT_TIMEZONE( <target_tz> , <source_timestamp> )

The first parameter source_tz is optional. But in Python an optional argument shouldn’t be placed at the first. So source_timezone is after source_time.

Example:
>>> import datetime
>>> from dateutil import tz
>>> datetime_with_tz = datetime.datetime(2022, 4, 6, 9, 0, 0, tzinfo=tz.tzoffset("myzone", -3600*7))
>>> datetime_with_no_tz = datetime.datetime(2022, 4, 6, 9, 0, 0)
>>> df = session.create_dataframe([[datetime_with_tz, datetime_with_no_tz]], schema=["a", "b"])
>>> result = df.select(convert_timezone(lit("UTC"), col("a")), convert_timezone(lit("UTC"), col("b"), lit("Asia/Shanghai"))).collect()
>>> result[0][0]
datetime.datetime(2022, 4, 6, 16, 0, tzinfo=<UTC>)
>>> result[0][1]
datetime.datetime(2022, 4, 6, 1, 0)
Copy