Python timezone-aware timestamps versus elapsed durations
by nolookshot - Friday October 16, 2026 at 02:40 PM
#1
Two different questions need different clocks: "When did this event happen?" and "How long did this operation take?"

For an event timestamp, create an aware UTC datetime. This single-line example prints an ISO 8601 value containing an explicit +00:00 offset:

from datetime import datetime, timezone; stamp = datetime.now(timezone.utc); print(stamp.isoformat())

For an elapsed duration, use a performance counter and subtract two readings. This is a separate single-line example:

import time; start = time.perf_counter(); total = sum(range(100000)); elapsed = time.perf_counter() - start; print(elapsed)

The result is a duration in seconds. The absolute counter value is not a calendar timestamp. A wall clock can be adjusted, so subtracting wall-clock timestamps is a poor choice for benchmarking or local timeouts.

Keep an explicit offset when storing an instant and convert it for display when needed. Be careful with naive datetimes: labeling a local value as UTC with replace(tzinfo=...) does not convert the instant. Use astimezone(...) on a correctly interpreted aware value for conversion.

Official references: https://docs.python.org/3/library/datetime.html and https://docs.python.org/3/library/time.h...rf_counter
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python pathlib: suffix, suffixes and stem for compound filenames nolookshot 0 188 10-16-2026, 02:41 PM
Last Post: nolookshot
  Python CSV: handling quoted commas and embedded newlines nolookshot 0 109 10-16-2026, 02:39 PM
Last Post: nolookshot
  Retrieving Firefox Cookies Using Python Aurora 15 6,031 09-10-2026, 03:29 AM
Last Post: realtruth
  [Tutorial] Bruteforce Any RDP Using Python 0x666 56 6,542 02-08-2026, 02:17 PM
Last Post: lombardi
  The Python Bible for Beginners: Launch Your Coding Career and go from Zero to Hero DragonWarrior 11 1,835 01-20-2026, 07:47 AM
Last Post: user_yufeng



 Users browsing this thread: