Skip to content

Commit 5450e2f

Browse files
committed
---
yaml --- r: 157610 b: refs/heads/snap-stage3 c: d1fc2de h: refs/heads/master v: v3
1 parent 1c3a575 commit 5450e2f

File tree

3 files changed

+128
-193
lines changed

3 files changed

+128
-193
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
refs/heads/master: 065caf34f5ff29e04605f95d9c5d511af219439a
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
4-
refs/heads/snap-stage3: b8e7c4fcb9655ff92f8f2feba4ba4221646acc6f
4+
refs/heads/snap-stage3: d1fc2dec79689fe6bd37c95f3fe5b7acd476fff6
55
refs/heads/try: 0ee4d8b0b112c608646fa75463ab4dc59132efd9
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b

branches/snap-stage3/src/libstd/time/duration.rs

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -317,26 +317,29 @@ impl Div<i32,Duration> for Duration {
317317

318318
impl fmt::Show for Duration {
319319
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
320-
let days = self.num_days();
321-
let secs = self.secs - days * SECS_PER_DAY;
320+
// technically speaking, negative duration is not valid ISO 8601,
321+
// but we need to print it anyway.
322+
let (abs, sign) = if self.secs < 0 { (-self, "-") } else { (*self, "") };
323+
324+
let days = abs.secs / SECS_PER_DAY;
325+
let secs = abs.secs - days * SECS_PER_DAY;
322326
let hasdate = days != 0;
323-
let hastime = (secs != 0 || self.nanos != 0) || !hasdate;
327+
let hastime = (secs != 0 || abs.nanos != 0) || !hasdate;
328+
329+
try!(write!(f, "{}P", sign));
324330

325-
try!(write!(f, "P"));
326331
if hasdate {
327-
// technically speaking the negative part is not the valid ISO 8601,
328-
// but we need to print it anyway.
329332
try!(write!(f, "{}D", days));
330333
}
331334
if hastime {
332-
if self.nanos == 0 {
335+
if abs.nanos == 0 {
333336
try!(write!(f, "T{}S", secs));
334-
} else if self.nanos % NANOS_PER_MILLI == 0 {
335-
try!(write!(f, "T{}.{:03}S", secs, self.nanos / NANOS_PER_MILLI));
336-
} else if self.nanos % NANOS_PER_MICRO == 0 {
337-
try!(write!(f, "T{}.{:06}S", secs, self.nanos / NANOS_PER_MICRO));
337+
} else if abs.nanos % NANOS_PER_MILLI == 0 {
338+
try!(write!(f, "T{}.{:03}S", secs, abs.nanos / NANOS_PER_MILLI));
339+
} else if abs.nanos % NANOS_PER_MICRO == 0 {
340+
try!(write!(f, "T{}.{:06}S", secs, abs.nanos / NANOS_PER_MICRO));
338341
} else {
339-
try!(write!(f, "T{}.{:09}S", secs, self.nanos));
342+
try!(write!(f, "T{}.{:09}S", secs, abs.nanos));
340343
}
341344
}
342345
Ok(())
@@ -540,13 +543,15 @@ mod tests {
540543
let d: Duration = Zero::zero();
541544
assert_eq!(d.to_string(), "PT0S".to_string());
542545
assert_eq!(Duration::days(42).to_string(), "P42D".to_string());
543-
assert_eq!(Duration::days(-42).to_string(), "P-42D".to_string());
546+
assert_eq!(Duration::days(-42).to_string(), "-P42D".to_string());
544547
assert_eq!(Duration::seconds(42).to_string(), "PT42S".to_string());
545548
assert_eq!(Duration::milliseconds(42).to_string(), "PT0.042S".to_string());
546549
assert_eq!(Duration::microseconds(42).to_string(), "PT0.000042S".to_string());
547550
assert_eq!(Duration::nanoseconds(42).to_string(), "PT0.000000042S".to_string());
548551
assert_eq!((Duration::days(7) + Duration::milliseconds(6543)).to_string(),
549552
"P7DT6.543S".to_string());
553+
assert_eq!(Duration::seconds(-86401).to_string(), "-P1DT1S".to_string());
554+
assert_eq!(Duration::nanoseconds(-1).to_string(), "-PT0.000000001S".to_string());
550555

551556
// the format specifier should have no effect on `Duration`
552557
assert_eq!(format!("{:30}", Duration::days(1) + Duration::milliseconds(2345)),

0 commit comments

Comments
 (0)