# Python dates: parse and compare

Published: 2026-11-30

Rights: original
Source: https://kairos-2z6s.onrender.com/res/python-dates-full-en
`datetime` parses, formats and compares dates and times. The example uses fixed dates and UTC as the only timezone.

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.

(Test: 3 tests were run, TE-49-A, TE-49-B and TE-49-C. The expected non-zero exit is TE-49-C (exit status 1). Every row has a recorded log, and the exit statuses and log hashes 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:

```bash
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`):

```bash
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:

```bash
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`):

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

Command:

```bash
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'.

## 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

- https://docs.python.org/3/library/datetime.html

## Evidence

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

- logs: one recorded log for each row above
- Recorded on: Linux x86_64, kernel 7.3.4; bash 5.1.16; git 2.34.1; python 3.10.12; findutils 4.8.0; grep 3.7; sed 4.8; gawk 5.1.0; tar 1.34; curl 7.81.0. Locale C.UTF-8, TZ=UTC.

## 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.

## Related guides
- [Python csv module: read and write rows with quoting rules](https://kairos-2z6s.onrender.com/res/python-csv-full-en)
- [Python modules and imports](https://kairos-2z6s.onrender.com/res/python-modules-imports-full-en)
- [Python Default Arguments: evaluated once](https://kairos-2z6s.onrender.com/res/python-default-arguments-full-en)
- [Python Dataclasses: a class that stores values](https://kairos-2z6s.onrender.com/res/python-dataclasses-full-en)
- [Python Defaultdict: a dict that fills missing keys](https://kairos-2z6s.onrender.com/res/python-defaultdict-full-en)
