1from pybind11_tests import chrono as m
2import datetime
3
4
5def test_chrono_system_clock():
6
7    # Get the time from both c++ and datetime
8    date1 = m.test_chrono1()
9    date2 = datetime.datetime.today()
10
11    # The returned value should be a datetime
12    assert isinstance(date1, datetime.datetime)
13
14    # The numbers should vary by a very small amount (time it took to execute)
15    diff = abs(date1 - date2)
16
17    # There should never be a days/seconds difference
18    assert diff.days == 0
19    assert diff.seconds == 0
20
21    # We test that no more than about 0.5 seconds passes here
22    # This makes sure that the dates created are very close to the same
23    # but if the testing system is incredibly overloaded this should still pass
24    assert diff.microseconds < 500000
25
26
27def test_chrono_system_clock_roundtrip():
28    date1 = datetime.datetime.today()
29
30    # Roundtrip the time
31    date2 = m.test_chrono2(date1)
32
33    # The returned value should be a datetime
34    assert isinstance(date2, datetime.datetime)
35
36    # They should be identical (no information lost on roundtrip)
37    diff = abs(date1 - date2)
38    assert diff.days == 0
39    assert diff.seconds == 0
40    assert diff.microseconds == 0
41
42
43def test_chrono_system_clock_roundtrip_date():
44    date1 = datetime.date.today()
45
46    # Roundtrip the time
47    datetime2 = m.test_chrono2(date1)
48    date2 = datetime2.date()
49    time2 = datetime2.time()
50
51    # The returned value should be a datetime
52    assert isinstance(datetime2, datetime.datetime)
53    assert isinstance(date2, datetime.date)
54    assert isinstance(time2, datetime.time)
55
56    # They should be identical (no information lost on roundtrip)
57    diff = abs(date1 - date2)
58    assert diff.days == 0
59    assert diff.seconds == 0
60    assert diff.microseconds == 0
61
62    # Year, Month & Day should be the same after the round trip
63    assert date1.year == date2.year
64    assert date1.month == date2.month
65    assert date1.day == date2.day
66
67    # There should be no time information
68    assert time2.hour == 0
69    assert time2.minute == 0
70    assert time2.second == 0
71    assert time2.microsecond == 0
72
73
74def test_chrono_system_clock_roundtrip_time():
75    time1 = datetime.datetime.today().time()
76
77    # Roundtrip the time
78    datetime2 = m.test_chrono2(time1)
79    date2 = datetime2.date()
80    time2 = datetime2.time()
81
82    # The returned value should be a datetime
83    assert isinstance(datetime2, datetime.datetime)
84    assert isinstance(date2, datetime.date)
85    assert isinstance(time2, datetime.time)
86
87    # Hour, Minute, Second & Microsecond should be the same after the round trip
88    assert time1.hour == time2.hour
89    assert time1.minute == time2.minute
90    assert time1.second == time2.second
91    assert time1.microsecond == time2.microsecond
92
93    # There should be no date information (i.e. date = python base date)
94    assert date2.year == 1970
95    assert date2.month == 1
96    assert date2.day == 1
97
98
99def test_chrono_duration_roundtrip():
100
101    # Get the difference between two times (a timedelta)
102    date1 = datetime.datetime.today()
103    date2 = datetime.datetime.today()
104    diff = date2 - date1
105
106    # Make sure this is a timedelta
107    assert isinstance(diff, datetime.timedelta)
108
109    cpp_diff = m.test_chrono3(diff)
110
111    assert cpp_diff.days == diff.days
112    assert cpp_diff.seconds == diff.seconds
113    assert cpp_diff.microseconds == diff.microseconds
114
115
116def test_chrono_duration_subtraction_equivalence():
117
118    date1 = datetime.datetime.today()
119    date2 = datetime.datetime.today()
120
121    diff = date2 - date1
122    cpp_diff = m.test_chrono4(date2, date1)
123
124    assert cpp_diff.days == diff.days
125    assert cpp_diff.seconds == diff.seconds
126    assert cpp_diff.microseconds == diff.microseconds
127
128
129def test_chrono_duration_subtraction_equivalence_date():
130
131    date1 = datetime.date.today()
132    date2 = datetime.date.today()
133
134    diff = date2 - date1
135    cpp_diff = m.test_chrono4(date2, date1)
136
137    assert cpp_diff.days == diff.days
138    assert cpp_diff.seconds == diff.seconds
139    assert cpp_diff.microseconds == diff.microseconds
140
141
142def test_chrono_steady_clock():
143    time1 = m.test_chrono5()
144    assert isinstance(time1, datetime.timedelta)
145
146
147def test_chrono_steady_clock_roundtrip():
148    time1 = datetime.timedelta(days=10, seconds=10, microseconds=100)
149    time2 = m.test_chrono6(time1)
150
151    assert isinstance(time2, datetime.timedelta)
152
153    # They should be identical (no information lost on roundtrip)
154    assert time1.days == time2.days
155    assert time1.seconds == time2.seconds
156    assert time1.microseconds == time2.microseconds
157
158
159def test_floating_point_duration():
160    # Test using a floating point number in seconds
161    time = m.test_chrono7(35.525123)
162
163    assert isinstance(time, datetime.timedelta)
164
165    assert time.seconds == 35
166    assert 525122 <= time.microseconds <= 525123
167
168    diff = m.test_chrono_float_diff(43.789012, 1.123456)
169    assert diff.seconds == 42
170    assert 665556 <= diff.microseconds <= 665557
171
172
173def test_nano_timepoint():
174    time = datetime.datetime.now()
175    time1 = m.test_nano_timepoint(time, datetime.timedelta(seconds=60))
176    assert(time1 == time + datetime.timedelta(seconds=60))
177