DateTimeRange Library
DateTimeRange (v2.3.2) is a Python library designed to handle operations on time ranges, such as checking time inclusion, finding intersections, truncating, and iterating through ranges. It is actively maintained with frequent updates and supports modern Python versions.
Common errors
-
ValueError: Timezone mismatch detected between start_datetime and end_datetime
cause Attempting to create a DateTimeRange or perform an operation where the start and end datetimes have incompatible or conflicting timezone information, which was not explicitly handled by earlier versions.fixEnsure `start_datetime` and `end_datetime` have consistent timezone information, or explicitly set the `timezone` argument during `DateTimeRange` instantiation or using `set_time_range` if your version supports it (v2.2.0+). Example: `DateTimeRange(dt1.astimezone(pytz.utc), dt2.astimezone(pytz.utc))`. -
TypeError: 'DateTimeRange' object has no attribute 'is_time_inversion'
cause Attempting to use new methods like `is_time_inversion` (added in v2.3.0) or `subtract` (added in v1.1.0) on an older version of the library.fixUpgrade the `datetimerange` library to the latest version using `pip install --upgrade datetimerange` to access new features and bug fixes. -
ValueError: Invalid datetime format: ...
cause This error can occur if datetime formatting methods encounter an unexpected value, as noted in the v2.3.2 release. It typically happens when parsing string inputs or converting between formats.fixEnsure that any datetime strings passed to `DateTimeRange` or its methods conform to expected formats. If using `start_time_format` or `end_time_format`, confirm they correctly match your input strings. Upgrade to v2.3.2 or later to benefit from improved `ValueError` handling in formatting methods.
Warnings
- breaking Dropped support for Python 3.7 and 3.8 in v2.3.1, and Python 3.6 in v2.0.0. Ensure your Python environment is 3.9 or newer to use recent versions of the library.
- gotcha Prior to v2.3.0 and v2.2.1, the library had issues handling timezone mismatches, particularly with the `range` and `intersection` methods. Operations involving datetimes with different timezones might have produced incorrect results.
- gotcha The `intersection_threshold` argument was introduced in v2.0.0 for `intersection` and `is_intersection` methods. If two ranges barely touch (e.g., end of one is start of another), the intersection logic might differ depending on this threshold. Prior versions might have treated such cases differently.
Install
-
pip install datetimerange
Imports
- DateTimeRange
from datetimerange import DateTimeRange
Quickstart
from datetime import datetime
from datetimerange import DateTimeRange
# Create a DateTimeRange instance
time_range = DateTimeRange("2023-01-01T10:00:00+0900", "2023-01-01T10:10:00+0900")
print(f"Initial range: {time_range}")
# Check if a datetime is within the range
check_time = datetime(2023, 1, 1, 10, 5, 0).astimezone(time_range.start_datetime.tzinfo)
is_within = check_time in time_range
print(f"Is {check_time} within range? {is_within}")
# Create an intersecting range
another_range = DateTimeRange("2023-01-01T10:05:00+0900", "2023-01-01T10:15:00+0900")
intersection = time_range.intersection(another_range)
print(f"Intersection with {another_range}: {intersection}")