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

前端 未结 3 472
执笔经年
执笔经年 2021-01-04 21:52

I am parsing dates and times in Rust using the chrono crate. The dates and times are from a website in which the date and time are from different sections of the page.

相关标签:
3条回答
  • 2021-01-04 21:53

    I've discovered chrono-tz and found it much easier to use. For example:

    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)
    }
    
    0 讨论(0)
  • 2021-01-04 21:57

    The Chrono documentation could probably be improved to make it easier to find how to do these things.

    Assuming this is your starting point:

    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);
    

    You can then use the FixedOffset to construct a DateTime:

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

    If you need to convert it to a DateTime<Utc>, you can do this:

    let dt_with_tz_utc: DateTime<Utc> = Utc.from_utc_datetime(&dt_with_tz.naive_utc());
    
    0 讨论(0)
  • 2021-01-04 22:15

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

    Here's an example of how you can incrementally parse multiple values from distinct strings, provide default values for unparsed information, and use Chrono's built-in timezone conversion.

    The key is to use the parse function to update a Parsed struct. You can use the StrftimeItems iterator to continue to use more readable format strings.

    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)
    }
    
    0 讨论(0)
提交回复
热议问题