monkey_wrench.date_time._common module

monkey_wrench.date_time._common.assert_datetime_is_timezone_aware(datetime_object: datetime, silent: bool = False) bool[source]

Assert that the datetime_object is timezone-aware.

Note

This function relies on assert_().

Examples

>>> assert_datetime_is_timezone_aware(datetime.now(), silent=True)
False
>>> assert_datetime_is_timezone_aware(datetime.now(UTC))
True
monkey_wrench.date_time._common.assert_start_precedes_end(start_datetime: datetime, end_datetime: datetime, silent: bool = False) bool[source]

Assert that the start_datetime is not later than the end_datetime.

Note

This function relies on assert_().

Examples

>>> # The following will not raise an exception.
>>> assert_start_precedes_end(datetime(2020, 1, 1), datetime(2020, 12, 31))
True
>>> # The following will raise an exception!
>>> assert_start_precedes_end(datetime(2020, 1, 2), datetime(2020, 1, 1), silent=True)
False
>>> # The following will raise an exception!
>>> # assert_start_precedes_end(datetime(2020, 1, 2), datetime(2020, 1, 1))
monkey_wrench.date_time._common.assert_datetime_has_past(datetime_instance: datetime, silent: bool = False) bool[source]

Assert that the datetime_instance is in not in the future.

Note

This function relies on assert_().

Examples

>>> # The following will not raise an exception.
>>> assert_datetime_has_past(datetime(2020, 1, 1, tzinfo=UTC))
True
>>> assert_datetime_has_past(datetime(2100, 1, 2, tzinfo=UTC), silent=True)
False
>>> # The following will raise an exception!
>>> # assert_has_datetime_past(datetime(2100, 1, 2, tzinfo=UTC))
monkey_wrench.date_time._common.number_of_days_in_month(year: Annotated[int, Gt(gt=0), FieldInfo(annotation=NoneType, required=True, metadata=[Ge(ge=1950), Le(le=2100)])], month: Annotated[int, Gt(gt=0), FieldInfo(annotation=NoneType, required=True, metadata=[Ge(ge=1), Le(le=12)])]) Annotated[int, Gt(gt=0), FieldInfo(annotation=NoneType, required=True, metadata=[Ge(ge=1), Le(le=31)])][source]

Return the number of days in a month, taking into account both common and leap years.

Examples

>>> # `2018` was a common year.
>>> number_of_days_in_month(2018, 2)
28
>>> # `2020` was a leap year.
>>> number_of_days_in_month(2020, 2)
29
monkey_wrench.date_time._common.floor_datetime_minutes_to_specific_snapshots(datetime_instance: datetime, snapshots: list[Annotated[int, Ge(ge=0), FieldInfo(annotation=NoneType, required=True, metadata=[Lt(lt=60)])]] | None = None) datetime[source]

Floor the given datetime instance to the closest minute from the given list of snapshots.

Parameters:
  • datetime_instance – The datetime instance to floor.

  • snapshots – A (sorted) list of minutes. Defaults to None, which means the given datetime instance will be returned as it is, without any modifications. As an example, for SEVIRI we have one snapshot per 15 minutes, starting from the 12th minute. As a result, we have [12, 27, 42, 57] for SEVIRI snapshots in an hour. The snapshots will be first sorted in increasing order.

Returns:

A datetime instance which is smaller than or equal to the given datetime_instance, such that the minute matches the closest minute from the snapshots.

Examples

>>> floor_datetime_minutes_to_specific_snapshots(
...  datetime(2020, 1, 1, 0, 3), [12, 27, 42, 57]
... )
datetime.datetime(2019, 12, 31, 23, 57)
>>> floor_datetime_minutes_to_specific_snapshots(
...  datetime(2020, 1, 1, 0, 58), [12, 27, 42, 57]
... )
datetime.datetime(2020, 1, 1, 0, 57)
>>> floor_datetime_minutes_to_specific_snapshots(
...  datetime(2020, 1, 1, 1, 30), [12, 27, 42, 57]
... )
datetime.datetime(2020, 1, 1, 1, 27)
>>> floor_datetime_minutes_to_specific_snapshots(
...  datetime(2020, 1, 1, 1, 27), [12, 27, 42, 57]
... )
datetime.datetime(2020, 1, 1, 1, 27)
>>> floor_datetime_minutes_to_specific_snapshots(
...  datetime(2020, 1, 1, 1, 26)
... )
datetime.datetime(2020, 1, 1, 1, 26)