From b0a4d5a84a64cc887f80213fb8a2f68fc107c324 Mon Sep 17 00:00:00 2001 From: Alexandr Nikitin Date: Tue, 3 Mar 2015 12:26:23 +0200 Subject: [PATCH 1/2] Fix conversion of date to UTC Method based on one of SO answers: http://stackoverflow.com/questions/948532/how-do-you-convert-a-javascript-date-to-utc --- strftime.js | 14 ++++++++++---- test/test.js | 1 + 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/strftime.js b/strftime.js index cdc5d45..6845863 100644 --- a/strftime.js +++ b/strftime.js @@ -279,10 +279,16 @@ }); } - function dateToUTC(d) { - var msDelta = (d.getTimezoneOffset() || 0) * 60000; - return new Date(d.getTime() + msDelta); - } + function dateToUTC(d) { + return new Date( + d.getUTCFullYear(), + d.getUTCMonth(), + d.getUTCDate(), + d.getUTCHours(), + d.getUTCMinutes(), + d.getUTCSeconds(), + d.getUTCMilliseconds()); + } var RequiredDateMethods = ['getTime', 'getTimezoneOffset', 'getDay', 'getDate', 'getMonth', 'getFullYear', 'getYear', 'getHours', 'getMinutes', 'getSeconds']; function quacksLikeDate(x) { diff --git a/test/test.js b/test/test.js index 48572cd..e2f2ec3 100755 --- a/test/test.js +++ b/test/test.js @@ -104,6 +104,7 @@ assert.format('%y', '11') assert.format('%Z', null, 'GMT') assert.format('%z', null, '+0000') assert.format('%%', '%') // any other char +assert.format('%F %T', null, '1970-01-01 00:00:00', new Date(0)) ok('GMT') From f7799a1d216ddda62a8b156d9846ff65aa115533 Mon Sep 17 00:00:00 2001 From: Sami Samhuri Date: Wed, 4 Mar 2015 09:14:58 -0800 Subject: [PATCH 2/2] correct fuzzy interpretation of old years by new Date() --- strftime.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/strftime.js b/strftime.js index 6845863..1d06d10 100644 --- a/strftime.js +++ b/strftime.js @@ -280,14 +280,22 @@ } function dateToUTC(d) { - return new Date( - d.getUTCFullYear(), + var year = d.getUTCFullYear(); + var date = new Date( + year, d.getUTCMonth(), d.getUTCDate(), d.getUTCHours(), d.getUTCMinutes(), d.getUTCSeconds(), - d.getUTCMilliseconds()); + d.getUTCMilliseconds() + ); + // In old dates, years is incorrectly interpreted as a 2-digit year with base 1900. + // Correct this by setting the year explicitly after the fuzzy creation process. + if (date.getFullYear() != year) { + date.setFullYear(year); + } + return date; } var RequiredDateMethods = ['getTime', 'getTimezoneOffset', 'getDay', 'getDate', 'getMonth', 'getFullYear', 'getYear', 'getHours', 'getMinutes', 'getSeconds'];