Henry Schreiner | d8c7ee0 | 2020-07-20 13:35:21 -0400 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 2 | from pybind11_tests import chrono as m |
| 3 | import datetime |
Trent Houliston | 352149e | 2016-08-25 23:08:04 +1000 | [diff] [blame] | 4 | |
| 5 | |
| 6 | def test_chrono_system_clock(): |
Trent Houliston | 352149e | 2016-08-25 23:08:04 +1000 | [diff] [blame] | 7 | |
| 8 | # Get the time from both c++ and datetime |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 9 | date1 = m.test_chrono1() |
Trent Houliston | 352149e | 2016-08-25 23:08:04 +1000 | [diff] [blame] | 10 | date2 = datetime.datetime.today() |
| 11 | |
| 12 | # The returned value should be a datetime |
| 13 | assert isinstance(date1, datetime.datetime) |
| 14 | |
| 15 | # The numbers should vary by a very small amount (time it took to execute) |
| 16 | diff = abs(date1 - date2) |
| 17 | |
Henry Schreiner | a876aac | 2020-08-16 11:18:47 -0400 | [diff] [blame^] | 18 | # There should never be a days difference |
Trent Houliston | 352149e | 2016-08-25 23:08:04 +1000 | [diff] [blame] | 19 | assert diff.days == 0 |
Trent Houliston | 352149e | 2016-08-25 23:08:04 +1000 | [diff] [blame] | 20 | |
Henry Schreiner | a876aac | 2020-08-16 11:18:47 -0400 | [diff] [blame^] | 21 | # Since datetime.datetime.today() calls time.time(), and on some platforms |
| 22 | # that has 1 second accuracy, we should always be less than 2 seconds. |
| 23 | assert diff.seconds < 2 |
Trent Houliston | 352149e | 2016-08-25 23:08:04 +1000 | [diff] [blame] | 24 | |
| 25 | |
| 26 | def test_chrono_system_clock_roundtrip(): |
Trent Houliston | 352149e | 2016-08-25 23:08:04 +1000 | [diff] [blame] | 27 | date1 = datetime.datetime.today() |
| 28 | |
| 29 | # Roundtrip the time |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 30 | date2 = m.test_chrono2(date1) |
Trent Houliston | 352149e | 2016-08-25 23:08:04 +1000 | [diff] [blame] | 31 | |
| 32 | # The returned value should be a datetime |
| 33 | assert isinstance(date2, datetime.datetime) |
| 34 | |
| 35 | # They should be identical (no information lost on roundtrip) |
| 36 | diff = abs(date1 - date2) |
| 37 | assert diff.days == 0 |
| 38 | assert diff.seconds == 0 |
| 39 | assert diff.microseconds == 0 |
| 40 | |
| 41 | |
phil-zxx | c6b699d | 2019-07-19 10:28:48 +0100 | [diff] [blame] | 42 | def test_chrono_system_clock_roundtrip_date(): |
| 43 | date1 = datetime.date.today() |
| 44 | |
| 45 | # Roundtrip the time |
| 46 | datetime2 = m.test_chrono2(date1) |
| 47 | date2 = datetime2.date() |
| 48 | time2 = datetime2.time() |
| 49 | |
| 50 | # The returned value should be a datetime |
| 51 | assert isinstance(datetime2, datetime.datetime) |
| 52 | assert isinstance(date2, datetime.date) |
| 53 | assert isinstance(time2, datetime.time) |
| 54 | |
| 55 | # They should be identical (no information lost on roundtrip) |
| 56 | diff = abs(date1 - date2) |
| 57 | assert diff.days == 0 |
| 58 | assert diff.seconds == 0 |
| 59 | assert diff.microseconds == 0 |
| 60 | |
| 61 | # Year, Month & Day should be the same after the round trip |
| 62 | assert date1.year == date2.year |
| 63 | assert date1.month == date2.month |
| 64 | assert date1.day == date2.day |
| 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 | def test_chrono_system_clock_roundtrip_time(): |
| 74 | time1 = datetime.datetime.today().time() |
| 75 | |
| 76 | # Roundtrip the time |
| 77 | datetime2 = m.test_chrono2(time1) |
| 78 | date2 = datetime2.date() |
| 79 | time2 = datetime2.time() |
| 80 | |
| 81 | # The returned value should be a datetime |
| 82 | assert isinstance(datetime2, datetime.datetime) |
| 83 | assert isinstance(date2, datetime.date) |
| 84 | assert isinstance(time2, datetime.time) |
| 85 | |
| 86 | # Hour, Minute, Second & Microsecond should be the same after the round trip |
| 87 | assert time1.hour == time2.hour |
| 88 | assert time1.minute == time2.minute |
| 89 | assert time1.second == time2.second |
| 90 | assert time1.microsecond == time2.microsecond |
| 91 | |
| 92 | # There should be no date information (i.e. date = python base date) |
| 93 | assert date2.year == 1970 |
| 94 | assert date2.month == 1 |
| 95 | assert date2.day == 1 |
| 96 | |
| 97 | |
Trent Houliston | 352149e | 2016-08-25 23:08:04 +1000 | [diff] [blame] | 98 | def test_chrono_duration_roundtrip(): |
Trent Houliston | 352149e | 2016-08-25 23:08:04 +1000 | [diff] [blame] | 99 | |
Dean Moldovan | bad1740 | 2016-11-20 21:21:54 +0100 | [diff] [blame] | 100 | # Get the difference between two times (a timedelta) |
Trent Houliston | 352149e | 2016-08-25 23:08:04 +1000 | [diff] [blame] | 101 | date1 = datetime.datetime.today() |
| 102 | date2 = datetime.datetime.today() |
| 103 | diff = date2 - date1 |
| 104 | |
| 105 | # Make sure this is a timedelta |
| 106 | assert isinstance(diff, datetime.timedelta) |
| 107 | |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 108 | cpp_diff = m.test_chrono3(diff) |
Trent Houliston | 352149e | 2016-08-25 23:08:04 +1000 | [diff] [blame] | 109 | |
| 110 | assert cpp_diff.days == diff.days |
| 111 | assert cpp_diff.seconds == diff.seconds |
| 112 | assert cpp_diff.microseconds == diff.microseconds |
| 113 | |
| 114 | |
| 115 | def test_chrono_duration_subtraction_equivalence(): |
Trent Houliston | 352149e | 2016-08-25 23:08:04 +1000 | [diff] [blame] | 116 | |
| 117 | date1 = datetime.datetime.today() |
| 118 | date2 = datetime.datetime.today() |
| 119 | |
| 120 | diff = date2 - date1 |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 121 | cpp_diff = m.test_chrono4(date2, date1) |
Trent Houliston | 352149e | 2016-08-25 23:08:04 +1000 | [diff] [blame] | 122 | |
| 123 | assert cpp_diff.days == diff.days |
| 124 | assert cpp_diff.seconds == diff.seconds |
| 125 | assert cpp_diff.microseconds == diff.microseconds |
| 126 | |
| 127 | |
phil-zxx | c6b699d | 2019-07-19 10:28:48 +0100 | [diff] [blame] | 128 | def test_chrono_duration_subtraction_equivalence_date(): |
| 129 | |
| 130 | date1 = datetime.date.today() |
| 131 | date2 = datetime.date.today() |
| 132 | |
| 133 | diff = date2 - date1 |
| 134 | cpp_diff = m.test_chrono4(date2, date1) |
| 135 | |
| 136 | assert cpp_diff.days == diff.days |
| 137 | assert cpp_diff.seconds == diff.seconds |
| 138 | assert cpp_diff.microseconds == diff.microseconds |
| 139 | |
| 140 | |
Trent Houliston | 352149e | 2016-08-25 23:08:04 +1000 | [diff] [blame] | 141 | def test_chrono_steady_clock(): |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 142 | time1 = m.test_chrono5() |
Trent Houliston | 2f59768 | 2016-09-13 20:40:28 +1000 | [diff] [blame] | 143 | assert isinstance(time1, datetime.timedelta) |
Trent Houliston | 352149e | 2016-08-25 23:08:04 +1000 | [diff] [blame] | 144 | |
| 145 | |
| 146 | def test_chrono_steady_clock_roundtrip(): |
Trent Houliston | 2f59768 | 2016-09-13 20:40:28 +1000 | [diff] [blame] | 147 | time1 = datetime.timedelta(days=10, seconds=10, microseconds=100) |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 148 | time2 = m.test_chrono6(time1) |
Trent Houliston | 352149e | 2016-08-25 23:08:04 +1000 | [diff] [blame] | 149 | |
Trent Houliston | 2f59768 | 2016-09-13 20:40:28 +1000 | [diff] [blame] | 150 | assert isinstance(time2, datetime.timedelta) |
Trent Houliston | 352149e | 2016-08-25 23:08:04 +1000 | [diff] [blame] | 151 | |
| 152 | # They should be identical (no information lost on roundtrip) |
Trent Houliston | 2f59768 | 2016-09-13 20:40:28 +1000 | [diff] [blame] | 153 | assert time1.days == time2.days |
| 154 | assert time1.seconds == time2.seconds |
| 155 | assert time1.microseconds == time2.microseconds |
| 156 | |
| 157 | |
| 158 | def test_floating_point_duration(): |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 159 | # Test using a floating point number in seconds |
| 160 | time = m.test_chrono7(35.525123) |
Trent Houliston | 2f59768 | 2016-09-13 20:40:28 +1000 | [diff] [blame] | 161 | |
| 162 | assert isinstance(time, datetime.timedelta) |
| 163 | |
| 164 | assert time.seconds == 35 |
Trent Houliston | 253e41c | 2016-09-28 00:59:21 +1000 | [diff] [blame] | 165 | assert 525122 <= time.microseconds <= 525123 |
Jason Rhinelander | e5456c2 | 2017-03-11 22:29:25 -0400 | [diff] [blame] | 166 | |
Jason Rhinelander | 391c754 | 2017-07-25 16:47:36 -0400 | [diff] [blame] | 167 | diff = m.test_chrono_float_diff(43.789012, 1.123456) |
Jason Rhinelander | e5456c2 | 2017-03-11 22:29:25 -0400 | [diff] [blame] | 168 | assert diff.seconds == 42 |
| 169 | assert 665556 <= diff.microseconds <= 665557 |
Alexander Gagarin | b3bf248 | 2019-06-13 12:17:10 +0500 | [diff] [blame] | 170 | |
| 171 | |
| 172 | def test_nano_timepoint(): |
| 173 | time = datetime.datetime.now() |
| 174 | time1 = m.test_nano_timepoint(time, datetime.timedelta(seconds=60)) |
| 175 | assert(time1 == time + datetime.timedelta(seconds=60)) |