From ca865e3c24a7c0d446dd051bbb6e3eff3366b805 Mon Sep 17 00:00:00 2001 From: Kang Seonghoon Date: Wed, 18 Feb 2015 00:00:30 +0900 Subject: [PATCH] added more tests for non-four-digit years and fixed edge cases. --- src/format/mod.rs | 4 ++-- src/format/strftime.rs | 12 ++++++------ src/naive/date.rs | 13 +++++++++++++ 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/format/mod.rs b/src/format/mod.rs index 7e6ffed..a5be021 100644 --- a/src/format/mod.rs +++ b/src/format/mod.rs @@ -290,8 +290,8 @@ pub fn format<'a, I>(w: &mut fmt::Formatter, date: Option<&NaiveDate>, time: Opt // non-four-digit years require an explicit sign as per ISO 8601 match pad { Pad::None => try!(write!(w, "{:+}", v)), - Pad::Zero => try!(write!(w, "{:+01$}", v, width)), - Pad::Space => try!(write!(w, "{:+1$}", v, width)), + Pad::Zero => try!(write!(w, "{:+01$}", v, width + 1)), + Pad::Space => try!(write!(w, "{:+1$}", v, width + 1)), } } else { match pad { diff --git a/src/format/strftime.rs b/src/format/strftime.rs index 358944f..4cbf729 100644 --- a/src/format/strftime.rs +++ b/src/format/strftime.rs @@ -160,9 +160,9 @@ impl<'a> Iterator for StrftimeItems<'a> { 'C' => Some(num0!(YearDiv100)), 'D' => Some(recons![num0!(Month), lit!("/"), num0!(Day), lit!("/"), num0!(YearMod100)]), - 'F' => Some(recons![num!(Year), lit!("-"), num0!(Month), lit!("-"), + 'F' => Some(recons![num0!(Year), lit!("-"), num0!(Month), lit!("-"), num0!(Day)]), - 'G' => Some(num!(IsoYear)), + 'G' => Some(num0!(IsoYear)), 'H' => Some(num0!(Hour)), 'I' => Some(num0!(Hour12)), 'M' => Some(num0!(Minute)), @@ -176,14 +176,14 @@ impl<'a> Iterator for StrftimeItems<'a> { 'W' => Some(num0!(WeekFromMon)), 'X' => Some(recons![num0!(Hour), lit!(":"), num0!(Minute), lit!(":"), num0!(Second)]), - 'Y' => Some(num!(Year)), + 'Y' => Some(num0!(Year)), 'Z' => Some(fix!(TimezoneName)), 'a' => Some(fix!(ShortWeekdayName)), 'b' => Some(fix!(ShortMonthName)), 'c' => Some(recons![fix!(ShortWeekdayName), sp!(" "), fix!(ShortMonthName), sp!(" "), nums!(Day), sp!(" "), num0!(Hour), lit!(":"), num0!(Minute), lit!(":"), num0!(Second), sp!(" "), - num!(Year)]), + num0!(Year)]), 'd' => Some(num0!(Day)), 'e' => Some(nums!(Day)), 'f' => Some(num0!(Nanosecond)), @@ -201,7 +201,7 @@ impl<'a> Iterator for StrftimeItems<'a> { 't' => Some(sp!("\t")), 'u' => Some(num!(WeekdayFromMon)), 'v' => Some(recons![nums!(Day), lit!("-"), fix!(ShortMonthName), lit!("-"), - num!(Year)]), + num0!(Year)]), 'w' => Some(num!(NumDaysFromSun)), 'x' => Some(recons![num0!(Month), lit!("/"), num0!(Day), lit!("/"), num0!(YearMod100)]), @@ -255,7 +255,7 @@ fn test_strftime_items() { assert_eq!(parse_and_collect("100%%"), [lit!("100"), lit!("%")]); assert_eq!(parse_and_collect("100%% ok"), [lit!("100"), lit!("%"), sp!(" "), lit!("ok")]); assert_eq!(parse_and_collect("%%PDF-1.0"), [lit!("%"), lit!("PDF-1.0")]); - assert_eq!(parse_and_collect("%Y-%m-%d"), [num!(Year), lit!("-"), num0!(Month), lit!("-"), + assert_eq!(parse_and_collect("%Y-%m-%d"), [num0!(Year), lit!("-"), num0!(Month), lit!("-"), num0!(Day)]); assert_eq!(parse_and_collect("[%F]"), parse_and_collect("[%Y-%m-%d]")); assert_eq!(parse_and_collect("%m %d"), [num0!(Month), sp!(" "), num0!(Day)]); diff --git a/src/naive/date.rs b/src/naive/date.rs index 9d961d7..791330b 100644 --- a/src/naive/date.rs +++ b/src/naive/date.rs @@ -873,6 +873,19 @@ mod tests { assert_eq!(d.format("%v").to_string(), " 4-Mar-2012"); assert_eq!(d.format("%t%n%%%n%t").to_string(), "\t\n%\n\t"); + // non-four-digit years + assert_eq!(NaiveDate::from_ymd(12345, 1, 1).format("%Y").to_string(), "+12345"); + assert_eq!(NaiveDate::from_ymd(1234, 1, 1).format("%Y").to_string(), "1234"); + assert_eq!(NaiveDate::from_ymd(123, 1, 1).format("%Y").to_string(), "0123"); + assert_eq!(NaiveDate::from_ymd(12, 1, 1).format("%Y").to_string(), "0012"); + assert_eq!(NaiveDate::from_ymd(1, 1, 1).format("%Y").to_string(), "0001"); + assert_eq!(NaiveDate::from_ymd(0, 1, 1).format("%Y").to_string(), "0000"); + assert_eq!(NaiveDate::from_ymd(-1, 1, 1).format("%Y").to_string(), "-0001"); + assert_eq!(NaiveDate::from_ymd(-12, 1, 1).format("%Y").to_string(), "-0012"); + assert_eq!(NaiveDate::from_ymd(-123, 1, 1).format("%Y").to_string(), "-0123"); + assert_eq!(NaiveDate::from_ymd(-1234, 1, 1).format("%Y").to_string(), "-1234"); + assert_eq!(NaiveDate::from_ymd(-12345, 1, 1).format("%Y").to_string(), "-12345"); + // corner cases assert_eq!(NaiveDate::from_ymd(2007, 12, 31).format("%G,%g,%U,%W,%V").to_string(), "2008,08,53,53,01");