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


gtsam
Author(s):
autogenerated on Thu Jul 4 2024 03:06:00