git_date/time/
format.rs

1use time::{format_description::FormatItem, macros::format_description};
2
3use crate::{time::Format, Time};
4
5/// E.g. `2018-12-24`
6pub const SHORT: &[FormatItem<'_>] = format_description!("[year]-[month]-[day]");
7
8/// E.g. `Thu, 18 Aug 2022 12:45:06 +0800`
9pub const RFC2822: &[FormatItem<'_>] = format_description!(
10    "[weekday repr:short], [day] [month repr:short] [year] [hour]:[minute]:[second] [offset_hour sign:mandatory][offset_minute]"
11);
12
13/// E.g. `Thu, 8 Aug 2022 12:45:06 +0800`. This is output by `git log --pretty=%aD`.
14pub const GIT_RFC2822: &[FormatItem<'_>] = format_description!(
15    "[weekday repr:short], \
16     [day padding:none] \
17     [month repr:short] \
18     [year] \
19     [hour]:[minute]:[second] \
20     [offset_hour sign:mandatory][offset_minute]"
21);
22
23/// E.g. `2022-08-17 22:04:58 +0200`
24pub const ISO8601: &[FormatItem<'_>] =
25    format_description!("[year]-[month]-[day] [hour]:[minute]:[second] [offset_hour sign:mandatory][offset_minute]");
26
27/// E.g. `2022-08-17T21:43:13+08:00`
28pub const ISO8601_STRICT: &[FormatItem<'_>] =
29    format_description!("[year]-[month]-[day]T[hour]:[minute]:[second][offset_hour sign:mandatory]:[offset_minute]");
30
31/// E.g. `123456789`
32pub const UNIX: Format<'static> = Format::Unix;
33
34/// E.g. `1660874655 +0800`
35pub const RAW: Format<'static> = Format::Raw;
36
37/// E.g. `Thu Sep 04 2022 10:45:06 -0400`, like the git `DEFAULT`, but with the year and time fields swapped.
38pub const GITOXIDE: &[FormatItem<'_>] = format_description!(
39    "[weekday repr:short] [month repr:short] [day] [year] [hour]:[minute]:[second] [offset_hour sign:mandatory][offset_minute]"
40);
41
42/// E.g. `Thu Sep 4 10:45:06 2022 -0400`. This is output by `git log --pretty=%ad`.
43pub const DEFAULT: &[FormatItem<'_>] = format_description!(
44    "[weekday repr:short] \
45     [month repr:short] \
46     [day padding:none] \
47     [hour]:[minute]:[second] \
48     [year] \
49     [offset_hour sign:mandatory][offset_minute]"
50);
51
52mod format_impls {
53    use time::format_description::FormatItem;
54
55    use crate::time::Format;
56
57    impl<'a> From<&'a [FormatItem<'a>]> for Format<'a> {
58        fn from(f: &'a [FormatItem<'a>]) -> Self {
59            Format::Custom(f)
60        }
61    }
62}
63
64/// Formatting
65impl Time {
66    /// Format this instance according to the given `format`.
67    ///
68    /// Use the [`format_description`](https://time-rs.github.io/book/api/format-description.html) macro to create and
69    /// validate formats at compile time, courtesy of the [`time`] crate.
70    pub fn format<'a>(&self, format: impl Into<Format<'a>>) -> String {
71        match format.into() {
72            Format::Custom(format) => self
73                .to_time()
74                .format(&format)
75                .expect("well-known format into memory never fails"),
76            Format::Unix => self.seconds_since_unix_epoch.to_string(),
77            Format::Raw => self.to_bstring().to_string(),
78        }
79    }
80}
81
82impl Time {
83    fn to_time(self) -> time::OffsetDateTime {
84        time::OffsetDateTime::from_unix_timestamp(self.seconds_since_unix_epoch as i64)
85            .expect("always valid unix time")
86            .to_offset(time::UtcOffset::from_whole_seconds(self.offset_in_seconds).expect("valid offset"))
87    }
88}