|
1 |
| -use time::{format_description::FormatItem, macros::format_description}; |
| 1 | +use time::macros::format_description; |
2 | 2 |
|
3 |
| -use crate::{time::Format, Time}; |
| 3 | +use crate::{ |
| 4 | + time::{CustomFormat, Format}, |
| 5 | + Time, |
| 6 | +}; |
4 | 7 |
|
5 | 8 | /// E.g. `2018-12-24`
|
6 |
| -pub const SHORT: &[FormatItem<'_>] = format_description!("[year]-[month]-[day]"); |
| 9 | +pub const SHORT: CustomFormat = CustomFormat(format_description!("[year]-[month]-[day]")); |
7 | 10 |
|
8 | 11 | /// E.g. `Thu, 18 Aug 2022 12:45:06 +0800`
|
9 |
| -pub const RFC2822: &[FormatItem<'_>] = format_description!( |
| 12 | +pub const RFC2822: CustomFormat = CustomFormat(format_description!( |
10 | 13 | "[weekday repr:short], [day] [month repr:short] [year] [hour]:[minute]:[second] [offset_hour sign:mandatory][offset_minute]"
|
11 |
| -); |
| 14 | +)); |
12 | 15 |
|
13 | 16 | /// E.g. `Thu, 8 Aug 2022 12:45:06 +0800`. This is output by `git log --pretty=%aD`.
|
14 |
| -pub const GIT_RFC2822: &[FormatItem<'_>] = format_description!( |
| 17 | +pub const GIT_RFC2822: CustomFormat = CustomFormat(format_description!( |
15 | 18 | "[weekday repr:short], \
|
16 | 19 | [day padding:none] \
|
17 | 20 | [month repr:short] \
|
18 | 21 | [year] \
|
19 | 22 | [hour]:[minute]:[second] \
|
20 | 23 | [offset_hour sign:mandatory][offset_minute]"
|
21 |
| -); |
| 24 | +)); |
22 | 25 |
|
23 | 26 | /// E.g. `2022-08-17 22:04:58 +0200`
|
24 |
| -pub const ISO8601: &[FormatItem<'_>] = |
25 |
| - format_description!("[year]-[month]-[day] [hour]:[minute]:[second] [offset_hour sign:mandatory][offset_minute]"); |
| 27 | +pub const ISO8601: CustomFormat = CustomFormat(format_description!( |
| 28 | + "[year]-[month]-[day] [hour]:[minute]:[second] [offset_hour sign:mandatory][offset_minute]" |
| 29 | +)); |
26 | 30 |
|
27 | 31 | /// E.g. `2022-08-17T21:43:13+08:00`
|
28 |
| -pub const ISO8601_STRICT: &[FormatItem<'_>] = |
29 |
| - format_description!("[year]-[month]-[day]T[hour]:[minute]:[second][offset_hour sign:mandatory]:[offset_minute]"); |
| 32 | +pub const ISO8601_STRICT: CustomFormat = CustomFormat(format_description!( |
| 33 | + "[year]-[month]-[day]T[hour]:[minute]:[second][offset_hour sign:mandatory]:[offset_minute]" |
| 34 | +)); |
30 | 35 |
|
31 | 36 | /// E.g. `123456789`
|
32 |
| -pub const UNIX: Format<'static> = Format::Unix; |
| 37 | +pub const UNIX: Format = Format::Unix; |
33 | 38 |
|
34 | 39 | /// E.g. `1660874655 +0800`
|
35 |
| -pub const RAW: Format<'static> = Format::Raw; |
| 40 | +pub const RAW: Format = Format::Raw; |
36 | 41 |
|
37 | 42 | /// E.g. `Thu Sep 04 2022 10:45:06 -0400`, like the git `DEFAULT`, but with the year and time fields swapped.
|
38 |
| -pub const GITOXIDE: &[FormatItem<'_>] = format_description!( |
| 43 | +pub const GITOXIDE: CustomFormat = CustomFormat(format_description!( |
39 | 44 | "[weekday repr:short] [month repr:short] [day] [year] [hour]:[minute]:[second] [offset_hour sign:mandatory][offset_minute]"
|
40 |
| -); |
| 45 | +)); |
41 | 46 |
|
42 | 47 | /// E.g. `Thu Sep 4 10:45:06 2022 -0400`. This is output by `git log --pretty=%ad`.
|
43 |
| -pub const DEFAULT: &[FormatItem<'_>] = format_description!( |
| 48 | +pub const DEFAULT: CustomFormat = CustomFormat(format_description!( |
44 | 49 | "[weekday repr:short] \
|
45 | 50 | [month repr:short] \
|
46 | 51 | [day padding:none] \
|
47 | 52 | [hour]:[minute]:[second] \
|
48 | 53 | [year] \
|
49 | 54 | [offset_hour sign:mandatory][offset_minute]"
|
50 |
| -); |
51 |
| - |
52 |
| -mod 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 |
| -} |
| 55 | +)); |
63 | 56 |
|
64 | 57 | /// Formatting
|
65 | 58 | impl Time {
|
66 | 59 | /// Format this instance according to the given `format`.
|
67 | 60 | ///
|
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 { |
| 61 | + /// Use [`Format::Unix`], [`Format::Raw`] or one of the custom formats |
| 62 | + /// defined in the [`format`](mod@crate::time::format) submodule. |
| 63 | + pub fn format(&self, format: impl Into<Format>) -> String { |
71 | 64 | self.format_inner(format.into())
|
72 | 65 | }
|
73 | 66 |
|
74 |
| - fn format_inner(&self, format: Format<'_>) -> String { |
| 67 | + fn format_inner(&self, format: Format) -> String { |
75 | 68 | match format {
|
76 |
| - Format::Custom(format) => self |
| 69 | + Format::Custom(CustomFormat(format)) => self |
77 | 70 | .to_time()
|
78 |
| - .format(&format) |
| 71 | + .format(format) |
79 | 72 | .expect("well-known format into memory never fails"),
|
80 | 73 | Format::Unix => self.seconds.to_string(),
|
81 | 74 | Format::Raw => self.to_bstring().to_string(),
|
|
0 commit comments