-
-
Notifications
You must be signed in to change notification settings - Fork 81
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for zero-padded timestamps and Unix-style timestamps #780
Conversation
What do you think about doing the zero-padding more similar to the assert_eq!(format!("Hello {:05}!", 5), "Hello 00005!");
assert_eq!(format!("Hello {:05}!", -5), "Hello -0005!");
assert_eq!(format!("{:#010x}!", 27), "0x0000001b!"); For us I would imagine it like this: |
Done |
Timestamp, | ||
TimestampMs, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's please keep this as Timestamp
. It could very well be seconds or microseconds or anything else.
@@ -197,20 +201,90 @@ impl<'a> Printer<'a> { | |||
build_formatted_string(&result, format, 0, self.record_log_level(), format.color) | |||
} | |||
|
|||
fn build_timestamp(&self, format: &LogFormat) -> String { | |||
fn build_timestamp(&self, format: &LogFormat, style: TimestampStyle) -> String { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please separate that into build_timestamp
and build_timestamp_unix
let ms = timestamp % 1000; | ||
timestamp /= 1000; | ||
let seconds = timestamp % 60; | ||
timestamp /= 60; | ||
let minutes = timestamp % 60; | ||
timestamp /= 60; | ||
let hours = timestamp % 24; | ||
timestamp /= 24; | ||
let days = timestamp; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let ms = timestamp % 1000; | |
timestamp /= 1000; | |
let seconds = timestamp % 60; | |
timestamp /= 60; | |
let minutes = timestamp % 60; | |
timestamp /= 60; | |
let hours = timestamp % 24; | |
timestamp /= 24; | |
let days = timestamp; | |
let div_rem = |x, y| (x / y, x % y); | |
let (timestamp, ms) = div_rem(timestamp, 1000); | |
let (timestamp, seconds) = div_rem(timestamp, 60); | |
let (timestamp, minutes) = div_rem(timestamp, 60); | |
let (timestamp, hours) = div_rem(timestamp, 24); | |
let days = timestamp; |
Some(Padding::Space) | None => record.timestamp().to_string(), | ||
Some(Padding::Zero) => { | ||
let mut result = String::new(); | ||
let width = format.width.unwrap_or(8); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why 8
in unwrap_or(8)
?
if days > 0 || width > 12 { | ||
let zero_padding = | ||
std::cmp::max(num_of_digits(days), width.saturating_sub(13)); | ||
write!( | ||
&mut result, | ||
"{days:0>0$}:{hours:0>2}:{minutes:0>2}:{seconds:0>2}.{ms:0>3}", | ||
zero_padding | ||
) | ||
.expect("failed to format timestamp"); | ||
} else if hours > 0 || width > 9 { | ||
let zero_padding = | ||
std::cmp::max(num_of_digits(hours), width.saturating_sub(10)); | ||
write!( | ||
&mut result, | ||
"{hours:0>0$}:{minutes:0>2}:{seconds:0>2}.{ms:0>3}", | ||
zero_padding | ||
) | ||
.expect("failed to format timestamp"); | ||
} else if minutes > 0 || width > 6 { | ||
let zero_padding = | ||
std::cmp::max(num_of_digits(minutes), width.saturating_sub(7)); | ||
write!( | ||
&mut result, | ||
"{minutes:0>0$}:{seconds:0>2}.{ms:0>3}", | ||
zero_padding | ||
) | ||
.expect("failed to format timestamp"); | ||
} else if seconds > 0 || width > 3 { | ||
let zero_padding = | ||
std::cmp::max(num_of_digits(seconds), width.saturating_sub(4)); | ||
write!(&mut result, "{seconds:0>0$}.{ms:0>3}", zero_padding) | ||
.expect("failed to format timestamp"); | ||
} else { | ||
let zero_padding = std::cmp::max(num_of_digits(ms), width); | ||
write!(&mut result, "{ms:0>0$}", zero_padding) | ||
.expect("failed to format timestamp"); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please explain this huge if-else?
I realized that the timestamp customization approach introduced with this PR is not the right approach, so I'll close this PR. I'll add the padding functionality to #783 instead, and I'll implement the timestamp option as a display hint in a separate PR. |
Opened #789 for the timestamp display hints |
This PR adds a new format specifier to format the timestamp like this "01:42:56.643".
This PR also extends the parser to support parsing format parameters like
"{t:>08"}
, to zero-pad timestamps.I also removed the
min_timestamp_width
/timing_align
stuff that was previously needed for formatting timestamps.