@@ -106,6 +106,12 @@ export const fastFormats: DefinedFormats = {
106106 email : / ^ [ a - z 0 - 9 . ! # $ % & ' * + / = ? ^ _ ` { | } ~ - ] + @ [ a - z 0 - 9 ] (?: [ a - z 0 - 9 - ] { 0 , 61 } [ a - z 0 - 9 ] ) ? (?: \. [ a - z 0 - 9 ] (?: [ a - z 0 - 9 - ] { 0 , 61 } [ a - z 0 - 9 ] ) ? ) * $ / i,
107107}
108108
109+ export const strictFormats : Partial < DefinedFormats > = {
110+ // date-time: http://tools.ietf.org/html/rfc3339#section-5.6
111+ time : fmtDef ( strict_time , compareTime ) ,
112+ "date-time" : fmtDef ( strict_date_time , compareDateTime ) ,
113+ }
114+
109115export const formatNames = Object . keys ( fullFormats ) as FormatName [ ]
110116
111117function isLeapYear ( year : number ) : boolean {
@@ -139,8 +145,11 @@ function compareDate(d1: string, d2: string): number | undefined {
139145}
140146
141147const TIME = / ^ ( \d \d ) : ( \d \d ) : ( \d \d ) ( \. \d + ) ? ( z | [ + - ] \d \d (?: : ? \d \d ) ? ) ? $ / i
148+ const PLUS_MINUS = / ^ [ + - ] /
149+ const TIMEZONE = / ^ [ Z z ] $ /
150+ const ISO_8601_TIME = / ^ [ + - ] (?: [ 0 1 ] [ 0 - 9 ] | 2 [ 0 - 4 ] ) (?: : ? [ 0 - 5 ] [ 0 - 9 ] ) ? $ /
142151
143- function time ( str : string , withTimeZone ?: boolean ) : boolean {
152+ function time ( str : string , withTimeZone ?: boolean , strict ?: boolean ) : boolean {
144153 const matches : string [ ] | null = TIME . exec ( str )
145154 if ( ! matches ) return false
146155
@@ -151,10 +160,19 @@ function time(str: string, withTimeZone?: boolean): boolean {
151160 return (
152161 ( ( hour <= 23 && minute <= 59 && second <= 59 ) ||
153162 ( hour === 23 && minute === 59 && second === 60 ) ) &&
154- ( ! withTimeZone || timeZone !== "" )
163+ ( ! withTimeZone ||
164+ ( strict
165+ ? TIMEZONE . test ( timeZone ) ||
166+ ( PLUS_MINUS . test ( timeZone ) && time ( timeZone . slice ( 1 ) + ":00" ) ) ||
167+ ISO_8601_TIME . test ( timeZone )
168+ : timeZone !== "" ) )
155169 )
156170}
157171
172+ function strict_time ( str : string , withTimeZone ?: boolean ) : boolean {
173+ return time ( str , withTimeZone , true )
174+ }
175+
158176function compareTime ( t1 : string , t2 : string ) : number | undefined {
159177 if ( ! ( t1 && t2 ) ) return undefined
160178 const a1 = TIME . exec ( t1 )
@@ -174,6 +192,11 @@ function date_time(str: string): boolean {
174192 return dateTime . length === 2 && date ( dateTime [ 0 ] ) && time ( dateTime [ 1 ] , true )
175193}
176194
195+ function strict_date_time ( str : string ) : boolean {
196+ const dateTime : string [ ] = str . split ( DATE_TIME_SEPARATOR )
197+ return dateTime . length === 2 && date ( dateTime [ 0 ] ) && strict_time ( dateTime [ 1 ] , true )
198+ }
199+
177200function compareDateTime ( dt1 : string , dt2 : string ) : number | undefined {
178201 if ( ! ( dt1 && dt2 ) ) return undefined
179202 const [ d1 , t1 ] = dt1 . split ( DATE_TIME_SEPARATOR )
0 commit comments