micktwomey / pyiso8601

ISO8601 formatted datetime parser for python
MIT License
34 stars 13 forks source link

parser not returning naive datetime when default_timezone=None #13

Closed hexcowboy closed 3 years ago

hexcowboy commented 3 years ago

When parsing an ISO time and supplying the default_timezone=None, a naive time should be returned. Instead, a UTC timezone datetime is returned.

From the annotations:

def parse_date(datestring, default_timezone=UTC):
"""
    :param default_timezone: A datetime tzinfo instance to use when no timezone
                             is specified in the datestring. If this is set to
                             None then a naive datetime object is returned.
"""

According to the Python docs,

A datetime object d is aware if both of the following hold:

  1. d.tzinfo is not None
  2. d.tzinfo.utcoffset(d) does not return None

Otherwise, d is naive.

Expected:

# https://stackoverflow.com/a/796019/16245971
naive = iso8601.parse_date("2021-06-24T14:54:41.634362969Z").replace(tzinfo=None)
assert(naive.tzinfo == None)
# -> True

Actual:

# https://github.com/micktwomey/pyiso8601/blob/c4fbb40894488eb61d62d089c3fa2fb8f66776cd/iso8601/iso8601.py#L163
naive = iso8601.parse_date("2021-06-24T14:54:41.634362969Z", default_timezone=None)
assert(naive.tzinfo == None)
# -> Asserion Error
micktwomey commented 3 years ago

Thanks for the report!

Just one thing, it looks like there is a Z (zulu) time zone in your example, when I try it I get UTC as the time zone (not exactly right, I know). Can you confirm this without the Z character? Thanks!

When I try without the Z uses the default:

❯ ipython
Python 3.9.0 (default, Oct  9 2020, 13:52:19)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.25.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import iso8601

In [2]: iso8601.parse_date("2021-06-24T14:54:41.634362969Z")
Out[2]: datetime.datetime(2021, 6, 24, 14, 54, 41, 634362, tzinfo=datetime.timezone.utc)

In [3]: iso8601.parse_date("2021-06-24T14:54:41.634362969")
Out[3]: datetime.datetime(2021, 6, 24, 14, 54, 41, 634362, tzinfo=datetime.timezone.utc)

In [4]: iso8601.parse_date("2021-06-24T14:54:41.634362969Z", default_timezone=None)
Out[4]: datetime.datetime(2021, 6, 24, 14, 54, 41, 634362, tzinfo=datetime.timezone.utc)

In [5]: iso8601.parse_date("2021-06-24T14:54:41.634362969", default_timezone=None)
Out[5]: datetime.datetime(2021, 6, 24, 14, 54, 41, 634362)
hexcowboy commented 3 years ago

Confirming that it is indeed the Zulu that is causing unexpected behavior

➜ ipython
Python 3.9.5 (default, May 10 2021, 19:47:53) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.25.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import iso8601                                                                              

In [2]: iso8601.parse_date("2013-10-15T18:30Z", default_timezone=None)                              
Out[2]: datetime.datetime(2013, 10, 15, 18, 30, tzinfo=datetime.timezone.utc)

In [3]: iso8601.parse_date("2013-10-15T18:30", default_timezone=None)                               
Out[3]: datetime.datetime(2013, 10, 15, 18, 30)
micktwomey commented 3 years ago

Cool, thanks for checking! It looks like the code is behaving itself as we're expecting, I'll close this issue but ping again if it's still a problem!