gix_date/time/
format.rs

1use crate::{
2    time::{CustomFormat, Format},
3    Time,
4};
5
6/// E.g. `2018-12-24`
7pub const SHORT: CustomFormat = CustomFormat("%Y-%m-%d");
8
9/// E.g. `Thu, 18 Aug 2022 12:45:06 +0800`
10pub const RFC2822: CustomFormat = CustomFormat("%a, %d %b %Y %H:%M:%S %z");
11
12/// E.g. `Thu, 8 Aug 2022 12:45:06 +0800`. This is output by `git log --pretty=%aD`.
13pub const GIT_RFC2822: CustomFormat = CustomFormat("%a, %-d %b %Y %H:%M:%S %z");
14
15/// E.g. `2022-08-17 22:04:58 +0200`
16pub const ISO8601: CustomFormat = CustomFormat("%Y-%m-%d %H:%M:%S %z");
17
18/// E.g. `2022-08-17T21:43:13+08:00`
19pub const ISO8601_STRICT: CustomFormat = CustomFormat("%Y-%m-%dT%H:%M:%S%:z");
20
21/// E.g. `123456789`
22pub const UNIX: Format = Format::Unix;
23
24/// E.g. `1660874655 +0800`
25pub const RAW: Format = Format::Raw;
26
27/// E.g. `Thu Sep 04 2022 10:45:06 -0400`, like the git `DEFAULT`, but with the year and time fields swapped.
28pub const GITOXIDE: CustomFormat = CustomFormat("%a %b %d %Y %H:%M:%S %z");
29
30/// E.g. `Thu Sep 4 10:45:06 2022 -0400`. This is output by `git log --pretty=%ad`.
31pub const DEFAULT: CustomFormat = CustomFormat("%a %b %-d %H:%M:%S %Y %z");
32
33/// Formatting
34impl Time {
35    /// Format this instance according to the given `format`.
36    ///
37    /// Use [`Format::Unix`], [`Format::Raw`] or one of the custom formats
38    /// defined in the [`format`](mod@crate::time::format) submodule.
39    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}