icalendar.tools module#
Utility functions for icalendar.
- icalendar.tools.is_date(dt)[source]#
Check if a value is a date but not a datetime.
This function distinguishes between
dateanddatetimeobjects, returningTrueonly for puredateinstances.- Parameters:
dt (
date|datetime) -- The date or datetime object to check.- Return type:
- Returns:
Trueif the value is adatebut not adatetime,Falseotherwise.
Example
>>> from datetime import date, datetime >>> from icalendar.tools import is_date >>> is_date(date(2024, 1, 15)) True >>> is_date(datetime(2024, 1, 15, 10, 30)) False
- icalendar.tools.is_datetime(dt)[source]#
Check if a value is a datetime.
- Parameters:
dt -- The date or datetime object to check.
- Returns:
Trueif the value is adatetime,Falseif it is only adate.
Example
>>> from datetime import date, datetime >>> from icalendar.tools import is_datetime >>> is_datetime(datetime(2024, 1, 15, 10, 30)) True >>> is_datetime(date(2024, 1, 15)) False
- icalendar.tools.is_pytz(tz)[source]#
Check if a timezone is a pytz timezone.
pytz timezones require special handling with
localize()andnormalize()methods for correct timezone calculations.
- icalendar.tools.is_pytz_dt(dt)[source]#
Check if a datetime uses a pytz timezone.
This function checks whether the datetime has a timezone attached and whether that timezone is a pytz timezone requiring special handling.
- Parameters:
dt -- The date or datetime object to check.
- Returns:
Trueif the value is adatetimewith a pytz timezone,Falseotherwise.
- icalendar.tools.normalize_pytz(dt)[source]#
Normalize a datetime after calculations when using pytz.
pytz requires the
normalize()function to be called after arithmetic operations to correctly adjust the timezone offset, especially around daylight saving time transitions.
- icalendar.tools.to_datetime(dt)[source]#
Convert a date to a datetime.
If the input is already a
datetime, it is returned unchanged. If the input is adate, it is converted to adatetimeat midnight.- Parameters:
- Return type:
- Returns:
A
datetimeobject. If the input was adate, the time component will be set to midnight (00:00:00).
Example
>>> from datetime import date, datetime >>> from icalendar.tools import to_datetime >>> to_datetime(date(2024, 1, 15)) datetime.datetime(2024, 1, 15, 0, 0) >>> to_datetime(datetime(2024, 1, 15, 10, 30)) datetime.datetime(2024, 1, 15, 10, 30)