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 crate::time::Format;
use crate::Time;
use time::format_description::FormatItem;
use time::macros::format_description;
pub const SHORT: &[FormatItem<'_>] = format_description!("[year]-[month]-[day]");
pub const RFC2822: &[FormatItem<'_>] = format_description!(
"[weekday repr:short], [day] [month repr:short] [year] [hour]:[minute]:[second] [offset_hour sign:mandatory][offset_minute]"
);
pub const ISO8601: &[FormatItem<'_>] =
format_description!("[year]-[month]-[day] [hour]:[minute]:[second] [offset_hour sign:mandatory][offset_minute]");
pub const ISO8601_STRICT: &[FormatItem<'_>] =
format_description!("[year]-[month]-[day]T[hour]:[minute]:[second][offset_hour sign:mandatory]:[offset_minute]");
pub const UNIX: Format<'static> = Format::Unix;
pub const RAW: Format<'static> = Format::Raw;
pub const DEFAULT: &[FormatItem<'_>] = format_description!(
"[weekday repr:short] [month repr:short] [day] [year] [hour]:[minute]:[second] [offset_hour sign:mandatory][offset_minute]"
);
mod format_impls {
use crate::time::Format;
use time::format_description::FormatItem;
impl<'a> From<&'a [FormatItem<'a>]> for Format<'a> {
fn from(f: &'a [FormatItem<'a>]) -> Self {
Format::Custom(f)
}
}
}
impl Time {
pub fn format<'a>(&self, format: impl Into<Format<'a>>) -> String {
match format.into() {
Format::Custom(format) => self
.to_time()
.format(&format)
.expect("well-known format into memory never fails"),
Format::Unix => self.seconds_since_unix_epoch.to_string(),
Format::Raw => self.to_bstring().to_string(),
}
}
}
impl Time {
fn to_time(self) -> time::OffsetDateTime {
time::OffsetDateTime::from_unix_timestamp(self.seconds_since_unix_epoch as i64)
.expect("always valid unix time")
.replace_offset(time::UtcOffset::from_whole_seconds(self.offset_in_seconds).expect("valid offset"))
}
}