Skip to content

Commit 2d58063

Browse files
committed
strptime: a few JSLint fixes, spacing streamlining, and remove accidental global
1 parent 778bd20 commit 2d58063

File tree

1 file changed

+40
-37
lines changed

1 file changed

+40
-37
lines changed

functions/datetime/strptime.js

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -14,39 +14,41 @@ function strptime(dateStr, format) {
1414
// Needs more thorough testing and examples
1515

1616
var retObj = {
17-
tm_sec: 0,
18-
tm_min: 0,
19-
tm_hour: 0,
20-
tm_mday: 0,
21-
tm_mon: 0,
22-
tm_year: 0,
23-
tm_wday: 0,
24-
tm_yday: 0,
25-
unparsed: ''
26-
},
27-
that = this,
28-
amPmOffset = 0,
29-
prevHour = false,
30-
_date = function() {
31-
var o = retObj;
32-
// We set date to at least 1 to ensure year or month doesn't go backwards
33-
return _reset(new Date(Date.UTC(o.tm_year + 1900, o.tm_mon, o.tm_mday || 1, o.tm_hour, o.tm_min, o.tm_sec)), o.tm_mday);
34-
};
35-
_reset = function(dateObj, realMday) {
36-
// realMday is to allow for a value of 0 in return results (but without
37-
// messing up the Date() object)
38-
var o = retObj;
39-
var d = dateObj;
40-
o.tm_sec = d.getUTCSeconds();
41-
o.tm_min = d.getUTCMinutes();
42-
o.tm_hour = d.getUTCHours();
43-
o.tm_mday = realMday === 0 ? realMday : d.getUTCDate();
44-
o.tm_mon = d.getUTCMonth();
45-
o.tm_year = d.getUTCFullYear() - 1900;
46-
o.tm_wday = realMday === 0 ? (d.getUTCDay() > 0 ? d.getUTCDay() - 1 : 6) : d.getUTCDay();
47-
var jan1 = new Date(Date.UTC(d.getUTCFullYear(), 0, 1));
48-
o.tm_yday = Math.ceil((d - jan1) / (1000 * 60 * 60 * 24));
49-
};
17+
tm_sec: 0,
18+
tm_min: 0,
19+
tm_hour: 0,
20+
tm_mday: 0,
21+
tm_mon: 0,
22+
tm_year: 0,
23+
tm_wday: 0,
24+
tm_yday: 0,
25+
unparsed: ''
26+
},
27+
i = 0,
28+
that = this,
29+
amPmOffset = 0,
30+
prevHour = false,
31+
_reset = function(dateObj, realMday) {
32+
// realMday is to allow for a value of 0 in return results (but without
33+
// messing up the Date() object)
34+
var jan1,
35+
o = retObj,
36+
d = dateObj;
37+
o.tm_sec = d.getUTCSeconds();
38+
o.tm_min = d.getUTCMinutes();
39+
o.tm_hour = d.getUTCHours();
40+
o.tm_mday = realMday === 0 ? realMday : d.getUTCDate();
41+
o.tm_mon = d.getUTCMonth();
42+
o.tm_year = d.getUTCFullYear() - 1900;
43+
o.tm_wday = realMday === 0 ? (d.getUTCDay() > 0 ? d.getUTCDay() - 1 : 6) : d.getUTCDay();
44+
jan1 = new Date(Date.UTC(d.getUTCFullYear(), 0, 1));
45+
o.tm_yday = Math.ceil((d - jan1) / (1000 * 60 * 60 * 24));
46+
},
47+
_date = function() {
48+
var o = retObj;
49+
// We set date to at least 1 to ensure year or month doesn't go backwards
50+
return _reset(new Date(Date.UTC(o.tm_year + 1900, o.tm_mon, o.tm_mday || 1, o.tm_hour, o.tm_min, o.tm_sec)), o.tm_mday);
51+
};
5052

5153
// BEGIN STATIC
5254
var _NWS = /\S/,
@@ -131,12 +133,13 @@ Oy
131133
};
132134

133135
// BEGIN PROCESSING CHARACTERS
134-
for (var i = 0, j = 0; i < format.length; i++) {
136+
for (i = 0, j = 0; i < format.length; i++) {
135137
if (format.charAt(i) === '%') {
136138
var literalPos = ['%', 'n', 't'].indexOf(format.charAt(i + 1));
137139
if (literalPos !== -1) {
138140
if (['%', '\n', '\t'].indexOf(dateStr.charAt(j)) === literalPos) { // a matched literal
139-
++i, ++j; // skip beyond
141+
++i;
142+
++j; // skip beyond
140143
continue;
141144
}
142145
// Format indicated a percent literal, but not actually present
@@ -322,7 +325,6 @@ Oy
322325
break;
323326
default:
324327
throw 'Unrecognized formatting character in strptime()';
325-
break;
326328
}
327329
} catch (e) {
328330
if (e === 'No match in string') { // Allow us to exit
@@ -340,7 +342,8 @@ Oy
340342
} else if (format.charAt(i).search(_NWS) !== -1) { // Any extra formatting characters besides white-space causes
341343
// problems (do check after WS though, as may just be WS in string before next character)
342344
return false;
343-
} else { // Extra WS in format
345+
}
346+
// Otherwise, Extra WS in format
344347
// Adjust strings when encounter non-matching whitespace, so they align in future checks above
345348
// Will check on next iteration (against same (non-WS) string character)
346349
}

0 commit comments

Comments
 (0)