Ejemplo n.º 1
0
 def test_get_minute(self):
     assert xdatetime.get().minute == datetime.now().minute
Ejemplo n.º 2
0
 def test_get_second(self):
     assert xdatetime.get().second == datetime.now().second
Ejemplo n.º 3
0
 def test_get_day(self):
     assert xdatetime.get().day == datetime.now().day
Ejemplo n.º 4
0
 def test_get_hour(self):
     assert xdatetime.get().hour == datetime.now().hour
Ejemplo n.º 5
0
 def test_get_now(self):
     assert xdatetime.get().format() == arrow.now().format(
         "YYYY-MM-DD HH:mm:ss")
Ejemplo n.º 6
0
 def test_get_month(self):
     assert xdatetime.get().month == datetime.now().month
Ejemplo n.º 7
0
 def test_date_to_time(self, get_date, result):
     assert xdatetime.get(get_date).format() == result
Ejemplo n.º 8
0
 def test_get_last(self, year, month, result):
     assert xdatetime.get(year, month).last == result
Ejemplo n.º 9
0
 def test_string_to_microsecond(self, all_datetime_string, formatting):
     assert xdatetime.get(all_datetime_string).format(
         formatting) == all_datetime_string
Ejemplo n.º 10
0
 def test_timestamp_to_microsecond(self, timestamp, result):
     assert xdatetime.get(timestamp).format() == result
Ejemplo n.º 11
0
 def test_timestamp_beijing_datetime(self, timestamp):
     assert xdatetime.get(timestamp).format() == arrow.get(
         timestamp, tzinfo=xdatetime.utc8()).format("YYYY-MM-DD HH:mm:ss")
Ejemplo n.º 12
0
 def test_timestamp_utc_datetime(self, timestamp):
     assert xdatetime.get(timestamp, tz=xdatetime.utc8(
         hours=0, minutes=0)).format() == arrow.get(timestamp).format(
             "YYYY-MM-DD HH:mm:ss")
Ejemplo n.º 13
0
 def test_get_utc_now(self):
     assert xdatetime.get(tz=xdatetime.utc8(hours=0, minutes=0)).format(
     ) == arrow.utcnow().format("YYYY-MM-DD HH:mm:ss")
Ejemplo n.º 14
0
 def test_get_microsecond(self):
     assert xdatetime.get().microsecond == datetime.now().microsecond
Ejemplo n.º 15
0
 def test_get_timestamp(self):
     assert int(xdatetime.get().timestamp) == int(time.time())
Ejemplo n.º 16
0
 def test_get_compare(self, start, end, hows):
     assert xdatetime.get(start, end).how == hows
Ejemplo n.º 17
0
 def test_get_year(self):
     assert xdatetime.get().year == datetime.now().year
Ejemplo n.º 18
0
 def test_get_middle(self, reference, start, result):
     assert xdatetime.get(reference, start).middle == result
