cppyabm  1.0.17
An agent-based library to integrate C++ and Python
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(
84  "time1",
85  [
86  datetime.datetime.today().time(),
87  datetime.time(0, 0, 0),
88  datetime.time(0, 0, 0, 1),
89  datetime.time(0, 28, 45, 109827),
90  datetime.time(0, 59, 59, 999999),
91  datetime.time(1, 0, 0),
92  datetime.time(5, 59, 59, 0),
93  datetime.time(5, 59, 59, 1),
94  ],
95 )
96 @pytest.mark.parametrize(
97  "tz",
98  [
99  None,
100  pytest.param("Europe/Brussels", marks=SKIP_TZ_ENV_ON_WIN),
101  pytest.param("Asia/Pyongyang", marks=SKIP_TZ_ENV_ON_WIN),
102  pytest.param("America/New_York", marks=SKIP_TZ_ENV_ON_WIN),
103  ],
104 )
105 def test_chrono_system_clock_roundtrip_time(time1, tz, monkeypatch):
106  if tz is not None:
107  monkeypatch.setenv("TZ", "/usr/share/zoneinfo/{}".format(tz))
108 
109  # Roundtrip the time
110  datetime2 = m.test_chrono2(time1)
111  date2 = datetime2.date()
112  time2 = datetime2.time()
113 
114  # The returned value should be a datetime
115  assert isinstance(datetime2, datetime.datetime)
116  assert isinstance(date2, datetime.date)
117  assert isinstance(time2, datetime.time)
118 
119  # Hour, Minute, Second & Microsecond should be the same after the round trip
120  assert time1.hour == time2.hour
121  assert time1.minute == time2.minute
122  assert time1.second == time2.second
123  assert time1.microsecond == time2.microsecond
124 
125  # There should be no date information (i.e. date = python base date)
126  assert date2.year == 1970
127  assert date2.month == 1
128  assert date2.day == 1
129 
130 
132 
133  # Get the difference between two times (a timedelta)
134  date1 = datetime.datetime.today()
135  date2 = datetime.datetime.today()
136  diff = date2 - date1
137 
138  # Make sure this is a timedelta
139  assert isinstance(diff, datetime.timedelta)
140 
141  cpp_diff = m.test_chrono3(diff)
142 
143  assert cpp_diff.days == diff.days
144  assert cpp_diff.seconds == diff.seconds
145  assert cpp_diff.microseconds == diff.microseconds
146 
147 
149 
150  date1 = datetime.datetime.today()
151  date2 = datetime.datetime.today()
152 
153  diff = date2 - date1
154  cpp_diff = m.test_chrono4(date2, date1)
155 
156  assert cpp_diff.days == diff.days
157  assert cpp_diff.seconds == diff.seconds
158  assert cpp_diff.microseconds == diff.microseconds
159 
160 
162 
163  date1 = datetime.date.today()
164  date2 = datetime.date.today()
165 
166  diff = date2 - date1
167  cpp_diff = m.test_chrono4(date2, date1)
168 
169  assert cpp_diff.days == diff.days
170  assert cpp_diff.seconds == diff.seconds
171  assert cpp_diff.microseconds == diff.microseconds
172 
173 
175  time1 = m.test_chrono5()
176  assert isinstance(time1, datetime.timedelta)
177 
178 
180  time1 = datetime.timedelta(days=10, seconds=10, microseconds=100)
181  time2 = m.test_chrono6(time1)
182 
183  assert isinstance(time2, datetime.timedelta)
184 
185  # They should be identical (no information lost on roundtrip)
186  assert time1.days == time2.days
187  assert time1.seconds == time2.seconds
188  assert time1.microseconds == time2.microseconds
189 
190 
192  # Test using a floating point number in seconds
193  time = m.test_chrono7(35.525123)
194 
195  assert isinstance(time, datetime.timedelta)
196 
197  assert time.seconds == 35
198  assert 525122 <= time.microseconds <= 525123
199 
200  diff = m.test_chrono_float_diff(43.789012, 1.123456)
201  assert diff.seconds == 42
202  assert 665556 <= diff.microseconds <= 665557
203 
204 
206  time = datetime.datetime.now()
207  time1 = m.test_nano_timepoint(time, datetime.timedelta(seconds=60))
208  assert time1 == time + datetime.timedelta(seconds=60)
209 
210 
212  resolutions = m.different_resolutions()
213  time = datetime.datetime.now()
214  resolutions.timestamp_h = time
215  resolutions.timestamp_m = time
216  resolutions.timestamp_s = time
217  resolutions.timestamp_ms = time
218  resolutions.timestamp_us = time
test_chrono.test_nano_timepoint
def test_nano_timepoint()
Definition: test_chrono.py:205
test_chrono.test_chrono_steady_clock_roundtrip
def test_chrono_steady_clock_roundtrip()
Definition: test_chrono.py:179
test_chrono.test_chrono_duration_subtraction_equivalence_date
def test_chrono_duration_subtraction_equivalence_date()
Definition: test_chrono.py:161
test_chrono.test_chrono_system_clock_roundtrip
def test_chrono_system_clock_roundtrip()
Definition: test_chrono.py:31
test_chrono.test_floating_point_duration
def test_floating_point_duration()
Definition: test_chrono.py:191
test_chrono.test_chrono_different_resolutions
def test_chrono_different_resolutions()
Definition: test_chrono.py:211
test_chrono.test_chrono_steady_clock
def test_chrono_steady_clock()
Definition: test_chrono.py:174
isinstance
bool isinstance(handle obj)
Definition: pytypes.h:386
abs
std::string abs(const Vector2 &)
Definition: test_operator_overloading.cpp:79
test_chrono.test_chrono_system_clock_roundtrip_time
def test_chrono_system_clock_roundtrip_time(time1, tz, monkeypatch)
Definition: test_chrono.py:105
test_chrono.test_chrono_duration_subtraction_equivalence
def test_chrono_duration_subtraction_equivalence()
Definition: test_chrono.py:148
test_chrono.test_chrono_system_clock_roundtrip_date
def test_chrono_system_clock_roundtrip_date()
Definition: test_chrono.py:47
test_chrono.test_chrono_duration_roundtrip
def test_chrono_duration_roundtrip()
Definition: test_chrono.py:131
test_chrono.test_chrono_system_clock
def test_chrono_system_clock()
Definition: test_chrono.py:9