@@ -149,57 +149,50 @@ function compareDate(d1: string, d2: string): number | undefined {
149149 return 0
150150}
151151
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
156153
157- function time ( str : string , withTimeZone ?: boolean , strict ?: boolean ) : boolean {
154+ function time ( str : string , withTimeZone ?: boolean , strictTime ?: boolean ) : boolean {
158155 const matches : string [ ] | null = TIME . exec ( str )
159156 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 )
165163 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 ) ) )
174168 )
175169}
176170
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 )
179173}
180174
181175function compareTime ( t1 : string , t2 : string ) : number | undefined {
182176 if ( ! ( t1 && t2 ) ) return undefined
183177 const a1 = TIME . exec ( t1 )
184178 const a2 = TIME . exec ( t2 )
185179 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 ]
188182 if ( t1 > t2 ) return 1
189183 if ( t1 < t2 ) return - 1
190184 return 0
191185}
192186
193187const DATE_TIME_SEPARATOR = / t | \s / i
194- function date_time ( str : string ) : boolean {
188+ function date_time ( str : string , strictTime ?: boolean ) : boolean {
195189 // http://tools.ietf.org/html/rfc3339#section-5.6
196190 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 )
198192}
199193
200194function 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 )
203196}
204197
205198function compareDateTime ( dt1 : string , dt2 : string ) : number | undefined {
0 commit comments