1
- // Times are parsed as UTC if no offset is specified
1
+ /**
2
+ * _Times are parsed as UTC if no offset is specified_
3
+ */
2
4
export class IsoDateParts {
3
5
static FULL_DATE_REGEX = / ^ ( [ + - ] \d { 6 } | \d { 4 } ) - ? ( [ 0 1 ] \d ) - ? ( [ 0 - 3 ] \d ) $ / ;
4
6
static DATETIME_REGEX = / ^ ( [ + - ] \d { 6 } | \d { 4 } ) - ? ( [ 0 1 ] \d ) - ? ( [ 0 - 3 ] \d ) [ T t ] ( [ 0 - 2 ] \d (?: [ \. \, ] \d + ) ? ) (?: : ? ( [ 0 - 5 ] \d (?: [ \. \, ] \d + ) ? ) (?: : ? ( [ 0 - 5 ] \d ) ) ? (?: [ \. \, ] ( \d { 1 , 9 } ) ) ? ) ? ( Z | [ + - ] [ 0 - 2 ] \d (?: : ? [ 0 - 5 ] \d ) ? ) ? $ / ;
@@ -15,6 +17,7 @@ export class IsoDateParts {
15
17
} ;
16
18
}
17
19
20
+ /** @param {RegExpMatchArray } match */
18
21
static getByDateTime (
19
22
_ , // full match
20
23
year = "" ,
@@ -40,6 +43,7 @@ export class IsoDateParts {
40
43
} ;
41
44
}
42
45
46
+ /** @param {string } str An [RFC 9557](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/PlainDateTime#rfc_9557_format)-compatible string */
43
47
static getParts ( str = "" ) {
44
48
let dateTimeMatch = str . match ( this . FULL_DATE_REGEX ) ?? str . match ( this . DATETIME_REGEX ) ;
45
49
if ( ! dateTimeMatch ) {
@@ -54,6 +58,24 @@ export class IsoDateParts {
54
58
}
55
59
56
60
export class IsoDate {
61
+ /** @type {number } */
62
+ year ;
63
+ /** @type {number } */
64
+ month ;
65
+ /** @type {number } */
66
+ day ;
67
+ /** @type {number } */
68
+ hours ;
69
+ /** @type {number } */
70
+ minutes ;
71
+ /** @type {number } */
72
+ seconds ;
73
+ /** @type {number } */
74
+ milliseconds ;
75
+ /** @type {string } */
76
+ source ;
77
+
78
+ /** @param {string } str An [RFC 9557](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/PlainDateTime#rfc_9557_format)-compatible string */
57
79
static parse ( str ) {
58
80
let parts = IsoDateParts . getParts ( str ) ;
59
81
if ( parts ) {
@@ -67,11 +89,22 @@ export class IsoDate {
67
89
throw new Error ( `Unsupported date format: ${ str } ` ) ;
68
90
}
69
91
92
+ /**
93
+ * @param {object } parts
94
+ * @param {number } parts.year
95
+ * @param {number } parts.month
96
+ * @param {number } parts.day
97
+ * @param {number } parts.hours
98
+ * @param {number } parts.minutes
99
+ * @param {number } parts.seconds
100
+ * @param {number } parts.milliseconds
101
+ */
70
102
constructor ( parts ) {
71
- // parts.day, parts.year, parts.month, parts.week
103
+ // parts.day, parts.year, parts.month
72
104
Object . assign ( this , parts ) ;
73
105
}
74
106
107
+ /** @returns {[number, number, number, number, number, number, number] } */
75
108
getArgs ( ) {
76
109
return [ this . year , this . month , this . day , this . hours , this . minutes , this . seconds , this . milliseconds ] ;
77
110
}
@@ -89,6 +122,7 @@ export class IsoDate {
89
122
}
90
123
}
91
124
125
+ /** @param {string } str An [RFC 9557](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/PlainDateTime#rfc_9557_format)-compatible string */
92
126
export function parse ( str ) {
93
127
return IsoDate . parse ( str ) ;
94
128
}
0 commit comments