Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9004321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:52:14+00:00 2026-06-16T00:52:14+00:00

I have an issue comparing outputs with dateutil and pytz . I’m creating a

  • 0

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 I get different answers. I suspect that dateutil sometimes gives wrong results because it has problems taking into account daylight saving time (at least, I read a comment about it) but I can’t find confirmation or a fix to that issue. This is the code:

import dateutil

u = dateutil.tz.tzutc()
date1 = datetime.datetime(2010, 5, 2, 11, 10, tzinfo=u)
# 2010-05-02 11:10:00+00:00

u2 = dateutil.tz.gettz('America/Chicago')
date2 = datetime.datetime(2010, 5, 2, 11, 10, tzinfo=u2)
# 2010-05-02 11:10:00-05:00


import pytz
u = pytz.timezone('UTC')
date1 = datetime.datetime(2010, 5, 2, 11, 10, tzinfo=u)

# 2010-05-02 11:10:00+00:00
u2 = pytz.timezone('America/Chicago')
date2 = datetime.datetime(2010, 5, 2, 11, 10, tzinfo=u2)

# 2010-05-02 11:10:00-06:00

So, what could be the problem here?

UPDATE:

I just tried this:

print u2.normalize(date1.astimezone(u2))
# 2010-05-02 06:10:00-05:00

So pytz needs normalize to consider DST?

UPDATE 2:

It seemed as if pytz and dateutil don’t give the answer for America/Argentina/San_Luis but this works:

import pytz, dateutil, datetime

now = datetime.datetime.now() 

for zone in pytz.all_timezones:
    utc_dateutil = dateutil.tz.tzutc()
    utcdate_dateutil = datetime.datetime(now.year, now.month, now.day, now.hour, now.minute, tzinfo=utc_dateutil)
    zone_dateutil = dateutil.tz.gettz(zone)
    newzone_dateutil = utcdate_dateutil.astimezone(zone_dateutil)
    
    utc_pytz = pytz.timezone('UTC')
    utcdate_pytz = datetime.datetime(now.year, now.month, now.day, now.hour, now.minute, tzinfo=utc_pytz)
    zone_pytz = pytz.timezone(zone)
    newzone_pytz = utcdate_pytz.astimezone(zone_pytz)
    assert newzone_dateutil == newzone_pytz

Am I missing something?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T00:52:15+00:00Added an answer on June 16, 2026 at 12:52 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We are new to ROR, We have issue in creating Login/Logout process in ROR
i have issue with comparing NAN value in C++, Visualstudio. I need to handle
I have an issue comparing a const char to a string... If I use
I have issue with: <form:checkboxes path=roles cssClass=checkbox items=${roleSelections} /> If previous line is used
I have issue that is reproduced on g++. VC++ doesn't meet any problems. So
Share your ideas please! I have issue to check the folder and convert a
Have an issue with marshall and unmarshall readers and writers. So here it is.
I have an issue with jquery and history.back(): I got a link: <a href=#
I have an issue of alphabetically sorting the contacts picked from the address book
I have an issue which I could not find answer for across the web.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.