简体   繁体   English

特征`std :: ops :: Add <std::time::Duration> `没有为`chrono :: DateTime实现 <chrono::Utc> `

[英]The trait `std::ops::Add<std::time::Duration>` is not implemented for `chrono::DateTime<chrono::Utc>`

extern crate chrono;
use chrono::{DateTime, Utc};
use std::time::Duration;

pub fn after(start: DateTime<Utc>) -> DateTime<Utc> {
    start + Duration::from_secs(1)
}

fails with: 失败了:

error[E0277]: cannot add `std::time::Duration` to `chrono::DateTime<chrono::Utc>`
 --> src/lib.rs:7:11
  |
7 |     start + Duration::from_secs(1_000_000_000)
  |           ^ no implementation for `chrono::DateTime<chrono::Utc> + std::time::Duration`
  |
  = help: the trait `std::ops::Add<std::time::Duration>` is not implemented for `chrono::DateTime<chrono::Utc>`

I couldn't find an implementation of Add to import. 我找不到Add to import的实现。 use chrono::* won't help. use chrono::*无济于事。

I see that datetime.rs has an impl for Add<chrono::oldtime::Duration> , but oldtime is private so I don't know how to create an oldtime::Duration . 我看到datetime.rsAdd<chrono::oldtime::Duration>有一个impl,但oldtime是私有的,所以我不知道如何创建oldtime::Duration

How do I get the Add impl I need? 如何获得我需要的Add impl? How do I convert std::time::Duration to chrono::oldtime::Duration ? 如何将std::time::Duration转换为chrono::oldtime::Duration Is there something I can import to convert implicitly? 有什么我可以导入隐式转换?

I'm using rustc 1.25.0 (84203cac6 2018-03-25) 我正在使用rustc 1.25.0 (84203cac6 2018-03-25)

This is almost answered with a quote from the chrono documentation : 这几乎是从chrono 文档中引用的:

Chrono currently uses the time::Duration type from the time crate to represent the magnitude of a time span. Chrono目前使用time::Duration类型来表示时间跨度的大小。 Since this has the same name to the newer, standard type for duration, the reference will refer this type as OldDuration . 由于此名称与较新的标准类型具有相同的持续时间,因此引用将此类型称为OldDuration [...] [...]

Chrono does not yet natively support the standard Duration type, but it will be supported in the future. Chrono尚未原生支持标准的Duration类型,但将来会支持它。 Meanwhile you can convert between two types with Duration::from_std and Duration::to_std methods. 同时,您可以使用Duration::from_stdDuration::to_std方法在两种类型之间进行转换。

So, adding a duration to a Chrono date-time has to be done with this OldDuration , which is actually exported from the root of the crate with the name Duration : 因此,必须使用此OldDuration为Chrono日期时间添加持续时间,该OldDuration实际上是名称为Duration 的crate的根目录导出的

use chrono::{DateTime, Utc, Duration as OldDuration};

Then, adding a duration can be done by either creating an OldDuration directly: 然后,可以通过直接创建OldDuration来添加持续时间:

pub fn after(start: DateTime<Utc>) -> DateTime<Utc> {
    start + OldDuration::seconds(1)
}

Or by converting a standard duration. 或者通过转换标准持续时间。

pub fn after(start: DateTime<Utc>) -> DateTime<Utc> {
    start + OldDuration::from_std(Duration::from_secs(1)).unwrap()
}

This experience might be improved before chrono reaches 1.0.0. chrono达到1.0.0之前,这种体验可能会得到改善。

There are functions to convert from and to std::time::Duration so you could just do: 有功能转换 std::time::Duration ,所以你可能只是这样做:

start + ::chrono::Duration::from_std(Duration::from_secs(1)).expect("1s can't overflow")

But if you can just stick with chrono , just stick with chrono : 但如果你能坚持使用chrono ,只需坚持chrono

use chrono::{DateTime, Utc, Duration};
start + Duration::seconds(1)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 `chrono::DateTime 没有实现特征`JsonSchema`<utc> `</utc> - the trait `JsonSchema` is not implemented for `chrono::DateTime<Utc>` 特征`可查询<diesel::sql_types::timestamptz, pg> ` 没有为 `chrono::DateTime 实现<utc> `</utc></diesel::sql_types::timestamptz,> - the trait `Queryable<diesel::sql_types::Timestamptz, Pg>` is not implemented for `chrono::DateTime<Utc>` 未为`T`实现特征`std :: ops :: Fn &lt;(char,)&gt;` - The trait `std::ops::Fn<(char,)>` is not implemented for `T` 特征 std::ops::Try 没有为 impl 实现 - the trait std::ops::Try is not implemented for impl 检查是否为 chrono::DateTime 的惯用方法<utc>是否在日期和时间范围内?</utc> - Idiomatic way to check if a chrono::DateTime<Utc> is within date and time range? 如何为本地特征实现 std::ops::Add? - How to implement std::ops::Add for a local trait? 如何计算两个 chrono::DateTime 之间的持续时间? - How to compute the duration between two chrono::DateTime? 如何为 Chrono UTC 添加天数? - How do I add days to a Chrono UTC? 特征绑定`chrono::DateTime<utc> : 来自Sql <diesel::sql_types::nullable<diesel::sql_types::timestamptz> ,pg&gt;`不满足</diesel::sql_types::nullable<diesel::sql_types::timestamptz></utc> - Trait bound `chrono::DateTime<Utc>: FromSql<diesel::sql_types::Nullable<diesel::sql_types::Timestamptz>, Pg>` is not satisfied 如何转换chrono`DateTime <UTC> `实例到&#39;DateTime <Local> `? - How do I convert a chrono `DateTime<UTC>` instance to `DateTime<Local>`?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM