Skip to content

Commit 66dff3c

Browse files
feat: implemented toISOString and fromString
1 parent 5632f50 commit 66dff3c

File tree

5 files changed

+5994
-711
lines changed

5 files changed

+5994
-711
lines changed

std/assembly/date.ts

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,46 @@ export class Date {
2020
return <i64>Date_now();
2121
}
2222

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+
2363
private epochMillis: i64;
2464

2565
constructor(epochMillis: i64) {
@@ -105,6 +145,50 @@ export class Date {
105145
this.epochMillis =
106146
i64(daysSinceEpoch(value, ymd.month, ymd.day)) * MILLIS_PER_DAY + mills;
107147
}
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+
);
108192
}
109193

110194
function throwIfNotInRange(value: i32, lower: i32, upper: i32): void {

std/assembly/index.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1710,6 +1710,7 @@ declare class Date {
17101710
): i64;
17111711
/** Returns the current UTC timestamp in milliseconds. */
17121712
static now(): i64;
1713+
static fromString(dateStr: string): Date;
17131714
/** Constructs a new date object from an UTC timestamp in milliseconds. */
17141715
constructor(value: i64);
17151716
/** Returns the UTC timestamp of this date in milliseconds. */
@@ -1732,6 +1733,8 @@ declare class Date {
17321733
setUTCMinutes(value: i32): void;
17331734
setUTCSeconds(value: i32): void;
17341735
setUTCMilliseconds(value: i32): void;
1736+
1737+
toISOString(): string;
17351738
}
17361739

17371740
/** Class for representing a runtime error. Base class of all errors. */

0 commit comments

Comments
 (0)