Skip to content

parse 'Z' timezone designator in iso8601 strings as Timezone('UTC'), not Timezone('+00:00') #441

@mfogel

Description

@mfogel

Hi, thanks so much for your work on pendulum. Much better so far than other alternatives I've been using for way too long.

It appears the iso8601 timezone designator Z is handled inconsistently between between parsing and rendering of datetimes to/from iso8601 strings.

On rendering a UTC-datetime to an iso8601 string, the timezone will be correctly represented as Z. Ex:

>>> pendulum.now('utc').to_iso8601_string()
'2020-02-05T20:05:37.364951Z'

However, on parsing an iso8601 string, Z is not parsed as UTC... it's parsed as Timezone('+00:00') timezone.

>>> pendulum.parse('2020-02-05T20:05:37.364951Z')
DateTime(2020, 2, 5, 20, 5, 37, 364951, tzinfo=Timezone('+00:00'))

This results in some inconsistencies when rendering & parsing back and forth from strings:

>>> now_iso_str = pendulum.now('utc').to_iso8601_string()
>>> now_iso_str
'2020-02-05T20:48:06.814968Z'
>>> now_iso_str_2 = pendulum.parse(now_iso_str).to_iso8601_string()
>>> now_iso_str_2
'2020-02-05T20:48:06.814968+00:00'
>>> now_iso_str == now_iso_str_2
False

I would propose that when parsing an iso8601 string, Z should be parsed directly to Timezone('UTC') rather than to Timezone('+00:00')

If this sounds like a change you would be open to @sdispater , let me know and I'll prepare a PR with tests. Cheers.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions