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

A timestamp without time zone.

Returns
Column

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

Examples

Example 1: Converts the timestamp without time zone sourceTs,

the source time zone sourceTz is None.

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

Example 2: Converts the timestamp without time zone sourceTs.

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