Ejemplo n.º 1
0
def test_time_int():
    value = 1590984783
    st = gmtime(value)
    ti = TimeInt(value)
    dt = ti.get_datetime()
    assert st.tm_zone == "UTC"
    assert dt.tzinfo == timezone.utc
    assert isinstance(ti, int)
    assert isinstance(ti, TimeInt)
    assert ti.year == st.tm_year == dt.year == 2020
    assert ti.month == st.tm_mon == dt.month == 6
    assert ti.day == st.tm_mday == dt.day == 1
    assert ti.hour == st.tm_hour == dt.hour == 4
    assert ti.minute == st.tm_min == dt.minute == 13
    assert ti.second == st.tm_sec == dt.second == 3
    assert ti > TimeInt.MIN
    assert ti < TimeInt.MAX
Ejemplo n.º 2
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