Skip to content

Date & time assertions

Assertions for datetime.date and datetime.datetime values.

Date and time assertions mixin.

is_before

is_before(other) -> Self

Asserts that val is a date and is before other date.

Parameters:

Name Type Description Default
other object

the other date, expected to be after val

required

Examples:

Usage:

import datetime

today = datetime.datetime.now()
yesterday = today - datetime.timedelta(days=1)

assert_that(yesterday).is_before(today)

Returns:

Name Type Description
AssertionBuilder Self

returns this instance to chain to the next assertion

Raises:

Type Description
AssertionError

if val is not before the given date

See Also

is_less_than() - numeric assertion, but also works with datetime is_less_than_or_equal_to() - also works with datetime

Source code in assertpy2/date.py
def is_before(self, other) -> Self:
    """Asserts that val is a date and is before other date.

    Args:
        other (object): the other date, expected to be after val

    Examples:
        Usage:

            import datetime

            today = datetime.datetime.now()
            yesterday = today - datetime.timedelta(days=1)

            assert_that(yesterday).is_before(today)

    Returns:
        AssertionBuilder: returns this instance to chain to the next assertion

    Raises:
        AssertionError: if val is **not** before the given date

    See Also:
        [`is_less_than()`][assertpy2.numeric.NumericMixin.is_less_than] - numeric assertion, but
            also works with datetime
        [`is_less_than_or_equal_to()`][assertpy2.numeric.NumericMixin.is_less_than_or_equal_to] -
            also works with datetime
    """
    _require_datetime(self.val, "val")
    _require_datetime(other, "given arg")
    if self.val >= other:
        return self.error(
            f"Expected <{self.val.strftime('%Y-%m-%d %H:%M:%S')}> to be before"
            f" <{other.strftime('%Y-%m-%d %H:%M:%S')}>, but was not."
        )
    return self

is_after

is_after(other) -> Self

Asserts that val is a date and is after other date.

Parameters:

Name Type Description Default
other object

the other date, expected to be before val

required

Examples:

Usage:

import datetime

today = datetime.datetime.now()
yesterday = today - datetime.timedelta(days=1)

assert_that(today).is_after(yesterday)

Returns:

Name Type Description
AssertionBuilder Self

returns this instance to chain to the next assertion

Raises:

Type Description
AssertionError

if val is not after the given date

See Also

is_greater_than() - numeric assertion, but also works with datetime is_greater_than_or_equal_to() - also works with datetime

Source code in assertpy2/date.py
def is_after(self, other) -> Self:
    """Asserts that val is a date and is after other date.

    Args:
        other (object): the other date, expected to be before val

    Examples:
        Usage:

            import datetime

            today = datetime.datetime.now()
            yesterday = today - datetime.timedelta(days=1)

            assert_that(today).is_after(yesterday)

    Returns:
        AssertionBuilder: returns this instance to chain to the next assertion

    Raises:
        AssertionError: if val is **not** after the given date

    See Also:
        [`is_greater_than()`][assertpy2.numeric.NumericMixin.is_greater_than] - numeric assertion,
            but also works with datetime
        [`is_greater_than_or_equal_to()`][assertpy2.numeric.NumericMixin.is_greater_than_or_equal_to] -
            also works with datetime
    """
    _require_datetime(self.val, "val")
    _require_datetime(other, "given arg")
    if self.val <= other:
        return self.error(
            f"Expected <{self.val.strftime('%Y-%m-%d %H:%M:%S')}> to be after"
            f" <{other.strftime('%Y-%m-%d %H:%M:%S')}>, but was not."
        )
    return self

is_before_or_equal_to

is_before_or_equal_to(other) -> Self

Asserts that val is a date and is before or equal to other date.

Parameters:

Name Type Description Default
other object

the other date, expected to be after or equal to val

required

Examples:

Usage:

import datetime

today = datetime.datetime.now()
yesterday = today - datetime.timedelta(days=1)

assert_that(yesterday).is_before_or_equal_to(today)
assert_that(today).is_before_or_equal_to(today)

Returns:

Name Type Description
AssertionBuilder Self

returns this instance to chain to the next assertion

Raises:

Type Description
AssertionError

