Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:17:52

0001 import datetime
0002 
0003 import pytest
0004 
0005 import env  # noqa: F401
0006 from pybind11_tests import chrono as m
0007 
0008 
0009 def test_chrono_system_clock():
0010 
0011     # Get the time from both c++ and datetime
0012     date0 = datetime.datetime.today()
0013     date1 = m.test_chrono1()
0014     date2 = datetime.datetime.today()
0015 
0016     # The returned value should be a datetime
0017     assert isinstance(date1, datetime.datetime)
0018 
0019     # The numbers should vary by a very small amount (time it took to execute)
0020     diff_python = abs(date2 - date0)
0021     diff = abs(date1 - date2)
0022 
0023     # There should never be a days difference
0024     assert diff.days == 0
0025 
0026     # Since datetime.datetime.today() calls time.time(), and on some platforms
0027     # that has 1 second accuracy, we compare this way
0028     assert diff.seconds <= diff_python.seconds
0029 
0030 
0031 def test_chrono_system_clock_roundtrip():
0032     date1 = datetime.datetime.today()
0033 
0034     # Roundtrip the time
0035     date2 = m.test_chrono2(date1)
0036 
0037     # The returned value should be a datetime
0038     assert isinstance(date2, datetime.datetime)
0039 
0040     # They should be identical (no information lost on roundtrip)
0041     diff = abs(date1 - date2)
0042     assert diff == datetime.timedelta(0)
0043 
0044 
0045 def test_chrono_system_clock_roundtrip_date():
0046     date1 = datetime.date.today()
0047 
0048     # Roundtrip the time
0049     datetime2 = m.test_chrono2(date1)
0050     date2 = datetime2.date()
0051     time2 = datetime2.time()
0052 
0053     # The returned value should be a datetime
0054     assert isinstance(datetime2, datetime.datetime)
0055     assert isinstance(date2, datetime.date)
0056     assert isinstance(time2, datetime.time)
0057 
0058     # They should be identical (no information lost on roundtrip)
0059     diff = abs(date1 - date2)
0060     assert diff.days == 0
0061     assert diff.seconds == 0
0062     assert diff.microseconds == 0
0063 
0064     # Year, Month & Day should be the same after the round trip
0065     assert date1 == date2
0066 
0067     # There should be no time information
0068     assert time2.hour == 0
0069     assert time2.minute == 0
0070     assert time2.second == 0
0071     assert time2.microsecond == 0
0072 
0073 
0074 SKIP_TZ_ENV_ON_WIN = pytest.mark.skipif(
0075     "env.WIN", reason="TZ environment variable only supported on POSIX"
0076 )
0077 
0078 
0079 @pytest.mark.parametrize(
0080     "time1",
0081     [
0082         datetime.datetime.today().time(),
0083         datetime.time(0, 0, 0),
0084         datetime.time(0, 0, 0, 1),
0085         datetime.time(0, 28, 45, 109827),
0086         datetime.time(0, 59, 59, 999999),
0087         datetime.time(1, 0, 0),
0088         datetime.time(5, 59, 59, 0),
0089         datetime.time(5, 59, 59, 1),
0090     ],
0091 )
0092 @pytest.mark.parametrize(
0093     "tz",
0094     [
0095         None,
0096         pytest.param("Europe/Brussels", marks=SKIP_TZ_ENV_ON_WIN),
0097         pytest.param("Asia/Pyongyang", marks=SKIP_TZ_ENV_ON_WIN),
0098         pytest.param("America/New_York", marks=SKIP_TZ_ENV_ON_WIN),
0099     ],
0100 )
0101 def test_chrono_system_clock_roundtrip_time(time1, tz, monkeypatch):
0102     if tz is not None:
0103         monkeypatch.setenv("TZ", f"/usr/share/zoneinfo/{tz}")
0104 
0105     # Roundtrip the time
0106     datetime2 = m.test_chrono2(time1)
0107     date2 = datetime2.date()
0108     time2 = datetime2.time()
0109 
0110     # The returned value should be a datetime
0111     assert isinstance(datetime2, datetime.datetime)
0112     assert isinstance(date2, datetime.date)
0113     assert isinstance(time2, datetime.time)
0114 
0115     # Hour, Minute, Second & Microsecond should be the same after the round trip
0116     assert time1 == time2
0117 
0118     # There should be no date information (i.e. date = python base date)
0119     assert date2.year == 1970
0120     assert date2.month == 1
0121     assert date2.day == 1
0122 
0123 
0124 def test_chrono_duration_roundtrip():
0125 
0126     # Get the difference between two times (a timedelta)
0127     date1 = datetime.datetime.today()
0128     date2 = datetime.datetime.today()
0129     diff = date2 - date1
0130 
0131     # Make sure this is a timedelta
0132     assert isinstance(diff, datetime.timedelta)
0133 
0134     cpp_diff = m.test_chrono3(diff)
0135 
0136     assert cpp_diff == diff
0137 
0138     # Negative timedelta roundtrip
0139     diff = datetime.timedelta(microseconds=-1)
0140     cpp_diff = m.test_chrono3(diff)
0141 
0142     assert cpp_diff == diff
0143 
0144 
0145 def test_chrono_duration_subtraction_equivalence():
0146 
0147     date1 = datetime.datetime.today()
0148     date2 = datetime.datetime.today()
0149 
0150     diff = date2 - date1
0151     cpp_diff = m.test_chrono4(date2, date1)
0152 
0153     assert cpp_diff == diff
0154 
0155 
0156 def test_chrono_duration_subtraction_equivalence_date():
0157 
0158     date1 = datetime.date.today()
0159     date2 = datetime.date.today()
0160 
0161     diff = date2 - date1
0162     cpp_diff = m.test_chrono4(date2, date1)
0163 
0164     assert cpp_diff == diff
0165 
0166 
0167 def test_chrono_steady_clock():
0168     time1 = m.test_chrono5()
0169     assert isinstance(time1, datetime.timedelta)
0170 
0171 
0172 def test_chrono_steady_clock_roundtrip():
0173     time1 = datetime.timedelta(days=10, seconds=10, microseconds=100)
0174     time2 = m.test_chrono6(time1)
0175 
0176     assert isinstance(time2, datetime.timedelta)
0177 
0178     # They should be identical (no information lost on roundtrip)
0179     assert time1 == time2
0180 
0181 
0182 def test_floating_point_duration():
0183     # Test using a floating point number in seconds
0184     time = m.test_chrono7(35.525123)
0185 
0186     assert isinstance(time, datetime.timedelta)
0187 
0188     assert time.seconds == 35
0189     assert 525122 <= time.microseconds <= 525123
0190 
0191     diff = m.test_chrono_float_diff(43.789012, 1.123456)
0192     assert diff.seconds == 42
0193     assert 665556 <= diff.microseconds <= 665557
0194 
0195 
0196 def test_nano_timepoint():
0197     time = datetime.datetime.now()
0198     time1 = m.test_nano_timepoint(time, datetime.timedelta(seconds=60))
0199     assert time1 == time + datetime.timedelta(seconds=60)
0200 
0201 
0202 def test_chrono_different_resolutions():
0203     resolutions = m.different_resolutions()
0204     time = datetime.datetime.now()
0205     resolutions.timestamp_h = time
0206     resolutions.timestamp_m = time
0207     resolutions.timestamp_s = time
0208     resolutions.timestamp_ms = time
0209     resolutions.timestamp_us = time