Extention to the Duration from the the time library, allowing calculating datetimes based on a relative representation of datetime.
If you cannot wait until proper crate/first release, put this in your Cargo.toml
:
toml
[dependencies]
relativedelta = {git = "https://github.com/timkaas/relativedelta"}
Optional features:
- [serde
][]: Enable serialization/deserialization via serde.
In the works: - [X] Hook up to travis.com. - [ ] Mitigation of month rounding error when init with floats or mul with floats. - [ ] Create a proper crate and publish on crates.io. - [ ] Documentation and doctest.
Examples:
```rust use relativedelta;
let year = 2020; let month = 4; let month2 = 3; let months = -11; let day = 28; let days = 31; let hour = 12; let min = 35; let sec = 48; let nsecs = -11111111111; let dt = Utc.ymd(year, month, day).andhms(hour, min, sec); let ddt = RelativeDeltaDateTime::years(1) .withmonth(month2) .withmonths(months) .withdays(days) .withnanoseconds(nsecs) .new();
let add1year = RelativeDeltaDateTime::years(1).new(); asserteq!(dt + add1year, Utc.ymd(2021, month, day).andhms(hour, min, sec));
let sub1year = RelativeDeltaDateTime::years(-1).new(); asserteq!(dt + sub1year, Utc.ymd(2019, month, day).andhms(hour, min, sec));
let setyear = RelativeDeltaDateTime::year(2010).new(); asserteq!(dt + setyear, Utc.ymd(2010, month, day).andhms(hour, min, sec));
let setyear = RelativeDeltaDateTime::year(-1).new(); asserteq!(dt + setyear, Utc.ymd(-1, month, day).andhms(hour, min, sec));
let add69months = RelativeDeltaDateTime::months(69).new(); // Expected after fix asserteq!(add69months.years, 5); asserteq!(add69months.months, 9); asserteq!(dt + add69months, Utc.ymd(2026, 1, day).andhms(hour, min, sec));
let sub6months = RelativeDeltaDateTime::months(-6).new(); asserteq!(dt + sub6months, Utc.ymd(2019, 10, day).andhms(hour, min, sec));
let sub47months = RelativeDeltaDateTime::months(-47).new(); // Expected after fix asserteq!(sub47months.years, -3); asserteq!(sub47months.months, -11); asserteq!(dt + sub47months, Utc.ymd(2016, 5, day).andhms(hour, min, sec));
let add400days = RelativeDeltaDateTime::days(400).new(); asserteq!(dt + add400days, Utc.ymd(2021, 6, 2).andhms(hour, min, sec));
let sub400days = RelativeDeltaDateTime::days(-400).new(); asserteq!(dt + sub400days, Utc.ymd(2019, 3, 25).andhms(hour, min, sec));
let pay1 = RelativeDeltaDateTime::day(1).withdays(-1).withmonth(3).withmonths(1).new(); asserteq!(dt + pay1, Utc.ymd(2020, 3, 31).and_hms(hour, min, sec));
let pay2 = RelativeDeltaDateTime::day(1).withdays(-1).withmonth(6).withmonths(1).new(); asserteq!(dt + pay2, Utc.ymd(2020, 6, 30).and_hms(hour, min, sec));
let pay3 = RelativeDeltaDateTime::day(1).withdays(-1).withmonth(9).withmonths(1).new(); asserteq!(dt + pay3, Utc.ymd(2020, 9, 30).and_hms(hour, min, sec));
let pay4 = RelativeDeltaDateTime::day(1).withdays(-1).withmonth(12).withmonths(1).new(); asserteq!(dt + pay4, Utc.ymd(2020, 12, 31).and_hms(hour, min, sec));
// Multiplication
let ddt = RelativeDeltaDateTime::years(10).andmonths(6).anddays(-15).andhours(23).new(); let r = ddt * 0.42f64; println!("{:?}", r);
// Init with floats let ddt = RelativeDeltaDateTime::ysmsdshsmsssnsf(-0.42, -15.7, -12.3, -5.32, 3.14, 0.15, 22232).new(); println!("testinitwithfloat {:?}", ddt);
let ddt = RelativeDeltaDateTime::ysmsdshsmsssnsf(1.5, -18.0, 0.0, 0.0, 0.0, 0.0, 0).new(); asserteq!( ddt, RelativeDeltaDateTime::yysmmsdds(None, 0, None, 0, None, 0) .and_hhsmmssss(None, 0, None, 0, None, 0) .new() ); ```