What Is a Useful Command-Line Exercise for Timestamps?
From the earlier reference collection. Read the current software blog.
Parse timestamps with explicit offsets, convert them to a common instant, and compare the result instead of comparing clock text.
Choose examples that expose the issue
A useful exercise uses two timestamps that show different local clock readings but represent the same instant. For example, 2026-04-10T09:00:00-07:00 and 2026-04-10T16:00:00Z refer to the same moment: the first is seven hours behind UTC. A string comparison says they differ; a time-aware parser converts each to a common timeline before comparing.
Include an example that genuinely differs as well, such as 2026-04-10T16:01:00Z, so the exercise demonstrates both equality and ordering. Explicit offsets make the interpretation testable. A timestamp such as 2026-04-10 09:00 leaves the zone unspecified and cannot be reliably compared across systems without an additional assumption.
Parse rather than slice text
Do not remove punctuation and compare the remaining digits. That approach ignores offsets, fractional seconds, and calendar rules. Use a date-time library that understands ISO 8601 offsets. Python’s standard library includes datetime tools; its documentation describes the language’s standard modules and types. A small script can parse offset-aware values, convert them to UTC, and subtract them to obtain a duration.
Keep the parser’s behavior explicit for naive timestamps. Either reject them, or attach a documented zone assumption at the boundary where the data enters your system. Silently treating every naive timestamp as the machine’s local zone makes results depend on where the command runs.
Compare instants and preserve presentation
After parsing, compare the represented instants, not their original strings. Two values may be equivalent even when their displayed offsets differ. For reporting, it can still be useful to retain the original text alongside the normalized UTC form; one helps a person understand the source’s local time, while the other supports consistent ordering.
Test edge cases such as a fractional second, a date boundary, a positive offset, and a missing offset. A conversion from late evening at one offset may fall on the next calendar day in UTC. This is a common reason that sorting by the date portion alone gives the wrong sequence.
Turn the exercise into a practical check
Have the command print the original values, their normalized forms, and the elapsed difference. For the equal example, the normalized values should match and the difference should be zero. For the one-minute-later value, the result should show a positive one-minute interval.
This makes a compact regression exercise for any importer that handles schedules, sensor observations, or logs. It teaches a durable distinction: clock text is a representation, while an offset-aware timestamp identifies an instant that can be compared consistently.