Movatterモバイル変換


[0]ホーム

URL:


Rust Cookbook

    Parsing and Displaying

    Examine the date and time

    chrono-badgecat-date-and-time-badge

    Gets the current UTCDateTime and its hour/minute/second viaTimelikeand its year/month/day/weekday viaDatelike.

    use chrono::{Datelike, Timelike, Utc};fn main() {    let now = Utc::now();    let (is_pm, hour) = now.hour12();    println!(        "The current UTC time is {:02}:{:02}:{:02} {}",        hour,        now.minute(),        now.second(),        if is_pm { "PM" } else { "AM" }    );    println!(        "And there have been {} seconds since midnight",        now.num_seconds_from_midnight()    );    let (is_common_era, year) = now.year_ce();    println!(        "The current UTC date is {}-{:02}-{:02} {:?} ({})",        year,        now.month(),        now.day(),        now.weekday(),        if is_common_era { "CE" } else { "BCE" }    );    println!(        "And the Common Era began {} days ago",        now.num_days_from_ce()    );}

    Convert date to UNIX timestamp and vice versa

    chrono-badgecat-date-and-time-badge

    Converts a date given byNaiveDate::from_ymd andNaiveTime::from_hmstoUNIX timestamp usingNaiveDateTime::timestamp.Then it calculates what was the date after one billion secondssince January 1, 1970 0:00:00 UTC, usingNaiveDateTime::from_timestamp.

    use chrono::{NaiveDate, NaiveDateTime};fn main() {    let date_time: NaiveDateTime = NaiveDate::from_ymd(2017, 11, 12).and_hms(17, 33, 44);    println!(        "Number of seconds between 1970-01-01 00:00:00 and {} is {}.",        date_time, date_time.timestamp());    let date_time_after_a_billion_seconds = NaiveDateTime::from_timestamp(1_000_000_000, 0);    println!(        "Date after a billion seconds since 1970-01-01 00:00:00 was {}.",        date_time_after_a_billion_seconds);}

    Display formatted date and time

    chrono-badgecat-date-and-time-badge

    Gets and displays the current time in UTC usingUtc::now. Formats thecurrent time in the well-known formatsRFC 2822 usingDateTime::to_rfc2822andRFC 3339 usingDateTime::to_rfc3339, and in a custom format usingDateTime::format.

    use chrono::{DateTime, Utc};fn main() {    let now: DateTime<Utc> = Utc::now();    println!("UTC now is: {}", now);    println!("UTC now in RFC 2822 is: {}", now.to_rfc2822());    println!("UTC now in RFC 3339 is: {}", now.to_rfc3339());    println!("UTC now in a custom format is: {}", now.format("%a %b %e %T %Y"));}

    Parse string into DateTime struct

    chrono-badgecat-date-and-time-badge

    Parses aDateTime struct from strings representing the well-known formatsRFC 2822,RFC 3339, and a custom format, usingDateTime::parse_from_rfc2822,DateTime::parse_from_rfc3339, andDateTime::parse_from_str respectively.

    Escape sequences that are available for theDateTime::parse_from_str can befound atchrono::format::strftime. Note that theDateTime::parse_from_strrequires that such a DateTime struct must be creatable that it uniquelyidentifies a date and a time. For parsing dates and times without timezones useNaiveDate,NaiveTime, andNaiveDateTime.

    use chrono::{DateTime, NaiveDate, NaiveDateTime, NaiveTime};use chrono::format::ParseError;fn main() -> Result<(), ParseError> {    let rfc2822 = DateTime::parse_from_rfc2822("Tue, 1 Jul 2003 10:52:37 +0200")?;    println!("{}", rfc2822);    let rfc3339 = DateTime::parse_from_rfc3339("1996-12-19T16:39:57-08:00")?;    println!("{}", rfc3339);    let custom = DateTime::parse_from_str("5.8.1994 8:00 am +0000", "%d.%m.%Y %H:%M %P %z")?;    println!("{}", custom);    let time_only = NaiveTime::parse_from_str("23:56:04", "%H:%M:%S")?;    println!("{}", time_only);    let date_only = NaiveDate::parse_from_str("2015-09-05", "%Y-%m-%d")?;    println!("{}", date_only);    let no_timezone = NaiveDateTime::parse_from_str("2015-09-05 23:56:04", "%Y-%m-%d %H:%M:%S")?;    println!("{}", no_timezone);    Ok(())}

    [8]ページ先頭

    ©2009-2025 Movatter.jp