-
Notifications
You must be signed in to change notification settings - Fork 73
add TimePadding with Right and AddZeros
#134
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,4 +1,4 @@ | ||||||||||||||
| use crate::config::{TargetPadding, TimeFormat}; | ||||||||||||||
| use crate::config::{TargetPadding, TimeFormat, TimePadding}; | ||||||||||||||
| use crate::{Config, LevelPadding, ThreadLogMode, ThreadPadding}; | ||||||||||||||
| use log::{LevelFilter, Record}; | ||||||||||||||
| use std::io::{Error, Write}; | ||||||||||||||
|
|
@@ -72,22 +72,35 @@ pub fn write_time<W>(write: &mut W, config: &Config) -> Result<(), Error> | |||||||||||||
| where | ||||||||||||||
| W: Write + Sized, | ||||||||||||||
| { | ||||||||||||||
| use time::error::Format; | ||||||||||||||
| use time::format_description::well_known::*; | ||||||||||||||
|
|
||||||||||||||
| let time = time::OffsetDateTime::now_utc().to_offset(config.time_offset); | ||||||||||||||
| let res = match config.time_format { | ||||||||||||||
| TimeFormat::Rfc2822 => time.format_into(write, &Rfc2822), | ||||||||||||||
| TimeFormat::Rfc3339 => time.format_into(write, &Rfc3339), | ||||||||||||||
| TimeFormat::Custom(format) => time.format_into(write, &format), | ||||||||||||||
| let formatted = match config.time_format { | ||||||||||||||
| TimeFormat::Rfc2822 => time.format(&Rfc2822), | ||||||||||||||
| TimeFormat::Rfc3339 => time.format(&Rfc3339), | ||||||||||||||
| TimeFormat::Custom(format) => time.format(&format), | ||||||||||||||
| }; | ||||||||||||||
| match res { | ||||||||||||||
| Err(Format::StdIo(err)) => return Err(err), | ||||||||||||||
| Err(err) => panic!("Invalid time format: {}", err), | ||||||||||||||
| _ => {} | ||||||||||||||
| let mut formatted: String = | ||||||||||||||
| formatted.unwrap_or_else(|err| panic!("Invalid time format: {}", err)); | ||||||||||||||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this drops the special casing for
Suggested change
|
||||||||||||||
|
|
||||||||||||||
| formatted = match config.time_padding { | ||||||||||||||
| TimePadding::Right(offset) => format!("{: <1$}", formatted, offset), | ||||||||||||||
| TimePadding::AddZeros => { | ||||||||||||||
| if matches!(config.time_format, TimeFormat::Rfc3339) { | ||||||||||||||
| // only appliciable with Rfc3339 | ||||||||||||||
| // | ||||||||||||||
| // the rfc3339 timestamp is 30 characters long, if it would not end with a 0 in the | ||||||||||||||
| // subseconds part. To fix this inconsistency, we add zeros before the Z. | ||||||||||||||
| while formatted.len() < 30 { | ||||||||||||||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe make the 30 into a constant?
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good idea |
||||||||||||||
| formatted.insert(formatted.len() - 1, '0') | ||||||||||||||
| } | ||||||||||||||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I feel like this is not very performant and there should be a way to not add the zeros one by one. |
||||||||||||||
| } | ||||||||||||||
| format!("{}", formatted) | ||||||||||||||
| } | ||||||||||||||
| TimePadding::Off => format!("{}", formatted), | ||||||||||||||
| }; | ||||||||||||||
|
|
||||||||||||||
| write!(write, " ")?; | ||||||||||||||
| write!(write, "{} ", formatted)?; | ||||||||||||||
| Ok(()) | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
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.
I think this modifies the current default (of no padding), which I would want to release as a minor version. Can we maybe consider this to be
Offby default?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.
Leaving the padding as
Offis certainly possible, but I would argue that setting a padding by default would improve usability a little.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.
I agree, but then we will need a new major version.