时间是什么?不同的人就会有不同的回答。古人根据日月星辰的运行规律,制定了历法。中国的 干支纪年,西方的公元纪年。再到近代的 格林尼治标准时间(Greenwich Mean Time,GMT) 直到现在基于原子钟的世界标准时间(世界协调时 UTC),以及计算机领域著名的UNIX时间 。
什么是计算机时间?
计算机时间到底是怎么来的?程序员必看的时间知识!
时间戳转换工具
如何计时、授时?
古代人们使用圭表、日晷、滴漏等工具计时;现代人们使用钟表、原子钟计时,并且可以无线电波、卫星、互联网等手段进行授时。
世界标准时间(世界协调时 UTC)
println!("Utc timestamp:{}", Utc::now().timestamp());
println!("Utc timestamp_millis:{}", Utc::now().timestamp_millis());
println!("Utc timestamp_nanos:{}", Utc::now().timestamp_nanos());
日期与字符串相互转换
// 1. DateTime -> &str (格式化输出日期)
let dt = Utc.ymd(2014, 11, 28).and_hms(12, 0, 9);
assert_eq!(
dt.format("%Y-%m-%d %H:%M:%S").to_string(),
"2014-11-28 12:00:09"
);
assert_eq!(dt.to_string(), "2014-11-28 12:00:09 UTC");
assert_eq!(dt.to_rfc2822(), "Fri, 28 Nov 2014 12:00:09 +0000");
assert_eq!(dt.to_rfc3339(), "2014-11-28T12:00:09+00:00");
assert_eq!(format!("{:?}", dt), "2014-11-28T12:00:09Z");
// 2. &str -> DateTime
let no_timezone = NaiveDateTime::parse_from_str("2015-09-05 23:56:04", "%Y-%m-%d %H:%M:%S")?;
println!("{}", no_timezone);
assert_eq!(NaiveDateTime::parse_from_str("2014-5-17T12:34:56+09:30", "%Y-%m-%dT%H:%M:%S%z"),
Ok(NaiveDate::from_ymd_opt(2014, 5, 17).unwrap().and_hms_opt(12, 34, 56).unwrap()));
时区
```text
UTC+3:30 伊朗德黑兰时间 (东3.5区时间)
UTC+5:30 印度新德里时间 (东5.5区时间)
UTC+8 中国北京时间 (东八区时间)
UTC+9 日本东京时间 (东九区时间)
UTC+10 (东10区时间)
UTC-5 美国东部时间(EST) (西五区时间)
UTC-8 美国太平洋标准时区(PST)(西八区时间)
```
UTC与本地时间
let local_time = Local::now();
let utc_time = DateTime::<Utc>::from_utc(local_time.naive_utc(), Utc);
let new_delhi_timezone = FixedOffset::east(5 * 3600 + 1800);
let china_timezone = FixedOffset::east(8 * 3600);
let japan_timezone = FixedOffset::east(9 * 3600);
let rio_timezone = FixedOffset::west(2 * 3600);
let est_timezone = FixedOffset::west(5 * 3600);
let pst_timezone = FixedOffset::west(8 * 3600);
println!("Local time now is {}", local_time);
println!("UTC time now is {}", utc_time);
// UTC --> Local
println!(
"Time in Bei Jing now is {}",
utc_time.with_timezone(&china_timezone)
);
println!(
"Time in Tokyo now is {}",
utc_time.with_timezone(&japan_timezone)
);
println!(
"Time in Rio de Janeiro now is {}",
utc_time.with_timezone(&rio_timezone)
);
println!(
"Time in New Delhi now is {}",
utc_time.with_timezone(&new_delhi_timezone)
);
println!(
"Time in EST now is {}",
utc_time.with_timezone(&est_timezone)
);
println!(
"Time in PST now is {}",
utc_time.with_timezone(&pst_timezone)
);
let local: DateTime<Local> = Local::now();
println!("{:?}", local.format("%Y-%m-%d %H:%M:%S").to_string());
// NaiveDateTime -> DateTime
let local_time = Local::now();
let dt = NaiveDate::from_ymd_opt(local_time.year(), local_time.month(), local_time.day())
.unwrap()
.and_hms_opt(local_time.hour(), local_time.minute(), local_time.second())
.unwrap();
持续时间/时间间隔( Duration)
fn day_earlier(date_time: DateTime<Utc>) -> Option<DateTime<Utc>> {
date_time.checked_sub_signed(Duration::days(1))
}
let almost_three_weeks_from_now = now
.checked_add_signed(Duration::weeks(2))
.and_then(|in_2weeks| in_2weeks.checked_add_signed(Duration::weeks(1)))
.and_then(day_earlier);
match almost_three_weeks_from_now {
Some(x) => println!("{}", x),
None => eprintln!("Almost three weeks from now overflows!"),
}
let today = Utc.ymd(2024,1,21);
let founding_date = Utc.ymd(1949,10,1);
println!("建国{}年",today.signed_duration_since(founding_date).num_days() / 365);