我有一个字符串类型为datetime的表。我想把它转换成utc时间戳。我的本地时区是cdt。我首先将datetime转换为时间戳。
table = table.withColumn('datetime_dt', unix_timestamp(col('datetime'), "yyyy-MM-dd HH:mm:ss").cast("timestamp"))
然后,我尝试将这个时间戳列转换为utc时间。
table = table.withColumn('datetime_UTC', to_utc_timestamp(table.datetime_dt, 'CDT'))
我也试过这个
table = table.withColumn('datetime_UTC', to_utc_timestamp(col('datetime_dt'), 'CDT'))
但它返回相同的结果。这里有一些例子。
------------------------------------------------------------------
| datetime | datetime_dt | datetime_UTC |
------------------------------------------------------------------
|2019-01-01 00:49:00 | 2019-01-01 00:49:00 | 2019-01-01 00:49:00 |
------------------------------------------------------------------
|2019-01-01 02:06:00 | 2019-01-01 02:06:00 | 2019-01-01 02:06:00 |
------------------------------------------------------------------
|2019-01-02 05:15:00 | 2019-01-02 05:15:00 | 2019-01-02 05:15:00 |
------------------------------------------------------------------
为什么它给出相同的时间而没有任何转换?我导入 to_utc_timestamp
从 pyspark.sql.functions
.
1条答案
按热度按时间dgjrabp21#
将时区指定为
CST
(或)America/Chicago
而不是CDT
,我们不必明确提到夏令时。spark内部计算并添加
+5:00/+6:00
以日期为准。Example:
```df.show()
+-------------------+
| dt|
+-------------------+
|2019-01-01 00:49:00|
|2019-11-01 00:49:00|
+-------------------+
df.withColumn('datetime_UTC', to_utc_timestamp(col('dt'), 'CST')).show(10,False)
or
df.withColumn('datetime_UTC', to_utc_timestamp(col('dt'), 'America/Chicago')).show(10,False)
+-------------------+---------------------+
|dt |datetime_UTC |
+-------------------+---------------------+
|2019-01-01 00:49:00|2019-01-01 06:49:00.0|
|2019-11-01 00:49:00|2019-11-01 05:49:00.0|
+-------------------+---------------------+