Skip to content

Calendar::date_until returns wrong results for negative durations #617

@catamorphism

Description

@catamorphism

The following test program:

use temporal_rs::{Calendar, PlainDate};
use temporal_rs::options::{DifferenceSettings, Unit};

fn main() {
    let later = PlainDate::new(2020, 1, 3, Calendar::GREGORIAN).unwrap();
    let earlier = PlainDate::new(2020, 1, 1, Calendar::GREGORIAN).unwrap();

    let mut settings = DifferenceSettings::default();
    settings.largest_unit = Some(Unit::Year);
    println!("With Gregorian calendar and largestUnit = years: {}", later.until(&earlier, settings).unwrap());

    println!("With Gregorian calendar and default settings: {}", later.until(&earlier, DifferenceSettings::default()).unwrap());

    let later_iso = PlainDate::new(2020, 1, 3, Calendar::ISO).unwrap();
    let earlier_iso = PlainDate::new(2020, 1, 1, Calendar::ISO).unwrap();
    println!("With ISO calendar and default settings: {}", later_iso.until(&earlier_iso, DifferenceSettings::default()).unwrap());

    println!("With ISO calendar and largestUnit = years: {}", later_iso.until(&earlier_iso, settings).unwrap());

    let later_month = PlainDate::new(2020, 3, 1, Calendar::GREGORIAN).unwrap();
    let earlier_month = PlainDate::new(2020, 1, 1, Calendar::GREGORIAN).unwrap();
    println!("With months difference: {}", later_month.until(&earlier_month, settings).unwrap());
}

prints:

With Gregorian calendar and largestUnit = years: P2D
With Gregorian calendar and default settings: -P2D
With ISO calendar and default settings: -P2D
With ISO calendar and largestUnit = years: -P2D
With months difference: P2M

The first result should be -P2D like the next three.

The bug is here. until() could have returned a negated duration, but the let duration = DateDuration::new... line constructs a non-negated duration.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    Status
    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions