4.2. Datetime ISO Standard

4.2.1. Rationale

  • ISO 8601 is an International Standard 2

4.2.2. Dates

  • Format: YYYY-mm-dd

  • Year-Month-Day

Example:

  • 1961-04-12

  • 1969-07-21

4.2.3. Time

  • Format: HH:MM:SS.ffffff or HH:MM:SS or HH:MM

  • 24 hour clock

  • Optional seconds and microseconds

  • 00:00 - midnight, at the beginning of a day

  • 24:00 - midnight, at the end of a day (not recommended)

  • 1969-12-31T24:00 is equal to 1970-01-01T00:00

Example:

  • 00:00

  • 06:07:00

  • 13:00:00.123

  • 23:59:59.999999

4.2.4. Date and Time

  • Format: YYYY-mm-ddTHH:MM:SS.ffffff

  • "T" separates date and time)

  • Optional seconds and microseconds

Example:

  • 1961-04-12T06:07

  • 1961-04-12T06:07:00

  • 1961-04-12T06:07:00.123

  • 1961-04-12T06:07:00.123456

4.2.5. Timezone

  • Format: YYYY-mm-ddTHH:MM:SS.ffffffUTC

  • Format: YYYY-mm-ddTHH:MM:SS.ffffffZ

  • Optional seconds and microseconds

  • "Z" (Zulu) means UTC

Time zone notation:

  • <time>UTC

  • <time>Z

  • <time>±hh:mm

  • <time>±hhmm

  • <time>±hh

Example:

  • 1961-04-12T06:07:00.123456Z

  • 1961-04-12T06:07:00.123456UTC

  • 1961-04-12T06:07:00.123456CEST

  • 1961-04-12T06:07:00.123456CET

  • 1961-04-12T06:07:00.123456+02:00

  • 1961-04-12T06:07:00.123456+0200

  • 1961-04-12T06:07:00.123456+02

4.2.6. Week

  • Format: YYYY-Www

  • The ISO 8601 definition for week 01 is the week with the first Thursday of the Gregorian year (i.e. of January) in it. 1

  • 2009-W01 - First week of 2009

  • 2009-W53 - Sunday 3 January 2010

4.2.7. Weekday

  • Format: YYYY-Www-dd

  • Note year/month changes during the week

  • 2009-W01-1 - Monday 29 December 2008

  • 2009-W53-7 - Sunday 3 January 2010

  • Week starts on Monday

  • ISO defines Monday as one

>>> from datetime import datetime
>>>
>>>
>>> dt = datetime(1969, 7, 21, 2, 56, 15)
>>>
>>> dt.isoweekday()
1
>>>
>>> dt.weekday()
0

4.2.8. Duration

  • Format: P...Y...M...DT...H...M...S

  • P - period - placed at the start of the duration representation

  • Y - number of years

  • M - number of months

  • W - number of weeks

  • D - number of days

  • T - precedes the time components of the representation

  • H - number of hours

  • M - number of minutes

  • S - number of seconds

P8Y3M8DT20H49M15S

8 years
3 months
8 days
20 hours
49 minutes
15 seconds

4.2.9. Python and ISO Format

Format to string in ISO-8601 standard:

>>> from datetime import date, time, datetime
>>>
>>>
>>> dt = datetime(1969, 7, 21, 2, 56, 15)
>>> d = date(1969, 7, 21)
>>> t = time(2, 56, 15)
>>>
>>> dt.isoformat()
'1969-07-21T02:56:15'
>>>
>>> d.isoformat()
'1969-07-21'
>>>
>>> t.isoformat()
'02:56:15'

Parse from string in ISO-8601 standard:

>>> from datetime import date, time, datetime
>>>
>>>
>>> datetime.fromisoformat('1969-07-21T02:56:15')
datetime.datetime(1969, 7, 21, 2, 56, 15)
>>>
>>> date.fromisoformat('1969-07-21')
datetime.date(1969, 7, 21)
>>>
>>> time.fromisoformat('02:56:15')
datetime.time(2, 56, 15)

Note, that .fromisoformat() is fault-tolerant:

>>> from datetime import date, time, datetime
>>>
>>>
>>> datetime.fromisoformat('1969-07-21T02:56:15')
datetime.datetime(1969, 7, 21, 2, 56, 15)
>>>
>>> datetime.fromisoformat('1969-07-21 02:56:15')
datetime.datetime(1969, 7, 21, 2, 56, 15)
>>>
>>> date.fromisoformat('1969-07-21')
datetime.date(1969, 7, 21)
>>>
>>> time.fromisoformat('02:56:15')
datetime.time(2, 56, 15)
>>>
>>> time.fromisoformat('2:56:15')
Traceback (most recent call last):
ValueError: Invalid isoformat string: '2:56:15'
>>>
>>> time.fromisoformat('2:56')
Traceback (most recent call last):
ValueError: Invalid isoformat string: '2:56'

4.2.10. References

1

https://en.wikipedia.org/wiki/ISO_week_date

2

https://en.wikipedia.org/wiki/ISO_8601

4.2.11. Assignments

Code 4.9. Solution
"""
* Assignment: Datetime ISO Format
* Complexity: easy
* Lines of code: 1 lines
* Time: 3 min

English:
    1. Define `result: str` with `DATA` converted to ISO-8601 format
    2. Run doctests - all must succeed

Polish:
    1. Zdefiniuj `result: str` z przekonwertowaną `DATA` do formatu ISO-8601
    2. Uruchom doctesty - wszystkie muszą się powieść

Tests:
    >>> import sys; sys.tracebacklimit = 0

    >>> assert type(result) is str, \
    'Variable `result` has invalid type, must be a str'

    >>> result
    '1969-07-21T02:56:15'
"""

from datetime import datetime


DATA = datetime(1969, 7, 21, 2, 56, 15)

# datetime: DATA in ISO-8601 format: '1969-07-21T02:56:15'
result = ...

Code 4.10. Solution
"""
* Assignment: Datetime ISO Parse
* Complexity: easy
* Lines of code: 1 lines
* Time: 3 min

English:
    1. Define `result: datetime` with converted `DATA` from ISO-8601
    2. Run doctests - all must succeed

Polish:
    1. Zdefiniuj `result: datetime` z przekonwertowaną `DATA` z ISO-8601
    2. Uruchom doctesty - wszystkie muszą się powieść

Tests:
    >>> import sys; sys.tracebacklimit = 0

    >>> assert type(result) is datetime, \
    'Variable `result` has invalid type, must be a datetime'

    >>> result
    datetime.datetime(1969, 7, 21, 2, 56, 15, 123000)
"""

from datetime import datetime


DATA = '1969-07-21T02:56:15.123'

# datetime: DATA from ISO-8601 format
result = ...