我需要 EST 中的当前时间戳,但
current_timestamp()
正在返回 PST。
尝试了以下代码,但它不起作用并显示 EST 时间之前 6 小时:
# Import the current_timestamp function
from pyspark.sql.functions import from_utc_timestamp
# Set the timezone to EST
spark.conf.set("spark.sql.session.timeZone", "EST")
# Get the current timestamp in EST
current_timestamp_est = spark.sql("SELECT from_utc_timestamp(current_timestamp(), 'EST') as current_timestamp_est")
# Show the current timestamp in EST
current_timestamp_est.show(truncate = False)
还尝试了下面的代码。但时间即将到来
2024-03-11 23:16:04.589275-04:00
。为什么它带有-4:00
?有没有办法摆脱-4:00
?
from datetime import datetime
from pytz import timezone
est = timezone('US/Eastern')
now_est = datetime.now(est)
#now_est1 = now_est[:26]
print(now_est)
还有其他方法可以在变量中获取 EST 中的当前时间戳吗?
要摆脱
-04:00
,您可以在第二种方法中使用 .strftime()
:
from datetime import datetime
from pytz import timezone
est = timezone('US/Eastern')
now_est = datetime.now(est).strftime('%Y-%m-%d %H:%M:%S')
print(now_est)
现在,对于第一种方法,不要将
EST
设置为时区 - 最好指出全球各地的一个位置,例如 America/New_York
,以消除偏移值 -04:00
,如下所示:
spark.conf.set("spark.sql.session.timeZone", "America/New_York")
current_timestamp_est = spark.sql("SELECT current_timestamp() as current_timestamp_est")
current_timestamp_est.show(truncate=False)