Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Off-by-one in precise_sub #13

Open
loxs opened this issue Sep 22, 2018 · 2 comments
Open

Off-by-one in precise_sub #13

loxs opened this issue Sep 22, 2018 · 2 comments
Labels
bug Something isn't working
Milestone

Comments

@loxs
Copy link
Collaborator

loxs commented Sep 22, 2018

Originally reported here by Stephen Dolan

I expected that if I use CalendarLib.Calendar.Precise.precise_sub to compute the difference between two dates, then I could add this difference to the earlier and get the later.

However, if I attempt this on [2025-03-31 17:53:04; 2026-07-19 12:15:23], then the result of add is 2026-07-20 12:15:23, not 2026-07-19 12:15:23.

@loxs loxs added the bug Something isn't working label Sep 22, 2018
@loxs loxs added this to the 2.0 milestone Sep 22, 2018
@loxs
Copy link
Collaborator Author

loxs commented Sep 29, 2018

This seems to have something to do with leap years (the library incorrectly thinks that 2026 is leap).

If we don't cross the boundaries of 2026-02-28, we get correct results:

utop # let d = Date.precise_sub (Date.make 2026 2 28) (Date.make 2025 3 31);;
val d : Date.Period.t = <abstr>
utop # let added = Date.add (Date.make 2025 3 31) d;;
val added : Period.date_field Date.date = <abstr>
utop # Printer.Date.to_string added;;
- : string = "2026-02-28"

But if we try with 2026-02-29 (should not even allow us to do it), we get this:

utop # let d = Date.precise_sub (Date.make 2026 2 29) (Date.make 2025 3 31);;
val d : Date.Period.t = <abstr>
utop # let added = Date.add (Date.make 2025 3 31) d;;
val added : Period.date_field Date.date = <abstr>
utop # Printer.Date.to_string added;;
- : string = "2026-03-01"

So it seems that precise_sub allows for wrong leap years while later add correctly figures out that 2026 is not leap.

@loxs
Copy link
Collaborator Author

loxs commented Sep 29, 2018

I was wrong, doesn't have anything to do with leap years:

let test_precise_sub start ending =
    Date.precise_sub ending start |> Date.add start |> Printer.Date.to_string

For this case it only happens for dates between 2025-07-10 and 2025-07-30:

utop # test_precise_sub (Date.make 2025 3 31) (Date.make 2026 7 9);;
- : string = "2026-07-09"
utop # test_precise_sub (Date.make 2025 3 31) (Date.make 2026 7 10);;
- : string = "2026-07-11"
utop # test_precise_sub (Date.make 2025 3 31) (Date.make 2026 7 30);;
- : string = "2026-07-31"
utop # test_precise_sub (Date.make 2025 3 31) (Date.make 2026 7 31);;
- : string = "2026-07-31"
utop # test_precise_sub (Date.make 2018 5 31) (Date.make 2018 7 2);;
- : string = "2018-07-03"

Seems to happen when jumping from the last day of an odd numbered month into July but not for all days of July:

utop # test_precise_sub (Date.make 2018 3 31) (Date.make 2018 7 20);;
- : string = "2018-07-21"
utop # test_precise_sub (Date.make 2018 3 31) (Date.make 2018 7 6);;
- : string = "2018-07-07"
utop # test_precise_sub (Date.make 2018 3 31) (Date.make 2018 7 2);;
- : string = "2018-07-02"
utop # test_precise_sub (Date.make 2018 3 31) (Date.make 2018 7 20);;
- : string = "2018-07-21"
utop # test_precise_sub (Date.make 2018 3 31) (Date.make 2018 7 6);;
- : string = "2018-07-07"
utop # test_precise_sub (Date.make 2018 3 31) (Date.make 2018 7 2);;
- : string = "2018-07-02"

So far I haven't been able to reproduce it with anything other than July

The smallest reproducible seems to be:

utop # test_precise_sub (Date.make 2018 5 31) (Date.make 2018 7 2);;
- : string = "2018-07-03"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant