@@ -14,39 +14,41 @@ function strptime(dateStr, format) {
14
14
// Needs more thorough testing and examples
15
15
16
16
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
+ } ;
50
52
51
53
// BEGIN STATIC
52
54
var _NWS = / \S / ,
131
133
} ;
132
134
133
135
// BEGIN PROCESSING CHARACTERS
134
- for ( var i = 0 , j = 0 ; i < format . length ; i ++ ) {
136
+ for ( i = 0 , j = 0 ; i < format . length ; i ++ ) {
135
137
if ( format . charAt ( i ) === '%' ) {
136
138
var literalPos = [ '%' , 'n' , 't' ] . indexOf ( format . charAt ( i + 1 ) ) ;
137
139
if ( literalPos !== - 1 ) {
138
140
if ( [ '%' , '\n' , '\t' ] . indexOf ( dateStr . charAt ( j ) ) === literalPos ) { // a matched literal
139
- ++ i , ++ j ; // skip beyond
141
+ ++ i ;
142
+ ++ j ; // skip beyond
140
143
continue ;
141
144
}
142
145
// Format indicated a percent literal, but not actually present
322
325
break ;
323
326
default :
324
327
throw 'Unrecognized formatting character in strptime()' ;
325
- break ;
326
328
}
327
329
} catch ( e ) {
328
330
if ( e === 'No match in string' ) { // Allow us to exit
340
342
} else if ( format . charAt ( i ) . search ( _NWS ) !== - 1 ) { // Any extra formatting characters besides white-space causes
341
343
// problems (do check after WS though, as may just be WS in string before next character)
342
344
return false ;
343
- } else { // Extra WS in format
345
+ }
346
+ // Otherwise, Extra WS in format
344
347
// Adjust strings when encounter non-matching whitespace, so they align in future checks above
345
348
// Will check on next iteration (against same (non-WS) string character)
346
349
}
0 commit comments