test_chrono.py
Go to the documentation of this file.
1 import datetime
2 
3 import pytest
4 
5 import env # noqa: F401
6 from pybind11_tests import chrono as m
7 
8 
10 
11  # Get the time from both c++ and datetime
12  date0 = datetime.datetime.today()
13  date1 = m.test_chrono1()
14  date2 = datetime.datetime.today()
15 
16  # The returned value should be a datetime
17  assert isinstance(date1, datetime.datetime)
18 
19  # The numbers should vary by a very small amount (time it took to execute)
20  diff_python = abs(date2 - date0)
21  diff = abs(date1 - date2)
22 
23  # There should never be a days difference
24  assert diff.days == 0
25 
26  # Since datetime.datetime.today() calls time.time(), and on some platforms
27  # that has 1 second accuracy, we compare this way
28  assert diff.seconds <= diff_python.seconds
29 
30 
32  date1 = datetime.datetime.today()
33 
34  # Roundtrip the time
35  date2 = m.test_chrono2(date1)
36 
37  # The returned value should be a datetime
38  assert isinstance(date2, datetime.datetime)
39 
40  # They should be identical (no information lost on roundtrip)
41  diff = abs(date1 - date2)
42  assert diff == datetime.timedelta(0)
43 
44 
46  date1 = datetime.date.today()
47 
48  # Roundtrip the time
49  datetime2 = m.test_chrono2(date1)
50  date2 = datetime2.date()
51  time2 = datetime2.time()
52 
53  # The returned value should be a datetime
54  assert isinstance(datetime2, datetime.datetime)
55  assert isinstance(date2, datetime.date)
56  assert isinstance(time2, datetime.time)
57 
58  # They should be identical (no information lost on roundtrip)
59  diff = abs(date1 - date2)
60  assert diff.days == 0
61  assert diff.seconds == 0
62  assert diff.microseconds == 0
63 
64  # Year, Month & Day should be the same after the round trip
65  assert date1 == date2
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 
74 SKIP_TZ_ENV_ON_WIN = pytest.mark.skipif(
75  "env.WIN", reason="TZ environment variable only supported on POSIX"
76 )
77 
78 
79 @pytest.mark.parametrize(
80  "time1",
81  [
82  datetime.datetime.today().time(),
83  datetime.time(0, 0, 0),
84  datetime.time(0, 0, 0, 1),
85  datetime.time(0, 28, 45, 109827),
86  datetime.time(0, 59, 59, 999999),
87  datetime.time(1, 0, 0),
88  datetime.time(5, 59, 59, 0),
89  datetime.time(5, 59, 59, 1),
90  ],
91 )
92 @pytest.mark.parametrize(
93  "tz",
94  [
95  None,
96  pytest.param("Europe/Brussels", marks=SKIP_TZ_ENV_ON_WIN),
97  pytest.param("Asia/Pyongyang", marks=SKIP_TZ_ENV_ON_WIN),
98  pytest.param("America/New_York", marks=SKIP_TZ_ENV_ON_WIN),
99  ],
100 )
101 def test_chrono_system_clock_roundtrip_time(time1, tz, monkeypatch):
102  if tz is not None:
103  monkeypatch.setenv("TZ", f"/usr/share/zoneinfo/{tz}")
104 
105  # Roundtrip the time
106  datetime2 = m.test_chrono2(time1)
107  date2 = datetime2.date()
108  time2 = datetime2.time()
109 
110  # The returned value should be a datetime
111  assert isinstance(datetime2, datetime.datetime)
112  assert isinstance(date2, datetime.date)
113  assert isinstance(time2, datetime.time)
114 
115  # Hour, Minute, Second & Microsecond should be the same after the round trip
116  assert time1 == time2
117 
118  # There should be no date information (i.e. date = python base date)
119  assert date2.year == 1970
120  assert date2.month == 1
121  assert date2.day == 1
122 
123 
125 
126  # Get the difference between two times (a timedelta)
127  date1 = datetime.datetime.today()
128  date2 = datetime.datetime.today()
129  diff = date2 - date1
130 
131  # Make sure this is a timedelta
132  assert isinstance(diff, datetime.timedelta)
133 
134  cpp_diff = m.test_chrono3(diff)
135 
136  assert cpp_diff == diff
137 
138  # Negative timedelta roundtrip
139  diff = datetime.timedelta(microseconds=-1)
140  cpp_diff = m.test_chrono3(diff)
141 
142  assert cpp_diff == diff
143 
144 
146 
147  date1 = datetime.datetime.today()
148  date2 = datetime.datetime.today()
149 
150  diff = date2 - date1
151  cpp_diff = m.test_chrono4(date2, date1)
152 
153  assert cpp_diff == diff
154 
155 
157 
158  date1 = datetime.date.today()
159  date2 = datetime.date.today()
160 
161  diff = date2 - date1
162  cpp_diff = m.test_chrono4(date2, date1)
163 
164  assert cpp_diff == diff
165 
166 
168  time1 = m.test_chrono5()
169  assert isinstance(time1, datetime.timedelta)
170 
171 
173  time1 = datetime.timedelta(days=10, seconds=10, microseconds=100)
174  time2 = m.test_chrono6(time1)
175 
176  assert isinstance(time2, datetime.timedelta)
177 
178  # They should be identical (no information lost on roundtrip)
179  assert time1 == time2
180 
181 
183  # Test using a floating point number in seconds
184  time = m.test_chrono7(35.525123)
185 
186  assert isinstance(time, datetime.timedelta)
187 
188  assert time.seconds == 35
189  assert 525122 <= time.microseconds <= 525123
190 
191  diff = m.test_chrono_float_diff(43.789012, 1.123456)
192  assert diff.seconds == 42
193  assert 665556 <= diff.microseconds <= 665557
194 
195 
197  time = datetime.datetime.now()
198  time1 = m.test_nano_timepoint(time, datetime.timedelta(seconds=60))
199  assert time1 == time + datetime.timedelta(seconds=60)
200 
201 
203  resolutions = m.different_resolutions()
204  time = datetime.datetime.now()
205  resolutions.timestamp_h = time
206  resolutions.timestamp_m = time
207  resolutions.timestamp_s = time
208  resolutions.timestamp_ms = time
209  resolutions.timestamp_us = time
def test_chrono_system_clock_roundtrip_date()
Definition: test_chrono.py:45
def test_chrono_duration_subtraction_equivalence()
Definition: test_chrono.py:145
bool isinstance(handle obj)
Definition: pytypes.h:700
def test_chrono_duration_roundtrip()
Definition: test_chrono.py:124
def test_chrono_system_clock()
Definition: test_chrono.py:9
def test_chrono_steady_clock()
Definition: test_chrono.py:167
#define time
def test_nano_timepoint()
Definition: test_chrono.py:196
def test_chrono_different_resolutions()
Definition: test_chrono.py:202
def test_chrono_system_clock_roundtrip()
Definition: test_chrono.py:31
def test_chrono_system_clock_roundtrip_time(time1, tz, monkeypatch)
Definition: test_chrono.py:101
def test_chrono_duration_subtraction_equivalence_date()
Definition: test_chrono.py:156
#define abs(x)
Definition: datatypes.h:17
def test_chrono_steady_clock_roundtrip()
Definition: test_chrono.py:172
def test_floating_point_duration()
Definition: test_chrono.py:182


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:37:45