0
// Cargo.toml
chrono = { version = "0.4", features = ["serde", "rustc-serialize"] }
let mut date: DateTime<Utc> = DateTime::default();

date += Duration::years(1000); // no Duration::years()
date += Duration::months(1000); // no Duration::months()
date += Duration::days(1000); 
date += Duration::hours(1000); 
date += Duration::minutes(1000);
date += Duration::seconds(1000);
date = date.with_year(date.year() + 1000).unwrap(); // Ok
date = date.with_month(date.month() + 1000).unwrap(); // if date.with_month() parameter is over 12, panick

what is best way that calculate Utc in rust chrono??

cafce25
  • 15,907
  • 4
  • 25
  • 31
mycatgib
  • 23
  • 4
  • what do you mean by "calculate UTC"? also, note that "year" or "month" are ambiguous quantities; neither of the two has a fixed number of days. – FObersteiner Mar 06 '23 at 16:49
  • Utc means DateTime variable(in src, let mut date). The meaning of year, month intened is ,like Duration::days, Duration::hours, a structure that can be added to DateTime – mycatgib Mar 06 '23 at 17:16

1 Answers1

1

Since a month can have anywhere from 28 to 31 days it's not a Duration, to still be able to work with them you can use the Months abstraction:

use chrono::Months;
date = date + Months::new(1000);
cafce25
  • 15,907
  • 4
  • 25
  • 31