Skip to content

Commit

Permalink
Fix date parsing:
Browse files Browse the repository at this point in the history
Previous algorithm failed at the last day of the current month. This was caused by the fact that setUTCMonth method was used. According to MDN(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setUTCMonth) it accepts two parameters. If second parameter (dayValue) is not specified then V8 uses current day value. So if you are setting April as month, and current system date is March 31 then if will be resolved as May 1 which doesn't works for us. Therefore, we will parse tokens separately and then create new Date() object using Date.UTC()
  • Loading branch information
inikulin committed Mar 30, 2015
1 parent 3b11467 commit 553d20b
Showing 1 changed file with 20 additions and 25 deletions.
45 changes: 20 additions & 25 deletions lib/cookie.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ function parseDate(str,strict) {
if (!str) {
return;
}
var found_time, found_dom, found_month, found_year;

/* RFC6265 S5.1.1:
* 2. Process each date-token sequentially in the order the date-tokens
Expand All @@ -98,6 +97,13 @@ function parseDate(str,strict) {
return;
}

var hour = null;
var minutes = null;
var seconds = null;
var day = null;
var month = null;
var year = null;

var date = new Date();
date.setMilliseconds(0);

Expand All @@ -115,12 +121,12 @@ function parseDate(str,strict) {
* the date-token, respectively. Skip the remaining sub-steps and continue
* to the next date-token.
*/
if (!found_time) {
if (seconds === null) {
result = TIME.exec(token);
if (result) {
var hour = parseInt(result[1], 10);
var minutes = parseInt(result[2], 10);
var seconds = parseInt(result[3], 10);
hour = parseInt(result[1], 10);
minutes = parseInt(result[2], 10);
seconds = parseInt(result[3], 10);
/* RFC6265 S5.1.1.5:
* [fail if]
* * the hour-value is greater than 23,
Expand All @@ -131,10 +137,6 @@ function parseDate(str,strict) {
return;
}

found_time = true;
date.setUTCHours(result[1]);
date.setUTCMinutes(result[2]);
date.setUTCSeconds(result[3]);
continue;
}
}
Expand All @@ -144,18 +146,16 @@ function parseDate(str,strict) {
* the day-of-month-value to the number denoted by the date-token. Skip
* the remaining sub-steps and continue to the next date-token.
*/
if (!found_dom) {
if (day === null) {
result = DAY_OF_MONTH.exec(token);
if (result) {
var dom = parseInt(result, 10);
day = parseInt(result, 10);
/* RFC6265 S5.1.1.5:
* [fail if] the day-of-month-value is less than 1 or greater than 31
*/
if(dom < 1 || dom > 31) {
if(day < 1 || day > 31) {
return;
}
found_dom = true;
date.setUTCDate(result[1]);
continue;
}
}
Expand All @@ -165,11 +165,10 @@ function parseDate(str,strict) {
* the month denoted by the date-token. Skip the remaining sub-steps and
* continue to the next date-token.
*/
if (!found_month) {
if (month === null) {
result = MONTH.exec(token);
if (result) {
found_month = true;
date.setUTCMonth(MONTH_TO_NUM[result[1].toLowerCase()]);
month = MONTH_TO_NUM[result[1].toLowerCase()];
continue;
}
}
Expand All @@ -179,10 +178,10 @@ function parseDate(str,strict) {
* denoted by the date-token. Skip the remaining sub-steps and continue to
* the next date-token.
*/
if (!found_year) {
if (year === null) {
result = YEAR.exec(token);
if (result) {
var year = parseInt(result[0], 10);
year = parseInt(result[0], 10);
/* From S5.1.1:
* 3. If the year-value is greater than or equal to 70 and less
* than or equal to 99, increment the year-value by 1900.
Expand All @@ -198,20 +197,16 @@ function parseDate(str,strict) {
if (year < 1601) {
return; // 5. ... the year-value is less than 1601
}

found_year = true;
date.setUTCFullYear(year);
continue;
}
}
}

if (!(found_time && found_dom && found_month && found_year)) {
if (seconds === null || day === null || month === null || year === null) {
return; // 5. ... at least one of the found-day-of-month, found-month, found-
// year, or found-time flags is not set,
}

return date;
return new Date(Date.UTC(year, month, day, hour, minutes, seconds));
}

function formatDate(date) {
Expand Down

0 comments on commit 553d20b

Please sign in to comment.