Skip to content

Commit 7a2fb2a

Browse files
committed
fix: preserve microseconds in Time differences
1 parent e06dace commit 7a2fb2a

3 files changed

Lines changed: 63 additions & 8 deletions

File tree

‎src/pendulum/time.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,11 +226,11 @@ def diff(self, dt: time | None = None, abs: bool = True) -> Duration:
226226

227227
us1 = (
228228
self.hour * SECS_PER_HOUR + self.minute * SECS_PER_MIN + self.second
229-
) * USECS_PER_SEC
229+
) * USECS_PER_SEC + self.microsecond
230230

231231
us2 = (
232232
dt.hour * SECS_PER_HOUR + dt.minute * SECS_PER_MIN + dt.second
233-
) * USECS_PER_SEC
233+
) * USECS_PER_SEC + dt.microsecond
234234

235235
klass = Duration
236236
if abs:

‎tests/time/test_diff.py‎

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from __future__ import annotations
22

3+
from datetime import time
4+
35
import pendulum
46

57
from pendulum import Time
@@ -96,6 +98,37 @@ def test_diff_in_seconds_ensure_is_truncated():
9698
assert dt.diff(dt.add(seconds=1.9)).in_seconds() == 1
9799

98100

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+
99132
def test_diff_for_humans_now_and_second():
100133
with pendulum.travel_to(pendulum.today().at(12, 34, 56)):
101134
now = pendulum.now().time()
@@ -167,42 +200,42 @@ def test_diff_for_humans_now_and_future_seconds():
167200

168201

169202
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):
171204
now = pendulum.now().time()
172205

173206
assert now.add(seconds=59).diff_for_humans() == "in 59 seconds"
174207

175208

176209
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):
178211
now = pendulum.now().time()
179212

180213
assert now.add(minutes=1).diff_for_humans() == "in 1 minute"
181214

182215

183216
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):
185218
now = pendulum.now().time()
186219

187220
assert now.add(minutes=2).diff_for_humans() == "in 2 minutes"
188221

189222

190223
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):
192225
now = pendulum.now().time()
193226

194227
assert now.add(minutes=59).diff_for_humans() == "in 59 minutes"
195228

196229

197230
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):
199232
now = pendulum.now().time()
200233

201234
assert now.add(hours=1).diff_for_humans() == "in 1 hour"
202235

203236

204237
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):
206239
now = pendulum.now().time()
207240

208241
assert now.add(hours=2).diff_for_humans() == "in 2 hours"

‎tests/time/test_sub.py‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,25 @@ def test_subtract_time():
111111

112112
with pytest.raises(TypeError):
113113
t3 - t
114+
115+
116+
def test_subtract_time_includes_microseconds():
117+
t = Time(12, 0, 1)
118+
t1 = Time(12, 0, 0, 500000)
119+
t2 = time(12, 0, 0, 500000)
120+
121+
diff = t - t1
122+
assert isinstance(diff, pendulum.Duration)
123+
assert diff.total_seconds() == 0.5
124+
125+
diff = t1 - t
126+
assert diff.total_seconds() == -0.5
127+
128+
diff = t - t2
129+
assert diff.total_seconds() == 0.5
130+
131+
diff = t2 - t
132+
assert diff.total_seconds() == -0.5
133+
134+
diff = Time(12, 0, 0, 999999) - t
135+
assert diff.total_seconds() == -1e-06

0 commit comments

Comments
 (0)