test_chrono.py revision 11986:c12e4625ab56
1
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    # 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    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
50    # Get the difference between two times (a timedelta)
51    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
87    assert isinstance(time1, datetime.timedelta)
88    assert isinstance(time2, datetime.timedelta)
89
90
91def test_chrono_steady_clock_roundtrip():
92    from pybind11_tests import test_chrono6
93    import datetime
94
95    time1 = datetime.timedelta(days=10, seconds=10, microseconds=100)
96    time2 = test_chrono6(time1)
97
98    assert isinstance(time2, datetime.timedelta)
99
100    # They should be identical (no information lost on roundtrip)
101    assert time1.days == time2.days
102    assert time1.seconds == time2.seconds
103    assert time1.microseconds == time2.microseconds
104
105
106def test_floating_point_duration():
107    from pybind11_tests import test_chrono7
108    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
116    assert 525122 <= time.microseconds <= 525123
117