Skip to main content

A library for parsing ISO 8601 strings.

Project description

Features

  • Pure Python implementation

  • No extra dependencies

  • Logical behavior

  • UTC offset represented as fixed-offset tzinfo

  • No regular expressions

Use

Parsing datetimes

To parse a typical ISO 8601 datetime string:

>>> import aniso8601
>>> aniso8601.parse_datetime('1977-06-10T12:00:00Z')
datetime.datetime(1977, 6, 10, 12, 0, tzinfo=<aniso8601.UTCOffset object at 0x7f44fadbbd90>)

Alternative delimiters can be specified, for example, a space:

>>> aniso8601.parse_datetime('1977-06-10 12:00:00Z', delimiter=' ')
datetime.datetime(1977, 6, 10, 12, 0, tzinfo=<aniso8601.UTCOffset object at 0x7f44fadbbf50>)

UTC offsets are supported:

>>> aniso8601.parse_datetime('1979-06-05T08:00:00-08:00')
datetime.datetime(1979, 6, 5, 8, 0, tzinfo=<aniso8601.UTCOffset object at 0x7f44fadbbf50>)

If a UTC offset is not specified, the returned datetime will be naive:

>>> aniso8601.parse_datetime('1983-01-22T08:00:00')
datetime.datetime(1983, 1, 22, 8, 0)

Parsing dates

To parse a date represented in an ISO 8601 string:

>>> import aniso8601
>>> aniso8601.parse_date('1984-04-23')
datetime.date(1984, 4, 23)

Basic format is supported as well:

>>> aniso8601.parse_date('19840423')
datetime.date(1984, 4, 23)

To parse a date using the ISO 8601 week date format:

>>> aniso8601.parse_date('1986-W38-1')
datetime.date(1986, 9, 15)

To parse an ISO 8601 ordinal date:

>>> aniso8601.parse_date('1988-132')
datetime.date(1988, 5, 11)

Parsing times

To parse a time formatted as an ISO 8601 string:

>>> import aniso8601
>>> aniso8601.parse_time('11:31:14')
datetime.time(11, 31, 14)

As with all of the above, basic format is supported:

>>> aniso8601.parse_time('113114')
datetime.time(11, 31, 14)

A UTC offset can be specified for times:

>>> aniso8601.parse_time('17:18:19-02:30')
datetime.time(17, 18, 19, tzinfo=<aniso8601.UTCOffset object at 0x7f44fad82c50>)
>>> aniso8601.parse_time('171819Z')
datetime.time(17, 18, 19, tzinfo=<aniso8601.UTCOffset object at 0x7f44fadbbd90>)

Reduced accuracy is supported:

>>> aniso8601.parse_time('21:42')
datetime.time(21, 42)
>>> aniso8601.parse_time('22')
datetime.time(22, 0)

A decimal fraction is always allowed on the lowest order element of an ISO 8601 formatted time:

>>> aniso8601.parse_time('22:33.5')
datetime.time(22, 33, 30)
>>> aniso8601.parse_time('23.75')
datetime.time(23, 45)

Tests

To run the unit tests:

$ python -m unittest aniso8601.test_aniso8601

References

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aniso8601-0.49.tar.gz (19.4 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page