Avoid glitches around DST change by inverting utc logic

This commit is contained in:
Raimund Bauer 2016-12-27 14:28:03 +01:00
parent f8e5b11c9b
commit 7d4802425e

View file

@ -166,7 +166,9 @@
if (_useUtcBasedDate) { if (_useUtcBasedDate) {
// how to avoid duplication of date instantiation for utc here? // how to avoid duplication of date instantiation for utc here?
// we tied to getTimezoneOffset of the current date // we tied to getTimezoneOffset of the current date
_cachedDate = new Date(_cachedDateTimestamp + getTimestampToUtcOffsetFor(_cachedDate) + _customTimezoneOffset); //_cachedDate = new Date(_cachedDateTimestamp + getTimestampToUtcOffsetFor(_cachedDate) + _customTimezoneOffset);
} else {
_cachedDate = new Date(_cachedDateTimestamp - getTimestampToUtcOffsetFor(_cachedDate) - _customTimezoneOffset);
} }
} }
else { else {
@ -178,7 +180,9 @@
timestamp = date.getTime(); timestamp = date.getTime();
if (_useUtcBasedDate) { if (_useUtcBasedDate) {
date = new Date(date.getTime() + getTimestampToUtcOffsetFor(date) + _customTimezoneOffset); //date = new Date(date.getTime() + getTimestampToUtcOffsetFor(date) + _customTimezoneOffset);
} else {
date = new Date(date.getTime() - getTimestampToUtcOffsetFor(date) - _customTimezoneOffset);
} }
} }
@ -230,19 +234,19 @@
// 'Thursday' // 'Thursday'
// case 'A': // case 'A':
case 65: case 65:
resultString += locale.days[date.getDay()]; resultString += locale.days[date.getUTCDay()];
break; break;
// 'January' // 'January'
// case 'B': // case 'B':
case 66: case 66:
resultString += locale.months[date.getMonth()]; resultString += locale.months[date.getUTCMonth()];
break; break;
// '19' // '19'
// case 'C': // case 'C':
case 67: case 67:
resultString += padTill2(Math.floor(date.getFullYear() / 100), padding); resultString += padTill2(Math.floor(date.getUTCFullYear() / 100), padding);
break; break;
// '01/01/70' // '01/01/70'
@ -260,13 +264,13 @@
// '00' // '00'
// case 'H': // case 'H':
case 72: case 72:
resultString += padTill2(date.getHours(), padding); resultString += padTill2(date.getUTCHours(), padding);
break; break;
// '12' // '12'
// case 'I': // case 'I':
case 73: case 73:
resultString += padTill2(hours12(date.getHours()), padding); resultString += padTill2(hours12(date.getUTCHours()), padding);
break; break;
// '000' // '000'
@ -278,13 +282,13 @@
// '00' // '00'
// case 'M': // case 'M':
case 77: case 77:
resultString += padTill2(date.getMinutes(), padding); resultString += padTill2(date.getUTCMinutes(), padding);
break; break;
// 'am' // 'am'
// case 'P': // case 'P':
case 80: case 80:
resultString += date.getHours() < 12 ? locale.am : locale.pm; resultString += date.getUTCHours() < 12 ? locale.am : locale.pm;
break; break;
// '00:00' // '00:00'
@ -296,7 +300,7 @@
// '00' // '00'
// case 'S': // case 'S':
case 83: case 83:
resultString += padTill2(date.getSeconds(), padding); resultString += padTill2(date.getUTCSeconds(), padding);
break; break;
// '00:00:00' // '00:00:00'
@ -326,7 +330,7 @@
// '1970' // '1970'
// case 'Y': // case 'Y':
case 89: case 89:
resultString += date.getFullYear(); resultString += date.getUTCFullYear();
break; break;
// 'GMT' // 'GMT'
@ -345,13 +349,13 @@
// 'Thu' // 'Thu'
// case 'a': // case 'a':
case 97: case 97:
resultString += locale.shortDays[date.getDay()]; resultString += locale.shortDays[date.getUTCDay()];
break; break;
// 'Jan' // 'Jan'
// case 'b': // case 'b':
case 98: case 98:
resultString += locale.shortMonths[date.getMonth()]; resultString += locale.shortMonths[date.getUTCMonth()];
break; break;
// '' // ''
@ -363,25 +367,25 @@
// '01' // '01'
// case 'd': // case 'd':
case 100: case 100:
resultString += padTill2(date.getDate(), padding); resultString += padTill2(date.getUTCDate(), padding);
break; break;
// ' 1' // ' 1'
// case 'e': // case 'e':
case 101: case 101:
resultString += padTill2(date.getDate(), padding == null ? ' ' : padding); resultString += padTill2(date.getUTCDate(), padding == null ? ' ' : padding);
break; break;
// 'Jan' // 'Jan'
// case 'h': // case 'h':
case 104: case 104:
resultString += locale.shortMonths[date.getMonth()]; resultString += locale.shortMonths[date.getUTCMonth()];
break; break;
// '000' // '000'
// case 'j': // case 'j':
case 106: case 106:
var y = new Date(date.getFullYear(), 0, 1); var y = new Date(date.getUTCFullYear(), 0, 1);
var day = Math.ceil((date.getTime() - y.getTime()) / (1000 * 60 * 60 * 24)); var day = Math.ceil((date.getTime() - y.getTime()) / (1000 * 60 * 60 * 24));
resultString += padTill3(day); resultString += padTill3(day);
break; break;
@ -389,19 +393,19 @@
// ' 0' // ' 0'
// case 'k': // case 'k':
case 107: case 107:
resultString += padTill2(date.getHours(), padding == null ? ' ' : padding); resultString += padTill2(date.getUTCHours(), padding == null ? ' ' : padding);
break; break;
// '12' // '12'
// case 'l': // case 'l':
case 108: case 108:
resultString += padTill2(hours12(date.getHours()), padding == null ? ' ' : padding); resultString += padTill2(hours12(date.getUTCHours()), padding == null ? ' ' : padding);
break; break;
// '01' // '01'
// case 'm': // case 'm':
case 109: case 109:
resultString += padTill2(date.getMonth() + 1, padding); resultString += padTill2(date.getUTCMonth() + 1, padding);
break; break;
// '\n' // '\n'
@ -413,13 +417,13 @@
// '1st' // '1st'
// case 'o': // case 'o':
case 111: case 111:
resultString += String(date.getDate()) + ordinal(date.getDate()); resultString += String(date.getUTCDate()) + ordinal(date.getUTCDate());
break; break;
// 'AM' // 'AM'
// case 'p': // case 'p':
case 112: case 112:
resultString += date.getHours() < 12 ? locale.AM : locale.PM; resultString += date.getUTCHours() < 12 ? locale.AM : locale.PM;
break; break;
// '12:00:00 AM' // '12:00:00 AM'
@ -443,7 +447,7 @@
// '4' // '4'
// case 'u': // case 'u':
case 117: case 117:
var day = date.getDay(); var day = date.getUTCDay();
resultString += day === 0 ? 7 : day; resultString += day === 0 ? 7 : day;
break; // 1 - 7, Monday is first day of the week break; // 1 - 7, Monday is first day of the week
@ -456,7 +460,7 @@
// '4' // '4'
// case 'w': // case 'w':
case 119: case 119:
resultString += date.getDay(); resultString += date.getUTCDay();
break; // 0 - 6, Sunday is first day of the week break; // 0 - 6, Sunday is first day of the week
// '12/31/69' // '12/31/69'
@ -468,7 +472,7 @@
// '70' // '70'
// case 'y': // case 'y':
case 121: case 121:
resultString += ('' + date.getFullYear()).slice(2); resultString += ('' + date.getUTCFullYear()).slice(2);
break; break;
// '+0000' // '+0000'
@ -591,7 +595,7 @@
// This works by shifting the weekday back by one day if we // This works by shifting the weekday back by one day if we
// are treating Monday as the first day of the week. // are treating Monday as the first day of the week.
var weekday = date.getDay(); var weekday = date.getUTCDay();
if (firstWeekday === 'monday') { if (firstWeekday === 'monday') {
if (weekday === 0) // Sunday if (weekday === 0) // Sunday
weekday = 6; weekday = 6;
@ -599,8 +603,8 @@
weekday--; weekday--;
} }
var firstDayOfYearUtc = Date.UTC(date.getFullYear(), 0, 1), var firstDayOfYearUtc = Date.UTC(date.getUTCFullYear(), 0, 1),
dateUtc = Date.UTC(date.getFullYear(), date.getMonth(), date.getDate()), dateUtc = Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate()),
yday = Math.floor((dateUtc - firstDayOfYearUtc) / 86400000), yday = Math.floor((dateUtc - firstDayOfYearUtc) / 86400000),
weekNum = (yday + 7 - weekday) / 7; weekNum = (yday + 7 - weekday) / 7;