A library for parsing ISO 8601 strings.
Project description
Features
Pure Python implementation
No extra dependencies
Logical behavior
Parse a time, get a datetime.time
Parse a date, get a datetime.date
Parse a date time, get a datetime.datetime
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file aniso8601-0.49.tar.gz.
File metadata
- Download URL: aniso8601-0.49.tar.gz
- Upload date:
- Size: 19.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4399c50b3c06c5d80d7d5e98d42b35e228e4c29f11cba2a5e199fa48af02a9bb
|
|
| MD5 |
c4ddccd394f5be7a2b71234b62654807
|
|
| BLAKE2b-256 |
5d71d395a542f97449288b032fddf65190ccfdbc6a6733ceda001c5412890880
|