Maheshjayachandran / closure-library

Automatically exported from code.google.com/p/closure-library
0 stars 0 forks source link

i18n.DateTimeFormat is off by a day when crossing over DST from New_York to Los_Angeles #416

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago

What steps will reproduce the problem?
1. Create an i18n.TimeZone with the America/LosAngeles Data that is in an 
example file. Like this:
var tz = goog.i18n.TimeZone.createTimeZone(LA timezone data that is in the 
example files);

2. Create an i18n.DateTimeFormat like this:
var formatter = new goog.i18n.DateTimeFormat('MM/dd/yyy HH:mm:ss z');
(Any formatter should work as long as you have Day and time so the error is 
easy to see)

3. This step is kind of tricky. My computer's time zone is America/New_York and 
to use these steps your computer should be on that timezone too. Otherwise 
you'll have to create a time that is on the DST transition day for your 
timezone. And if you are in the America/PDT or PST then you will have to use 
another timezone besides LA. Here is what I did: 

Create a new Date that is on a DST transition day and time. Note that it has to 
be at 1 o'clock, other cases work. Like this:
myDate = new Date(2011, 10, 06, 1, 0, 0);

4. Lastly, use the formatter to output the date in the LA timezone:
formatter.format(myDate, tz)

What is the expected output? What do you see instead?
The expected Output is 11/05/2011 22:00:00 PDT or 11/05/2011 23:00:00 PDT 
depending on if the original date was in PDT or PST. Either of those answers 
would be acceptable. Probably the 23:00:00 one b\c the date creates to PST.

Instead what I see is 11/06/2011 23:00:00 PDT. As you can see this is off by 
one day.

What version of the product are you using? On what operating system?
I am using revision 1545 on linux ubuntu

Please provide any additional information below.

Original issue reported on code.google.com by Mike.For...@gmail.com on 3 Feb 2012 at 8:49

GoogleCodeExporter commented 9 years ago
Ok, easier way to reproduce the problem.

From above, do 
step 1. create the LA timezone.
step 2. create the formatter
**step 3. create a date with this timestamp: 1320562620000
          myDate = new Date(1320562620000)
step 4. use formatter to output

Expected output: 11/05/2011 23:57:00:0000 PDT
Actual output: 11/06/2011 23:57:00:0000 PDT

Using the same version of product and linux ubuntu

Original comment by Mike.For...@gmail.com on 16 Feb 2012 at 2:06

GoogleCodeExporter commented 9 years ago
Proposed Solution: Not 100% Sure on this, but I think this will work. Basically 
If the hour difference between the original date and the new time does not 
equal the expected amount (calculated based upon offsets of date and timezone), 
we assume that it is because the day has changed. Thus we add or subtract a day 
accordingly. This code is in the goog.i18n.DateTimeFormat.prototype.format 
function.

// in daylight time switch on/off hour, diff adjustment could alter time
  // because of timeZone offset change, move 1 day forward or backward.
  if (opt_timeZone &&
      dateForDate.getTimezoneOffset() != date.getTimezoneOffset()) {
    dateChange = diff > 0 ? 1 : -1;
    diff += diff > 0 ? -24 * 60 * 60000 : 24 * 60 * 60000;
    dateForTime = new Date(date.getTime() + diff);
    if (Math.abs(date.getHours() - dateForTime.getHours()) !== 
           Math.abs(opt_timeZone.getOffset(date) - date.getTimezoneOffset()) / 60) {
    dateForDate = new Date(date.getTime() + (dateChange * 24 * 60 * 60000));
    }
  }

Original comment by Mike.For...@gmail.com on 16 Feb 2012 at 4:18

GoogleCodeExporter commented 9 years ago
So that solution doesn't work 100% of the time. A better solution that I think 
will work is just adding 

dateForDate = new Date(dateForDate.getTime() + (
                        dateForDate.getTimezoneOffset() -
                        date.getTimezoneOffset()) * 60000);

instead of the dateChange assignment and if statement. This will update the day 
if it changes based on the time update. This is necessary b\c sometimes the 
time update switches the date.

Original comment by Mike.For...@gmail.com on 16 Apr 2012 at 6:39