Skip to content

Commit

Permalink
Add support for 9-digit millisecond precision to isoTimestamp #348
Browse files Browse the repository at this point in the history
ISO-8601 does not specify the number of digits in the decimal fraction.

JavaScript `new Date().toISOString()` which is compliant to ISO-8601, has a precision of three digits (miliseconds precision).
Java `Instant.now().toString()` which is compliant to ISO-8601, has a precision of nine digits (nanoseconds precision).

This change will allow timestamps with nanoseconds precision.
  • Loading branch information
WtfJoke committed Jan 4, 2024
1 parent 4b50ce8 commit 89a92d4
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
2 changes: 1 addition & 1 deletion library/src/regex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export const ISO_TIME_SECOND_REGEX = /^(?:0\d|1\d|2[0-3])(?::[0-5]\d){2}$/u;
* [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) timestamp regex.
*/
export const ISO_TIMESTAMP_REGEX =
/^\d{4}-(?:0[1-9]|1[0-2])-(?:[12]\d|0[1-9]|3[01])T(?:0\d|1\d|2[0-3])(?::[0-5]\d){2}\.\d{3}Z$/u;
/^\d{4}-(?:0[1-9]|1[0-2])-(?:[12]\d|0[1-9]|3[01])T(?:0\d|1\d|2[0-3])(?::[0-5]\d){2}\.\d{3,9}Z$/u;

/**
* [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) week regex.
Expand Down
2 changes: 2 additions & 0 deletions library/src/validations/isoTimestamp/isoTimestamp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ describe('isoTimestamp', () => {
expect(validate._parse(value2).output).toBe(value2);
const value3 = '9999-12-31T23:59:59.999Z';
expect(validate._parse(value3).output).toBe(value3);
const value4 = '2024-01-04T17:40:21.157953900Z';
expect(validate._parse(value4).output).toBe(value4);

expect(validate._parse('').issues).toBeTruthy();
expect(validate._parse('2023-07-11T17:26:27.243').issues).toBeTruthy();
Expand Down
10 changes: 9 additions & 1 deletion library/src/validations/isoTimestamp/isoTimestamp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,15 @@ export type IsoTimestampValidation<TInput extends string> =
/**
* Creates a pipeline validation action that validates a timestamp.
*
* Format: yyyy-mm-ddThh:mm:ss.sssZ
* Format: yyyy-mm-ddThh:mm:ss.sssssssssZ
* - yyyy: 4-digit year
* - mm: 2-digit month
* - dd: 2-digit day
* - hh: 2-digit hour (00-23)
* - mm: 2-digit minute (00-59)
* - ss: 2-digit second (00-59)
* - sssssssss: Up to 9-digit millisecond precision
* - Z: Timezone offset (e.g., +00:00 or -07:00, Z=UTC+0)
*
* Hint: The regex used cannot validate the maximum number of days based on
* year and month. For example, "2023-06-31T00:00:00.000Z" is valid although
Expand Down

0 comments on commit 89a92d4

Please sign in to comment.