use crate::{
time::{CustomFormat, Format},
Time,
};
pub const SHORT: CustomFormat = CustomFormat("%Y-%m-%d");
pub const RFC2822: CustomFormat = CustomFormat("%a, %d %b %Y %H:%M:%S %z");
pub const GIT_RFC2822: CustomFormat = CustomFormat("%a, %-d %b %Y %H:%M:%S %z");
pub const ISO8601: CustomFormat = CustomFormat("%Y-%m-%d %H:%M:%S %z");
pub const ISO8601_STRICT: CustomFormat = CustomFormat("%Y-%m-%dT%H:%M:%S%:z");
pub const UNIX: Format = Format::Unix;
pub const RAW: Format = Format::Raw;
pub const GITOXIDE: CustomFormat = CustomFormat("%a %b %d %Y %H:%M:%S %z");
pub const DEFAULT: CustomFormat = CustomFormat("%a %b %-d %H:%M:%S %Y %z");
impl Time {
pub fn format(&self, format: impl Into<Format>) -> String {
self.format_inner(format.into())
}
fn format_inner(&self, format: Format) -> String {
match format {
Format::Custom(CustomFormat(format)) => self.to_time().strftime(format).to_string(),
Format::Unix => self.seconds.to_string(),
Format::Raw => self.to_bstring().to_string(),
}
}
}
impl Time {
fn to_time(self) -> jiff::Zoned {
let offset = jiff::tz::Offset::from_seconds(self.offset).expect("valid offset");
jiff::Timestamp::from_second(self.seconds)
.expect("always valid unix time")
.to_zoned(offset.to_time_zone())
}
}