@@ -317,29 +317,26 @@ impl Div<i32,Duration> for Duration {
317
317
318
318
impl fmt:: Show for Duration {
319
319
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
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 ;
320
+ let days = self . num_days ( ) ;
321
+ let secs = self . secs - days * SECS_PER_DAY ;
326
322
let hasdate = days != 0 ;
327
- let hastime = ( secs != 0 || abs. nanos != 0 ) || !hasdate;
328
-
329
- try!( write ! ( f, "{}P" , sign) ) ;
323
+ let hastime = ( secs != 0 || self . nanos != 0 ) || !hasdate;
330
324
325
+ try!( write ! ( f, "P" ) ) ;
331
326
if hasdate {
327
+ // technically speaking the negative part is not the valid ISO 8601,
328
+ // but we need to print it anyway.
332
329
try!( write ! ( f, "{}D" , days) ) ;
333
330
}
334
331
if hastime {
335
- if abs . nanos == 0 {
332
+ if self . nanos == 0 {
336
333
try!( write ! ( f, "T{}S" , secs) ) ;
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 ) ) ;
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 ) ) ;
341
338
} else {
342
- try!( write ! ( f, "T{}.{:09}S" , secs, abs . nanos) ) ;
339
+ try!( write ! ( f, "T{}.{:09}S" , secs, self . nanos) ) ;
343
340
}
344
341
}
345
342
Ok ( ( ) )
@@ -543,15 +540,13 @@ mod tests {
543
540
let d: Duration = Zero :: zero ( ) ;
544
541
assert_eq ! ( d. to_string( ) , "PT0S" . to_string( ) ) ;
545
542
assert_eq ! ( Duration :: days( 42 ) . to_string( ) , "P42D" . to_string( ) ) ;
546
- assert_eq ! ( Duration :: days( -42 ) . to_string( ) , "-P42D " . to_string( ) ) ;
543
+ assert_eq ! ( Duration :: days( -42 ) . to_string( ) , "P-42D " . to_string( ) ) ;
547
544
assert_eq ! ( Duration :: seconds( 42 ) . to_string( ) , "PT42S" . to_string( ) ) ;
548
545
assert_eq ! ( Duration :: milliseconds( 42 ) . to_string( ) , "PT0.042S" . to_string( ) ) ;
549
546
assert_eq ! ( Duration :: microseconds( 42 ) . to_string( ) , "PT0.000042S" . to_string( ) ) ;
550
547
assert_eq ! ( Duration :: nanoseconds( 42 ) . to_string( ) , "PT0.000000042S" . to_string( ) ) ;
551
548
assert_eq ! ( ( Duration :: days( 7 ) + Duration :: milliseconds( 6543 ) ) . to_string( ) ,
552
549
"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( ) ) ;
555
550
556
551
// the format specifier should have no effect on `Duration`
557
552
assert_eq ! ( format!( "{:30}" , Duration :: days( 1 ) + Duration :: milliseconds( 2345 ) ) ,
0 commit comments