1use crate::{
2 time::{CustomFormat, Format},
3 Time,
4};
5
6pub const SHORT: CustomFormat = CustomFormat("%Y-%m-%d");
8
9pub const RFC2822: CustomFormat = CustomFormat("%a, %d %b %Y %H:%M:%S %z");
11
12pub const GIT_RFC2822: CustomFormat = CustomFormat("%a, %-d %b %Y %H:%M:%S %z");
14
15pub const ISO8601: CustomFormat = CustomFormat("%Y-%m-%d %H:%M:%S %z");
17
18pub const ISO8601_STRICT: CustomFormat = CustomFormat("%Y-%m-%dT%H:%M:%S%:z");
20
21pub const UNIX: Format = Format::Unix;
23
24pub const RAW: Format = Format::Raw;
26
27pub const GITOXIDE: CustomFormat = CustomFormat("%a %b %d %Y %H:%M:%S %z");
29
30pub const DEFAULT: CustomFormat = CustomFormat("%a %b %-d %H:%M:%S %Y %z");
32
33impl Time {
35 pub fn format(&self, format: impl Into<Format>) -> String {
40 self.format_inner(format.into())
41 }
42
43 fn format_inner(&self, format: Format) -> String {
44 match format {
45 Format::Custom(CustomFormat(format)) => self.to_time().strftime(format).to_string(),
46 Format::Unix => self.seconds.to_string(),
47 Format::Raw => self.to_bstring().to_string(),
48 }
49 }
50}
51
52impl Time {
53 fn to_time(self) -> jiff::Zoned {
54 let offset = jiff::tz::Offset::from_seconds(self.offset).expect("valid offset");
55 jiff::Timestamp::from_second(self.seconds)
56 .expect("always valid unix time")
57 .to_zoned(offset.to_time_zone())
58 }
59}