test_chrono.py
Go to the documentation of this file.
1 # -*- coding: utf-8 -*-
2 from pybind11_tests import chrono as m
3 import datetime
4 import pytest
5 
6 import env # noqa: F401
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.days == 0
43  assert diff.seconds == 0
44  assert diff.microseconds == 0
45 
46 
48  date1 = datetime.date.today()
49 
50  # Roundtrip the time
51  datetime2 = m.test_chrono2(date1)
52  date2 = datetime2.date()
53  time2 = datetime2.time()
54 
55  # The returned value should be a datetime
56  assert isinstance(datetime2, datetime.datetime)
57  assert isinstance(date2, datetime.date)
58  assert isinstance(time2, datetime.time)
59 
60  # They should be identical (no information lost on roundtrip)
61  diff = abs(date1 - date2)
62  assert diff.days == 0
63  assert diff.seconds == 0
64  assert diff.microseconds == 0
65 
66  # Year, Month & Day should be the same after the round trip
67  assert date1.year == date2.year
68  assert date1.month == date2.month
69  assert date1.day == date2.day
70 
71  # There should be no time information
72  assert time2.hour == 0
73  assert time2.minute == 0
74  assert time2.second == 0
75  assert time2.microsecond == 0
76 
77 
78 SKIP_TZ_ENV_ON_WIN = pytest.mark.skipif(
79  "env.WIN", reason="TZ environment variable only supported on POSIX"
80 )
81 
82 
83 @pytest.mark.parametrize("time1", [
84  datetime.datetime.today().time(),
85  datetime.time(0, 0, 0),
86  datetime.time(0, 0, 0, 1),
87  datetime.time(0, 28, 45, 109827),
88  datetime.time(0, 59, 59, 999999),
89  datetime.time(1, 0, 0),
90  datetime.time(5, 59, 59, 0),
91  datetime.time(5, 59, 59, 1),
92 ])
93 @pytest.mark.parametrize("tz", [
94  None,
95  pytest.param("Europe/Brussels", marks=SKIP_TZ_ENV_ON_WIN),
96  pytest.param("Asia/Pyongyang", marks=SKIP_TZ_ENV_ON_WIN),
97  pytest.param("America/New_York", marks=SKIP_TZ_ENV_ON_WIN),
98 ])
99 def test_chrono_system_clock_roundtrip_time(time1, tz, monkeypatch):
100  if tz is not None:
101  monkeypatch.setenv("TZ", "/usr/share/zoneinfo/{}".format(tz))
102 
103  # Roundtrip the time
104  datetime2 = m.test_chrono2(time1)
105  date2 = datetime2.date()
106  time2 = datetime2.time()
107 
108  # The returned value should be a datetime
109  assert isinstance(datetime2, datetime.datetime)
110  assert isinstance(date2, datetime.date)
111  assert isinstance(time2, datetime.time)
112 
113  # Hour, Minute, Second & Microsecond should be the same after the round trip
114  assert time1.hour == time2.hour
115  assert time1.minute == time2.minute
116  assert time1.second == time2.second
117  assert time1.microsecond == time2.microsecond
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 
127  # Get the difference between two times (a timedelta)
128  date1 = datetime.datetime.today()
129  date2 = datetime.datetime.today()
130  diff = date2 - date1
131 
132  # Make sure this is a timedelta
133  assert isinstance(diff, datetime.timedelta)
134 
135  cpp_diff = m.test_chrono3(diff)
136 
137  assert cpp_diff.days == diff.days
138  assert cpp_diff.seconds == diff.seconds
139  assert cpp_diff.microseconds == diff.microseconds
140 
141 
143 
144  date1 = datetime.datetime.today()
145  date2 = datetime.datetime.today()
146 
147  diff = date2 - date1
148  cpp_diff = m.test_chrono4(date2, date1)
149 
150  assert cpp_diff.days == diff.days
151  assert cpp_diff.seconds == diff.seconds
152  assert cpp_diff.microseconds == diff.microseconds
153 
154 
156 
157  date1 = datetime.date.today()
158  date2 = datetime.date.today()
159 
160  diff = date2 - date1
161  cpp_diff = m.test_chrono4(date2, date1)
162 
163  assert cpp_diff.days == diff.days
164  assert cpp_diff.seconds == diff.seconds
165  assert cpp_diff.microseconds == diff.microseconds
166 
167 
169  time1 = m.test_chrono5()
170  assert isinstance(time1, datetime.timedelta)
171 
172 
174  time1 = datetime.timedelta(days=10, seconds=10, microseconds=100)
175  time2 = m.test_chrono6(time1)
176 
177  assert isinstance(time2, datetime.timedelta)
178 
179  # They should be identical (no information lost on roundtrip)
180  assert time1.days == time2.days
181  assert time1.seconds == time2.seconds
182  assert time1.microseconds == time2.microseconds
183 
184 
186  # Test using a floating point number in seconds
187  time = m.test_chrono7(35.525123)
188 
189  assert isinstance(time, datetime.timedelta)
190 
191  assert time.seconds == 35
192  assert 525122 <= time.microseconds <= 525123
193 
194  diff = m.test_chrono_float_diff(43.789012, 1.123456)
195  assert diff.seconds == 42
196  assert 665556 <= diff.microseconds <= 665557
197 
198 
200  time = datetime.datetime.now()
201  time1 = m.test_nano_timepoint(time, datetime.timedelta(seconds=60))
202  assert(time1 == time + datetime.timedelta(seconds=60))
203 
204 
206  resolutions = m.different_resolutions()
207  time = datetime.datetime.now()
208  resolutions.timestamp_h = time
209  resolutions.timestamp_m = time
210  resolutions.timestamp_s = time
211  resolutions.timestamp_ms = time
212  resolutions.timestamp_us = time
def test_chrono_system_clock_roundtrip_date()
Definition: test_chrono.py:47
def test_chrono_duration_subtraction_equivalence()
Definition: test_chrono.py:142
bool isinstance(handle obj)
Definition: pytypes.h:384
def test_chrono_duration_roundtrip()
Definition: test_chrono.py:125
def test_chrono_system_clock()
Definition: test_chrono.py:9
def test_chrono_steady_clock()
Definition: test_chrono.py:168
#define time
def test_nano_timepoint()
Definition: test_chrono.py:199
def test_chrono_different_resolutions()
Definition: test_chrono.py:205
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:99
def test_chrono_duration_subtraction_equivalence_date()
Definition: test_chrono.py:155
#define abs(x)
Definition: datatypes.h:17
def test_chrono_steady_clock_roundtrip()
Definition: test_chrono.py:173
def test_floating_point_duration()
Definition: test_chrono.py:185


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:46:03