Ejemplo n.º 19
0
class TestXDateTimeGet(object):

    # 获取本地时间
    def test_get_now(self):
        assert xdatetime.get().format() == arrow.now().format(
            "YYYY-MM-DD HH:mm:ss")

    # 获取UTC时间
    def test_get_utc_now(self):
        assert xdatetime.get(tz=xdatetime.utc8(hours=0, minutes=0)).format(
        ) == arrow.utcnow().format("YYYY-MM-DD HH:mm:ss")

    timestamp_data = [
        1584689499.2525, 1584689499, 1584689499.252555, 1584689499.12457899999,
        1584
    ]

    # 时间戳转时间格式(UTC)
    @pytest.mark.parametrize("timestamp", timestamp_data)
    def test_timestamp_utc_datetime(self, timestamp):
        assert xdatetime.get(timestamp, tz=xdatetime.utc8(
            hours=0, minutes=0)).format() == arrow.get(timestamp).format(
                "YYYY-MM-DD HH:mm:ss")

    # 时间戳转时间格式(北京时间)
    @pytest.mark.parametrize("timestamp", timestamp_data)
    def test_timestamp_beijing_datetime(self, timestamp):
        assert xdatetime.get(timestamp).format() == arrow.get(
            timestamp, tzinfo=xdatetime.utc8()).format("YYYY-MM-DD HH:mm:ss")

    all_datetime_string = [
        # 完整日期,时间,微秒,时区
        ("2020-03-20T22:09:06.252525+0800", "%Y-%m-%dT%H:%M:%S.%f%z"),
        ("2020-03-20 10:09:06.252525+0800", "%Y-%m-%d %H:%M:%S.%f%z"),
        ("2020/03/20T10:09:06.252525+0800", "%Y/%m/%dT%H:%M:%S.%f%z"),

        # 日期时间
        ("2020-03-20T22:09:06", "%Y-%m-%dT%H:%M:%S"),
        ("2020-03-20 10:09:06", "%Y-%m-%d %H:%M:%S"),
        ("2020/03/20 10:09:06", "%Y/%m/%d %H:%M:%S"),
        ("2020.03.20 10:09:06", "%Y.%m.%d %H:%M:%S"),

        # 秒级时间
        ("15:22:56", "%H:%M:%S"),

        # 微秒级时间
        ("15:22:56.252525", "%H:%M:%S.%f"),
    ]

    # 时间字符串转时间(完整日期,时间,微秒,时区)
    @pytest.mark.parametrize("all_datetime_string,formatting",
                             all_datetime_string)
    def test_string_to_microsecond(self, all_datetime_string, formatting):
        assert xdatetime.get(all_datetime_string).format(
            formatting) == all_datetime_string

    timestamp_string = [(1585609370, "2020-03-31 07:02:50"),
                        ("1585609370", "2020-03-31 07:02:50")]

    # 时间字符串转时间(时间戳)
    @pytest.mark.parametrize("timestamp,result", timestamp_string)
    def test_timestamp_to_microsecond(self, timestamp, result):
        assert xdatetime.get(timestamp).format() == result

    datetime_to_time = [(datetime(2020, 3, 23, 21, 56,
                                  12), "2020-03-23 21:56:12"),
                        (datetime(2020, 3, 23), "2020-03-23 00:00:00")]

    # datetime转时间
    @pytest.mark.parametrize("get_datetime,result", datetime_to_time)
    def test_datetime_to_time(self, get_datetime, result):
        assert xdatetime.get(get_datetime).format() == result

    date_to_time = [(date(2020, 3, 23), "2020-03-23 00:00:00")]

    # date转时间
    @pytest.mark.parametrize("get_date,result", date_to_time)
    def test_date_to_time(self, get_date, result):
        assert xdatetime.get(get_date).format() == result

    # 获取时间戳
    def test_get_timestamp(self):
        assert int(xdatetime.get().timestamp) == int(time.time())

    # 获取年
    def test_get_year(self):
        assert xdatetime.get().year == datetime.now().year

    # 获取月
    def test_get_month(self):
        assert xdatetime.get().month == datetime.now().month

    # 获取日
    def test_get_day(self):
        assert xdatetime.get().day == datetime.now().day

    # 获取时
    def test_get_hour(self):
        assert xdatetime.get().hour == datetime.now().hour

    # 获取分
    def test_get_minute(self):
        assert xdatetime.get().minute == datetime.now().minute

    # 获取秒
    def test_get_second(self):
        assert xdatetime.get().second == datetime.now().second

    # 获取毫秒
    def test_get_microsecond(self):
        assert xdatetime.get().microsecond == datetime.now().microsecond

    get_shift = [
        (xdatetime.get().shift(years=1), arrow.now().shift(years=1)),
        (xdatetime.get().shift(months=1), arrow.now().shift(months=1)),
        (xdatetime.get().shift(days=1), arrow.now().shift(days=1)),
        (xdatetime.get().shift(hours=1), arrow.now().shift(hours=1)),
        (xdatetime.get().shift(minutes=1), arrow.now().shift(minutes=1)),
        (xdatetime.get().shift(seconds=1), arrow.now().shift(seconds=1)),
        (xdatetime.get().shift(microseconds=1),
         arrow.now().shift(microseconds=1)),
        (xdatetime.get().shift(weeks=1), arrow.now().shift(weeks=1)),
    ]

    # 时间推移
    @pytest.mark.parametrize("get_shift,arrow_shift", get_shift)
    def test_get_shift(self, get_shift, arrow_shift):
        assert get_shift.format() == arrow_shift.format("YYYY-MM-DD HH:mm:ss")

    get_replace = [
        (xdatetime.get().replace(year=2015), arrow.now().replace(year=2015)),
        (xdatetime.get().replace(month=1), arrow.now().replace(month=1)),
        (xdatetime.get().replace(day=1), arrow.now().replace(day=1)),
        (xdatetime.get().replace(hour=1), arrow.now().replace(hour=1)),
        (xdatetime.get().replace(minute=1), arrow.now().replace(minute=1)),
        (xdatetime.get().replace(second=1), arrow.now().replace(second=1)),
        (xdatetime.get().replace(microsecond=1),
         arrow.now().replace(microsecond=1)),
    ]

    # 时间替换
    @pytest.mark.parametrize("get_replace,arrow_replace", get_replace)
    def test_get_replace(self, get_replace, arrow_replace):
        assert get_replace.format() == arrow_replace.format(
            "YYYY-MM-DD HH:mm:ss")

    get_how = [("2020-04-28 10:52:52", "1988-07-20 17:31:12", 1002648100),
               ("1999-04-28 11:54:56", "2020-04-28 11:57:01", -662774525)]

    # 时间比较
    @pytest.mark.parametrize("start,end,hows", get_how)
    def test_get_compare(self, start, end, hows):
        assert xdatetime.get(start, end).how == hows

    get_last = [(2020, 4, "2020-04-30"), (2020, 12, "2020-12-31"),
                (2020, 2, "2020-02-29"), (2021, 2, "2021-02-28")]

    # 指定月最后一天
    @pytest.mark.parametrize("year,month,result", get_last)
    def test_get_last(self, year, month, result):
        assert xdatetime.get(year, month).last == result

    ger_middle = [
        ("2027-04-01", ["1988-04-14", "2020-05-14"], False),
        ("2027-04-01 12:15:4", ["1988-04-14", "2020-05-14"], False),
        ("1988-04-01 08:04:01", ["1988-04-01", "2020-05-14"], True),
        ("2020-04-15", ["2020-04-14", "2020-05-14 12:12:14"], True),
        ("1999-04-01", ["1988-04-14", "2020-05-14"], True),
        ("1989-04-01", ["1988-04-14 12:12:15", "2020-05-14"], True),
    ]

    # 判断时间是否在指定的时间区间内
    @pytest.mark.parametrize("reference,start,result", ger_middle)
    def test_get_middle(self, reference, start, result):
        assert xdatetime.get(reference, start).middle == result