Skip to content

Commit 8b6909c

Browse files
authored
Change Timestamp::fmt to format the year as YYYY (#88)
1 parent e9cba4e commit 8b6909c

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

src/sql_type/timestamp.rs

+15-7
Original file line numberDiff line numberDiff line change
@@ -352,8 +352,14 @@ impl fmt::Display for Timestamp {
352352
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
353353
write!(
354354
f,
355-
"{}-{:02}-{:02} {:02}:{:02}:{:02}",
356-
self.year, self.month, self.day, self.hour, self.minute, self.second
355+
"{}{:04}-{:02}-{:02} {:02}:{:02}:{:02}",
356+
if self.year < 0 { "-" } else { "" },
357+
self.year.abs(),
358+
self.month,
359+
self.day,
360+
self.hour,
361+
self.minute,
362+
self.second
357363
)?;
358364
match self.precision {
359365
1 => write!(f, ".{:01}", self.nanosecond / 100000000)?,
@@ -556,18 +562,20 @@ mod tests {
556562
assert_eq!(ts.to_string(), "2012-03-04 05:06:07.890123456 -08:45");
557563
ts.precision = 0;
558564
assert_eq!(ts.to_string(), "2012-03-04 05:06:07 -08:45");
565+
ts.year = 3;
566+
assert_eq!(ts.to_string(), "0003-03-04 05:06:07 -08:45");
559567
ts.year = -123;
560-
assert_eq!(ts.to_string(), "-123-03-04 05:06:07 -08:45");
568+
assert_eq!(ts.to_string(), "-0123-03-04 05:06:07 -08:45");
561569
let mut ts = ts.and_tz_offset(-3600 - 1800)?;
562570
assert_eq!(ts.tz_hour_offset, -1);
563571
assert_eq!(ts.tz_minute_offset, -30);
564-
assert_eq!(ts.to_string(), "-123-03-04 05:06:07 -01:30");
572+
assert_eq!(ts.to_string(), "-0123-03-04 05:06:07 -01:30");
565573
ts.tz_hour_offset = 0;
566-
assert_eq!(ts.to_string(), "-123-03-04 05:06:07 -00:30");
574+
assert_eq!(ts.to_string(), "-0123-03-04 05:06:07 -00:30");
567575
ts.tz_minute_offset = 30;
568-
assert_eq!(ts.to_string(), "-123-03-04 05:06:07 +00:30");
576+
assert_eq!(ts.to_string(), "-0123-03-04 05:06:07 +00:30");
569577
ts.tz_minute_offset = 0;
570-
assert_eq!(ts.to_string(), "-123-03-04 05:06:07 +00:00");
578+
assert_eq!(ts.to_string(), "-0123-03-04 05:06:07 +00:00");
571579
Ok(())
572580
}
573581

0 commit comments

Comments
 (0)