时间在使用pytz和datetime模块进行strptime和strftime转换时的差异。

huangapple go评论58阅读模式
英文:

Difference seen when time is converted between strptime and strftime while using pytz and datetime module

问题

original_value = (datetime.datetime.now(pytz.utc)) + datetime.timedelta(minutes=30)
current_schedule = original_value.strftime("%Y-%m-%dT%H:%M:%S.%fZ")
new_value = datetime.datetime.strptime(current_schedule, "%Y-%m-%dT%H:%M:%S.%fZ").timestamp()

现在理论上 original_value.timestamp() 应该等于 new_value 变量,但你会注意到一个等于或接近其机器所在时区的时间差异。

对我来说,时间差大约为17000秒或19000秒,这也不一致。

如何在两者之间一致地找到差异,使其理想情况下应该为0秒。

我使用的函数:

import datetime
import pytz

ab = (datetime.datetime.now(pytz.utc)) + datetime.timedelta(minutes=30)
current_schedule = ab.strftime("%Y-%m-%dT%H:%M:%S.%fZ")
new_time = datetime.datetime.strptime(current_schedule, "%Y-%m-%dT%H:%M:%S.%fZ").timestamp()
print("Value Initially: {}".format(ab.timestamp()))
print("Value post conversion: {}".format(new_time))
time_diff = new_time - ab.timestamp()
print(time_diff)

输出:

Value Initially: 1680806908.778667
Value post conversion: 1680787108.778667
-19800.0

请解释为什么会发生这种情况?转换后的时间应该与原始时间相同,因为它们只是从一种形式转换为另一种形式!

如何解决这个问题?

英文:
original_value = (datetime.datetime.now(pytz.utc)) + datetime.timedelta(minutes=30)
current_schedule = original_value..strftime("%Y-%m-%dT%H:%M:%S.%fZ")
new_value = datetime.datetime.strptime(current_schedule,"%Y-%m-%dT%H:%M:%S.%fZ").timestamp()

Now theoretically original_value.timestamp() should be equal to new_value variable, but one will notice a time difference of equivalent or at least close to time zone their machine is in.
For me it was around 17000 seconds or 19000 seconds, That was also not consistent.

How to consistently find the difference between both of them to be 0 seconds as ideally they should be.

Function I used:

import datetime
import pytz

ab = (datetime.datetime.now(pytz.utc)) + datetime.timedelta(minutes=30)
current_schedule = ab.strftime("%Y-%m-%dT%H:%M:%S.%fZ")
new_time = datetime.datetime.strptime(current_schedule,"%Y-%m-%dT%H:%M:%S.%fZ").timestamp()
print("Value Initially: {}".format(ab.timestamp()))
print("Value post converstion: {}".format(new_time))
time_diff = new_time - ab.timestamp()
print(time_diff)

Output:

Value Initially: 1680806908.778667
Value post converstion: 1680787108.778667
-19800.0

Please explain why this happened? Post time should be same as pre time as they are same only being transformed from one to other!

How to fix this issue?

答案1

得分: 1

ab 包含带时区信息的时间数据,而

new_time = datetime.datetime.strptime(current_schedule, "%Y-%m-%dT%H:%M:%S.%fZ")

不包含时区信息,因此在调用 timestamp 方法时将使用本地时区作为参考。因此,ab.timestamp() 将以UTC时间为基准,而 new_time 将以本地时区表示。您将得到本地时区与UTC之间的差异(以秒为单位)。

要解决此问题,在使用 timestamp() 之前,请指定时区。可以通过使用 replace(tzinfo=pytz.UTC) 或更好地在 strftimestrptime 中使用格式 "%Y-%m-%dT%H:%M:%S.%f%z" 来实现。

英文:

ab has time with timezone data where as

new_time = datetime.datetime.strptime(current_schedule,"%Y-%m-%dT%H:%M:%S.%fZ")

won't be having the timezone details which make it to use your local timezone as the reference while calling timestamp method. So, ab.timestamp() will be in UTC and the value new_time will be in local timezone. The difference you get will be the difference between the local timezone and UTC in seconds.

To resolve this, specify the timezone before using timestamp(). This can be done either by using replace(tzinfo=pytz.UTC) or better use the format "%Y-%m-%dT%H:%M:%S.%f%z" in both strftime and strptime.

huangapple
  • 本文由 发表于 2023年4月7日 02:29:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75952676.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定