我如何使用 Chrono 从 NaiveDate 转到特定时区?

How do I go from a NaiveDate to a specific TimeZone with Chrono?

我正在使用 chrono crate 在 Rust 中解析日期和时间。日期和时间来自网站,其中日期和时间来自页面的不同部分。

日期以 %d/%m/%Y 格式显示(例如:27/08/2018)。时间只显示小时(例如:12、10、21 等)

我想将这些日期时间存储为 UTC,这样我就可以以 "timezone agnostic" 的方式计算从现在到给定日期时间的剩余时间。我知道这些日期时间来自哪个时区(巴黎时间)。

我根据日期输入创建了一个 NaiveDate(这是一项正在进行的工作,因此还没有错误处理):

let naive_date = NaiveDate::parse_from_str(date, "%d/%m/%Y").unwrap()

从那时起,假设我有一个包含小时的字符串,获取 UTC DateTime 的最佳方法是什么?

我迷失在各种 TimeZone/Offset 特征中,不知道我是否应该使用 LocalFixedOffset 然后转换为 Utc

Chrono 文档可能会得到改进,以便更容易找到如何执行这些操作。

假设这是您的起点:

use chrono::{DateTime, FixedOffset, NaiveDate, NaiveDateTime, NaiveTime, TimeZone, Utc};

// The date you parsed
let date = NaiveDate::from_ymd(2018, 5, 13);
// The known 1 hour time offset in seconds
let tz_offset = FixedOffset::east(1 * 3600);
// The known time
let time = NaiveTime::from_hms(17, 0, 0);
// Naive date time, with no time zone information
let datetime = NaiveDateTime::new(date, time);

然后您可以使用 FixedOffset 构造一个 DateTime:

let dt_with_tz: DateTime<FixedOffset> = tz_offset.from_local_datetime(&datetime).unwrap();

如果您需要将其转换为 DateTime<Utc>,您可以这样做:

let dt_with_tz_utc: DateTime<Utc> = Utc.from_utc_datetime(&dt_with_tz.naive_utc());

The dates and times are from a website in which the date and time are from different sections of the page.

这是一个示例,说明如何增量地 解析来自不同字符串的多个值,为未解析的信息提供默认值,并使用 Chrono 的内置时区转换。

关键是使用parse function to update a Parsed struct. You can use the StrftimeItems迭代器继续使用更具可读性的格式字符串。

extern crate chrono;

use chrono::prelude::*;

fn example(date: &str, hour: &str) -> chrono::ParseResult<DateTime<Utc>> {
    use chrono::format::{self, strftime::StrftimeItems, Parsed};

    // Set up a struct to perform successive parsing into
    let mut p = Parsed::default();

    // Parse the date information
    format::parse(&mut p, date.trim(), StrftimeItems::new("%d/%m/%Y"))?;
    // Parse the time information and provide default values we don't parse
    format::parse(&mut p, hour.trim(), StrftimeItems::new("%H"))?;
    p.minute = Some(0);
    p.second = Some(0);

    // Convert parsed information into a DateTime in the Paris timezone
    let paris_time_zone_offset = FixedOffset::east(1 * 3600);
    let dt = p.to_datetime_with_timezone(&paris_time_zone_offset)?;

    // You can also use chrono-tz instead of hardcoding it
    // let dt = p.to_datetime_with_timezone(&chrono_tz::Europe::Paris)?;

    // Convert to UTC
    Ok(dt.with_timezone(&Utc))
}

fn main() {
    let date = "27/08/2018";
    let hour = "12";

    println!("dt = {:?}", example(date, hour)); // Ok(2018-08-27T11:00:00Z)
}

我发现 chrono-tz 并发现它更易于使用。例如:

pub fn create_date_time_from_paris(date: NaiveDate, time: NaiveTime) -> DateTime<Utc> {
    let naive_datetime = NaiveDateTime::new(date, time);
    let paris_time = Paris.from_local_datetime(&naive_datetime).unwrap();
    paris_time.with_timezone(&Utc)
}