Skip to content

Commit 72d172d

Browse files
committed
uucore: time: Take Write trait as parameter, return UResult
Gives us more flexiblity to print directly to stdout, and removes the need to similar error handling across utilities.
1 parent 3b4c472 commit 72d172d

File tree

2 files changed

+8
-9
lines changed

2 files changed

+8
-9
lines changed

src/uu/ls/src/ls.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3004,7 +3004,6 @@ fn display_date(
30043004
};
30053005

30063006
uucore::time::format_system_time(out, time, fmt)
3007-
.map_err(|x| USimpleError::new(1, x.to_string()))
30083007
}
30093008

30103009
#[allow(dead_code)]

src/uucore/src/lib/features/time.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,22 @@
1010
use jiff::Zoned;
1111
use jiff::fmt::StdIoWrite;
1212
use jiff::fmt::strtime::{BrokenDownTime, Config};
13+
use std::io::Write;
1314
use std::time::{SystemTime, UNIX_EPOCH};
1415

16+
use crate::error::{UResult, USimpleError};
17+
1518
/// Format the given date according to this time format style.
16-
fn format_zoned(out: &mut Vec<u8>, zoned: Zoned, fmt: &str) -> Result<(), jiff::Error> {
19+
fn format_zoned<W: Write>(out: &mut W, zoned: Zoned, fmt: &str) -> UResult<()> {
1720
let tm = BrokenDownTime::from(&zoned);
1821
let mut out = StdIoWrite(out);
1922
let config = Config::new().lenient(true);
2023
tm.format_with_config(&config, fmt, &mut out)
24+
.map_err(|x| USimpleError::new(1, x.to_string()))
2125
}
2226

2327
/// Format a `SystemTime` according to given fmt, and append to vector out.
24-
pub fn format_system_time(
25-
out: &mut Vec<u8>,
26-
time: SystemTime,
27-
fmt: &str,
28-
) -> Result<(), jiff::Error> {
28+
pub fn format_system_time<W: Write>(out: &mut W, time: SystemTime, fmt: &str) -> UResult<()> {
2929
let zoned: Result<Zoned, _> = time.try_into();
3030
match zoned {
3131
Ok(zoned) => format_zoned(out, zoned, fmt),
@@ -39,10 +39,10 @@ pub fn format_system_time(
3939
let ts = if time > UNIX_EPOCH {
4040
time.duration_since(UNIX_EPOCH).unwrap().as_secs()
4141
} else {
42-
out.extend(b"-"); // Add negative sign
42+
out.write_all(b"-")?; // Add negative sign
4343
UNIX_EPOCH.duration_since(time).unwrap().as_secs()
4444
};
45-
out.extend(ts.to_string().as_bytes());
45+
out.write_all(ts.to_string().as_bytes())?;
4646
Ok(())
4747
}
4848
}

0 commit comments

Comments
 (0)