2 # pylint: disable=comparison-with-itself,unneeded-not
4 from datetime
import datetime
, timedelta
, timezone
10 assert rarfile
.load_vint(b
"\x00", 0) == (0, 1)
11 assert rarfile
.load_vint(b
"\x80\x01", 0) == (1 << 7, 2)
12 assert rarfile
.load_vint(b
"\x80\x80\x01", 0) == (1 << 14, 3)
13 assert rarfile
.load_vint(b
"\x80\x80\x80\x01", 0) == (1 << 21, 4)
14 assert rarfile
.load_vint(b
"\x80\x80\x80\x80\x01", 0) == (1 << 28, 5)
15 assert rarfile
.load_vint(b
"\x80\x80\x80\x80\x80\x01", 0) == (1 << 35, 6)
16 assert rarfile
.load_vint(b
"\x80" * 10 + b
"\x01", 0) == (1 << 70, 11)
19 def test_to_datetime():
20 assert rarfile
.to_datetime((2020, 0, 0, 0, 0, 0)) == datetime(2020, 1, 1, 0, 0, 0)
21 assert rarfile
.to_datetime((2020, 60, 60, 60, 60, 60)) == datetime(2020, 12, 31, 23, 59, 59)
22 assert rarfile
.to_datetime((2020, 2, 30, 60, 60, 60)) == datetime(2020, 2, 28, 23, 59, 59)
23 assert rarfile
.to_datetime((2021, 2, 30, 60, 60, 60)) == datetime(2021, 2, 28, 23, 59, 59)
26 def test_to_nsdatetime():
27 base
= datetime(2020, 1, 1, 0, 0, 0, tzinfo
=timezone
.utc
)
28 assert rarfile
.to_nsdatetime(base
, 0) is base
30 res
= rarfile
.to_nsdatetime(base
, 1000)
31 assert res
== base
.replace(microsecond
=1)
32 assert isinstance(res
, datetime
)
33 assert res
.isoformat(" ") == "2020-01-01 00:00:00.000001+00:00"
35 res
= rarfile
.to_nsdatetime(base
, 1001)
36 assert isinstance(res
, datetime
)
37 assert isinstance(res
, rarfile
.nsdatetime
)
38 assert res
.microsecond
== 1
39 assert res
.nanosecond
== 1001
40 assert res
.isoformat(" ") == "2020-01-01 00:00:00.000001001+00:00"
41 assert res
.isoformat(" ", "auto") == "2020-01-01 00:00:00.000001001+00:00"
42 assert res
.isoformat(" ", "microseconds") == "2020-01-01 00:00:00.000001+00:00"
45 def test_nsdatetime_cmp():
46 nsdatetime
= rarfile
.nsdatetime
48 n1
= nsdatetime(2000, 1, 1, 9, 15, 30, nanosecond
=100200300, tzinfo
=timezone
.utc
)
49 n2
= nsdatetime(2000, 1, 1, 9, 15, 30, nanosecond
=100200301, tzinfo
=timezone
.utc
)
50 n3
= nsdatetime(2000, 1, 1, 9, 15, 30, nanosecond
=100200402, tzinfo
=timezone
.utc
)
52 d1
= datetime(2000, 1, 1, 9, 15, 30, 100100, timezone
.utc
)
53 d2
= datetime(2000, 1, 1, 9, 15, 30, 100200, timezone
.utc
)
54 d3
= datetime(2000, 1, 1, 9, 15, 30, 100300, timezone
.utc
)
56 n2x
= n2
+ timedelta(seconds
=0)
57 assert not isinstance(n2x
, nsdatetime
)
58 assert not hasattr(n2x
, "_nanoseconds")
60 assert hash(n2x
) == hash(d2
)
61 assert hash(n2
) != hash(d2
)
63 # compare nsdatetime only
94 def test_nsdatetime_astimezone():
95 nsdatetime
= rarfile
.nsdatetime
96 X1
= timezone(timedelta(hours
=1), "X1")
98 n1
= nsdatetime(2000, 1, 1, 9, 15, 30, nanosecond
=100200402, tzinfo
=timezone
.utc
)
99 n2
= n1
.astimezone(X1
)
100 assert n2
.nanosecond
== n1
.nanosecond
101 assert (n1
.year
, n1
.month
, n1
.day
) == (n2
.year
, n2
.month
, n2
.day
)
102 assert (n1
.hour
, n1
.minute
, n1
.second
) == (n2
.hour
- 1, n2
.minute
, n2
.second
)