pyspark.sql.functions.convert_timezone#

pyspark.sql.functions.convert_timezone(sourceTz, targetTz, sourceTs)[source]#

Converts the timestamp without time zone sourceTs from the sourceTz time zone to targetTz.

New in version 3.5.0.

Parameters
sourceTzColumn, optional

The time zone for the input timestamp. If it is missed, the current session time zone is used as the source time zone.

targetTzColumn

The time zone to which the input timestamp should be converted.

sourceTsColumn or column name

A timestamp without time zone.

Returns
Column

A new column that contains a timestamp for converted time zone.

Examples

>>> spark.conf.set("spark.sql.session.timeZone", "America/Los_Angeles")

Example 1: Converts the timestamp without time zone sourceTs.

>>> import pyspark.sql.functions as sf
>>> df = spark.createDataFrame([('2015-04-08 00:00:00',)], ['ts'])
>>> df.select(
...     '*',
...     sf.convert_timezone(None, sf.lit('Asia/Hong_Kong'), 'ts')
... ).show() 
+-------------------+--------------------------------------------------------+
|                 ts|convert_timezone(current_timezone(), Asia/Hong_Kong, ts)|
+-------------------+--------------------------------------------------------+
|2015-04-08 00:00:00|                                     2015-04-08 15:00:00|
+-------------------+--------------------------------------------------------+

Example 2: Converts the timestamp with time zone sourceTs.

>>> import pyspark.sql.functions as sf
>>> df = spark.createDataFrame([('2015-04-08 15:00:00',)], ['ts'])
>>> df.select(
...     '*',
...     sf.convert_timezone(sf.lit('Asia/Hong_Kong'), sf.lit('America/Los_Angeles'), df.ts)
... ).show()
+-------------------+---------------------------------------------------------+
|                 ts|convert_timezone(Asia/Hong_Kong, America/Los_Angeles, ts)|
+-------------------+---------------------------------------------------------+
|2015-04-08 15:00:00|                                      2015-04-08 00:00:00|
+-------------------+---------------------------------------------------------+
>>> spark.conf.unset("spark.sql.session.timeZone")