Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Sum type for duration? #520

Closed
jakevossen5 opened this issue Jan 5, 2021 · 1 comment · Fixed by #522
Closed

Implement Sum type for duration? #520

jakevossen5 opened this issue Jan 5, 2021 · 1 comment · Fixed by #522

Comments

@jakevossen5
Copy link
Contributor

Here is a basic example:

use std::vec;

use chrono::{DateTime, Duration, NaiveDateTime, TimeZone, Utc};
fn main() {
    let dt = Utc.ymd(1970, 1, 1).and_hms_nano(0, 0, 1, 444);
    let dt_2 = Utc.ymd(1977, 1, 1).and_hms_nano(0, 0, 1, 444);
    let dt_3 = Utc.ymd(1970, 1, 1).and_hms_nano(0, 0, 1, 444);
    let dt_4 = Utc.ymd(2020, 1, 1).and_hms_nano(0, 0, 1, 444);

    let dur_1 = dt_2 - dt;
    let dur_2 = dt_3 - dt;
    let dur_3 = dt_4 - dt;

    let durations = vec![dur_1, dur_2, dur_3];

    let total: Duration = durations.iter().sum();
}

Running cargo check results in

error[E0277]: the trait bound `chrono::Duration: Sum<&chrono::Duration>` is not satisfied
  --> src/main.rs:17:44
   |
17 |     let total: Duration = durations.iter().sum();
   |                                            ^^^ the trait `Sum<&chrono::Duration>` is not implemented for `chrono::Duration`

The Duration in the std has Sum implemented (https://doc.rust-lang.org/std/time/struct.Duration.html#impl-Sum%3CDuration%3E), it would be nice if chrono had it too. If this is a feature that should be added, I would be willing to take a look at implementing it.

@quodlibetor
Copy link
Contributor

No reason it isn't implemented, a PR would be welcome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants