Rust:chrono Library, Date,time and other _rust

Source: Internet
Author: User
Tags assert datetime

Parsing can do with three methods:

The standard fromstr trait (and parse method on a string) can is used for parsing datetime, datetime and datetime values. This parses what the {:?} (std::fmt::D ebug) format specifier prints, and requires the offset to is present.

DateTime::p arse_from_str parses a date and time with offsets and returns DateTime. This should was used when the "offset is" a part of input and the caller cannot guess that. It cannot be used as the offset can be missing. DateTime::p arse_from_rfc2822 and datetime::p arse_from_rfc3339 are similar but for well-known.

Offset::d atetime_from_str is similar but returns datetime of given offset. When the explicit offset is missing from the input, it simply uses given offset. It issues an error when the input contains a explicit offset different from the current offset.

Use Chrono::p relude::*;
Let DT = Utc.ymd (2014, each). And_hms (12, 0, 9);

Let Fixed_dt = Dt.with_timezone (&fixedoffset::east (9*3600)); Method 1 assert_eq!
("2014-11-28t12:00:09z" .parse::<datetime<utc>> (), OK (Dt.clone ())); assert_eq!
("2014-11-28t21:00:09+09:00" .parse::<datetime<utc>> (), OK (Dt.clone ())); assert_eq!

("2014-11-28t21:00:09+09:00" .parse::<datetime<fixedoffset>> (), OK (Fixed_dt.clone ())); Method 2 assert_eq!
(DateTime::p arse_from_str ("2014-11-28 21:00:09 +09:00", "%y-%m-%d%h:%m:%s%z"), OK (Fixed_dt.clone ()); assert_eq!
(DateTime::p arse_from_rfc2822 ("Fri, Nov 2014 21:00:09 +0900"), Ok (Fixed_dt.clone ()); assert_eq!

(DateTime::p arse_from_rfc3339 ("2014-11-28t21:00:09+09:00"), OK (Fixed_dt.clone ()); Method 3 assert_eq!
(Utc.datetime_from_str ("2014-11-28 12:00:09", "%y-%m-%d%h:%m:%s"), OK (Dt.clone ()); assert_eq!

(Utc.datetime_from_str ("Fri Nov 12:00:09 2014",%a%b%e%T%Y), OK (Dt.clone ()); OopsThe year is missing! Assert!
(Utc.datetime_from_str ("Fri Nov 12:00:09",%a%b%e%T).%Y ());
Oops, the format string does not include the ' year ' at all! Assert!
(Utc.datetime_from_str ("Fri Nov 12:00:09",%a%b%e%T). Is_err ());
Oops, the weekday is incorrect! Assert! (Utc.datetime_from_str ("Sat Nov 12:00:09 2014",%a%b%e%T).%Y ());

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.