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    ///
40    /// Note that this can fail if the timezone isn't valid and the format requires a conversion to [`jiff::Zoned`].
41    pub fn format(&self, format: impl Into<Format>) -> Result<String, jiff::Error> {
42        self.format_inner(format.into())
43    }
44
45    /// Like [`Self::format()`], but on time conversion error, produce the [UNIX] format instead
46    /// to make it infallible.
47    pub fn format_or_unix(&self, format: impl Into<Format>) -> String {
48        self.format_inner(format.into())
49            .unwrap_or_else(|_| self.seconds.to_string())
50    }
51
52    fn format_inner(&self, format: Format) -> Result<String, jiff::Error> {
53        Ok(match format {
54            Format::Custom(CustomFormat(format)) => self.to_zoned()?.strftime(format).to_string(),
55            Format::Unix => self.seconds.to_string(),
56            Format::Raw => self.to_string(),
57        })
58    }
59}
60
61impl Time {
62    /// Produce a `Zoned` time for complex time computations and limitless formatting.
63    pub fn to_zoned(self) -> Result<jiff::Zoned, jiff::Error> {
64        let offset = jiff::tz::Offset::from_seconds(self.offset)?;
65        Ok(jiff::Timestamp::from_second(self.seconds)?.to_zoned(offset.to_time_zone()))
66    }
67}