|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
| 3 | +from datetime import time |
| 4 | + |
3 | 5 | import pendulum |
4 | 6 |
|
5 | 7 | from pendulum import Time |
@@ -96,6 +98,37 @@ def test_diff_in_seconds_ensure_is_truncated(): |
96 | 98 | assert dt.diff(dt.add(seconds=1.9)).in_seconds() == 1 |
97 | 99 |
|
98 | 100 |
|
| 101 | +def test_diff_includes_microseconds(): |
| 102 | + dt = Time(12, 0, 0, 500000) |
| 103 | + diff = dt.diff(Time(12, 0, 1)) |
| 104 | + |
| 105 | + assert diff.total_seconds() == 0.5 |
| 106 | + assert diff.microseconds == 500000 |
| 107 | + assert diff.in_seconds() == 0 |
| 108 | + |
| 109 | + |
| 110 | +def test_diff_includes_microseconds_negative_with_sign(): |
| 111 | + dt = Time(12, 0, 1) |
| 112 | + diff = dt.diff(Time(12, 0, 0, 250000), False) |
| 113 | + |
| 114 | + assert diff.total_seconds() == -0.75 |
| 115 | + assert diff.microseconds == -750000 |
| 116 | + |
| 117 | + |
| 118 | +def test_diff_includes_microseconds_within_the_same_second(): |
| 119 | + dt = Time(12, 0, 0, 1) |
| 120 | + assert dt.diff(Time(12, 0, 0, 999999)).total_seconds() == 0.999998 |
| 121 | + |
| 122 | + |
| 123 | +def test_diff_includes_microseconds_of_native_time(): |
| 124 | + dt = Time(12, 0, 0) |
| 125 | + assert dt.diff(time(12, 0, 0, 750000)).total_seconds() == 0.75 |
| 126 | + |
| 127 | + |
| 128 | +def test_diff_min_and_max(): |
| 129 | + assert Time.min.diff(Time.max).total_seconds() == 86399.999999 |
| 130 | + |
| 131 | + |
99 | 132 | def test_diff_for_humans_now_and_second(): |
100 | 133 | with pendulum.travel_to(pendulum.today().at(12, 34, 56)): |
101 | 134 | now = pendulum.now().time() |
@@ -167,42 +200,42 @@ def test_diff_for_humans_now_and_future_seconds(): |
167 | 200 |
|
168 | 201 |
|
169 | 202 | def test_diff_for_humans_now_and_nearly_future_minute(): |
170 | | - with pendulum.travel_to(pendulum.today().at(12, 34, 56)): |
| 203 | + with pendulum.travel_to(pendulum.today().at(12, 34, 56), freeze=True): |
171 | 204 | now = pendulum.now().time() |
172 | 205 |
|
173 | 206 | assert now.add(seconds=59).diff_for_humans() == "in 59 seconds" |
174 | 207 |
|
175 | 208 |
|
176 | 209 | def test_diff_for_humans_now_and_future_minute(): |
177 | | - with pendulum.travel_to(pendulum.today().at(12, 34, 56)): |
| 210 | + with pendulum.travel_to(pendulum.today().at(12, 34, 56), freeze=True): |
178 | 211 | now = pendulum.now().time() |
179 | 212 |
|
180 | 213 | assert now.add(minutes=1).diff_for_humans() == "in 1 minute" |
181 | 214 |
|
182 | 215 |
|
183 | 216 | def test_diff_for_humans_now_and_future_minutes(): |
184 | | - with pendulum.travel_to(pendulum.today().at(12, 34, 56)): |
| 217 | + with pendulum.travel_to(pendulum.today().at(12, 34, 56), freeze=True): |
185 | 218 | now = pendulum.now().time() |
186 | 219 |
|
187 | 220 | assert now.add(minutes=2).diff_for_humans() == "in 2 minutes" |
188 | 221 |
|
189 | 222 |
|
190 | 223 | def test_diff_for_humans_now_and_nearly_future_hour(): |
191 | | - with pendulum.travel_to(pendulum.today().at(12, 34, 56)): |
| 224 | + with pendulum.travel_to(pendulum.today().at(12, 34, 56), freeze=True): |
192 | 225 | now = pendulum.now().time() |
193 | 226 |
|
194 | 227 | assert now.add(minutes=59).diff_for_humans() == "in 59 minutes" |
195 | 228 |
|
196 | 229 |
|
197 | 230 | def test_diff_for_humans_now_and_future_hour(): |
198 | | - with pendulum.travel_to(pendulum.today().at(12, 34, 56)): |
| 231 | + with pendulum.travel_to(pendulum.today().at(12, 34, 56), freeze=True): |
199 | 232 | now = pendulum.now().time() |
200 | 233 |
|
201 | 234 | assert now.add(hours=1).diff_for_humans() == "in 1 hour" |
202 | 235 |
|
203 | 236 |
|
204 | 237 | def test_diff_for_humans_now_and_future_hours(): |
205 | | - with pendulum.travel_to(pendulum.today().at(12, 34, 56)): |
| 238 | + with pendulum.travel_to(pendulum.today().at(12, 34, 56), freeze=True): |
206 | 239 | now = pendulum.now().time() |
207 | 240 |
|
208 | 241 | assert now.add(hours=2).diff_for_humans() == "in 2 hours" |
|
0 commit comments