php-src icon indicating copy to clipboard operation
php-src copied to clipboard

Incorrect DST Calculation in Intl

Open jimbo2150 opened this issue 4 years ago • 5 comments

Description

The following code:

<?php
date_default_timezone_set('America/New_York');

$cal = new IntlGregorianCalendar(2022, 2, 12, 1, 59, 59);
echo IntlDateFormatter::formatObject($cal), "\n";

$cal->add(IntlCalendar::FIELD_DAY_OF_MONTH, 1);
echo IntlDateFormatter::formatObject($cal), "\n";

$cal->add(IntlCalendar::FIELD_SECOND, 1);
echo IntlDateFormatter::formatObject($cal), "\n";

Resulted in this output:

Mar 12, 2022, 1:59:59 AM
Mar 13, 2022, 1:59:59 AM
Mar 13, 2022, 3:00:00 AM

But I expected this output instead:

Mar 12, 2022, 1:59:59 AM
Mar 13, 2022, 2:59:59 AM
Mar 13, 2022, 3:00:00 AM

Daylight saving time (DST) is not taken into account when calculating non-time values in Intl calendar class. If I add a second, hour, or minute, the result ends up being correct - except in the above example where March 12 @ 2:00 is already passed by a day & adding 1 second to March 13 then adds the additional hour. But adding days, months, years fails to calculate the correct time when DST boundaries are crossed.

PHP Version

PHP 8.1.5

Operating System

No response

jimbo2150 avatar Apr 23 '22 20:04 jimbo2150

I don't follow. You know that DST started at 2am on March 13th, right?

damianwadley avatar Apr 23 '22 20:04 damianwadley

Was testing some things and changed the date, but still get the same output reguardless:

<?php
date_default_timezone_set('America/New_York');

$cal = new IntlGregorianCalendar(2022, 2, 13, 1, 59, 59);
echo IntlDateFormatter::formatObject($cal), "\n";

$cal->add(IntlCalendar::FIELD_DAY_OF_MONTH, 1);
echo IntlDateFormatter::formatObject($cal), "\n";

$cal->add(IntlCalendar::FIELD_SECOND, 1);
echo IntlDateFormatter::formatObject($cal), "\n";
Mar 13, 2022, 1:59:59 AM
Mar 14, 2022, 1:59:59 AM
Mar 14, 2022, 2:00:00 AM

jimbo2150 avatar Apr 23 '22 21:04 jimbo2150

Because you're adding 1 day. As in a calendar day. Which, on March 13th, was only 23 hours long.

If you're thinking that it should be 2:59:59 because that's 24 hours later then you need to be adding 24 hours. https://3v4l.org/pj5rP

damianwadley avatar Apr 23 '22 21:04 damianwadley

If I were creating a new date with that time, I would agree. But it's going from March 13 prior to the DST change to a date after. Seems a bit strange that adding hours specifically would account for the change while adding a day does not. They both cross a DST boundary and seems like they should both account for it.

Also don't think the majority of people are going to think, I need to add a month, let me calculate that in hours and add it to make sure DST is accounted for... No, they will just add a day, a month, etc. and end up with the wrong time (unless they are also changing a time value along with a day/month value).

jimbo2150 avatar Apr 23 '22 22:04 jimbo2150

They both cross a DST boundary and seems like they should both account for it.

That's a valid opinion, one that I think most human beings would not consider themselves, but a lot of languages and libraries don't work that way. Because Dates Are Hard™️, and there is no single obvious solution to everything.

Here, what libicu (which powers the intl extension) and actually also PHP (in the date extension) do is take the simplest approach: literally add.

  Mar 13th 1:59am
+ 1 day
=================
  Mar 14th 1:59am

As long as the result makes sense* then there's nothing else that needs to be done. With hours it works like

  Mar 13th 1:59am
+          24 hours
=================
  Mar 13th 1:59am
+          1 hour  2:59am -> 3:59am because of DST
+          1 hour  4:59am
+          1 hour  5:59am
+          ...
=================
 Mar 14th 2:59am

(* Mar 12th 2:00am + 1 day = Mar 13th 2:00am which doesn't make sense because of DST and so is "corrected" to 3:00am.)

damianwadley avatar Apr 23 '22 22:04 damianwadley