Python offers extensive support for times and durations; here are some examples.
To run the program, save it as time_example.py and use python:
In this Python example, we use the datetime module for time operations and pytz for timezone support. The timedelta class is used for duration calculations.
Note that Python’s datetime doesn’t have nanosecond precision (it goes up to microseconds), so we simulate nanoseconds by multiplying seconds by 1e9.
The strftime method is used to format the date as a string, which we use to get the weekday name.
Python’s comparison operators (<, >, ==) work directly on datetime objects, making time comparisons straightforward.
Next, we’ll look at the related idea of time relative to the Unix epoch.