blob: 55094edbfa6647af32834a63b046f6d3ef0a2afc [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
Trent Houliston253e41c2016-09-28 00:59:21 +100021 # 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
Trent Houliston352149e2016-08-25 23:08:04 +100025
26
27def test_chrono_system_clock_roundtrip():
28 from pybind11_tests import test_chrono2
29 import datetime
30
31 date1 = datetime.datetime.today()
32
33 # Roundtrip the time
34 date2 = test_chrono2(date1)
35
36 # The returned value should be a datetime
37 assert isinstance(date2, datetime.datetime)
38
39 # They should be identical (no information lost on roundtrip)
40 diff = abs(date1 - date2)
41 assert diff.days == 0
42 assert diff.seconds == 0
43 assert diff.microseconds == 0
44
45
46def test_chrono_duration_roundtrip():
47 from pybind11_tests import test_chrono3
48 import datetime
49
Dean Moldovanbad17402016-11-20 21:21:54 +010050 # Get the difference between two times (a timedelta)
Trent Houliston352149e2016-08-25 23:08:04 +100051 date1 = datetime.datetime.today()
52 date2 = datetime.datetime.today()
53 diff = date2 - date1
54
55 # Make sure this is a timedelta
56 assert isinstance(diff, datetime.timedelta)
57
58 cpp_diff = test_chrono3(diff)
59
60 assert cpp_diff.days == diff.days
61 assert cpp_diff.seconds == diff.seconds
62 assert cpp_diff.microseconds == diff.microseconds
63
64
65def test_chrono_duration_subtraction_equivalence():
66 from pybind11_tests import test_chrono4
67 import datetime
68
69 date1 = datetime.datetime.today()
70 date2 = datetime.datetime.today()
71
72 diff = date2 - date1
73 cpp_diff = test_chrono4(date2, date1)
74
75 assert cpp_diff.days == diff.days
76 assert cpp_diff.seconds == diff.seconds
77 assert cpp_diff.microseconds == diff.microseconds
78
79
80def test_chrono_steady_clock():
81 from pybind11_tests import test_chrono5
82 import datetime
83
84 time1 = test_chrono5()
85 time2 = test_chrono5()
86
Trent Houliston2f597682016-09-13 20:40:28 +100087 assert isinstance(time1, datetime.timedelta)
88 assert isinstance(time2, datetime.timedelta)
Trent Houliston352149e2016-08-25 23:08:04 +100089
90
91def test_chrono_steady_clock_roundtrip():
92 from pybind11_tests import test_chrono6
93 import datetime
94
Trent Houliston2f597682016-09-13 20:40:28 +100095 time1 = datetime.timedelta(days=10, seconds=10, microseconds=100)
Trent Houliston352149e2016-08-25 23:08:04 +100096 time2 = test_chrono6(time1)
97
Trent Houliston2f597682016-09-13 20:40:28 +100098 assert isinstance(time2, datetime.timedelta)
Trent Houliston352149e2016-08-25 23:08:04 +100099
100 # They should be identical (no information lost on roundtrip)
Trent Houliston2f597682016-09-13 20:40:28 +1000101 assert time1.days == time2.days
102 assert time1.seconds == time2.seconds
103 assert time1.microseconds == time2.microseconds
104
105
106def test_floating_point_duration():
Jason Rhinelandere5456c22017-03-11 22:29:25 -0400107 from pybind11_tests import test_chrono7, test_chrono_float_diff
Trent Houliston2f597682016-09-13 20:40:28 +1000108 import datetime
109
110 # Test using 35.525123 seconds as an example floating point number in seconds
111 time = test_chrono7(35.525123)
112
113 assert isinstance(time, datetime.timedelta)
114
115 assert time.seconds == 35
Trent Houliston253e41c2016-09-28 00:59:21 +1000116 assert 525122 <= time.microseconds <= 525123
Jason Rhinelandere5456c22017-03-11 22:29:25 -0400117
118 diff = test_chrono_float_diff(43.789012, 1.123456)
119 assert diff.seconds == 42
120 assert 665556 <= diff.microseconds <= 665557