github.com/kaydxh/golang@v0.0.131/pkg/gocv/cgo/third_path/pybind11/tests/test_chrono.py (about)

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