Skip to content

Commit e61832e

Browse files
committed
fix: change Time::format_or_raw() to Time::format_or_unix()
As it turns out, `raw` could also panic, but `unix` cannot.
1 parent d5e194d commit e61832e

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

gix-date/src/time/format.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,11 @@ impl Time {
4242
self.format_inner(format.into())
4343
}
4444

45-
/// Like [`Self::format()`], but on time conversion error, produce the [RAW] format instead to make it
46-
/// infallible.
47-
pub fn format_or_raw(&self, format: impl Into<Format>) -> String {
48-
self.format_inner(format.into()).unwrap_or_else(|_| self.to_string())
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())
4950
}
5051

5152
fn format_inner(&self, format: Format) -> Result<String, jiff::Error> {

gix-date/tests/time/format.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,21 @@ fn git_rfc2822() -> gix_testtools::Result {
6666

6767
#[test]
6868
fn default() -> gix_testtools::Result {
69+
assert_eq!(time().format(format::GITOXIDE)?, "Fri Nov 30 1973 00:03:09 +0230");
70+
assert_eq!(time_dec1().format(format::GITOXIDE)?, "Sat Dec 01 1973 00:03:09 +0230");
71+
Ok(())
72+
}
73+
74+
#[test]
75+
fn format_or_unix() {
6976
assert_eq!(
70-
time().format(gix_date::time::format::GITOXIDE)?,
71-
"Fri Nov 30 1973 00:03:09 +0230"
72-
);
73-
assert_eq!(
74-
time_dec1().format(gix_date::time::format::GITOXIDE)?,
75-
"Sat Dec 01 1973 00:03:09 +0230"
77+
Time {
78+
seconds: 42,
79+
offset: 7200 * 60
80+
}
81+
.format_or_unix(format::GITOXIDE),
82+
"42"
7683
);
77-
Ok(())
7884
}
7985

8086
#[test]

0 commit comments

Comments
 (0)