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

后端 未结 9 1271
离开以前
离开以前 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:31

    By default, the datetime object is naive in Python, so you need to make both of them either naive or aware datetime objects. This can be done using:

    import datetime
    import pytz
    
    utc=pytz.UTC
    
    challenge.datetime_start = utc.localize(challenge.datetime_start) 
    challenge.datetime_end = utc.localize(challenge.datetime_end) 
    # now both the datetime objects are aware, and you can compare them
    

    Note: This would raise a ValueError if tzinfo is already set. If you are not sure about that, just use

    start_time = challenge.datetime_start.replace(tzinfo=utc)
    end_time = challenge.datetime_end.replace(tzinfo=utc)
    

    BTW, you could format a UNIX timestamp in datetime.datetime object with timezone info as following

    d = datetime.datetime.utcfromtimestamp(int(unix_timestamp))
    d_with_tz = datetime.datetime(
        year=d.year,
        month=d.month,
        day=d.day,
        hour=d.hour,
        minute=d.minute,
        second=d.second,
        tzinfo=pytz.UTC)
    
    0 讨论(0)
  • 2020-12-02 08:39

    It is working form me. Here I am geeting the table created datetime and adding 10 minutes on the datetime. later depending on the current time, Expiry Operations are done.

    from datetime import datetime, time, timedelta
    import pytz
    

    Added 10 minutes on database datetime

    table_datetime = '2019-06-13 07:49:02.832969' (example)

    # Added 10 minutes on database datetime
    # table_datetime = '2019-06-13 07:49:02.832969' (example)
    
    table_expire_datetime = table_datetime + timedelta(minutes=10 )
    
    # Current datetime
    current_datetime = datetime.now()
    
    
    # replace the timezone in both time
    expired_on = table_expire_datetime.replace(tzinfo=utc)
    checked_on = current_datetime.replace(tzinfo=utc)
    
    
    if expired_on < checked_on:
        print("Time Crossed)
    else:
        print("Time not crossed ")
    

    It worked for me.

    0 讨论(0)
  • 2020-12-02 08:39

    Just:

    dt = datetimeObject.strftime(format) # format = your datetime format ex) '%Y %d %m'
    dt = datetime.datetime.strptime(dt,format)
    

    So do this:

    start_time = challenge.datetime_start.strftime('%Y %d %m %H %M %S')
    start_time = datetime.datetime.strptime(start_time,'%Y %d %m %H %M %S')
    
    end_time = challenge.datetime_end.strftime('%Y %d %m %H %M %S')
    end_time = datetime.datetime.strptime(end_time,'%Y %d %m %H %M %S')
    

    and then use start_time and end_time

    0 讨论(0)
  • 2020-12-02 08:39

    You are trying to set the timezone for date_time which already has a timezone. Use replace and astimezone functions.

    local_tz = pytz.timezone('Asia/Kolkata')
    
    current_time = datetime.now().replace(tzinfo=pytz.utc).astimezone(local_tz)
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-02 08:43

    datetime.datetime.now is not timezone aware.

    Django comes with a helper for this, which requires pytz

    from django.utils import timezone
    now = timezone.now()
    

    You should be able to compare now to challenge.datetime_start

    0 讨论(0)
提交回复
热议问题