pyspark.sql.functions.convert_timezone

pyspark.sql.functions.convert_timezone(sourceTz: Optional[pyspark.sql.column.Column], targetTz: pyspark.sql.column.Column, sourceTs: ColumnOrName) → pyspark.sql.column.Column[source]

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

New in version 3.5.0.

Parameters
sourceTzColumn

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

timestamp for converted time zone.

Examples

>>> df = spark.createDataFrame([('2015-04-08',)], ['dt'])
>>> df.select(convert_timezone(   
...     None, lit('Asia/Hong_Kong'), 'dt').alias('ts')
... ).show()
+-------------------+
|                 ts|
+-------------------+
|2015-04-08 00:00:00|
+-------------------+
>>> df.select(convert_timezone(
...     lit('America/Los_Angeles'), lit('Asia/Hong_Kong'), 'dt').alias('ts')
... ).show()
+-------------------+
|                 ts|
+-------------------+
|2015-04-08 15:00:00|
+-------------------+