if val is not before or equal to the given date

Source code in assertpy2/date.py
def is_before_or_equal_to(self, other) -> Self:
    """Asserts that val is a date and is before or equal to other date.

    Args:
        other (object): the other date, expected to be after or equal to val

    Examples:
        Usage:

            import datetime

            today = datetime.datetime.now()
            yesterday = today - datetime.timedelta(days=1)

            assert_that(yesterday).is_before_or_equal_to(today)
            assert_that(today).is_before_or_equal_to(today)

    Returns:
        AssertionBuilder: returns this instance to chain to the next assertion

    Raises:
        AssertionError: if val is **not** before or equal to the given date
    """
    _require_datetime(self.val, "val")
    _require_datetime(other, "given arg")
    if self.val > other:
        return self.error(
            f"Expected <{self.val.strftime('%Y-%m-%d %H:%M:%S')}> to be before or equal to"
            f" <{other.strftime('%Y-%m-%d %H:%M:%S')}>, but was not."
        )
    return self

is_after_or_equal_to

is_after_or_equal_to(other) -> Self

Asserts that val is a date and is after or equal to other date.

Parameters:

Name Type Description Default
other object

the other date, expected to be before or equal to val

required

Examples:

Usage:

import datetime

today = datetime.datetime.now()
yesterday = today - datetime.timedelta(days=1)

assert_that(today).is_after_or_equal_to(yesterday)
assert_that(today).is_after_or_equal_to(today)

Returns:

Name Type Description
AssertionBuilder Self

returns this instance to chain to the next assertion

Raises:

Type Description
AssertionError

if val is not after or equal to the given date

Source code in assertpy2/date.py
def is_after_or_equal_to(self, other) -> Self:
    """Asserts that val is a date and is after or equal to other date.

    Args:
        other (object): the other date, expected to be before or equal to val

    Examples:
        Usage:

            import datetime

            today = datetime.datetime.now()
            yesterday = today - datetime.timedelta(days=1)

            assert_that(today).is_after_or_equal_to(yesterday)
            assert_that(today).is_after_or_equal_to(today)

    Returns:
        AssertionBuilder: returns this instance to chain to the next assertion

    Raises:
        AssertionError: if val is **not** after or equal to the given date
    """
    _require_datetime(self.val, "val")
    _require_datetime(other, "given arg")
    if self.val < other:
        return self.error(
            f"Expected <{self.val.strftime('%Y-%m-%d %H:%M:%S')}> to be after or equal to"
            f" <{other.strftime('%Y-%m-%d %H:%M:%S')}>, but was not."
        )
    return self

is_equal_to_ignoring_milliseconds

is_equal_to_ignoring_milliseconds(other) -> Self

Asserts that val is a date and is equal to other date to the second.

Parameters:

Name Type Description Default
other object

the other date, expected to be equal to the second

required

Examples:

Usage:

import datetime

d1 = datetime.datetime(2020, 1, 2, 3, 4, 5, 6)       # 2020-01-02 03:04:05.000006
d2 = datetime.datetime(2020, 1, 2, 3, 4, 5, 777777)  # 2020-01-02 03:04:05.777777

assert_that(d1).is_equal_to_ignoring_milliseconds(d2)

Returns:

Name Type Description
AssertionBuilder Self

returns this instance to chain to the next assertion

Raises:

Type Description
AssertionError

if val is not equal to the given date to the second

Source code in assertpy2/date.py
def is_equal_to_ignoring_milliseconds(self, other) -> Self:
    """Asserts that val is a date and is equal to other date to the second.

    Args:
        other (object): the other date, expected to be equal to the second

    Examples:
        Usage:

            import datetime

            d1 = datetime.datetime(2020, 1, 2, 3, 4, 5, 6)       # 2020-01-02 03:04:05.000006
            d2 = datetime.datetime(2020, 1, 2, 3, 4, 5, 777777)  # 2020-01-02 03:04:05.777777

            assert_that(d1).is_equal_to_ignoring_milliseconds(d2)

    Returns:
        AssertionBuilder: returns this instance to chain to the next assertion

    Raises:
        AssertionError: if val is **not** equal to the given date to the second
    """
    _require_datetime(self.val, "val")
    _require_datetime(other, "given arg")
    if (
        self.val.date() != other.date()
        or self.val.hour != other.hour
        or self.val.minute != other.minute
        or self.val.second != other.second
    ):
        return self.error(
            f"Expected <{self.val.strftime('%Y-%m-%d %H:%M:%S')}> to be equal to"
            f" <{other.strftime('%Y-%m-%d %H:%M:%S')}>, but was not."
        )
    return self

