0

I've been going through the documentation for chrono and chronoutil for Rust but I think I am missing something when trying to add a month to a NaiveDate.

I have a very simple Rust program:

use chrono::NaiveDate;
use chronoutil::relative_duration::RelativeDuration;

fn main() {

    let start = NaiveDate::from_ymd(2020, 1, 31);
    let one_month = RelativeDuration::months(1);
    let end = start + one_month;
    println!("{}", end.format("%Y-%m-%d").to_string());

}

But I am getting a mismatched types error on compile

     let end = start + one_month;
                       ^^^^^^^^^ expected struct `chrono::Duration`, found struct `RelativeDuration`

It doesn't seem very happy with me adding the RelativeDuration to the NaiveDate.

Including dependencies in Cargo.toml:

[dependencies]
chrono = "0.2.16"
chronoutil = "0.2.3"
E_net4
  • 27,810
  • 13
  • 101
  • 139
TheLovelySausage
  • 3,838
  • 15
  • 56
  • 106
  • 1
    Any reason why not `let end = start.checked_add_months(1)` ? [NaiveDate::checked_add_months](https://docs.rs/chrono/latest/chrono/naive/struct.NaiveDate.html#method.checked_add_months) – Alexey S. Larionov Aug 04 '22 at 15:59
  • 1
    That's strange because it almost looks like the basic example of the `chronoutil` crate... if you try `use chronoutil::RelativeDuration`, does it work? – jthulhu Aug 04 '22 at 16:01
  • @AlexeyLarionov I tried `checked_add_months` but it got a different error `help: there is an associated function with a similar name: `checked_add`` – TheLovelySausage Aug 04 '22 at 16:06
  • @BlackBeans I know right! I did try with `chronoutil::RelativeDuration` but got pretty much the same error – TheLovelySausage Aug 04 '22 at 16:07
  • 1
    You're using an insanely old version of `chrono`. You can't upgrade? – isaactfa Aug 04 '22 at 16:36
  • @isaactfa I will try to upgrade and check – TheLovelySausage Aug 04 '22 at 16:38
  • 1
    I still don't think you can add a `RelativeDuration` to a `NaiveDate` but should be able to use `.checked_add_months(Months::new(1))` then. – isaactfa Aug 04 '22 at 16:39
  • @isaactfa you were right but it seems the newer version of `chrono` supports months anyway. Does that mean that chronoutils has become redundant? – TheLovelySausage Aug 04 '22 at 16:54
  • 1
    I've never used it, I couldn't tell you. – isaactfa Aug 04 '22 at 17:03
  • 1
    @isaactfa thanks for being so helpful though. I can't justify using `chronoutil` for my needs with all the new `chrono` features but thanks so much for your help – TheLovelySausage Aug 04 '22 at 20:22

0 Answers0