Skip to content

Commit 5a88413

Browse files
committed
gix-date: make time a private dependency of gix-date
This should make the swap from `time` to `jiff` easier. This comment[1] indicates that it's okay for `time` to be a public dependency, but since this patch series is about swapping `time` for `jiff`, it seemed appropriate to take this step first. And in particular, it was *almost* already the case that `time` was a private dependency of `gix-date`. The only thing we really had to button up was the exposure of `time`'s custom formatting description language. Jiff doesn't support `time`'s custom formatting machinery and instead uses a strftime/strptime like API. We could expose that instead, but since nothing (other than a test) was actually utilizing `time`'s custom formatting machinery external to `gix-date`, I figured we might as well completely encapsulate it. [1]: #471 (comment)
1 parent 55cffe4 commit 5a88413

File tree

4 files changed

+47
-52
lines changed

4 files changed

+47
-52
lines changed

gix-date/src/parse.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,18 @@ pub(crate) mod function {
3232
return Ok(Time::new(42, 1800));
3333
}
3434

35-
Ok(if let Ok(val) = Date::parse(input, SHORT) {
35+
Ok(if let Ok(val) = Date::parse(input, SHORT.0) {
3636
let val = val.with_hms(0, 0, 0).expect("date is in range").assume_utc();
3737
Time::new(val.unix_timestamp(), val.offset().whole_seconds())
3838
} else if let Ok(val) = OffsetDateTime::parse(input, &well_known::Rfc2822) {
3939
Time::new(val.unix_timestamp(), val.offset().whole_seconds())
40-
} else if let Ok(val) = OffsetDateTime::parse(input, ISO8601) {
40+
} else if let Ok(val) = OffsetDateTime::parse(input, ISO8601.0) {
4141
Time::new(val.unix_timestamp(), val.offset().whole_seconds())
42-
} else if let Ok(val) = OffsetDateTime::parse(input, ISO8601_STRICT) {
42+
} else if let Ok(val) = OffsetDateTime::parse(input, ISO8601_STRICT.0) {
4343
Time::new(val.unix_timestamp(), val.offset().whole_seconds())
44-
} else if let Ok(val) = OffsetDateTime::parse(input, GITOXIDE) {
44+
} else if let Ok(val) = OffsetDateTime::parse(input, GITOXIDE.0) {
4545
Time::new(val.unix_timestamp(), val.offset().whole_seconds())
46-
} else if let Ok(val) = OffsetDateTime::parse(input, DEFAULT) {
46+
} else if let Ok(val) = OffsetDateTime::parse(input, DEFAULT.0) {
4747
Time::new(val.unix_timestamp(), val.offset().whole_seconds())
4848
} else if let Ok(val) = SecondsSinceUnixEpoch::from_str(input) {
4949
// Format::Unix

gix-date/src/time/format.rs

Lines changed: 28 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,74 @@
1-
use time::{format_description::FormatItem, macros::format_description};
1+
use time::macros::format_description;
22

3-
use crate::{time::Format, Time};
3+
use crate::{
4+
time::{CustomFormat, Format},
5+
Time,
6+
};
47

58
/// 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]"));
710

811
/// 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!(
1013
"[weekday repr:short], [day] [month repr:short] [year] [hour]:[minute]:[second] [offset_hour sign:mandatory][offset_minute]"
11-
);
14+
));
1215

1316
/// 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!(
1518
"[weekday repr:short], \
1619
[day padding:none] \
1720
[month repr:short] \
1821
[year] \
1922
[hour]:[minute]:[second] \
2023
[offset_hour sign:mandatory][offset_minute]"
21-
);
24+
));
2225

2326
/// 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+
));
2630

2731
/// 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+
));
3035

3136
/// E.g. `123456789`
32-
pub const UNIX: Format<'static> = Format::Unix;
37+
pub const UNIX: Format = Format::Unix;
3338

3439
/// E.g. `1660874655 +0800`
35-
pub const RAW: Format<'static> = Format::Raw;
40+
pub const RAW: Format = Format::Raw;
3641

3742
/// 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!(
3944
"[weekday repr:short] [month repr:short] [day] [year] [hour]:[minute]:[second] [offset_hour sign:mandatory][offset_minute]"
40-
);
45+
));
4146

4247
/// 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!(
4449
"[weekday repr:short] \
4550
[month repr:short] \
4651
[day padding:none] \
4752
[hour]:[minute]:[second] \
4853
[year] \
4954
[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+
));
6356

6457
/// Formatting
6558
impl Time {
6659
/// Format this instance according to the given `format`.
6760
///
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 {
7164
self.format_inner(format.into())
7265
}
7366

74-
fn format_inner(&self, format: Format<'_>) -> String {
67+
fn format_inner(&self, format: Format) -> String {
7568
match format {
76-
Format::Custom(format) => self
69+
Format::Custom(CustomFormat(format)) => self
7770
.to_time()
78-
.format(&format)
71+
.format(format)
7972
.expect("well-known format into memory never fails"),
8073
Format::Unix => self.seconds.to_string(),
8174
Format::Raw => self.to_bstring().to_string(),

gix-date/src/time/mod.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,26 @@ pub enum Sign {
1919

2020
/// Various ways to describe a time format.
2121
#[derive(Debug, Clone, Copy)]
22-
pub enum Format<'a> {
23-
/// A custom format typically defined with the [`format_description`][time::format_description] macro.
24-
Custom(&'a [time::format_description::FormatItem<'a>]),
22+
pub enum Format {
23+
/// A custom format limited to what's in the
24+
/// [`format`](mod@crate::time::format) submodule.
25+
Custom(CustomFormat),
2526
/// The seconds since 1970, also known as unix epoch, like `1660874655`.
2627
Unix,
2728
/// The seconds since 1970, followed by the offset, like `1660874655 +0800`
2829
Raw,
2930
}
3031

32+
/// A custom format for printing and parsing time.
33+
#[derive(Clone, Copy, Debug)]
34+
pub struct CustomFormat(pub(crate) &'static [time::format_description::FormatItem<'static>]);
35+
36+
impl From<CustomFormat> for Format {
37+
fn from(custom_format: CustomFormat) -> Format {
38+
Format::Custom(custom_format)
39+
}
40+
}
41+
3142
///
3243
#[allow(clippy::empty_docs)]
3344
pub mod format;

gix-date/tests/time/format.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use gix_date::{
22
time::{format, Format, Sign},
33
Time,
44
};
5-
use time::macros::format_description;
65

76
#[test]
87
fn short() {
@@ -72,14 +71,6 @@ fn git_default() {
7271
)
7372
}
7473

75-
#[test]
76-
fn custom_compile_time() {
77-
assert_eq!(
78-
time().format(format_description!("[year]-[month]-[day] [hour]:[minute]:[second]")),
79-
"1973-11-30 00:03:09",
80-
);
81-
}
82-
8374
fn time() -> Time {
8475
Time {
8576
seconds: 123456789,

0 commit comments

Comments
 (0)