@@ -149,57 +149,50 @@ function compareDate(d1: string, d2: string): number | undefined {
149
149
return 0
150
150
}
151
151
152
- const TIME = / ^ ( \d \d ) : ( \d \d ) : ( \d \d ) ( \. \d + ) ? ( z | [ + - ] \d \d (?: : ? \d \d ) ? ) ? $ / i
153
- const PLUS_MINUS = / ^ [ + - ] /
154
- const TIMEZONE = / ^ [ Z z ] $ /
155
- const ISO_8601_TIME = / ^ [ + - ] (?: [ 0 1 ] [ 0 - 9 ] | 2 [ 0 - 4 ] ) (?: : ? [ 0 - 5 ] [ 0 - 9 ] ) ? $ /
152
+ const TIME = / ^ ( \d \d ) : ( \d \d ) : ( \d \d (?: \. \d + ) ? ) ( z | ( [ + - ] \d \d ) (?: : ? ( \d \d ) ) ? ) ? $ / i
156
153
157
- function time ( str : string , withTimeZone ?: boolean , strict ?: boolean ) : boolean {
154
+ function time ( str : string , withTimeZone ?: boolean , strictTime ?: boolean ) : boolean {
158
155
const matches : string [ ] | null = TIME . exec ( str )
159
156
if ( ! matches ) return false
160
-
161
- const hour : number = + matches [ 1 ]
162
- const minute : number = + matches [ 2 ]
163
- const second : number = + matches [ 3 ]
164
- const timeZone : string = matches [ 5 ]
157
+ const hr : number = + matches [ 1 ]
158
+ const min : number = + matches [ 2 ]
159
+ const sec : number = + matches [ 3 ]
160
+ const tz : string | undefined = matches [ 4 ]
161
+ const tzH : number = + ( matches [ 5 ] || 0 )
162
+ const tzM : number = + ( matches [ 6 ] || 0 )
165
163
return (
166
- ( ( hour <= 23 && minute <= 59 && second <= 59 ) ||
167
- ( hour === 23 && minute === 59 && second === 60 ) ) &&
168
- ( ! withTimeZone ||
169
- ( strict
170
- ? TIMEZONE . test ( timeZone ) ||
171
- ( PLUS_MINUS . test ( timeZone ) && time ( timeZone . slice ( 1 ) + ":00" ) ) ||
172
- ISO_8601_TIME . test ( timeZone )
173
- : timeZone !== "" ) )
164
+ ( ( hr <= 23 && min <= 59 && sec < 60 && tzH <= 24 && tzM < 60 ) ||
165
+ // leap second
166
+ ( hr - tzH === 23 && min - tzM === 59 && sec < 61 && tzH <= 24 && tzM < 60 ) ) &&
167
+ ( ! withTimeZone || ( tz !== "" && ( ! strictTime || ! ! tz ) ) )
174
168
)
175
169
}
176
170
177
- function strict_time ( str : string , withTimeZone ?: boolean ) : boolean {
178
- return time ( str , withTimeZone , true )
171
+ function strict_time ( str : string ) : boolean {
172
+ return time ( str , true , true )
179
173
}
180
174
181
175
function compareTime ( t1 : string , t2 : string ) : number | undefined {
182
176
if ( ! ( t1 && t2 ) ) return undefined
183
177
const a1 = TIME . exec ( t1 )
184
178
const a2 = TIME . exec ( t2 )
185
179
if ( ! ( a1 && a2 ) ) return undefined
186
- t1 = a1 [ 1 ] + a1 [ 2 ] + a1 [ 3 ] + ( a1 [ 4 ] || "" )
187
- t2 = a2 [ 1 ] + a2 [ 2 ] + a2 [ 3 ] + ( a2 [ 4 ] || "" )
180
+ t1 = a1 [ 1 ] + a1 [ 2 ] + a1 [ 3 ]
181
+ t2 = a2 [ 1 ] + a2 [ 2 ] + a2 [ 3 ]
188
182
if ( t1 > t2 ) return 1
189
183
if ( t1 < t2 ) return - 1
190
184
return 0
191
185
}
192
186
193
187
const DATE_TIME_SEPARATOR = / t | \s / i
194
- function date_time ( str : string ) : boolean {
188
+ function date_time ( str : string , strictTime ?: boolean ) : boolean {
195
189
// http://tools.ietf.org/html/rfc3339#section-5.6
196
190
const dateTime : string [ ] = str . split ( DATE_TIME_SEPARATOR )
197
- return dateTime . length === 2 && date ( dateTime [ 0 ] ) && time ( dateTime [ 1 ] , true )
191
+ return dateTime . length === 2 && date ( dateTime [ 0 ] ) && time ( dateTime [ 1 ] , true , strictTime )
198
192
}
199
193
200
194
function strict_date_time ( str : string ) : boolean {
201
- const dateTime : string [ ] = str . split ( DATE_TIME_SEPARATOR )
202
- return dateTime . length === 2 && date ( dateTime [ 0 ] ) && strict_time ( dateTime [ 1 ] , true )
195
+ return date_time ( str , true )
203
196
}
204
197
205
198
function compareDateTime ( dt1 : string , dt2 : string ) : number | undefined {
0 commit comments