Ejemplo n.º 1
0
def test_errors():
    with pytest.raises(ValueError):
        TimeInt(-1)
    with pytest.raises(ValueError):
        TimeInt(TimeInt.MAX + 1)
    with pytest.raises(ValueError):
        TimeInt("bogus")
    TimeInt(0)
    TimeInt(TimeInt.MAX)
    ti = TimeInt(49299112)
    with pytest.raises(ValueError):
        ti.trunc("bogus")
    with pytest.raises(ValueError):
        ti.trunc(TimeTruncUnit.DAY, num=-2)
    with pytest.raises(ValueError):
        ti.trunc(TimeTruncUnit.WEEK, num=2)
    with pytest.raises(ValueError):
        ti.trunc(TimeTruncUnit.MONTH, num=0)
Ejemplo n.º 2
0
def test_trunc():
    values = (2018, 8, 18, 12, 0, 1, 0, 0, 0)
    epoch = timegm(values)
    raw_ti = TimeInt(epoch)

    ti = raw_ti.trunc(TimeTruncUnit.YEAR, num=6)
    assert ti.get_pretty() == "2016"
    ti = raw_ti.trunc(TimeTruncUnit.MONTH, num=3)
    assert ti.get_pretty() == "2018-07"
    ti = raw_ti.trunc(TimeTruncUnit.WEEK)
    assert ti.get_pretty() == "2018-08-12"
    ti = raw_ti.trunc(TimeTruncUnit.DAY, num=7)
    assert ti.get_pretty() == "2018-08-15"
    ti = raw_ti.trunc(TimeTruncUnit.HOUR, num=5)
    assert ti.get_pretty() == "2018-08-18 10 AM"
    ti = raw_ti.trunc(TimeTruncUnit.MINUTE, num=2)
    assert ti.get_pretty() == "2018-08-18 12 PM"
Ejemplo n.º 3
0
def test_time(year, month, day, hour, minute, second):
    dt = datetime(
        year=year,
        month=month,
        day=day,
        hour=hour,
        minute=minute,
        second=second,
        tzinfo=timezone.utc,
    )
    dt_minute = datetime(
        year=year,
        month=month,
        day=day,
        hour=hour,
        minute=minute,
        second=0,
        tzinfo=timezone.utc,
    )
    dt_hour = datetime(
        year=year,
        month=month,
        day=day,
        hour=hour,
        minute=0,
        second=0,
        tzinfo=timezone.utc,
    )
    dt_day = datetime(year=year,
                      month=month,
                      day=day,
                      hour=0,
                      minute=0,
                      second=0,
                      tzinfo=timezone.utc)
    dt_month = datetime(year=year,
                        month=month,
                        day=1,
                        hour=0,
                        minute=0,
                        second=0,
                        tzinfo=timezone.utc)
    dt_year = datetime(year=year,
                       month=1,
                       day=1,
                       hour=0,
                       minute=0,
                       second=0,
                       tzinfo=timezone.utc)
    time_minute = TimeInt(dt_minute.timestamp())
    time_hour = TimeInt(dt_hour.timestamp())
    time_day = TimeInt(dt_day.timestamp())
    time_month = TimeInt(dt_month.timestamp())
    time_year = TimeInt(dt_year.timestamp())
    time = TimeInt(dt.timestamp())
    # time attrs equal to dt counterpart
    dt2 = time.get_datetime()
    assert time.year == dt.year == dt2.year
    assert time.month == dt.month == dt2.month
    assert time.day == dt.day == dt2.day
    assert time.hour == dt.hour == dt2.hour
    assert time.minute == dt.minute == dt2.minute
    assert time.second == dt.second == dt2.second
    assert dt == dt2
    # For TimeInt week starts at Sunday, for datetime its Monday.
    assert time.weekday == (dt.weekday() + 1) % 7
    # times greater than their truncated parts
    assert time >= time_minute
    assert time >= time_hour
    assert time >= time_day
    assert time >= time_month
    assert time >= time_year
    # time attrs
    assert time.second == second
    assert time.minute == minute
    assert time.hour == hour
    assert time.day == day
    assert time.month == month
    assert time.year == year
    # time_minute attrs
    assert time_minute.second == 0
    assert time_minute.minute == minute
    assert time_minute.hour == hour
    assert time_minute.day == day
    assert time_minute.month == month
    assert time_minute.year == year
    # time_hour attrs
    assert time_hour.second == 0
    assert time_hour.minute == 0
    assert time_hour.hour == hour
    assert time_hour.month == month
    assert time_hour.year == year
    # time_day attrs
    assert time_day.second == 0
    assert time_day.minute == 0
    assert time_day.hour == 0
    assert time_day.day == day
    assert time_day.month == month
    assert time_day.year == year
    # time_month attrs
    assert time_month.second == 0
    assert time_month.minute == 0
    assert time_month.hour == 0
    assert time_month.day == 1
    assert time_month.month == month
    assert time_month.year == year
    # time_year attrs
    assert time_year.second == 0
    assert time_year.minute == 0
    assert time_year.hour == 0
    assert time_year.day == 1
    assert time_year.month == 1
    assert time_year.year == year

    assert time.trunc_minute() == time_minute
    assert time.trunc_hour() == time_hour
    assert time.trunc_day() == time_day
    assert time.trunc_month() == time_month
    assert time.trunc_year() == time_year
    assert time.trunc(TimeTruncUnit.MINUTE) == time_minute
    assert time.trunc(TimeTruncUnit.HOUR) == time_hour
    assert time.trunc(TimeTruncUnit.DAY) == time_day
    assert time.trunc(TimeTruncUnit.MONTH) == time_month
    assert time.trunc(TimeTruncUnit.YEAR) == time_year
Ejemplo n.º 4
0
 def __init__(self, time: TimeInt):
     """Round down time to most recent time that fell evenly on scale."""
     self.time = time.trunc(self.unit, num=self.num)