@@ -106,6 +106,12 @@ export const fastFormats: DefinedFormats = {
106
106
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,
107
107
}
108
108
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
+
109
115
export const formatNames = Object . keys ( fullFormats ) as FormatName [ ]
110
116
111
117
function isLeapYear ( year : number ) : boolean {
@@ -139,8 +145,11 @@ function compareDate(d1: string, d2: string): number | undefined {
139
145
}
140
146
141
147
const 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 ] ) ? $ /
142
151
143
- function time ( str : string , withTimeZone ?: boolean ) : boolean {
152
+ function time ( str : string , withTimeZone ?: boolean , strict ?: boolean ) : boolean {
144
153
const matches : string [ ] | null = TIME . exec ( str )
145
154
if ( ! matches ) return false
146
155
@@ -151,10 +160,19 @@ function time(str: string, withTimeZone?: boolean): boolean {
151
160
return (
152
161
( ( hour <= 23 && minute <= 59 && second <= 59 ) ||
153
162
( 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 !== "" ) )
155
169
)
156
170
}
157
171
172
+ function strict_time ( str : string , withTimeZone ?: boolean ) : boolean {
173
+ return time ( str , withTimeZone , true )
174
+ }
175
+
158
176
function compareTime ( t1 : string , t2 : string ) : number | undefined {
159
177
if ( ! ( t1 && t2 ) ) return undefined
160
178
const a1 = TIME . exec ( t1 )
@@ -174,6 +192,11 @@ function date_time(str: string): boolean {
174
192
return dateTime . length === 2 && date ( dateTime [ 0 ] ) && time ( dateTime [ 1 ] , true )
175
193
}
176
194
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
+
177
200
function compareDateTime ( dt1 : string , dt2 : string ) : number | undefined {
178
201
if ( ! ( dt1 && dt2 ) ) return undefined
179
202
const [ d1 , t1 ] = dt1 . split ( DATE_TIME_SEPARATOR )
0 commit comments