#timezone #iata #time #zone #codes #mapping #airport

airports

提供IATA机场代码与时区之间的映射

2个版本

0.0.2 2022年6月28日
0.0.1 2022年6月28日

#44#zone

MIT/Apache

47KB

airports

将机场IATA代码映射到时区的库

示例

use airports::Airports;

fn main() {
    let db = Airports::new();
    println!("lhr: {:?}", db.get_tz_name("lhr"));
    println!("LHR: {:?}", db.get_tz_name("LHR"));
    println!("SOMETHING: {:?}", db.get_tz_name("SOMETHING"));
}

打印出

lhr: Some("Europe/London")
LHR: Some("Europe/London")
SOMETHING: None

也可以直接将IATA代码映射到IANA时区,并以chrono_tz Tz的形式包装在选项中,以便使用。

use airports::Airports;
use chrono::{DateTime, TimeZone, Utc};
use chrono_tz::Tz;

fn main() {
    let db = Airports::new();
    println!("SFO timezone: {:?}", db.get_tz_name("sfo").unwrap());

    match db.get_tz("SFO") {
        Some(t) => {
            let x: DateTime<Tz> = Utc::now().with_timezone(&t);
            println!("Current time in SFO: {}", x);
        }
        None => println!("Not found!"),
    }
}
SFO timezone: "America/Los_Angeles"
Current time in SFO: 2022-06-28 08:31:43.613397 PDT

依赖项

~3–4MB
~67K SLoC