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
143
use crate::Format;
use std::error::Error;
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_date_time() -> DateTime {
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");
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>> {
OffsetDateTime::now_local()
.map(DateTime::Local)
.map_err(|e| e.into())
}
#[cfg(feature = "tzdb")]
pub fn local_now() -> Result<Self, Box<dyn Error>> {
let local_time = tzdb::now::local()?;
let time_zone_offset =
UtcOffset::from_whole_seconds(local_time.local_time_type().ut_offset())?;
let local_date_time = OffsetDateTime::from_unix_timestamp(local_time.unix_time())?
.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::*;
use regex::Regex;
#[test]
fn test_source_date_epoch() {
std::env::set_var("SOURCE_DATE_EPOCH", "1628080443");
let time = now_date_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());
let regex = Regex::new(
r#"^[0-9]{4}-[0-9]{2}-[0-9]{2}\s[0-9]{2}:[0-9]{2}:[0-9]{2}\s[+][0-9]{2}:[0-9]{2}"#,
)
.unwrap();
assert!(regex.is_match(&time));
println!("local now:{}", time); 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");
}
}