Times
Times is a small, minimalistic Python library for handling time conversions between universal time and arbitrary timezones. Version 0.7, the latest release, was rewritten to be implemented on top of the 'Arrow' library, providing a consistent interface while leveraging Arrow's capabilities. The project has not seen active development since around 2015.
Warnings
- deprecated The `times` library itself recommends using the 'Arrow' library directly. Version 0.7 was rewritten on top of Arrow, and the project has been inactive since 2015. It is advisable to use 'Arrow' (or the standard 'datetime' module with 'zoneinfo' or 'pytz') for new projects.
- gotcha When accepting local time input, `times.to_universal()` requires you to explicitly specify the source timezone if the input is a naive datetime (without timezone information). If the input string already contains a timezone offset, you must *not* provide a separate timezone argument, as it will auto-detect from the string.
- gotcha While `times.to_local()` converts a universal time to a local datetime object, the documentation advises using `times.format()` directly for presentation. This is because `to_local()` may inherit complexities of `pytz` regarding ambiguous or non-existent times during DST transitions, especially if further date arithmetic is performed on the resulting local datetime.
Install
-
pip install times
Imports
- times
import times
Quickstart
import times
import datetime
# Example 1: Accepting local time and converting to universal time
# The second argument can be a pytz.timezone instance or a timezone string.
# If local_time already has timezone info, omit the source timezone argument.
local_time_str_naive = '2012-02-01 11:31:45'
universal_time = times.to_universal(local_time_str_naive, 'Europe/Amsterdam')
print(f"Local (naive) to Universal: {universal_time}")
# Example 2: Accepting datetime string with offset (auto-detects timezone)
universal_time_offset = times.to_universal('2012-02-03 11:59:03-0500')
print(f"String with offset to Universal: {universal_time_offset}")
# Example 3: Presenting universal time to a local timezone
# (using the universal_time from above)
formatted_local_time = times.format(universal_time, 'America/New_York', '%Y-%m-%d %H:%M:%S %Z%z')
print(f"Universal to formatted local: {formatted_local_time}")
# Example 4: Converting universal datetime to local representation (less recommended for direct formatting)
local_dt_obj = times.to_local(universal_time, 'Asia/Tokyo')
print(f"Universal to local datetime object: {local_dt_obj}")