Skip to content
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

Closed
wants to merge 7 commits into from

Conversation

andresovela
Copy link
Contributor

@andresovela andresovela commented Oct 2, 2023

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.

@Urhengulas
Copy link
Member

What do you think about doing the zero-padding more similar to the std::fmt grammar? https://doc.rust-lang.org/std/fmt/#sign0

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: "{t:05} [{L}] {s}".

@andresovela
Copy link
Contributor Author

Done

Timestamp,
TimestampMs,
Copy link
Member

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 {
Copy link
Member

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

Comment on lines +225 to +233
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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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);
Copy link
Member

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)?

Comment on lines +243 to +279
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");
}
Copy link
Member

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?

@andresovela
Copy link
Contributor Author

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.

@andresovela
Copy link
Contributor Author

Opened #789 for the timestamp display hints

@andresovela andresovela deleted the timestamp-formats branch October 15, 2023 14:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants