Convert time to timezone

    0

    0

    Giovanny Gongora

    Codiga's Rust Recipes

    from local to another timezone

    use chrono::{DateTime, FixedOffset, Local, Utc};
    
    fn main() {
      let local_time = Local::now();
      let utc_time = DateTime::<Utc>::from_utc(local_time.naive_utc(), Utc);
      let china_timezone = FixedOffset::east(8 * 3600);
      let rio_timezone = FixedOffset::west(2 * 3600);
      println!("Local time now is {}", local_time);
      println!("UTC time now is {}", utc_time);
      println!(
        "Time in Hong Kong now is {}",
        utc_time.with_timezone(&china_timezone)
      );
      println!("Time in Rio de Janeiro now is {}", utc_time.with_timezone(&rio_timezone));
    }
    
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    Legal
    • Security
    • Privacy Policy
    • Code Privacy
    • Terms of Service
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.