KKAIROS
Practical guideIllustrative4 min read

Python dates: parse and compare

datetime parses, formats and compares dates and times. The example uses fixed dates and UTC as the only timezone.

Published:

Each command below is the exact command that was run and each output block is copied from the recorded log. Test ids, log hashes and exit statuses are in the evidence table.

TE-49-A: fixed dates parse deterministically; comparing naive and aware datetimes raises TypeError

No setup needed: the command below creates what it uses in the throwaway folder.

Command:

python3 -c "from datetime import datetime, timezone; d=datetime.strptime('2026-01-02','%Y-%m-%d'); print(d.isoformat(), d.tzinfo); a=datetime(2026,1,1,tzinfo=timezone.utc); n=datetime(2026,1,1)
try:
    a>n
except TypeError as e:
    print('TypeError', e)"

Recorded output:

2026-01-02T00:00:00 None
TypeError can't compare offset-naive and offset-aware datetimes

This run checks the exit status and records the output; it asserts no expected text.

Exit status: 0. This is the status recorded in the log for TE-49-A.

TE-49-B: adding a timedelta to a fixed datetime, the days of a difference between two datetimes, and strftime on a date

Setup for this run, inside a throwaway directory (mktemp -d):

cat > s.py <<'EOS'
from datetime import datetime, timedelta
print(datetime(2026, 2, 1) + timedelta(days=28))
print((datetime(2026, 3, 1)-datetime(2026, 2, 1)).days)
print(datetime(2026, 1, 2).strftime('%d/%m/%Y'))
EOS

Command:

python3 s.py

Recorded output:

2026-03-01 00:00:00
28
02/01/2026

Exit status: 0. This status and the output match what was written before the run.

TE-49-C: expected non-zero exit: strptime with month 13 raises ValueError when nothing catches it

Setup for this run, inside a throwaway directory (mktemp -d):

cat > s.py <<'EOS'
from datetime import datetime
datetime.strptime('2026-13-01', '%Y-%m-%d')
EOS

Command:

python3 s.py

Recorded output: none, standard output was empty.

Standard error from the same run:

Traceback (most recent call last):
  File "/tmp/b26-z9f_ek77/work/s.py", line 2, in <module>
    datetime.strptime('2026-13-01', '%Y-%m-%d')
  File "/usr/lib/python3.10/_strptime.py", line 568, in _strptime_datetime
    tt, fraction, gmtoff_fraction = _strptime(data_string, format)
  File "/usr/lib/python3.10/_strptime.py", line 349, in _strptime
    raise ValueError("time data %r does not match format %r" %
ValueError: time data '2026-13-01' does not match format '%Y-%m-%d'

Exit status: 1. This status and the output match what was written before the run. The path in the traceback line is a temporary folder and differs on each machine and each run.

Reading the recorded output

The first line is the isoformat of the parsed date, 2026-01-02T00:00:00, followed by None, which is the tzinfo of that datetime, so the parsed value has no timezone. The second line shows that the comparison a>n, where a was created with timezone.utc and n without a timezone, raised TypeError. The message is can't compare offset-naive and offset-aware datetimes. Only the > comparison was run.

TE-49-B: adding 28 days to the first of February 2026 gave 2026-03-01 00:00:00, the difference between the first of March and the first of February has 28 days, and strftime with %d/%m/%Y printed 02/01/2026. TE-49-C: strptime with the month 13 stopped with exit status 1. The last line of standard error is ValueError: time data '2026-13-01' does not match format '%Y-%m-%d'.

Ad

Limits

The example uses fixed dates and UTC as the only timezone. One run shows that comparing a naive datetime with an aware one raises TypeError; other zones and DST are not tested here. TE-49-B subtracts one pair of fixed dates, and only reads the days of the difference.

Source

Evidence

Evidence table

| Test | What it checks | Exit | Verdict | Log sha256 (first 12) | |---|---|---|---|---| | TE-49-A | fixed dates parse deterministically; comparing naive and aware datetimes raises TypeError | 0 | PASS | 8c1e160877cf | | TE-49-B | adding a timedelta to a fixed datetime, the days of a difference between two datetimes, and strftime on a date | 0 | PASS | dcaa20dd9b68 | | TE-49-C | expected non-zero exit: strptime with month 13 raises ValueError when nothing catches it | 1 | PASS | 18f08fdf2d35 |

Every row has a recorded log holding the setup commands, the command, stdout, stderr, exit status and the final directory state. The first 12 characters of the log's sha256 are in the table.

All full guides: All guides