奇怪的 Python 日期比较行为
Weird Python Date Comparison Behaviour
我遇到了奇怪的 Python 日期时间比较问题。我得到两个字符串格式的日期时间(来自 REST 调用,JSON)开始日期时间和结束日期时间。我必须比较当前日期时间,如果它落在这个范围内(开始日期时间和结束日期时间)采取一些行动。已知这些日期的时区是US/Eastern(美国北卡罗来纳州)。这是代码,
from pytz import timezone
from datetime import datetime
def main():
# All the dates are in US/Eastern time zone. Time zone for USA, North Carolina
# These dates are received from REST call in JSON
# I need to compare the current date time, if that falls within this range take some action
start_date_str = "7/17/2018 11:30:00 AM"
end_date_str = "7/17/2018 2:30:00 PM"
# To simulate current date time within the range, I will update the current date time value.
current_est_time = datetime.now(tz=timezone('US/Eastern'))
current_est_time = current_est_time.replace(year=2018, month=7, day=17, hour=12, minute=26)
print current_est_time
start_date = datetime.strptime(start_date_str,"%m/%d/%Y %I:%M:%S %p").replace(tzinfo=timezone('US/Eastern'))
end_date = datetime.strptime(end_date_str, "%m/%d/%Y %I:%M:%S %p").replace(tzinfo=timezone('US/Eastern'))
print start_date <= current_est_time <= end_date
if __name__ == '__main__':
main()
如果分钟值是26,比较打印True,如果是25,打印False。
以上代码示例的输出为
2018-07-17 12:26:06.646643-04:00
True
如果将 current_est_time 变量的分钟值更改为 25,则输出为
2018-07-17 12:25:16.582573-04:00
False
有人可以帮我吗,我哪里出错了?
错误是由不同的偏移 datetime.now() 调用引起的,returns:
2018-07-17 12:26:06.646643-04:00
但是您的日期比较没有类似的结构,它们在比较之前将如下所示:
2018-07-17 14:30:00-04:56
两个值的偏移量不同,当前为 4:00,开始和结束日期为 4:56
以下代码将修复它:
from pytz import timezone
from datetime import datetime
def main():
# All the dates are in US/Eastern time zone. Time zone for USA, North Carolina
# These dates are received from REST call in JSON
# I need to compare the current date time, if that falls within this range take some action
start_date_str = "7/17/2018 11:30:00 AM"
end_date_str = "7/17/2018 2:30:00 PM"
# To simulate current date time within the range, I will update the current date time value.
current_est_time = datetime.now(tz=timezone('US/Eastern'))
current_est_time = current_est_time.replace(year=2018, month=7, day=17, hour=12, minute=24).replace(tzinfo=timezone("US/Eastern"))
print (current_est_time)
start_date = datetime.strptime(start_date_str,"%m/%d/%Y %I:%M:%S %p").replace(tzinfo=timezone('US/Eastern'))
end_date = datetime.strptime(end_date_str, "%m/%d/%Y %I:%M:%S %p").replace(tzinfo=timezone('US/Eastern'))
print(start_date)
print(current_est_time)
print(end_date)
print (start_date <= current_est_time <= end_date)
if __name__ == '__main__':
main()
P.S。它是用 python3 编写的,但应该可以正常工作
进一步调试此问题后,.replace 方法如何处理 datetime 对象的时区信息似乎存在问题。如果我们使用 .replace 方法添加时区,start_date 中的 tzinfo 对象有 _tzname = "LMT" 和 current_est_time 中的 tzinfo 对象有 _tzname = "EDT"。这就是为什么比较结果不一致的原因。
通过参考 Why doesn't pytz localize() produce a datetime object with tzinfo matching the tz object that localized it?
,看起来 "EDT" 是正确的值。所以在我看来,这是实现它的正确方法,
from datetime import datetime
import pytz
def main():
process_date_time(25)
process_date_time(26)
def process_date_time(min):
# All the dates are in US/Eastern time zone. Time zone for USA, North Carolina
# These dates are received from REST call in JSON
# I need to compare the current date time, if that falls within this range take some action
tz = pytz.timezone('US/Eastern')
start_date_str = "7/17/2018 11:30:00 AM"
end_date_str = "7/17/2018 2:30:00 PM"
# To simulate current date time within the range, I will update the current date time value.
dt = datetime.now()
current_est_time = tz.localize(dt)
current_est_time = current_est_time.replace(year=2018, month=7, day=17, hour=12, minute=min)
print current_est_time
start_date_1 = datetime.strptime(start_date_str, "%m/%d/%Y %I:%M:%S %p")
end_date_1 = datetime.strptime(end_date_str, "%m/%d/%Y %I:%M:%S %p")
start_date = tz.localize(start_date_1)
end_date = tz.localize(end_date_1)
print start_date <= current_est_time <= end_date
if __name__ == '__main__':
main()
我遇到了奇怪的 Python 日期时间比较问题。我得到两个字符串格式的日期时间(来自 REST 调用,JSON)开始日期时间和结束日期时间。我必须比较当前日期时间,如果它落在这个范围内(开始日期时间和结束日期时间)采取一些行动。已知这些日期的时区是US/Eastern(美国北卡罗来纳州)。这是代码,
from pytz import timezone
from datetime import datetime
def main():
# All the dates are in US/Eastern time zone. Time zone for USA, North Carolina
# These dates are received from REST call in JSON
# I need to compare the current date time, if that falls within this range take some action
start_date_str = "7/17/2018 11:30:00 AM"
end_date_str = "7/17/2018 2:30:00 PM"
# To simulate current date time within the range, I will update the current date time value.
current_est_time = datetime.now(tz=timezone('US/Eastern'))
current_est_time = current_est_time.replace(year=2018, month=7, day=17, hour=12, minute=26)
print current_est_time
start_date = datetime.strptime(start_date_str,"%m/%d/%Y %I:%M:%S %p").replace(tzinfo=timezone('US/Eastern'))
end_date = datetime.strptime(end_date_str, "%m/%d/%Y %I:%M:%S %p").replace(tzinfo=timezone('US/Eastern'))
print start_date <= current_est_time <= end_date
if __name__ == '__main__':
main()
如果分钟值是26,比较打印True,如果是25,打印False。
以上代码示例的输出为
2018-07-17 12:26:06.646643-04:00
True
如果将 current_est_time 变量的分钟值更改为 25,则输出为
2018-07-17 12:25:16.582573-04:00
False
有人可以帮我吗,我哪里出错了?
错误是由不同的偏移 datetime.now() 调用引起的,returns:
2018-07-17 12:26:06.646643-04:00
但是您的日期比较没有类似的结构,它们在比较之前将如下所示:
2018-07-17 14:30:00-04:56
两个值的偏移量不同,当前为 4:00,开始和结束日期为 4:56
以下代码将修复它:
from pytz import timezone
from datetime import datetime
def main():
# All the dates are in US/Eastern time zone. Time zone for USA, North Carolina
# These dates are received from REST call in JSON
# I need to compare the current date time, if that falls within this range take some action
start_date_str = "7/17/2018 11:30:00 AM"
end_date_str = "7/17/2018 2:30:00 PM"
# To simulate current date time within the range, I will update the current date time value.
current_est_time = datetime.now(tz=timezone('US/Eastern'))
current_est_time = current_est_time.replace(year=2018, month=7, day=17, hour=12, minute=24).replace(tzinfo=timezone("US/Eastern"))
print (current_est_time)
start_date = datetime.strptime(start_date_str,"%m/%d/%Y %I:%M:%S %p").replace(tzinfo=timezone('US/Eastern'))
end_date = datetime.strptime(end_date_str, "%m/%d/%Y %I:%M:%S %p").replace(tzinfo=timezone('US/Eastern'))
print(start_date)
print(current_est_time)
print(end_date)
print (start_date <= current_est_time <= end_date)
if __name__ == '__main__':
main()
P.S。它是用 python3 编写的,但应该可以正常工作
进一步调试此问题后,.replace 方法如何处理 datetime 对象的时区信息似乎存在问题。如果我们使用 .replace 方法添加时区,start_date 中的 tzinfo 对象有 _tzname = "LMT" 和 current_est_time 中的 tzinfo 对象有 _tzname = "EDT"。这就是为什么比较结果不一致的原因。
通过参考 Why doesn't pytz localize() produce a datetime object with tzinfo matching the tz object that localized it?
,看起来 "EDT" 是正确的值。所以在我看来,这是实现它的正确方法,
from datetime import datetime
import pytz
def main():
process_date_time(25)
process_date_time(26)
def process_date_time(min):
# All the dates are in US/Eastern time zone. Time zone for USA, North Carolina
# These dates are received from REST call in JSON
# I need to compare the current date time, if that falls within this range take some action
tz = pytz.timezone('US/Eastern')
start_date_str = "7/17/2018 11:30:00 AM"
end_date_str = "7/17/2018 2:30:00 PM"
# To simulate current date time within the range, I will update the current date time value.
dt = datetime.now()
current_est_time = tz.localize(dt)
current_est_time = current_est_time.replace(year=2018, month=7, day=17, hour=12, minute=min)
print current_est_time
start_date_1 = datetime.strptime(start_date_str, "%m/%d/%Y %I:%M:%S %p")
end_date_1 = datetime.strptime(end_date_str, "%m/%d/%Y %I:%M:%S %p")
start_date = tz.localize(start_date_1)
end_date = tz.localize(end_date_1)
print start_date <= current_est_time <= end_date
if __name__ == '__main__':
main()