fix: correct swapped operands in Std.Time subtraction instances (#12919)

This PR fixes the `HSub PlainTime Duration` instance, which had its
operands reversed: it computed `duration - time` instead of `time -
duration`. For example, subtracting 2 minutes from `time("13:02:01")`
would give `time("10:57:59")` rather than the expected
`time("13:00:01")`. We also noticed that `HSub PlainDateTime
Millisecond.Offset` is similarly affected.

Closes #12918
This commit is contained in:
Jesse Alama
2026-03-16 11:52:06 +01:00
committed by GitHub
parent 2d999d7622
commit fa9a32b5c8
3 changed files with 57 additions and 2 deletions

View File

@@ -566,7 +566,7 @@ instance : HAdd PlainDateTime Millisecond.Offset PlainDateTime where
hAdd := addMilliseconds
instance : HSub PlainDateTime Millisecond.Offset PlainDateTime where
hSub := addMilliseconds
hSub := subMilliseconds
instance : HAdd PlainDateTime Second.Offset PlainDateTime where
hAdd := addSeconds

View File

@@ -384,7 +384,7 @@ instance : HAdd PlainTime Duration PlainTime where
hAdd pt d := PlainTime.ofNanoseconds (d.toNanoseconds + pt.toNanoseconds)
instance : HSub PlainTime Duration PlainTime where
hSub pt d := PlainTime.ofNanoseconds (d.toNanoseconds - pt.toNanoseconds)
hSub pt d := PlainTime.ofNanoseconds (pt.toNanoseconds - d.toNanoseconds)
end Duration
end Time

55
tests/elab/timeSub.lean Normal file
View File

@@ -0,0 +1,55 @@
import Std.Time
open Std.Time
def dt := datetime("2000-01-20T03:02:01")
/--
info: datetime("2000-01-20T03:02:01.500000000")
-/
#guard_msgs in
#eval dt + (500 : Millisecond.Offset)
/--
info: datetime("2000-01-20T03:02:00.500000000")
-/
#guard_msgs in
#eval dt - (500 : Millisecond.Offset)
/--
info: datetime("2000-01-20T03:02:04.000000000")
-/
#guard_msgs in
#eval dt + (3000 : Millisecond.Offset)
/--
info: datetime("2000-01-20T03:01:58.000000000")
-/
#guard_msgs in
#eval dt - (3000 : Millisecond.Offset)
/--
info: true
-/
#guard_msgs in
#eval (dt + (1234 : Millisecond.Offset) - (1234 : Millisecond.Offset)) == dt
def t := time("13:02:01")
def dur := Duration.ofSeconds (120 : Second.Offset)
/--
info: time("13:04:01.000000000")
-/
#guard_msgs in
#eval t + dur
/--
info: time("13:00:01.000000000")
-/
#guard_msgs in
#eval t - dur
/--
info: true
-/
#guard_msgs in
#eval (t + dur - dur) == t