奇怪的Python日期比较行为

奇怪的Python日期比较行为,python,python-2.7,python-2.x,python-datetime,pytz,Python,Python 2.7,Python 2.x,Python Datetime,Pytz,我面临着奇怪的Python日期时间比较问题。我得到两个字符串格式的日期时间(来自REST调用,JSON)开始日期时间和结束日期时间。我必须比较当前日期时间,如果它在这个范围内(开始日期时间和结束日期时间),采取一些行动。众所周知,这些日期的时区为美国/东部(美国北卡罗来纳州)。这是密码 from pytz import timezone from datetime import datetime def main(): # All the dates are in US/Easter

我面临着奇怪的Python日期时间比较问题。我得到两个字符串格式的日期时间(来自REST调用,JSON)开始日期时间和结束日期时间。我必须比较当前日期时间,如果它在这个范围内(开始日期时间和结束日期时间),采取一些行动。众所周知,这些日期的时区为美国/东部(美国北卡罗来纳州)。这是密码

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()

如果将当前时间变量的分钟值更改为25,则输出为
2018-07-17 12:25:16.582573-04:00

False



有谁能帮我一下,我哪里出错了?

错误是由不同的偏移量datetime.now()调用引起的,该调用返回:

2018-07-17 12:26:06.646643-04:00
但是,您的日期比较没有类似的结构,在比较之前,它们将如下所示:

2018-07-17 14:30:00-04:56
对于当前its4:00和开始和结束日期its4: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()
从pytz导入时区
从日期时间导入日期时间
def main():
#所有日期都在美国/东部时区。美国时区,北卡罗来纳州
#这些日期是从JSON中的REST调用接收的
#我需要比较当前日期和时间,如果在这个范围内,请采取一些措施
开始日期=“2018年7月17日上午11:30:00”
结束日期=“2018年7月17日下午2:30:00”
#要模拟范围内的当前日期时间,我将更新当前日期时间值。
当前时间=日期时间。现在(tz=时区(“美国/东部”))
当前时间=当前时间。替换(年=2018,月=7,日=17,小时=12,分钟=24)。替换(tzinfo=时区(“美国/东部”))
打印(当前时间)
开始日期=日期时间.strtime(开始日期,“%m/%d/%Y%I:%m:%S%p”)。替换(tzinfo=时区('US/东部'))
end_date=datetime.strtime(end_date_str,“%m/%d/%Y%I:%m:%S%p”)。替换(tzinfo=timezone('US/Eastern'))
打印(开始日期)
打印(当前时间)
打印(结束日期)

print(start_date进一步调试此问题后,似乎存在一个问题。replace方法如何将时区信息处理到datetime对象。如果使用.replace方法添加时区,则start_date中的tzinfo对象具有_tzname=“LMT”,而当前_est_time中的tzinfo对象具有_tzname=“EDT”.这就是比较结果不一致的原因。
通过引用
,看起来“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()
从日期时间导入日期时间
进口皮茨
def main():
处理日期时间(25)
处理日期时间(26)
def过程日期时间(分钟):
#所有日期均为美国/东部时区。美国时区为北卡罗来纳州
#这些日期是从JSON中的REST调用接收的
#我需要比较当前日期和时间,如果在这个范围内,请采取一些措施
tz=pytz.时区(“美国/东部”)
开始日期=“2018年7月17日上午11:30:00”
结束日期=“2018年7月17日下午2:30:00”
#要模拟范围内的当前日期时间,我将更新当前日期时间值。
dt=datetime.now()
当前时间=tz.本地化(dt)
当前时间=当前时间。替换(年=2018,月=7,日=17,小时=12,分钟=min)
打印当前最短时间
开始日期1=datetime.strtime(开始日期“%m/%d/%Y%I:%m:%S%p”)
end_date_1=datetime.strtime(end_date_str,“%m/%d/%Y%I:%m:%S%p”)
开始日期=tz.本地化(开始日期1)
结束日期=tz.本地化(结束日期1)

打印开始日期你能提供你写的最后一次考试吗?输入/输出。上述代码示例的输出为2018-07-17 12:27:34.806142-04:00 True。如果你将当前时间变量的分钟值更改为25,输出为2018-07-17 12:25:16.582573-04:00 False代码对你有用吗???嗨,请查看我的最新答案。谢谢你的合作mment Inder!但我认为毫秒在这里不是问题。如果您只是将微秒=0添加到当前的_est_time.replace,它会删除微秒/毫秒值,但问题仍然存在。您的代码可以工作,因为您还删除了时区信息。简单的方法是只编写当前的_est_time=当前的_est_time.replace(tzinfo=None)并且在代码的后面部分中不使用任何时区信息。现在我将处理这个(删除时区信息)问题。@SaurabhDeshpande为之前的错误感到抱歉
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()