5.6. Datetime Parse

  • Parsing - analyze (a sentence) into its parts and describe their syntactic roles.

5.6.1. Parsing dates

>>> from datetime import datetime

Datetime parsing from string:

>>> x = '1961-04-12 06:07'
>>>
>>> datetime.strptime(x, '%Y-%m-%d %H:%M')
datetime.datetime(1961, 4, 12, 6, 7)

5.6.2. Leading Zero

Mind that while parsing dates without leading zero, you do not use %#H or %-H as it was for formatting. One should simply use %H to capture hour:

>>> x = '1961-04-12 6:07'
>>>
>>> datetime.strptime(x, '%Y-%m-%d %H:%M')
datetime.datetime(1961, 4, 12, 6, 7)

5.6.3. String Fitting

If there are any other characters in the string, such as commas, brackets spaces, colons, dashes etc, they should be reflected in the format string.

>>> x = 'Apr 12th, 1961 6:07 am'
>>>
>>> datetime.strptime(x, '%b %dth, %Y %I:%M %p')
datetime.datetime(1961, 4, 12, 6, 7)
>>> x = '12 April 1961 at 6:07 am'
>>>
>>> datetime.strptime(x, '%d %B %Y at %I:%M %p')
datetime.datetime(1961, 4, 12, 6, 7)

Omitting any of those values will result with an error:

>>> x = '12 April 1961 at 6:07 am'
>>>
>>> datetime.strptime(x, '%d %B %Y %I:%M %p')
Traceback (most recent call last):
ValueError: time data '12 April 1961 at 6:07 am' does not match format '%d %B %Y %I:%M %p'

5.6.4. Time Zone

  • More information in Datetime Timezone

>>> x = '12 April 1961 6:07 UTC'
>>>
>>> datetime.strptime(x, '%d %B %Y %H:%M %Z')
datetime.datetime(1961, 4, 12, 6, 7)
>>> x = '1961-04-12 6:07 local'
>>>
>>> datetime.strptime(x, '%Y-%m-%d %H:%M')
Traceback (most recent call last):
ValueError: unconverted data remains:  local
>>> x = '1961-04-12 6:07 local'
>>>
>>> datetime.strptime(x, '%Y-%m-%d %H:%M %Z')
Traceback (most recent call last):
ValueError: time data '1961-04-12 6:07 local' does not match format '%Y-%m-%d %H:%M %Z'
>>> x = '1961-04-12 6:07 local'
>>>
>>> datetime.strptime(x, '%Y-%m-%d %H:%M local')
datetime.datetime(1961, 4, 12, 6, 7)

5.6.5. Assignments

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

English:
    1. Define `result: datetime` with parsed date `DATA`
    2. Run doctests - all must succeed

Polish:
    1. Zdefiniuj `result: datetime` ze sparsowaną datą `DATA`
    2. Uruchom doctesty - wszystkie muszą się powieść

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

    >>> assert result is not Ellipsis, \
    'Assign result to variable: `result`'
    >>> assert type(result) is datetime, \
    'Variable `result` has invalid type, must be a datetime'

    >>> result
    datetime.datetime(1969, 7, 21, 0, 0)
"""

from datetime import datetime


DATA = 'July 21, 1969'

# DATA from US long format
# type: datetime
result = ...

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

English:
    1. Define `result: datetime` with parsed date `DATA`
    2. Run doctests - all must succeed

Polish:
    1. Zdefiniuj `result: datetime` ze sparsowaną datą `DATA`
    2. Uruchom doctesty - wszystkie muszą się powieść

Hints:
    * `%dst`

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

    >>> assert result is not Ellipsis, \
    'Assign result to variable: `result`'
    >>> assert type(result) is datetime, \
    'Variable `result` has invalid type, must be a datetime'

    >>> result
    datetime.datetime(1969, 7, 21, 0, 0)
"""

from datetime import datetime


DATA = 'July 21st, 1969'

# DATA from long US format with ordinals
# type: datetime
result = ...

Code 5.21. Solution
"""
* Assignment: Datetime Parse List
* Complexity: medium
* Lines of code: 8 lines
* Time: 3 min

English:
    1. Define `result: list[datetime]` with parsed `DATA` dates
    2. Run doctests - all must succeed

Polish:
    1. Zdefiniuj `result: list[datetime]` ze sparsowanymi datami `DATA`
    2. Uruchom doctesty - wszystkie muszą się powieść

Hints:
    * `for ... in`
    * `try ... except`
    * ``dt.strptime()``
    * ``list.append()``

Tests:
    >>> import sys; sys.tracebacklimit = 0
    >>> from pprint import pprint
    >>> result = list(result)

    >>> assert result is not Ellipsis, \
    'Assign result to variable: `result`'
    >>> assert type(result) is list, \
    'Variable `result` has invalid type, must be a list'
    >>> assert all(type(element) is datetime for element in result), \
    'All elements in `result` must be a datetime'

    >>> pprint(result, width=30)
    [datetime.datetime(1969, 7, 21, 0, 0),
     datetime.datetime(1969, 7, 22, 0, 0)]
"""

from datetime import date, datetime


DATA = [
    'July 21st, 1969',
    'July 22nd, 1969',
]

# parsed DATA
# type: list[date]
result = ...


Code 5.22. Solution
"""
* Assignment: Datetime Parse Many
* Complexity: medium
* Lines of code: 7 lines
* Time: 5 min

English:
    1. Define `result: list[datetime]` with parsed `DATA` dates
    2. Run doctests - all must succeed

Polish:
    1. Zdefiniuj `result: list[datetime]` ze sparsowanymi datami `DATA`
    2. Uruchom doctesty - wszystkie muszą się powieść

Hints:
    * `for ... in`
    * nested `try ... except`
    * FORMATS = []
    * for fmt in FORMATS
    * helper function

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

    >>> assert result is not Ellipsis, \
    'Assign result to variable: `result`'
    >>> result = list(result)
    >>> assert type(result) is list, \
    'Variable `result` has invalid type, must be a list'
    >>> assert all(type(element) is datetime for element in result), \
    'All elements in `result` must be a datetime'

    >>> result  # doctest: +NORMALIZE_WHITESPACE
    [datetime.datetime(1969, 7, 21, 0, 0),
     datetime.datetime(1969, 7, 22, 0, 0),
     datetime.datetime(1969, 7, 23, 0, 0),
     datetime.datetime(1969, 7, 24, 0, 0)]
"""

from datetime import datetime, date


DATA = [
    'July 21st, 1969',
    'July 22nd, 1969',
    'July 23rd, 1969',
    'July 24th, 1969',
]

FORMATS = [
    '%B %dst, %Y',
    '%B %dnd, %Y',
    '%B %drd, %Y',
    '%B %dth, %Y',
]


# DATA elements in datetime format
# type: list[date]
result = ...