-
Notifications
You must be signed in to change notification settings - Fork 542
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
Fix rfc 2822 format when day is less than 10 #249
Conversation
@@ -494,10 +494,17 @@ pub fn format<'a, I>(w: &mut fmt::Formatter, date: Option<&NaiveDate>, time: Opt | |||
RFC2822 => // same to `%a, %e %b %Y %H:%M:%S %z` | |||
if let (Some(d), Some(t), Some(&(_, off))) = (date, time, off) { | |||
let sec = t.second() + t.nanosecond() / 1_000_000_000; | |||
try!(write!(w, "{}, {:2} {} {:04} {:02}:{:02}:{:02} ", |
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 tried : let fmt_str = if (d.day() < 10) { "{}, {:1} {} {:04} {:02}:{:02}:{:02} " } else { "{}, {:2} {} {:04} {:02}:{:02}:{:02} " };
but then I was stuck at error error: format argument must be a string literal.
when i tried to assign format string to a variable.
My rust is not strong.
Let me know the right way to fix it, i will do that.
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.
Also, Is Tue, 03 Feb 2015 23:16:09 +0500
be better ?
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.
Since this number is guaranteed to be between 1 and 31 inclusive, it should be fine to just remove the the width specifier -- just use {}
. I think that's what should be done here.
If you ever need to deal with something like this again, though, you can use the $
width specifier to supply the width as a separate argument. In this case it would be something like:
let wwwwidth = if d.day() < 10 { 1 } else { 2 };
try!(write!(w, "{}, {:$} {} {:04} {:02}:{:02}:{:02} ",
SHORT_WEEKDAYS[d.weekday().num_days_from_monday() as usize],
d.day(), wwwwidth, SHORT_MONTHS[d.month0() as usize], d.year(),
t.hour(), t.minute(), sec));
Again, I don't think we should do that here, but it's kind of nifty.
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.
Done.
Any suggestions ? |
Sorry, this fell through the cracks, I'll think about it more deeply but a cursory glance looks like you're removing padding in cases where it is single-digits? I believe that the current design double-spaces to keep multiple lines aligned no matter what day they fall on. Do you want to check if RFC 2822 explicitly states whether that is permissible? |
@quodlibetor The current implementation keeps The idea is to always keep only The rfc recommends to fold the white spaces (replace multiple white spaces to single white space).
The full grammar is :
|
Okay, I think you've convinced me. My inclination is that this is a breaking change and so it should wait for Chrono 0.5. It should get in there, so I've added it to that milestone. There's no real timeline for that release right now, sadly. I would happily immediately take a PR to the chrono docs explaining the fact that the date is space-padded. I would like to also say that I would also take a PR changing the alignment from space-padded to zero-padded, but the problem is that often, as I'm sure you've encountered, time-parsing code is sensitive to whitespace so any code that relies on this existing behavior might get busted if we eliminate double spaces. On the other hand, all code that can handle chrono-formatted RFC-2833 timestamps must already be capable of handling arbitrary whitespace, so maybe it's not that big of a deal, and I should just accept this PR after you've addressed my one comment. On the other other hand, accepting this PR will ruin alignment of any terminal apps that are depending on it for their display. So I still think this needs to at least wait for 0.5, but I'm obviously a bit torn and am open to argument. |
@quodlibetor I have addressed your one comment. I am not sure if this should go in 0.5 or before. I will leave that to your best judgement. |
Thanks @ashishnegi I am not sure that it's super important, but I am going to hold off on this until 0.5, unless other people object to that. |
@quodlibetor Are we going to merge this ? |
Ping. |
Seems dead. |
Test :
EDT.ymd(2015, 2, 3).and_hms_milli(23, 16, 9, 150)
generatesTue,<space><space>3 Feb 2015 23:16:09 +0500
i.e. 2 spaces before day's value3
.It should be
Tue,<space>3 Feb 2015 23:16:09 +0500
Fixed it.