Skip to content

Commit d1c0780

Browse files
committed
uucore: time: Add show_error parameter to format_system_time
du prints a warning when the time is out of bounds.
1 parent 64565ba commit d1c0780

File tree

3 files changed

+22
-12
lines changed

3 files changed

+22
-12
lines changed

src/uu/du/src/du.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@ impl StatPrinter {
580580
if let Some(time) = self.time {
581581
let secs = get_time_secs(time, stat)?;
582582
let time = UNIX_EPOCH + Duration::from_secs(secs);
583-
uucore::time::format_system_time(&mut stdout(), time, &self.time_format)?;
583+
uucore::time::format_system_time(&mut stdout(), time, &self.time_format, true)?;
584584
print!("\t");
585585
}
586586

src/uu/ls/src/ls.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3003,7 +3003,7 @@ fn display_date(
30033003
_ => &config.time_format_recent,
30043004
};
30053005

3006-
uucore::time::format_system_time(out, time, fmt)
3006+
uucore::time::format_system_time(out, time, fmt, false)
30073007
}
30083008

30093009
#[allow(dead_code)]

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

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use std::io::Write;
1414
use std::time::{SystemTime, UNIX_EPOCH};
1515

1616
use crate::error::{UResult, USimpleError};
17+
use crate::show_error;
1718

1819
/// Format the given date according to this time format style.
1920
fn format_zoned<W: Write>(out: &mut W, zoned: Zoned, fmt: &str) -> UResult<()> {
@@ -25,7 +26,12 @@ fn format_zoned<W: Write>(out: &mut W, zoned: Zoned, fmt: &str) -> UResult<()> {
2526
}
2627

2728
/// Format a `SystemTime` according to given fmt, and append to vector out.
28-
pub fn format_system_time<W: Write>(out: &mut W, time: SystemTime, fmt: &str) -> UResult<()> {
29+
pub fn format_system_time<W: Write>(
30+
out: &mut W,
31+
time: SystemTime,
32+
fmt: &str,
33+
show_error: bool,
34+
) -> UResult<()> {
2935
let zoned: Result<Zoned, _> = time.try_into();
3036
match zoned {
3137
Ok(zoned) => format_zoned(out, zoned, fmt),
@@ -36,13 +42,16 @@ pub fn format_system_time<W: Write>(out: &mut W, time: SystemTime, fmt: &str) ->
3642
// but it still far enough in the future/past to be unlikely to matter:
3743
// jiff: Year between -9999 to 9999 (UTC) [-377705023201..=253402207200]
3844
// GNU: Year fits in signed 32 bits (timezone dependent)
39-
let ts = if time > UNIX_EPOCH {
40-
time.duration_since(UNIX_EPOCH).unwrap().as_secs()
45+
let ts: i64 = if time > UNIX_EPOCH {
46+
time.duration_since(UNIX_EPOCH).unwrap().as_secs() as i64
4147
} else {
42-
out.write_all(b"-")?; // Add negative sign
43-
UNIX_EPOCH.duration_since(time).unwrap().as_secs()
48+
-(UNIX_EPOCH.duration_since(time).unwrap().as_secs() as i64)
4449
};
45-
out.write_all(ts.to_string().as_bytes())?;
50+
let str = ts.to_string();
51+
if show_error {
52+
show_error!("time '{str}' is out of range");
53+
}
54+
out.write_all(str.as_bytes())?;
4655
Ok(())
4756
}
4857
}
@@ -60,11 +69,12 @@ mod tests {
6069

6170
let time = UNIX_EPOCH;
6271
let mut out = Vec::new();
63-
format_system_time(&mut out, time, "%Y-%m-%d %H:%M").expect("Formatting error.");
72+
format_system_time(&mut out, time, "%Y-%m-%d %H:%M", false).expect("Formatting error.");
6473
assert_eq!(String::from_utf8(out).unwrap(), "1970-01-01 00:00");
6574

6675
let mut out = Vec::new();
67-
format_system_time(&mut out, time, "%Y-%m-%d %H:%M:%S.%N %z").expect("Formatting error.");
76+
format_system_time(&mut out, time, "%Y-%m-%d %H:%M:%S.%N %z", false)
77+
.expect("Formatting error.");
6878
assert_eq!(
6979
String::from_utf8(out).unwrap(),
7080
"1970-01-01 00:00:00.000000000 +0000"
@@ -76,12 +86,12 @@ mod tests {
7686
fn test_large_system_time() {
7787
let time = UNIX_EPOCH + Duration::from_secs(67_768_036_191_763_200);
7888
let mut out = Vec::new();
79-
format_system_time(&mut out, time, "%Y-%m-%d %H:%M").expect("Formatting error.");
89+
format_system_time(&mut out, time, "%Y-%m-%d %H:%M", false).expect("Formatting error.");
8090
assert_eq!(String::from_utf8(out).unwrap(), "67768036191763200");
8191

8292
let time = UNIX_EPOCH - Duration::from_secs(67_768_040_922_076_800);
8393
let mut out = Vec::new();
84-
format_system_time(&mut out, time, "%Y-%m-%d %H:%M").expect("Formatting error.");
94+
format_system_time(&mut out, time, "%Y-%m-%d %H:%M", false).expect("Formatting error.");
8595
assert_eq!(String::from_utf8(out).unwrap(), "-67768040922076800");
8696
}
8797
}

0 commit comments

Comments
 (0)