blob: f1817e44f66c3ef306ef83c74e8bcec9bfe9c323 [file] [log] [blame]
Henry Schreinerd8c7ee02020-07-20 13:35:21 -04001# -*- coding: utf-8 -*-
Jason Rhinelander391c7542017-07-25 16:47:36 -04002from pybind11_tests import chrono as m
3import datetime
Trent Houliston352149e2016-08-25 23:08:04 +10004
5
6def test_chrono_system_clock():
Trent Houliston352149e2016-08-25 23:08:04 +10007
8 # Get the time from both c++ and datetime
Jason Rhinelander391c7542017-07-25 16:47:36 -04009 date1 = m.test_chrono1()
Trent Houliston352149e2016-08-25 23:08:04 +100010 date2 = datetime.datetime.today()
11
12 # The returned value should be a datetime
13 assert isinstance(date1, datetime.datetime)
14
15 # The numbers should vary by a very small amount (time it took to execute)
16 diff = abs(date1 - date2)
17
18 # There should never be a days/seconds difference
19 assert diff.days == 0
20 assert diff.seconds == 0
21
Trent Houliston253e41c2016-09-28 00:59:21 +100022 # We test that no more than about 0.5 seconds passes here
23 # This makes sure that the dates created are very close to the same
24 # but if the testing system is incredibly overloaded this should still pass
25 assert diff.microseconds < 500000
Trent Houliston352149e2016-08-25 23:08:04 +100026
27
28def test_chrono_system_clock_roundtrip():
Trent Houliston352149e2016-08-25 23:08:04 +100029 date1 = datetime.datetime.today()
30
31 # Roundtrip the time
Jason Rhinelander391c7542017-07-25 16:47:36 -040032 date2 = m.test_chrono2(date1)
Trent Houliston352149e2016-08-25 23:08:04 +100033
34 # The returned value should be a datetime
35 assert isinstance(date2, datetime.datetime)
36
37 # They should be identical (no information lost on roundtrip)
38 diff = abs(date1 - date2)
39 assert diff.days == 0
40 assert diff.seconds == 0
41 assert diff.microseconds == 0
42
43
phil-zxxc6b699d2019-07-19 10:28:48 +010044def test_chrono_system_clock_roundtrip_date():
45 date1 = datetime.date.today()
46
47 # Roundtrip the time
48 datetime2 = m.test_chrono2(date1)
49 date2 = datetime2.date()
50 time2 = datetime2.time()
51
52 # The returned value should be a datetime
53 assert isinstance(datetime2, datetime.datetime)
54 assert isinstance(date2, datetime.date)
55 assert isinstance(time2, datetime.time)
56
57 # They should be identical (no information lost on roundtrip)
58 diff = abs(date1 - date2)
59 assert diff.days == 0
60 assert diff.seconds == 0
61 assert diff.microseconds == 0
62
63 # Year, Month & Day should be the same after the round trip
64 assert date1.year == date2.year
65 assert date1.month == date2.month
66 assert date1.day == date2.day
67
68 # There should be no time information
69 assert time2.hour == 0
70 assert time2.minute == 0
71 assert time2.second == 0
72 assert time2.microsecond == 0
73
74
75def test_chrono_system_clock_roundtrip_time():
76 time1 = datetime.datetime.today().time()
77
78 # Roundtrip the time
79 datetime2 = m.test_chrono2(time1)
80 date2 = datetime2.date()
81 time2 = datetime2.time()
82
83 # The returned value should be a datetime
84 assert isinstance(datetime2, datetime.datetime)
85 assert isinstance(date2, datetime.date)
86 assert isinstance(time2, datetime.time)
87
88 # Hour, Minute, Second & Microsecond should be the same after the round trip
89 assert time1.hour == time2.hour
90 assert time1.minute == time2.minute
91 assert time1.second == time2.second
92 assert time1.microsecond == time2.microsecond
93
94 # There should be no date information (i.e. date = python base date)
95 assert date2.year == 1970
96 assert date2.month == 1
97 assert date2.day == 1
98
99
Trent Houliston352149e2016-08-25 23:08:04 +1000100def test_chrono_duration_roundtrip():
Trent Houliston352149e2016-08-25 23:08:04 +1000101
Dean Moldovanbad17402016-11-20 21:21:54 +0100102 # Get the difference between two times (a timedelta)
Trent Houliston352149e2016-08-25 23:08:04 +1000103 date1 = datetime.datetime.today()
104 date2 = datetime.datetime.today()
105 diff = date2 - date1
106
107 # Make sure this is a timedelta
108 assert isinstance(diff, datetime.timedelta)
109
Jason Rhinelander391c7542017-07-25 16:47:36 -0400110 cpp_diff = m.test_chrono3(diff)
Trent Houliston352149e2016-08-25 23:08:04 +1000111
112 assert cpp_diff.days == diff.days
113 assert cpp_diff.seconds == diff.seconds
114 assert cpp_diff.microseconds == diff.microseconds
115
116
117def test_chrono_duration_subtraction_equivalence():
Trent Houliston352149e2016-08-25 23:08:04 +1000118
119 date1 = datetime.datetime.today()
120 date2 = datetime.datetime.today()
121
122 diff = date2 - date1
Jason Rhinelander391c7542017-07-25 16:47:36 -0400123 cpp_diff = m.test_chrono4(date2, date1)
Trent Houliston352149e2016-08-25 23:08:04 +1000124
125 assert cpp_diff.days == diff.days
126 assert cpp_diff.seconds == diff.seconds
127 assert cpp_diff.microseconds == diff.microseconds
128
129
phil-zxxc6b699d2019-07-19 10:28:48 +0100130def test_chrono_duration_subtraction_equivalence_date():
131
132 date1 = datetime.date.today()
133 date2 = datetime.date.today()
134
135 diff = date2 - date1
136 cpp_diff = m.test_chrono4(date2, date1)
137
138 assert cpp_diff.days == diff.days
139 assert cpp_diff.seconds == diff.seconds
140 assert cpp_diff.microseconds == diff.microseconds
141
142
Trent Houliston352149e2016-08-25 23:08:04 +1000143def test_chrono_steady_clock():
Jason Rhinelander391c7542017-07-25 16:47:36 -0400144 time1 = m.test_chrono5()
Trent Houliston2f597682016-09-13 20:40:28 +1000145 assert isinstance(time1, datetime.timedelta)
Trent Houliston352149e2016-08-25 23:08:04 +1000146
147
148def test_chrono_steady_clock_roundtrip():
Trent Houliston2f597682016-09-13 20:40:28 +1000149 time1 = datetime.timedelta(days=10, seconds=10, microseconds=100)
Jason Rhinelander391c7542017-07-25 16:47:36 -0400150 time2 = m.test_chrono6(time1)
Trent Houliston352149e2016-08-25 23:08:04 +1000151
Trent Houliston2f597682016-09-13 20:40:28 +1000152 assert isinstance(time2, datetime.timedelta)
Trent Houliston352149e2016-08-25 23:08:04 +1000153
154 # They should be identical (no information lost on roundtrip)
Trent Houliston2f597682016-09-13 20:40:28 +1000155 assert time1.days == time2.days
156 assert time1.seconds == time2.seconds
157 assert time1.microseconds == time2.microseconds
158
159
160def test_floating_point_duration():
Jason Rhinelander391c7542017-07-25 16:47:36 -0400161 # Test using a floating point number in seconds
162 time = m.test_chrono7(35.525123)
Trent Houliston2f597682016-09-13 20:40:28 +1000163
164 assert isinstance(time, datetime.timedelta)
165
166 assert time.seconds == 35
Trent Houliston253e41c2016-09-28 00:59:21 +1000167 assert 525122 <= time.microseconds <= 525123
Jason Rhinelandere5456c22017-03-11 22:29:25 -0400168
Jason Rhinelander391c7542017-07-25 16:47:36 -0400169 diff = m.test_chrono_float_diff(43.789012, 1.123456)
Jason Rhinelandere5456c22017-03-11 22:29:25 -0400170 assert diff.seconds == 42
171 assert 665556 <= diff.microseconds <= 665557
Alexander Gagarinb3bf2482019-06-13 12:17:10 +0500172
173
174def test_nano_timepoint():
175 time = datetime.datetime.now()
176 time1 = m.test_nano_timepoint(time, datetime.timedelta(seconds=60))
177 assert(time1 == time + datetime.timedelta(seconds=60))