Skip to content

Commit 4a3d081

Browse files
committed
Data.parse works different in PHP & JS when truthy
See: http://phpjs.org/functions/strtotime/#comment-983199779 Also deleted redundant 'if' to check for text === 'now' Also added | 0 missing to avoid float point numbers.
1 parent 2ae1bed commit 4a3d081

File tree

1 file changed

+9
-14
lines changed

1 file changed

+9
-14
lines changed

functions/datetime/strtotime.js

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
function strtotime (text, now) {
1+
2+
3+
function strtotime(text, now) {
24
// Convert string representation of date and time to a timestamp
35
//
46
// version: 1109.2015
@@ -34,23 +36,16 @@ function strtotime (text, now) {
3436
.toLowerCase();
3537

3638
if (text === 'now') {
37-
return now === null || isNaN(now) ? new Date().getTime() / 1000 | 0 : now | 0;
38-
}
39-
if (!isNaN(parsed = Date.parse(text))) {
40-
return parsed / 1000 | 0;
41-
}
42-
if (text === 'now') {
43-
return new Date().getTime() / 1000; // Return seconds, not milli-seconds
44-
}
45-
if (!isNaN(parsed = Date.parse(text))) {
46-
return parsed / 1000;
39+
return now === null || isNaN(now) ?
40+
new Date().getTime() / 1000 | 0 :
41+
now | 0;
4742
}
4843

4944
match = text.match(/^(\d{2,4})-(\d{2})-(\d{2})(?:\s(\d{1,2}):(\d{2})(?::\d{2})?)?(?:\.(\d+)?)?$/);
5045
if (match) {
51-
year = match[1] >= 0 && match[1] <= 69 ? +match[1] + 2000 : match[1];
46+
year = match[1] >= 0 && match[1] <= 69 ? + match[1] + 2000 : match[1];
5247
return new Date(year, parseInt(match[2], 10) - 1, match[3],
53-
match[4] || 0, match[5] || 0, match[6] || 0, match[7] || 0) / 1000;
48+
match[4] || 0, match[5] || 0, match[6] || 0, match[7] || 0) / 1000 | 0;
5449
}
5550

5651
date = now ? new Date(now * 1000) : new Date();
@@ -139,5 +134,5 @@ function strtotime (text, now) {
139134
//if (!match.every(process))
140135
// return false;
141136

142-
return (date.getTime() / 1000);
137+
return (date.getTime() / 1000) | 0;
143138
}

0 commit comments

Comments
 (0)