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 date and datetime objects, returning True only for pure date instances.

Parameters:

dt (date | datetime) -- The date or datetime object to check.

Return type:

bool

Returns:

True if the value is a date but not a datetime, False otherwise.

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:

True if the value is a datetime, False if it is only a date.

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() and normalize() methods for correct timezone calculations.

Parameters:

tz (tzinfo) -- The timezone info object to check.

Return type:

bool

Returns:

True if the timezone is a pytz timezone (has a localize attribute), False otherwise.

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:

True if the value is a datetime with a pytz timezone, False otherwise.

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.

Parameters:

dt (date | datetime) -- The date or datetime to normalize.

Return type:

date | datetime

Returns:

The normalized datetime if it uses pytz, otherwise the input unchanged.

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 a date, it is converted to a datetime at midnight.

Parameters:

dt (date | datetime) -- The date or datetime to convert.

Return type:

datetime

Returns:

A datetime object. If the input was a date, 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)