1010 * works as designed.
1111 */
1212import { describe , test , expect } from '@jest/globals' ;
13+ import { IsoDate } from './IsoDatetime.js' ;
1314import { IsoDatetime } from './IsoDatetime.js' ;
1415
1516describe ( 'IsoDatetime.parse – valid inputs' , ( ) => {
@@ -61,22 +62,30 @@ describe('IsoDatetime.parse – valid inputs', () => {
6162 expect ( dt . toString ( ) ) . toBe ( '2025-03-01T02:30:00Z' ) ;
6263 } ) ;
6364
64- test ( 'leap year date with offset' , ( ) => {
65- const str = '2024-02-29T23:00:00+01:00' ;
66- const dt = IsoDatetime . parse ( str ) ;
67- // 23:00 +01:00 => 22:00Z on leap day
68- expect ( dt . toString ( ) ) . toBe ( '2024-02-29T22:00:00Z' ) ;
65+ const valid = [
66+ { input : '2024-02-29T23:00:00+01:00' , expected : '2024-02-29T22:00:00Z' } , // leap year date with offset
67+ { input : '2025-03-01' , expected : '2025-03-01T00:00:00Z' } , // date only
68+ { input : '2025-03-01T12:34:56.789' , expected : '2025-03-01T12:34:56.789Z' } , // missing Z
69+ // currently does not allow the following even though it is valid in ISO 8601
70+ // { input: '2025-03', expected: '2024-03' }, // month only
71+ // { input: '2025', expected: '2024' }, // year only
72+ ] ;
73+
74+ valid . forEach ( ( { input, expected } ) => {
75+ test ( `IsoDatetime.parse(${ input } )` , ( ) => {
76+ const datetime = IsoDatetime . parse ( input ) ;
77+ expect ( datetime . toString ( ) ) . toBe ( expected ) ;
78+ } ) ;
6979 } ) ;
7080} ) ;
7181
82+
7283describe ( 'IsoDatetime.parse – invalid inputs' , ( ) => {
7384 const invalid = [
74- '2025-03-01' , // date only
7585 '2025-03' , // month only
7686 '2025' , // year only
7787 '2025-03-01 12:34:56Z' , // no 'T' separator
7888 '2025-03-01T12:34Z' , // missing seconds
79- '2025-03-01T12:34:56.789' , // missing Z
8089 '2025-13-01T12:34:56Z' , // bad month
8190 '2025-00-01T12:34:56Z' , // bad month
8291 '2025-02-30T12:34:56Z' , // bad number of days in February
@@ -95,6 +104,24 @@ describe('IsoDatetime.parse – invalid inputs', () => {
95104 } ) ;
96105} ) ;
97106
107+
108+ describe ( 'IsoDatetime.fromIsoDate()' , ( ) => {
109+ const valid = [
110+ { input : '2025-03-02' , expected : '2025-03-02T00:00:00Z' } , // date only
111+ { input : '2025-03' , expected : '2025-03-01T00:00:00Z' } , // month only
112+ { input : '2025' , expected : '2025-01-01T00:00:00Z' } , // year only
113+ ] ;
114+
115+ valid . forEach ( ( { input, expected } ) => {
116+ test ( `IsoDatetime.fromIsoDate(${ input } )` , ( ) => {
117+ const date = IsoDate . parse ( input ) ;
118+ const datetime = IsoDatetime . fromIsoDate ( date ) ;
119+ expect ( datetime . toString ( ) ) . toBe ( expected ) ;
120+ } ) ;
121+ } ) ;
122+ } ) ;
123+
124+
98125describe ( 'IsoDatetime.toString – formatting' , ( ) => {
99126 const tests = [
100127 { input : '2025-03-01T12:34:56Z' , expected : '2025-03-01T12:34:56Z' } ,
0 commit comments