File tree Expand file tree Collapse file tree 5 files changed +50
-6
lines changed Expand file tree Collapse file tree 5 files changed +50
-6
lines changed Original file line number Diff line number Diff line change @@ -366,11 +366,13 @@ To allow timezone offsets:
366
366
``` ts
367
367
const datetime = z .iso .datetime ({ offset: true });
368
368
369
- datetime .parse (" 2020-01-01T00:00:00+02:00" ); // ✅
370
- datetime .parse (" 2020-01-01T00:00:00.123+02:00" ); // ✅ (millis optional)
371
- datetime .parse (" 2020-01-01T00:00:00.123+0200" ); // ✅ (millis optional)
372
- datetime .parse (" 2020-01-01T00:00:00.123+02" ); // ✅ (only offset hours)
373
- datetime .parse (" 2020-01-01T00:00:00Z" ); // ✅ (Z still supported)
369
+ // result is normalized to RFC 3339 format
370
+ datetime .parse (" 2020-01-01T00:00:00+02" ); // ✅ "2020-01-01T00:00:00+02:00"
371
+ datetime .parse (" 2020-01-01T00:00:00+0200" ); // ✅ "2020-01-01T00:00:00+02:00"
372
+ datetime .parse (" 2020-01-01T00:00:00+02:00" ); // ✅ "2020-01-01T00:00:00+02:00"
373
+
374
+ // Z is still supported
375
+ datetime .parse (" 2020-01-01T00:00:00Z" ); // ✅
374
376
```
375
377
376
378
To allow unqualified (timezone-less) datetimes:
Original file line number Diff line number Diff line change @@ -816,6 +816,21 @@ test("datetime parsing", () => {
816
816
expect ( ( ) => datetimeOffset4Ms . parse ( "2020-10-14T17:42:29.124+00:00" ) ) . toThrow ( ) ;
817
817
} ) ;
818
818
819
+ test ( "datetime offset normalization" , ( ) => {
820
+ const a = z . iso . datetime ( { offset : true } ) ;
821
+ expect ( {
822
+ a : a . parse ( "2020-10-14T17:42:29+02" ) ,
823
+ b : a . parse ( "2020-10-14T17:42:29+0200" ) ,
824
+ c : a . parse ( "2020-10-14T17:42:29+02:00" ) ,
825
+ } ) . toMatchInlineSnapshot ( `
826
+ {
827
+ "a": "2020-10-14T17:42:29+02:00",
828
+ "b": "2020-10-14T17:42:29+02:00",
829
+ "c": "2020-10-14T17:42:29+02:00",
830
+ }
831
+ ` ) ;
832
+ } ) ;
833
+
819
834
test ( "date parsing" , ( ) => {
820
835
const date = z . string ( ) . date ( ) ;
821
836
date . parse ( "1970-01-01" ) ;
Original file line number Diff line number Diff line change @@ -107,7 +107,9 @@ export function datetime(args: {
107
107
108
108
const opts : string [ ] = [ ] ;
109
109
opts . push ( args . local ? `Z?` : `Z` ) ;
110
- if ( args . offset ) opts . push ( `([+-]\\d{2}:?\\d{2})` ) ;
110
+ // if (args.offset) opts.push(`([+-]\\d{2}:?\\d{2})`);
111
+ // minutes, colon optional
112
+ if ( args . offset ) opts . push ( `([+-]\\d{2}(?::?\\d{2})?)` ) ;
111
113
regex = `${ regex } (${ opts . join ( "|" ) } )` ;
112
114
return new RegExp ( `^${ regex } $` ) ;
113
115
}
Original file line number Diff line number Diff line change @@ -604,6 +604,20 @@ export const $ZodISODateTime: core.$constructor<$ZodISODateTime> = /*@__PURE__*/
604
604
( inst , def ) : void => {
605
605
def . pattern ??= regexes . datetime ( def ) ;
606
606
$ZodStringFormat . init ( inst , def ) ;
607
+
608
+ const _super = inst . _zod . check ;
609
+ inst . _zod . check = ( payload ) => {
610
+ _super ( payload ) ;
611
+
612
+ // normalize timezone offset
613
+ // add colon & minutes if missing
614
+ // if no offset, return early
615
+ const curr = payload . value ;
616
+ if ( / [ + - ] \d \d $ / . test ( curr ) ) payload . value = curr + ":00" ;
617
+ else if ( / [ + - ] \d \d \d \d $ / . test ( curr ) ) {
618
+ payload . value = curr . slice ( 0 , - 2 ) + ":" + curr . slice ( - 2 ) ;
619
+ }
620
+ } ;
607
621
}
608
622
) ;
609
623
Original file line number Diff line number Diff line change 1
1
import { z } from "zod/v4" ;
2
2
3
3
z ;
4
+
5
+ // const schema = z.iso.datetime({ offset: true, local: true });
6
+ // console.dir(schema.parse("2023-10-01T12:00:00.132"), { depth: null });
7
+
8
+ const datetime = z . iso . datetime ( { offset : true } ) ;
9
+
10
+ datetime . parse ( "2020-01-01T00:00:00+02:00" ) ; // ✅
11
+ datetime . parse ( "2020-01-01T00:00:00.123+02:00" ) ; // ✅ (millis optional)
12
+ datetime . parse ( "2020-01-01T00:00:00.123+0200" ) ; // ✅ (millis optional)
13
+ datetime . parse ( "2020-01-01T00:00:00.123+02" ) ; // ✅ (only offset hours)
14
+ datetime . parse ( "2020-01-01T00:00:00Z" ) ; // ✅ (Z still supported)
You can’t perform that action at this time.
0 commit comments