Skip to content

Commit

Permalink
improve ISO8601 support
Browse files Browse the repository at this point in the history
  • Loading branch information
qraynaud committed Oct 2, 2024
1 parent 5adbb60 commit 02e0009
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/schema/yaml-1.1/timestamp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,18 @@ export const timestamp: ScalarTag & { test: RegExp } = {
// may be omitted altogether, resulting in a date format. In such a case, the time part is
// assumed to be 00:00:00Z (start of day, UTC).
test: RegExp(
'^([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})' + // YYYY-Mm-Dd
'^([0-9]{4})-?([0-9]{1,2})-?([0-9]{1,2})' + // YYYY-?Mm-?Dd
'(?:' + // time is optional
'(?:t|T|[ \\t]+)' + // t | T | whitespace
'([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2}(\\.[0-9]+)?)' + // Hh:Mm:Ss(.ss)?
'(?:[ \\t]*(Z|[-+][012]?[0-9](?::[0-9]{2})?))?' + // Z | +5 | -03:30
')?$'
'([0-9]{1,2})' + // Hh
'(?:' + // minutes are optional
':?([0-9]{1,2})' + // :?Mm
'(?:' + // seconds are optionals
':?([0-9]{1,2}(\\.[0-9]{1,3})?)' + // :?Ss(.sss)?
'(?:[ \\t]*(Z|[-+][012]?[0-9](?::?[0-9]{2})?))?' + // Z | +5 | -03:30
')?' + // end seconds
')?' + // end minutes
')?$' // end time
),

resolve(str) {
Expand Down Expand Up @@ -116,5 +122,5 @@ export const timestamp: ScalarTag & { test: RegExp } = {
},

stringify: ({ value }) =>
(value as Date).toISOString().replace(/((T00:00)?:00)?\.000Z$/, '')
(value as Date).toISOString().replace(/(((T00)?:00)?:00)?\.000Z$/, '')
}
9 changes: 9 additions & 0 deletions tests/doc/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,9 @@ english: null
---
canonical: 2001-12-15T02:59:43.1Z
valid iso8601: 2001-12-14t21:59:43.10-05:00
no secs: 2001-12-14T01:02
no mins: 2001-12-14T01
simplified iso: 20011214T215943.10-5
space separated: 2001-12-14 21:59:43.10 -5
no time zone (Z): 2001-12-15 2:59:43.10
date (00:00:00Z): 2002-12-14`
Expand All @@ -671,6 +674,9 @@ date (00:00:00Z): 2002-12-14`
expect(doc.toJSON()).toMatchObject({
canonical: '2001-12-15T02:59:43.100Z',
'valid iso8601': '2001-12-15T02:59:43.100Z',
'no secs': '2001-12-14T01:02:00.000Z',
'no mins': '2001-12-14T01:00:00.000Z',
'simplified iso': '2001-12-15T02:59:43.100Z',
'space separated': '2001-12-15T02:59:43.100Z',
'no time zone (Z)': '2001-12-15T02:59:43.100Z',
'date (00:00:00Z)': '2002-12-14T00:00:00.000Z'
Expand All @@ -679,6 +685,9 @@ date (00:00:00Z): 2002-12-14`
---
canonical: 2001-12-15T02:59:43.100Z
valid iso8601: 2001-12-15T02:59:43.100Z
no secs: 2001-12-14T01:02
no mins: 2001-12-14T01
simplified iso: 2001-12-15T02:59:43.100Z
space separated: 2001-12-15T02:59:43.100Z
no time zone (Z): 2001-12-15T02:59:43.100Z
date (00:00:00Z): 2002-12-14\n`)
Expand Down

0 comments on commit 02e0009

Please sign in to comment.