Skip to content

Commit fa20d32

Browse files
committed
strtotime should return false instead of 0 on failure
Changed return 0 with return false. Improved code formatting. Changed binary operators with logical operators
1 parent afad582 commit fa20d32

File tree

1 file changed

+53
-40
lines changed

1 file changed

+53
-40
lines changed

functions/datetime/strtotime.js

Lines changed: 53 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ function strtotime (text, now) {
2222
// * returns 3: 1127041200
2323
// * example 4: strtotime('2009-05-04 08:30:00 GMT');
2424
// * returns 4: 1241425800
25-
var parsed, match, today, year, date, days, ranges, len, times, regex, i;
25+
var parsed, match, today, year, date, days, ranges, len, times, regex, i, fail = false;
2626

2727
if (!text) {
28-
return null;
28+
return fail;
2929
}
3030

3131
// Unecessary spaces
@@ -41,96 +41,107 @@ function strtotime (text, now) {
4141
// etc...etc...
4242
// ...therefore we manually parse lots of common date formats
4343
match = text.match(/^(\d{1,4})([\-\.\/\:])(\d{1,2})([\-\.\/\:])(\d{1,4})(?:\s(\d{1,2}):(\d{2})?:?(\d{2})?)?(?:\s([A-Z]+)?)?$/);
44+
4445
if (match && match[2] === match[4]) {
4546
if (match[1] > 1901) {
4647
switch (match[2]) {
4748
case '-': { // YYYY-M-D
48-
if (match[3]>12 | match[5]>31) {
49-
return(0);
49+
if (match[3] > 12 || match[5] > 31) {
50+
return fail;
5051
}
52+
5153
return new Date(match[1], parseInt(match[3], 10) - 1, match[5],
52-
match[6] || 0, match[7] || 0, match[8] || 0, match[9] || 0) / 1000;
54+
match[6] || 0, match[7] || 0, match[8] || 0, match[9] || 0) / 1000;
5355
}
5456
case '.': { // YYYY.M.D is not parsed by strtotime()
55-
return(0);
57+
return fail;
5658
}
5759
case '/': { // YYYY/M/D
58-
if (match[3]>12 | match[5]>31) {
59-
return(0);
60+
if (match[3] > 12 || match[5] > 31) {
61+
return fail;
6062
}
63+
6164
return new Date(match[1], parseInt(match[3], 10) - 1, match[5],
62-
match[6] || 0, match[7] || 0, match[8] || 0, match[9] || 0) / 1000;
65+
match[6] || 0, match[7] || 0, match[8] || 0, match[9] || 0) / 1000;
6366
}
6467
}
65-
}
66-
else if (match[5]>1901) {
68+
} else if (match[5] > 1901) {
6769
switch (match[2]) {
6870
case '-': { // D-M-YYYY
69-
if (match[3]>12 | match[1]>31) {
70-
return(0);
71+
if (match[3] > 12 || match[1] > 31) {
72+
return fail;
7173
}
74+
7275
return new Date(match[5], parseInt(match[3], 10) - 1, match[1],
73-
match[6] || 0, match[7] || 0, match[8] || 0, match[9] || 0) / 1000;
76+
match[6] || 0, match[7] || 0, match[8] || 0, match[9] || 0) / 1000;
7477
}
7578
case '.': { // D.M.YYYY
76-
if (match[3]>12 | match[1]>31) {
77-
return(0);
79+
if (match[3] > 12 || match[1] > 31) {
80+
return fail;
7881
}
82+
7983
return new Date(match[5], parseInt(match[3], 10) - 1, match[1],
80-
match[6] || 0, match[7] || 0, match[8] || 0, match[9] || 0) / 1000;
84+
match[6] || 0, match[7] || 0, match[8] || 0, match[9] || 0) / 1000;
8185
}
8286
case '/': { // M/D/YYYY
83-
if (match[1]>12 | match[3]>31) {
84-
return(0);
87+
if (match[1] > 12 || match[3] > 31) {
88+
return fail;
8589
}
90+
8691
return new Date(match[5], parseInt(match[1], 10) - 1, match[3],
87-
match[6] || 0, match[7] || 0, match[8] || 0, match[9] || 0) / 1000;
92+
match[6] || 0, match[7] || 0, match[8] || 0, match[9] || 0) / 1000;
8893
}
8994
}
9095
}
9196
else {
9297
switch (match[2]) {
9398
case '-': { // YY-M-D
94-
if (match[3]>12 | match[5]>31 | (match[1] < 70 & match[1]>38)) {
95-
return(0);
99+
if (match[3] > 12 || match[5] > 31 || (match[1] < 70 && match[1] > 38)) {
100+
return fail;
96101
}
102+
97103
year = match[1] >= 0 && match[1] <= 38 ? +match[1] + 2000 : match[1];
98104
return new Date(year, parseInt(match[3], 10) - 1, match[5],
99-
match[6] || 0, match[7] || 0, match[8] || 0, match[9] || 0) / 1000;
105+
match[6] || 0, match[7] || 0, match[8] || 0, match[9] || 0) / 1000;
100106
}
101107
case '.': { // D.M.YY or H.MM.SS
102-
if (match[5]>=70) { // D.M.YY
103-
if (match[3]>12 | match[1]>31) {
104-
return(0);
108+
if (match[5] >= 70) { // D.M.YY
109+
if (match[3]>12 || match[1]>31) {
110+
return fail;
105111
}
112+
106113
return new Date(match[5], parseInt(match[3], 10) - 1, match[1],
107-
match[6] || 0, match[7] || 0, match[8] || 0, match[9] || 0) / 1000;
114+
match[6] || 0, match[7] || 0, match[8] || 0, match[9] || 0) / 1000;
108115
}
109-
if (match[5]<60 & !(match[6])) { // H.MM.SS
110-
if (match[1]>23 | match[3]>59) {
111-
return(0);
116+
if (match[5] < 60 && !match[6]) { // H.MM.SS
117+
if (match[1] > 23 || match[3] > 59) {
118+
return fail;
112119
}
120+
113121
today = new Date();
114122
return new Date(today.getFullYear(), today.getMonth(), today.getDate(),
115-
match[1] || 0, match[3] || 0, match[5] || 0, match[9] || 0) / 1000;
123+
match[1] || 0, match[3] || 0, match[5] || 0, match[9] || 0) / 1000;
116124
}
117-
return(0); // invalid format, cannot be parsed
125+
126+
return fail; // invalid format, cannot be parsed
118127
}
119128
case '/': { // M/D/YY
120-
if (match[1]>12 | match[3]>31 | (match[5] < 70 & match[5]>38)) {
121-
return(0);
129+
if (match[1] > 12 || match[3] > 31 || (match[5] < 70 && match[5] > 38)) {
130+
return fail;
122131
}
132+
123133
year = match[5] >= 0 && match[5] <= 38 ? +match[5] + 2000 : match[5];
124134
return new Date(year, parseInt(match[1], 10) - 1, match[3],
125-
match[6] || 0, match[7] || 0, match[8] || 0, match[9] || 0) / 1000;
135+
match[6] || 0, match[7] || 0, match[8] || 0, match[9] || 0) / 1000;
126136
}
127137
case ':': { // HH:MM:SS
128-
if (match[1]>23 | match[3]>59 | match[5]>59) {
129-
return(0);
138+
if (match[1] > 23 || match[3] > 59 || match[5] > 59) {
139+
return fail;
130140
}
141+
131142
today = new Date();
132143
return new Date(today.getFullYear(), today.getMonth(), today.getDate(),
133-
match[1] || 0, match[3] || 0, match[5] || 0) / 1000;
144+
match[1] || 0, match[3] || 0, match[5] || 0) / 1000;
134145
}
135146
}
136147
}
@@ -198,6 +209,7 @@ function strtotime (text, now) {
198209
if (ranges.hasOwnProperty(range) && !splt[1].match(/^mon(day|\.)?$/i)) {
199210
return date['set' + ranges[range]](date['get' + ranges[range]]() + num);
200211
}
212+
201213
if (range === 'wee') {
202214
return date.setDate(date.getDate() + (num * 7));
203215
}
@@ -208,6 +220,7 @@ function strtotime (text, now) {
208220
else if (!typeIsNumber) {
209221
return false;
210222
}
223+
211224
return true;
212225
}
213226

@@ -218,12 +231,12 @@ function strtotime (text, now) {
218231

219232
match = text.match(new RegExp(regex, 'gi'));
220233
if (!match) {
221-
return false;
234+
return fail;
222235
}
223236

224237
for (i = 0, len = match.length; i < len; i++) {
225238
if (!process(match[i])) {
226-
return false;
239+
return fail;
227240
}
228241
}
229242

0 commit comments

Comments
 (0)