Skip to content

Commit 220bb1a

Browse files
committed
date: refactor date printing into its own method
1 parent 8f03ead commit 220bb1a

File tree

1 file changed

+27
-22
lines changed

1 file changed

+27
-22
lines changed

src/uu/date/src/date.rs

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,32 @@ impl DateSource {
250250
}
251251
}
252252

253+
fn print_date(format_string: &str, date: DateTime<FixedOffset>) -> UResult<()> {
254+
let format_string = custom_time_format(format_string);
255+
// Refuse to pass this string to chrono as it is crashing in this crate
256+
if format_string.contains("%#z") {
257+
return Err(USimpleError::new(
258+
1,
259+
format!("invalid format {}", format_string.replace("%f", "%N")),
260+
));
261+
}
262+
// Hack to work around panic in chrono,
263+
// TODO - remove when a fix for https://github.com/chronotope/chrono/issues/623 is released
264+
let format_items = StrftimeItems::new(format_string.as_str());
265+
if format_items.clone().any(|i| i == Item::Error) {
266+
return Err(USimpleError::new(
267+
1,
268+
format!("invalid format {}", format_string.replace("%f", "%N")),
269+
));
270+
}
271+
let formatted = date
272+
.format_with_items(format_items)
273+
.to_string()
274+
.replace("%f", "%N");
275+
println!("{formatted}");
276+
Ok(())
277+
}
278+
253279
#[uucore::main]
254280
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
255281
let matches = uu_app().try_get_matches_from(args)?;
@@ -300,28 +326,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
300326
for date in dates {
301327
match date {
302328
Ok(date) => {
303-
let format_string = custom_time_format(format_string);
304-
// Refuse to pass this string to chrono as it is crashing in this crate
305-
if format_string.contains("%#z") {
306-
return Err(USimpleError::new(
307-
1,
308-
format!("invalid format {}", format_string.replace("%f", "%N")),
309-
));
310-
}
311-
// Hack to work around panic in chrono,
312-
// TODO - remove when a fix for https://github.com/chronotope/chrono/issues/623 is released
313-
let format_items = StrftimeItems::new(format_string.as_str());
314-
if format_items.clone().any(|i| i == Item::Error) {
315-
return Err(USimpleError::new(
316-
1,
317-
format!("invalid format {}", format_string.replace("%f", "%N")),
318-
));
319-
}
320-
let formatted = date
321-
.format_with_items(format_items)
322-
.to_string()
323-
.replace("%f", "%N");
324-
println!("{formatted}");
329+
print_date(format_string, date)?;
325330
}
326331
Err((input, _err)) => show!(USimpleError::new(
327332
1,

0 commit comments

Comments
 (0)