我正在使用chrono crate,想要计算Duration
两个DateTime
s 之间的值。
use chrono::Utc;
use chrono::offset::TimeZone;
let start_of_period = Utc.ymd(2020, 1, 1).and_hms(0, 0, 0);
let end_of_period = Utc.ymd(2021, 1, 1).and_hms(0, 0, 0);
// What should I enter here?
//
// The goal is to find a duration so that
// start_of_period + duration == end_of_period
// I expect duration to be of type std::time
let duration = ...
let nb_of_days = duration.num_days();
Run Code Online (Sandbox Code Playgroud)
Pet*_*all 10
DateTime
实现Sub<DateTime>
,因此您只需从第一个日期中减去最近的日期即可:
let duration = end_of_period - start_of_period;
println!("num days = {}", duration.num_days());
Run Code Online (Sandbox Code Playgroud)