dateutil and pytz give different results

后端 未结 1 1736
臣服心动
臣服心动 2021-01-01 17:46

I have an issue comparing outputs with dateutil and pytz. I\'m creating a aware datetime object (UTC) and then converting to a given time zone, but

相关标签:
1条回答
  • 2021-01-01 18:07

    Edit: The discrepancy discussed below no longer exists when using

    >>> dateutil.__version__
    '1.5'
    
    >>> pytz.__version__
    '2012c'
    

    The pytz module warns,

    this library differs from the documented Python API for tzinfo implementations; if you want to create local wallclock times you need to use the localize() method

    and further on

    This library only supports two ways of building a localized time. The first is to use the localize() method provided by the pytz library.

    In [61]: u4 = pytz.timezone('America/Chicago')
    In [62]: print(u4.localize(datetime.datetime(2010, 5, 2, 11, 10)))
    2010-05-02 11:10:00-05:00
    

    The other way is to use the astimezone method, which is used to convert a timezone-aware datetime into another timezone-aware datetime.

    And to be completely explicit, it warns against constructing a timezone-aware datetime using the tzinfo argument:

    Unfortunately using the tzinfo argument of the standard datetime constructors ‘’does not work’’ with pytz for many timezones.


    Let's test the hypothesis that

    datetime.datetime(year, month, day, hour, minute, tzinfo = dateutil_tz)
    

    equals

    pytz_tz.localize(datetime.datetime(year, month, day, hour, minute))
    

    with this code:

    import dateutil.tz
    import datetime
    import pytz
    
    now  = datetime.datetime.now()
    
    for name in pytz.all_timezones:
        dateutil_tz = dateutil.tz.gettz(name)
        pytz_tz = pytz.timezone(name)
        dateutil_date = datetime.datetime(
            now.year, now.month, now.day, now.hour, now.minute, tzinfo = dateutil_tz)
        pytz_date = pytz_tz.localize(datetime.datetime(
            now.year, now.month, now.day, now.hour, now.minute))
    
        try:
            assert dateutil_date.isoformat() == pytz_date.isoformat()
        except AssertionError:
            print(name)
            print(dateutil_date.isoformat())
            print(pytz_date.isoformat())           
    

    The code yields:

    America/Argentina/San_Luis
    2012-12-18T22:32:00-04:00 <-- dateutil datetime
    2012-12-18T22:32:00-03:00 <-- pytz's datetime
    

    So my hypothesis was wrong: dateutil and pytz return different results.

    So which one is correct? I'm not really sure, but according to this website, currently,

    America/Argentina/San_Luis time zone offset is: 
    UTC / GMT -03:00 hours
    

    so it appears pytz is correct.

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