is_equal_to_ignoring_seconds

is_equal_to_ignoring_seconds(other) -> Self

Asserts that val is a date and is equal to other date to the minute.

Parameters:

Name Type Description Default
other object

the other date, expected to be equal to the minute

required

Examples:

Usage:

import datetime

d1 = datetime.datetime(2020, 1, 2, 3, 4, 5)   # 2020-01-02 03:04:05
d2 = datetime.datetime(2020, 1, 2, 3, 4, 55)  # 2020-01-02 03:04:55

assert_that(d1).is_equal_to_ignoring_seconds(d2)

Returns:

Name Type Description
AssertionBuilder Self

returns this instance to chain to the next assertion

Raises:

Type Description
AssertionError

if val is not equal to the given date to the minute

Source code in assertpy2/date.py
def is_equal_to_ignoring_seconds(self, other) -> Self:
    """Asserts that val is a date and is equal to other date to the minute.

    Args:
        other (object): the other date, expected to be equal to the minute

    Examples:
        Usage:

            import datetime

            d1 = datetime.datetime(2020, 1, 2, 3, 4, 5)   # 2020-01-02 03:04:05
            d2 = datetime.datetime(2020, 1, 2, 3, 4, 55)  # 2020-01-02 03:04:55

            assert_that(d1).is_equal_to_ignoring_seconds(d2)

    Returns:
        AssertionBuilder: returns this instance to chain to the next assertion

    Raises:
        AssertionError: if val is **not** equal to the given date to the minute
    """
    _require_datetime(self.val, "val")
    _require_datetime(other, "given arg")
    if self.val.date() != other.date() or self.val.hour != other.hour or self.val.minute != other.minute:
        return self.error(
            f"Expected <{self.val.strftime('%Y-%m-%d %H:%M')}> to be equal to"
            f" <{other.strftime('%Y-%m-%d %H:%M')}>, but was not."
        )
    return self

is_equal_to_ignoring_time

is_equal_to_ignoring_time(other) -> Self

Asserts that val is a date and is equal to other date ignoring time.

Parameters:

Name Type Description Default
other object

the other date, expected to be equal ignoring time

required

Examples:

Usage:

import datetime

d1 = datetime.datetime(2020, 1, 2, 3, 4, 5)     # 2020-01-02 03:04:05
d2 = datetime.datetime(2020, 1, 2, 13, 44, 55)  # 2020-01-02 13:44:55

assert_that(d1).is_equal_to_ignoring_time(d2)

Returns:

Name Type Description
AssertionBuilder Self

returns this instance to chain to the next assertion

Raises:

Type Description
AssertionError

if val is not equal to the given date ignoring time

Source code in assertpy2/date.py
def is_equal_to_ignoring_time(self, other) -> Self:
    """Asserts that val is a date and is equal to other date ignoring time.

    Args:
        other (object): the other date, expected to be equal ignoring time

    Examples:
        Usage:

            import datetime

            d1 = datetime.datetime(2020, 1, 2, 3, 4, 5)     # 2020-01-02 03:04:05
            d2 = datetime.datetime(2020, 1, 2, 13, 44, 55)  # 2020-01-02 13:44:55

            assert_that(d1).is_equal_to_ignoring_time(d2)

    Returns:
        AssertionBuilder: returns this instance to chain to the next assertion

    Raises:
        AssertionError: if val is **not** equal to the given date ignoring time
    """
    _require_datetime(self.val, "val")
    _require_datetime(other, "given arg")
    if self.val.date() != other.date():
        return self.error(
            f"Expected <{self.val.strftime('%Y-%m-%d')}> to be equal to"
            f" <{other.strftime('%Y-%m-%d')}>, but was not."
        )
    return self