Rust implementation of relativedelta
known from Python's dateutil library.
Extension to the Duration
from the the time library, which allows for calculating datetimes based on a relative representation of date and time.
Put this in your Cargo.toml
:
toml
[dependencies]
relativedelta = "0.2"
Optional features:
- [serde1
][]: Enable serialization/deserialization via serde.
In the pipeline: - [X] Hook up to travis.com. - [ ] Mitigation of month rounding error when init with floats or mul with floats. - [X] Create a proper crate and publish on crates.io. - [ ] Documentation and doctest. - [X] Code coverage setup and badge with travis.com and codecov.io
Examples:
```rust // Construction let years1 = RelativeDelta::withyears(1).new(); let months12 = RelativeDelta::withmonths(12).new(); assert_eq!(years1, months12);
let years1 = RelativeDelta::withyears(1).anddays(32).new(); // If same parameter is specified twice, only the latest is applied. let months6 = RelativeDelta::withmonths(12).withmonths(6).new(); asserteq!(months6, RelativeDelta::withmonths(6).new()); // Below is identical to: RelativeDelta::yysmmsdds(Some(2020), 1, Some(1), 3, None, 12).new(); let rddt = RelativeDelta::withyear(2020).andyears(1).andmonth(Some(1)).andmonths(3).and_days(12).new();
// Two or more RelativeDeltas can be added and substracted. However, note that constants are lost in the process. let lhs = RelativeDelta::yysmmsdds(Some(2020), -4, Some(1), 3, None, 0).new(); let rhs = RelativeDelta::yysmmsdds(Some(2020), 1, Some(1), 42, None, 0).new(); asserteq!(lhs + rhs, RelativeDelta::withyears(-3).andmonths(45).new()); asserteq!(lhs - rhs, RelativeDelta::withyears(-5).andmonths(-39).new()); asserteq!(-lhs + rhs, RelativeDelta::withyears(5).and_months(39).new());
// The RelativeDelta can be multiplied with a f64. asserteq!(rhs * 0.5, RelativeDelta::withyears(2).andyear(Some(2020)).andmonths(3).and_month(Some(1)).new());
// This crates party piece is the ability to calculate dates based on already existing chrono::DateTime // If one would like to get the last day of the month that one is currently in, it could be done with: println!("{}", Utc::now() + RelativeDelta::withday(1).andmonths(1).and_days(-1).new()); // Above first sets the day of the month to the 1st, then adds a month and subtracts a day. ```