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
use std::{convert::TryInto, ops::Sub};
use crate::{time::Sign, Time};
impl Time {
pub fn new(seconds_since_unix_epoch: u32, offset_in_seconds: i32) -> Self {
Time {
seconds_since_unix_epoch,
offset_in_seconds,
sign: offset_in_seconds.into(),
}
}
pub fn now_utc() -> Self {
let seconds_since_unix_epoch = time::OffsetDateTime::now_utc()
.sub(std::time::SystemTime::UNIX_EPOCH)
.whole_seconds()
.try_into()
.expect("this is not year 2038");
Self {
seconds_since_unix_epoch,
offset_in_seconds: 0,
sign: Sign::Plus,
}
}
pub fn now_local() -> Option<Self> {
let now = time::OffsetDateTime::now_utc();
let seconds_since_unix_epoch = now
.sub(std::time::SystemTime::UNIX_EPOCH)
.whole_seconds()
.try_into()
.expect("this is not year 2038");
let offset_in_seconds = time::UtcOffset::local_offset_at(now).ok()?.whole_seconds();
Self {
seconds_since_unix_epoch,
offset_in_seconds,
sign: offset_in_seconds.into(),
}
.into()
}
pub fn now_local_or_utc() -> Self {
let now = time::OffsetDateTime::now_utc();
let seconds_since_unix_epoch = now
.sub(std::time::SystemTime::UNIX_EPOCH)
.whole_seconds()
.try_into()
.expect("this is not year 2038");
let offset_in_seconds = time::UtcOffset::local_offset_at(now)
.map(|ofs| ofs.whole_seconds())
.unwrap_or(0);
Self {
seconds_since_unix_epoch,
offset_in_seconds,
sign: offset_in_seconds.into(),
}
}
}