@@ -20,6 +20,46 @@ export class Date {
20
20
return < i64 > Date_now ( ) ;
21
21
}
22
22
23
+ static fromString ( dateTimeString : string ) : Date {
24
+ let hour : i32 = 0 ,
25
+ minute : i32 = 0 ,
26
+ second : i32 = 0 ,
27
+ millisecond : i32 = 0 ;
28
+ let dateString : string ;
29
+
30
+ if ( dateTimeString . includes ( "T" ) ) {
31
+ // includes a time component
32
+ const parts = dateTimeString . split ( "T" ) ;
33
+ const timeString = parts [ 1 ] ;
34
+ // parse the HH-MM-SS component
35
+ const timeParts = timeString . split ( ":" ) ;
36
+ hour = I32 . parseInt ( timeParts [ 0 ] ) ;
37
+ minute = I32 . parseInt ( timeParts [ 1 ] ) ;
38
+ if ( timeParts [ 2 ] . includes ( "." ) ) {
39
+ // includes milliseconds
40
+ const secondParts = timeParts [ 2 ] . split ( "." ) ;
41
+ second = I32 . parseInt ( secondParts [ 0 ] ) ;
42
+ millisecond = I32 . parseInt ( secondParts [ 1 ] ) ;
43
+ } else {
44
+ second = I32 . parseInt ( timeParts [ 2 ] ) ;
45
+ }
46
+ dateString = parts [ 0 ] ;
47
+ } else {
48
+ dateString = dateTimeString ;
49
+ }
50
+ // parse the YYYY-MM-DD component
51
+ const parts = dateString . split ( "-" ) ;
52
+ const year = I32 . parseInt (
53
+ parts [ 0 ] . length == 2 ? "19" + parts [ 0 ] : parts [ 0 ]
54
+ ) ;
55
+ const month = I32 . parseInt ( parts [ 1 ] ) ;
56
+ const day = I32 . parseInt ( parts [ 2 ] ) ;
57
+
58
+ return new Date (
59
+ epochMillis ( year , month , day , hour , minute , second , millisecond )
60
+ ) ;
61
+ }
62
+
23
63
private epochMillis : i64 ;
24
64
25
65
constructor ( epochMillis : i64 ) {
@@ -105,6 +145,50 @@ export class Date {
105
145
this . epochMillis =
106
146
i64 ( daysSinceEpoch ( value , ymd . month , ymd . day ) ) * MILLIS_PER_DAY + mills ;
107
147
}
148
+
149
+ toISOString ( ) : string {
150
+ const ymd = ymdFromEpochDays ( i32 ( this . epochMillis / MILLIS_PER_DAY ) ) ;
151
+
152
+ let yearStr = ymd . year . toString ( ) ;
153
+ if ( yearStr . length > 4 ) {
154
+ yearStr = "+" + yearStr . padStart ( 6 , "0" ) ;
155
+ }
156
+
157
+ return (
158
+ yearStr +
159
+ "-" +
160
+ ymd . month . toString ( ) . padStart ( 2 , "0" ) +
161
+ "-" +
162
+ ymd . day . toString ( ) . padStart ( 2 , "0" ) +
163
+ "T" +
164
+ this . getUTCHours ( ) . toString ( ) . padStart ( 2 , "0" ) +
165
+ ":" +
166
+ this . getUTCMinutes ( ) . toString ( ) . padStart ( 2 , "0" ) +
167
+ ":" +
168
+ this . getUTCSeconds ( ) . toString ( ) . padStart ( 2 , "0" ) +
169
+ "." +
170
+ this . getUTCMilliseconds ( ) . toString ( ) . padStart ( 3 , "0" ) +
171
+ "Z"
172
+ ) ;
173
+ }
174
+ }
175
+
176
+ function epochMillis (
177
+ year : i32 ,
178
+ month : i32 ,
179
+ day : i32 ,
180
+ hour : i32 ,
181
+ minute : i32 ,
182
+ second : i32 ,
183
+ milliseconds : i32
184
+ ) : i64 {
185
+ return (
186
+ i64 ( daysSinceEpoch ( year , month , day ) ) * MILLIS_PER_DAY +
187
+ hour * MILLIS_PER_HOUR +
188
+ minute * MILLIS_PER_MINUTE +
189
+ second * MILLIS_PER_SECOND +
190
+ milliseconds
191
+ ) ;
108
192
}
109
193
110
194
function throwIfNotInRange ( value : i32 , lower : i32 , upper : i32 ) : void {
0 commit comments