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

Fix rfc 2822 format when day is less than 10 #249

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1367,8 +1367,12 @@ mod tests {
"2015-02-18T23:16:09+00:00");
assert_eq!(EDT.ymd(2015, 2, 18).and_hms_milli(23, 16, 9, 150).to_rfc2822(),
"Wed, 18 Feb 2015 23:16:09 +0500");
assert_eq!(EDT.ymd(2015, 2, 3).and_hms_milli(23, 16, 9, 150).to_rfc2822(),
"Tue, 3 Feb 2015 23:16:09 +0500");
assert_eq!(EDT.ymd(2015, 2, 18).and_hms_milli(23, 16, 9, 150).to_rfc3339(),
"2015-02-18T23:16:09.150+05:00");
assert_eq!(EDT.ymd(2015, 2, 3).and_hms_milli(23, 16, 9, 150).to_rfc3339(),
"2015-02-03T23:16:09.150+05:00");
assert_eq!(EDT.ymd(2015, 2, 18).and_hms_micro(23, 59, 59, 1_234_567).to_rfc2822(),
"Wed, 18 Feb 2015 23:59:60 +0500");
assert_eq!(EDT.ymd(2015, 2, 18).and_hms_micro(23, 59, 59, 1_234_567).to_rfc3339(),
Expand Down
11 changes: 9 additions & 2 deletions src/format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pub enum Pad {
/// If the number is too long or (in some cases) negative, it is printed as is.
///
/// The **parsing width** is the maximal width to be scanned.
/// The parser only tries to consume from one to given number of digits (greedily).
/// The parser only tries to consume from one to given number of digits (greedily).
/// It also trims the preceding whitespaces if any.
/// It cannot parse the negative number, so some date and time cannot be formatted then
/// parsed with the same formatting items.
Expand Down Expand Up @@ -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} ",
Copy link
Author

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.

Copy link
Author

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 ?

Copy link
Contributor

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.

Copy link
Author

Choose a reason for hiding this comment

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

Done.

if d.day() < 10 {
try!(write!(w, "{}, {:1} {} {:04} {:02}:{:02}:{:02} ",
SHORT_WEEKDAYS[d.weekday().num_days_from_monday() as usize],
d.day(), SHORT_MONTHS[d.month0() as usize], d.year(),
t.hour(), t.minute(), sec));
} else {
try!(write!(w, "{}, {:2} {} {:04} {:02}:{:02}:{:02} ",
SHORT_WEEKDAYS[d.weekday().num_days_from_monday() as usize],
d.day(), SHORT_MONTHS[d.month0() as usize], d.year(),
t.hour(), t.minute(), sec));
}
Some(write_local_minus_utc(w, off, false, false))
} else {
None
Expand Down