Is the addition of a duration to a date-time defined in ISO 8601?

Posted by Benjamin on Programmers See other posts from Programmers or by Benjamin
Published on 2012-12-19T12:13:57Z Indexed on 2012/12/19 17:13 UTC
Read the original article Hit count: 212

Filed under:
|

I've writing a date-time library, and need to implement the addition of a duration to a date-time.

If I add a 1 month duration: P1M to the 31st March 2012: 2012-03-31, does the standard define what the result is?

Because the resulting date (31st April) does not exist, there are at least two options:

  • Fall back to the last day of the resulting month. This is the approach currently taken by the ThreeTen API, the (alpha) reference implementation of JSR-310:
ZonedDateTime date = ZonedDateTime.parse("2012-03-31T00:00:00Z");
Period duration = Period.parse("P1M");
System.out.println(date.plus(duration).toString());

// 2012-04-30T00:00Z
  • Carry the extra day to the next month. This is the approach taken by the DateTime class in PHP:
$date = new DateTime('2012-03-31T00:00:00Z');
$duration = new DateInterval('P1M');
echo $date->add($duration)->format('c');

// 2012-05-01T00:00:00+00:00

I'm surprised that two date-time libraries contradict on this point, so I'm wondering whether the standard defines the result of this operation?

© Programmers or respective owner

Related posts about time

Related posts about iso