[英]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.rs
对Add<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 asOldDuration
. 由于此名称与较新的标准类型具有相同的持续时间,因此引用将此类型称为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 withDuration::from_std
andDuration::to_std
methods. 同时,您可以使用Duration::from_std
和Duration::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.