async def test_time_binary() -> None: assert "foo>=00:00" == str( filter.BinaryFilter({">=": ("foo", date.Time(0, 0, UTC))}) ) assert "foo<=23:59" == str( filter.BinaryFilter({"<=": ("foo", date.Time(23, 59, UTC))}) ) assert "foo<=03:09" == str( filter.BinaryFilter({"<=": ("foo", date.Time(3, 9, UTC))}) ) assert "foo<=03:09[Europe/Paris]" == str( filter.BinaryFilter( {"<=": ("foo", date.Time(3, 9, zoneinfo.ZoneInfo("Europe/Paris")))} ) ) now = date.utcnow() f = filter.BinaryFilter({"<=": ("foo", date.Time(5, 8, UTC))}) assert await f(FakePR({"foo": now.replace(hour=5, minute=8)})) assert await f(FakePR({"foo": now.replace(hour=2, minute=1)})) assert await f(FakePR({"foo": now.replace(hour=5, minute=1)})) assert not await f(FakePR({"foo": now.replace(hour=6, minute=2)})) assert not await f(FakePR({"foo": now.replace(hour=8, minute=9)})) f = filter.BinaryFilter({">=": ("foo", date.Time(5, 8, UTC))}) assert await f(FakePR({"foo": now.replace(hour=5, minute=8)})) assert not await f(FakePR({"foo": now.replace(hour=2, minute=1)})) assert not await f(FakePR({"foo": now.replace(hour=5, minute=1)})) assert await f(FakePR({"foo": now.replace(hour=6, minute=2)})) assert await f(FakePR({"foo": now.replace(hour=8, minute=9)})) f = filter.BinaryFilter( {">=": ("foo", date.Time(5, 8, zoneinfo.ZoneInfo("Europe/Paris")))} ) assert await f(FakePR({"foo": now.replace(hour=4, minute=8)})) assert not await f(FakePR({"foo": now.replace(hour=1, minute=1)})) assert not await f(FakePR({"foo": now.replace(hour=4, minute=1)})) assert await f(FakePR({"foo": now.replace(hour=5, minute=2)})) assert await f(FakePR({"foo": now.replace(hour=7, minute=9)}))
"<=": ( "current-day-of-week", date.DayOfWeek(5), ) }, ) }, { "and": ( { ">=": ( "current-time", date.Time( 12, 0, tzinfo=zoneinfo.ZoneInfo( "Europe/Paris" ), ), ) }, { "<=": ( "current-time", date.Time( 23, 59, tzinfo=zoneinfo.ZoneInfo( "Europe/Paris" ), ),
def test_time_compare(): utc = datetime.timezone.utc with freeze_time("2021-09-22T08:00:05", tz_offset=0): assert datetime.datetime(2021, 9, 22, 8, 0, 5, tzinfo=utc) >= date.Time(8, 0, utc) with freeze_time("2012-01-14T12:15:00", tz_offset=0): assert date.Time(12, 0, utc) < date.utcnow() assert date.Time(15, 45, utc) > date.utcnow() assert date.Time(12, 15, utc) == date.utcnow() assert date.utcnow() > date.Time(12, 0, utc) assert date.utcnow() < date.Time(15, 45, utc) assert date.utcnow() == date.Time(12, 15, utc) assert date.Time(13, 15, utc) == date.Time(13, 15, utc) assert date.Time(13, 15, utc) < date.Time(15, 15, utc) assert date.Time(15, 0, utc) > date.Time(5, 0, utc) # TZ that endup the same day zone = zoneinfo.ZoneInfo("Europe/Paris") assert date.Time(10, 0, zone) < date.utcnow() assert date.Time(18, 45, zone) > date.utcnow() assert date.Time(13, 15, zone) == date.utcnow() assert date.utcnow() > date.Time(10, 0, zone) assert date.utcnow() < date.Time(18, 45, zone) assert date.utcnow() == date.Time(13, 15, zone) assert date.Time(13, 15, zone) == date.Time(13, 15, zone) assert date.Time(13, 15, zone) < date.Time(15, 15, zone) assert date.Time(15, 0, zone) > date.Time(5, 0, zone) # TZ that endup the next day GMT + 13 zone = zoneinfo.ZoneInfo("Pacific/Auckland") assert date.Time(0, 2, zone) < date.utcnow() assert date.Time(2, 9, zone) > date.utcnow() assert date.Time(1, 15, zone) == date.utcnow() assert date.utcnow() > date.Time(0, 2, zone) assert date.utcnow() < date.Time(2, 9, zone) assert date.utcnow() == date.Time(1, 15, zone) assert date.Time(13, 15, zone) == date.Time(13, 15, zone) assert date.Time(13, 15, zone) < date.Time(15, 15, zone) assert date.Time(15, 0, zone) > date.Time(5, 0, zone) assert date.utcnow() == date.utcnow() assert (date.utcnow() > date.utcnow()) is False
async def test_multiple_near_datetime() -> None: with freeze_time("2012-01-14T05:08:00", tz_offset=0) as frozen_time: tzinfo = UTC now = frozen_time().replace(tzinfo=tzinfo) now_time = date.Time(now.hour, now.minute, tzinfo) atmidnight = now.replace( day=now.day + 1, hour=0, second=0, minute=0, microsecond=0, ) in_two_hours = now + datetime.timedelta(hours=2) assert tzinfo == in_two_hours.tzinfo in_two_hours_time = date.Time( in_two_hours.hour, in_two_hours.minute, in_two_hours.tzinfo ) soon = now + datetime.timedelta(minutes=1) f = filter.NearDatetimeFilter( { "or": [ {"<=": ("foo", in_two_hours_time)}, {"<=": ("foo", now_time)}, ] } ) frozen_time.move_to(now) assert await f(FakePR({"foo": frozen_time().replace(tzinfo=UTC)})) == soon frozen_time.move_to(now.replace(hour=2, minute=1)) assert await f(FakePR({"foo": frozen_time().replace(tzinfo=UTC)})) == now frozen_time.move_to(now.replace(hour=6, minute=8)) assert ( await f(FakePR({"foo": frozen_time().replace(tzinfo=UTC)})) == in_two_hours ) frozen_time.move_to(now.replace(hour=8, minute=9)) assert await f(FakePR({"foo": frozen_time().replace(tzinfo=UTC)})) == atmidnight frozen_time.move_to(now.replace(hour=18, minute=9)) assert await f(FakePR({"foo": frozen_time().replace(tzinfo=UTC)})) == atmidnight f = filter.NearDatetimeFilter( { "and": [ {">=": ("foo", in_two_hours_time)}, {">=": ("foo", now_time)}, ] } ) frozen_time.move_to(now) assert await f(FakePR({"foo": frozen_time().replace(tzinfo=UTC)})) == soon frozen_time.move_to(now.replace(hour=2, minute=1)) assert await f(FakePR({"foo": frozen_time().replace(tzinfo=UTC)})) == now frozen_time.move_to(now.replace(hour=6, minute=8)) assert ( await f(FakePR({"foo": frozen_time().replace(tzinfo=UTC)})) == in_two_hours ) frozen_time.move_to(now.replace(hour=8, minute=9)) assert await f(FakePR({"foo": frozen_time().replace(tzinfo=UTC)})) == atmidnight frozen_time.move_to(now.replace(hour=18, minute=9)) assert await f(FakePR({"foo": frozen_time().replace(tzinfo=UTC)})) == atmidnight f = filter.NearDatetimeFilter( { "or": [ {">=": ("foo", in_two_hours_time)}, {">=": ("foo", now_time)}, ] } ) frozen_time.move_to(now) assert await f(FakePR({"foo": frozen_time().replace(tzinfo=UTC)})) == soon frozen_time.move_to(now.replace(hour=2, minute=1)) assert await f(FakePR({"foo": frozen_time().replace(tzinfo=UTC)})) == now frozen_time.move_to(now.replace(hour=6, minute=8)) assert ( await f(FakePR({"foo": frozen_time().replace(tzinfo=UTC)})) == in_two_hours ) frozen_time.move_to(now.replace(hour=8, minute=9)) assert await f(FakePR({"foo": frozen_time().replace(tzinfo=UTC)})) == atmidnight frozen_time.move_to(now.replace(hour=18, minute=9)) assert await f(FakePR({"foo": frozen_time().replace(tzinfo=UTC)})) == atmidnight f = filter.NearDatetimeFilter( { "and": [ {"<=": ("foo", in_two_hours_time)}, {"<=": ("foo", now_time)}, ] } ) frozen_time.move_to(now) assert await f(FakePR({"foo": frozen_time().replace(tzinfo=UTC)})) == soon frozen_time.move_to(now.replace(hour=2, minute=1)) assert await f(FakePR({"foo": frozen_time().replace(tzinfo=UTC)})) == now frozen_time.move_to(now.replace(hour=6, minute=8)) assert ( await f(FakePR({"foo": frozen_time().replace(tzinfo=UTC)})) == in_two_hours ) frozen_time.move_to(now.replace(hour=8, minute=9)) assert await f(FakePR({"foo": frozen_time().replace(tzinfo=UTC)})) == atmidnight frozen_time.move_to(now.replace(hour=18, minute=9)) assert await f(FakePR({"foo": frozen_time().replace(tzinfo=UTC)})) == atmidnight
async def test_time_near_datetime() -> None: with freeze_time("2012-01-06T05:08:00", tz_offset=0) as frozen_time: tzinfo = UTC now = frozen_time().replace(tzinfo=tzinfo) time_now = date.Time(now.hour, now.minute, tzinfo) atmidnight = now.replace( day=now.day + 1, hour=0, second=0, minute=0, microsecond=0, ) nextday = now.replace(day=now.day + 1) soon = now.replace(minute=now.minute + 1) f = filter.NearDatetimeFilter({"<=": ("foo", time_now)}) frozen_time.move_to(now) assert await f(FakePR({"foo": frozen_time().replace(tzinfo=UTC)})) == soon frozen_time.move_to(now.replace(hour=2, minute=1)) assert await f(FakePR({"foo": frozen_time().replace(tzinfo=UTC)})) == now frozen_time.move_to(now.replace(hour=5, minute=1)) assert await f(FakePR({"foo": frozen_time().replace(tzinfo=UTC)})) == now frozen_time.move_to(now.replace(hour=6, minute=2)) assert await f(FakePR({"foo": frozen_time().replace(tzinfo=UTC)})) == atmidnight frozen_time.move_to(now.replace(hour=8, minute=9)) assert await f(FakePR({"foo": frozen_time().replace(tzinfo=UTC)})) == atmidnight assert await f(FakePR({"foo": None})) == date.DT_MAX f = filter.NearDatetimeFilter({"<": ("foo", time_now)}) frozen_time.move_to(now) assert await f(FakePR({"foo": frozen_time().replace(tzinfo=UTC)})) == atmidnight frozen_time.move_to(now.replace(hour=2, minute=1)) assert await f(FakePR({"foo": frozen_time().replace(tzinfo=UTC)})) == now frozen_time.move_to(now.replace(hour=5, minute=1)) assert await f(FakePR({"foo": frozen_time().replace(tzinfo=UTC)})) == now frozen_time.move_to(now.replace(hour=6, minute=2)) assert await f(FakePR({"foo": frozen_time().replace(tzinfo=UTC)})) == atmidnight frozen_time.move_to(now.replace(hour=8, minute=9)) assert await f(FakePR({"foo": frozen_time().replace(tzinfo=UTC)})) == atmidnight assert await f(FakePR({"foo": None})) == date.DT_MAX f = filter.NearDatetimeFilter({">=": ("foo", time_now)}) frozen_time.move_to(now) assert await f(FakePR({"foo": frozen_time().replace(tzinfo=UTC)})) == soon frozen_time.move_to(now.replace(hour=2, minute=1)) assert await f(FakePR({"foo": frozen_time().replace(tzinfo=UTC)})) == now frozen_time.move_to(now.replace(hour=5, minute=1)) assert await f(FakePR({"foo": frozen_time().replace(tzinfo=UTC)})) == now frozen_time.move_to(now.replace(hour=6, minute=2)) assert await f(FakePR({"foo": frozen_time().replace(tzinfo=UTC)})) == atmidnight frozen_time.move_to(now.replace(hour=8, minute=9)) assert await f(FakePR({"foo": frozen_time().replace(tzinfo=UTC)})) == atmidnight assert await f(FakePR({"foo": None})) == date.DT_MAX f = filter.NearDatetimeFilter({">": ("foo", time_now)}) frozen_time.move_to(now) assert await f(FakePR({"foo": frozen_time().replace(tzinfo=UTC)})) == atmidnight frozen_time.move_to(now.replace(hour=2, minute=1)) assert await f(FakePR({"foo": frozen_time().replace(tzinfo=UTC)})) == now frozen_time.move_to(now.replace(hour=5, minute=1)) assert await f(FakePR({"foo": frozen_time().replace(tzinfo=UTC)})) == now frozen_time.move_to(now.replace(hour=6, minute=2)) assert await f(FakePR({"foo": frozen_time().replace(tzinfo=UTC)})) == atmidnight frozen_time.move_to(now.replace(hour=8, minute=9)) assert await f(FakePR({"foo": frozen_time().replace(tzinfo=UTC)})) == atmidnight assert await f(FakePR({"foo": None})) == date.DT_MAX f = filter.NearDatetimeFilter({"=": ("foo", time_now)}) frozen_time.move_to(now) assert await f(FakePR({"foo": frozen_time().replace(tzinfo=UTC)})) == soon frozen_time.move_to(now.replace(hour=2, minute=1)) assert await f(FakePR({"foo": frozen_time().replace(tzinfo=UTC)})) == now frozen_time.move_to(now.replace(hour=5, minute=1)) assert await f(FakePR({"foo": frozen_time().replace(tzinfo=UTC)})) == now frozen_time.move_to(now.replace(hour=6, minute=2)) assert await f(FakePR({"foo": frozen_time().replace(tzinfo=UTC)})) == nextday frozen_time.move_to(now.replace(hour=8, minute=9)) assert await f(FakePR({"foo": frozen_time().replace(tzinfo=UTC)})) == nextday assert await f(FakePR({"foo": None})) == date.DT_MAX f = filter.NearDatetimeFilter({"!=": ("foo", time_now)}) frozen_time.move_to(now) assert await f(FakePR({"foo": frozen_time().replace(tzinfo=UTC)})) == soon frozen_time.move_to(now.replace(hour=2, minute=1)) assert await f(FakePR({"foo": frozen_time().replace(tzinfo=UTC)})) == now frozen_time.move_to(now.replace(hour=5, minute=1)) assert await f(FakePR({"foo": frozen_time().replace(tzinfo=UTC)})) == now frozen_time.move_to(now.replace(hour=6, minute=2)) assert await f(FakePR({"foo": frozen_time().replace(tzinfo=UTC)})) == nextday frozen_time.move_to(now.replace(hour=8, minute=9)) assert await f(FakePR({"foo": frozen_time().replace(tzinfo=UTC)})) == nextday assert await f(FakePR({"foo": None})) == date.DT_MAX