blob: aa4748092a443d65dc5f7c384787eebad051ecc1 [file] [log] [blame]
Trent Houliston352149e2016-08-25 23:08:04 +10001
2
3def test_chrono_system_clock():
4 from pybind11_tests import test_chrono1
5 import datetime
6
7 # Get the time from both c++ and datetime
8 date1 = 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 # 500 microseconds is a very long time to execute this
22 assert diff.microseconds < 500
23
24
25def test_chrono_system_clock_roundtrip():
26 from pybind11_tests import test_chrono2
27 import datetime
28
29 date1 = datetime.datetime.today()
30
31 # Roundtrip the time
32 date2 = test_chrono2(date1)
33
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
44def test_chrono_duration_roundtrip():
45 from pybind11_tests import test_chrono3
46 import datetime
47
48 # Get the difference betwen two times (a timedelta)
49 date1 = datetime.datetime.today()
50 date2 = datetime.datetime.today()
51 diff = date2 - date1
52
53 # Make sure this is a timedelta
54 assert isinstance(diff, datetime.timedelta)
55
56 cpp_diff = test_chrono3(diff)
57
58 assert cpp_diff.days == diff.days
59 assert cpp_diff.seconds == diff.seconds
60 assert cpp_diff.microseconds == diff.microseconds
61
62
63def test_chrono_duration_subtraction_equivalence():
64 from pybind11_tests import test_chrono4
65 import datetime
66
67 date1 = datetime.datetime.today()
68 date2 = datetime.datetime.today()
69
70 diff = date2 - date1
71 cpp_diff = test_chrono4(date2, date1)
72
73 assert cpp_diff.days == diff.days
74 assert cpp_diff.seconds == diff.seconds
75 assert cpp_diff.microseconds == diff.microseconds
76
77
78def test_chrono_steady_clock():
79 from pybind11_tests import test_chrono5
80 import datetime
81
82 time1 = test_chrono5()
83 time2 = test_chrono5()
84
85 assert isinstance(time1, datetime.time)
86 assert isinstance(time2, datetime.time)
87
88
89def test_chrono_steady_clock_roundtrip():
90 from pybind11_tests import test_chrono6
91 import datetime
92
93 time1 = datetime.time(second=10, microsecond=100)
94 time2 = test_chrono6(time1)
95
96 assert isinstance(time2, datetime.time)
97
98 # They should be identical (no information lost on roundtrip)
99 assert time1.hour == time2.hour
100 assert time1.minute == time2.minute
101 assert time1.second == time2.second
102 assert time1.microsecond == time2.microsecond