gpt4 book ai didi

python - 减去从字符串转换的日期时间时,total_seconds() 是错误的

转载 作者:行者123 更新时间:2023-12-04 12:59:00 25 4
gpt4 key购买 nike

total_seconds()当我这样做时是不正确的:

from datetime import timedelta, datetime
from pytz import timezone

timezone = timezone('Australia/Sydney')
startDate = datetime.now(timezone)
dateStr = '2020-05-18 20:12:30' # In our brain we know this time is in Sydney Time
endDate = datetime.strptime(dateStr, '%Y-%m-%d %H:%M:%S').replace(tzinfo=timezone)

diff = endDate - startDate
print(diff.total_seconds()) # incorrect answer

当两个 datetime 对象原本都是 datetime 对象时,你将它们相减,它们是对的
from datetime import timedelta, datetime
from pytz import timezone

timezone = timezone('Australia/Sydney')
startDate = datetime.now(timezone)
endDate = datetime.now(timezone) + timedelta(hours=2, seconds=32)

diff = endDate - startDate
print(diff.total_seconds()) # correct answer

我该如何解决我的问题?

最佳答案

因此,在日期时间、时区和偏移的可怕世界中,似乎所有事情都是这些奇怪而美妙的事情之一。这个问题似乎源于 pytz.timezone将返回一个包含多个时区的时区对象。

{
(datetime.timedelta(seconds=36300), datetime.timedelta(0), 'LMT'): <DstTzInfo 'Australia/Sydney' LMT+10:05:00 STD>,
(datetime.timedelta(seconds=36000), datetime.timedelta(0), 'AEST'): <DstTzInfo 'Australia/Sydney' AEST+10:00:00 STD>,
(datetime.timedelta(seconds=39600), datetime.timedelta(seconds=3600), 'AEDT'): <DstTzInfo 'Australia/Sydney' AEDT+11:00:00 DST>
}

似乎当您将时区传递给 now 方法时,它会根据您设置中的某些本地 TZINFO 从您选择的 3 个时区中选择时区。但是,当传递时区进行替换时,它只是选择相差 300 的 LMT。快速提及 LMT:

Local Mean Time Today: While Local Mean Time does not directly determine civil time these days, it is still used to make sure our clocks follow the Sun as closely as possible. UT1, a version of Universal Time is the Local Mean Time at the prime meridian in Greenwich, London. It is one of the components used to calculate Coordinated Universal Time (UTC), the time scale used to determine local times worldwide.

LMT is also used by astronomers around the world to time their observations.



基本上你的问题来自 datetime.now()作用于本地时区和 datetime.replace()作用于 LMT 时区。因此,正如我在我的帖子中提到的那样,始终如一地创建您的日期,或者通过替换来创建它们(尽管就实际时间而言,您仍然会相差 5 分钟,但差异是正确的。)

更新

如果你同时想要 datetime对象位于悉尼本地时间,然后您可以像使用 datetime.now() 之前一样创建您的日期。 .但是您应该根据时区对象创建结束日期,要求它根据您的喜好对其进行本地化。

from datetime import datetime
from pytz import timezone

timezone = timezone('Australia/Sydney')
startDate = datetime.now(timezone)
dateStr = '2020-05-18 18:52:30' # In our brain we know this time is in Sydney Time
endDate = timezone.localize(datetime.strptime(dateStr, '%Y-%m-%d %H:%M:%S'))
print(startDate, endDate, sep="\n")
diff = endDate - startDate
print(diff.total_seconds())

输出

2020-05-18 18:51:24.722614+10:00
2020-05-18 18:52:30+10:00
65.277386

关于python - 减去从字符串转换的日期时间时,total_seconds() 是错误的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61865054/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com