我有一个表,其中有字符串类型的日期时间。我想将其转换为 UTC 时间戳。我本地的时区是 CDT。我首先将日期时间转换为时间戳。
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 |
------------------------------------------------------------------
为什么它给出的时间相同而没有任何转换?我从 pyspark.sql.functions
导入 to_utc_timestamp
。
最佳答案
将时区指定为 CST
(或)America/Chicago
而不是 CDT
,我们不必明确提及夏令时。
- Spark 在内部根据日期计算并添加
+5:00/+6:00
。
示例:
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|
#+-------------------+---------------------+
关于apache-spark - PySpark to_utc_timestamp 返回相同时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60725934/