PySpark date_trunc 修改时区:如何防止?

PySpark date_trunc modifies the timezone: how to prevent it?

上下文: 我使用从 pyspark.sql.functions 导入的 date_trunc 函数将时间戳截断为分钟。

df_truncated = df.withColumn('dt', date_trunc('minute', df["timestamp"]))
df_truncated.show(truncate=False)

输出如下

+------------------------+-------------------+
|timestamp               |dt                 |
+------------------------+-------------------+
|2020-01-02T00:30:47.178Z|2020-01-02 02:30:00|
|2020-01-02T00:30:47.160Z|2020-01-02 02:30:00|
|2020-01-02T00:30:46.327Z|2020-01-02 02:30:00|
|2020-01-02T00:30:45.003Z|2020-01-02 02:30:00|
|2020-01-02T00:30:44.054Z|2020-01-02 02:30:00|
+------------------------+-------------------+

问题:问题是 "adds" 两个小时到原始时间戳 - 从 utc 转换为本地时间。

问题:如何避免这种情况?我是否需要手动截断时间戳或 date_trunc 函数的某些参数未记录?或者我是否需要访问 spark 全局设置,如果是,那么如何或哪些设置?

你能试试这个吗,然后告诉我。

##  Here i am selecting the substring of the column "timestamp". Choose everthing till the seconds and convert that to a timestamp.

df.withColumn("hour", F.to_timestamp(F.substring("timestamp_value", 0, 19), "yyyy-MM-dd'T'HH:mm:ss")).show()

+-------------------------+-------------------+
|timestamp                |hour               |
+-------------------------+-------------------+
|2017-08-01T14:30:00+05:30|2017-08-01 14:30:00|
|2017-08-01T14:30:00+06:30|2017-08-01 14:30:00|
|2017-08-01T14:30:00+07:30|2017-08-01 14:30:00|
+-------------------------+-------------------+

更多技巧可以参考link:Link