-
Notifications
You must be signed in to change notification settings - Fork 41
/
data_time.rs
142 lines (124 loc) · 4.55 KB
/
data_time.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
use crate::Format;
use std::error::Error;
#[cfg(feature = "tzdb")]
use std::time::{SystemTime, UNIX_EPOCH};
use time::format_description::well_known::{Rfc2822, Rfc3339};
#[cfg(feature = "tzdb")]
use time::UtcOffset;
use time::{format_description, OffsetDateTime};
pub enum DateTime {
Local(OffsetDateTime),
Utc(OffsetDateTime),
}
pub fn now_data_time() -> DateTime {
// Enable reproducibility for uses of `now_data_time` by respecting the
// `SOURCE_DATE_EPOCH` env variable.
//
// https://reproducible-builds.org/docs/source-date-epoch/
println!("cargo:rerun-if-env-changed=SOURCE_DATE_EPOCH");
match std::env::var_os("SOURCE_DATE_EPOCH") {
None => DateTime::now(),
Some(timestamp) => {
let epoch = timestamp
.into_string()
.expect("Input SOURCE_DATE_EPOCH could not be parsed")
.parse::<i64>()
.expect("Input SOURCE_DATE_EPOCH could not be cast to a number");
// BuildTime::Utc(Utc.timestamp(epoch, 0))
DateTime::Utc(OffsetDateTime::from_unix_timestamp(epoch).unwrap())
}
}
}
impl Default for DateTime {
fn default() -> Self {
Self::now()
}
}
impl DateTime {
pub fn now() -> Self {
Self::local_now().unwrap_or_else(|_| DateTime::Utc(OffsetDateTime::now_utc()))
}
pub fn offset_datetime() -> OffsetDateTime {
let date_time = Self::now();
match date_time {
DateTime::Local(time) | DateTime::Utc(time) => time,
}
}
#[cfg(not(feature = "tzdb"))]
pub fn local_now() -> Result<Self, Box<dyn Error>> {
// Warning: Attempt to create a new OffsetDateTime with the current date and time in the local offset. If the offset cannot be determined, an error is returned.
// At present, using it on MacOS return error. Use it with careful.
// Suggestion use feature tzdb crate exposed function at below.
OffsetDateTime::now_local()
.map(DateTime::Local)
.map_err(|e| e.into())
}
#[cfg(feature = "tzdb")]
pub fn local_now() -> Result<Self, Box<dyn Error>> {
// expensive call, should be cached
let time_zone_local = tzdb::local_tz().ok_or("Could not get local timezone")?;
let duration_since_epoch = SystemTime::now().duration_since(UNIX_EPOCH)?;
let local_time_type =
time_zone_local.find_local_time_type(duration_since_epoch.as_secs().try_into()?)?;
let time_zone_offset = UtcOffset::from_whole_seconds(local_time_type.ut_offset())?;
let local_date_time =
(OffsetDateTime::UNIX_EPOCH + duration_since_epoch).to_offset(time_zone_offset);
Ok(DateTime::Local(local_date_time))
}
pub fn timestamp_2_utc(time_stamp: i64) -> Self {
let time = OffsetDateTime::from_unix_timestamp(time_stamp).unwrap();
DateTime::Utc(time)
}
pub fn to_rfc2822(&self) -> String {
match self {
DateTime::Local(dt) | DateTime::Utc(dt) => dt.format(&Rfc2822).unwrap(),
}
}
pub fn to_rfc3339(&self) -> String {
match self {
DateTime::Local(dt) | DateTime::Utc(dt) => dt.format(&Rfc3339).unwrap(),
}
}
}
impl Format for DateTime {
fn human_format(&self) -> String {
match self {
DateTime::Local(dt) | DateTime::Utc(dt) => dt.human_format(),
}
}
}
impl Format for OffsetDateTime {
fn human_format(&self) -> String {
let fmt = format_description::parse(
"[year]-[month]-[day] [hour]:[minute]:[second] [offset_hour \
sign:mandatory]:[offset_minute]",
)
.unwrap();
self.format(&fmt).unwrap()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_source_date_epoch() {
std::env::set_var("SOURCE_DATE_EPOCH", "1628080443");
let time = now_data_time();
assert_eq!(time.human_format(), "2021-08-04 12:34:03 +00:00");
}
#[test]
fn test_local_now_human_format() {
let time = DateTime::local_now().unwrap().human_format();
#[cfg(unix)]
assert!(!std::fs::read("/etc/localtime").unwrap().is_empty());
println!("local now:{}", time); // 2022-07-14 00:40:05 +08:00
assert_eq!(time.len(), 26);
}
#[test]
fn test_timestamp_2_utc() {
let time = DateTime::timestamp_2_utc(1628080443);
assert_eq!(time.to_rfc2822(), "Wed, 04 Aug 2021 12:34:03 +0000");
assert_eq!(time.to_rfc3339(), "2021-08-04T12:34:03Z");
assert_eq!(time.human_format(), "2021-08-04 12:34:03 +00:00");
}
}