Can't compare naive and aware datetime.now() <= challenge.datetime_end

后端 未结 9 1318
离开以前
离开以前 2020-12-02 07:58

I am trying to compare the current date and time with dates and times specified in models using comparison operators:

if challenge.datetime_start <= datet         


        
9条回答
  •  孤街浪徒
    2020-12-02 08:40

    So the way I would solve this problem is to make sure the two datetimes are in the right timezone.

    I can see that you are using datetime.now() which will return the systems current time, with no tzinfo set.

    tzinfo is the information attached to a datetime to let it know what timezone it is in. If you are using naive datetime you need to be consistent through out your system. I would highly recommend only using datetime.utcnow()

    seeing as somewhere your are creating datetime that have tzinfo associated with them, what you need to do is make sure those are localized (has tzinfo associated) to the correct timezone.

    Take a look at Delorean, it makes dealing with this sort of thing much easier.

提交回复
热议问题