/
timestamp.rs
56 lines (47 loc) · 1.31 KB
/
timestamp.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
use chrono::{DateTime, NaiveDateTime, TimeZone, Utc};
use std::{convert::TryInto, time::SystemTime};
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub struct Timestamp(i64);
impl Timestamp {
pub fn from_rfc3339(s: &str) -> anyhow::Result<Self> {
Ok(Self(DateTime::parse_from_rfc3339(s)?.timestamp()))
}
pub fn now() -> anyhow::Result<Self> {
Ok(Self(
SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)?
.as_secs()
.try_into()?,
))
}
pub fn to_rfc3339(&self) -> String {
Utc.from_utc_datetime(&NaiveDateTime::from_timestamp(self.0, 0))
.to_rfc3339_opts(chrono::SecondsFormat::Secs, true)
}
}
impl From<i64> for Timestamp {
fn from(timestamp: i64) -> Self {
Self(timestamp)
}
}
impl From<Timestamp> for i64 {
fn from(timestamp: Timestamp) -> Self {
timestamp.0
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn i64_convert_test() {
assert_eq!(i64::from(Timestamp::from(100_i64)), 100_i64);
}
#[test]
fn string_convert_test() -> anyhow::Result<()> {
assert_eq!(
Timestamp::from_rfc3339("2021-07-03T08:46:05+09:00")?.to_rfc3339(),
"2021-07-02T23:46:05Z"
);
Ok(())
}
}