Skip to content

Commit 6d59f71

Browse files
authored
Add toUTCString for Date (AssemblyScript#1831)
1 parent d610f01 commit 6d59f71

File tree

5 files changed

+2121
-1050
lines changed

5 files changed

+2121
-1050
lines changed

std/assembly/date.ts

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ export class Date {
9999
this.day = _day;
100100
}
101101

102-
getTime(): i64 {
102+
@inline getTime(): i64 {
103103
return this.epochMillis;
104104
}
105105

@@ -114,19 +114,19 @@ export class Date {
114114
return time;
115115
}
116116

117-
getUTCFullYear(): i32 {
117+
@inline getUTCFullYear(): i32 {
118118
return this.year;
119119
}
120120

121-
getUTCMonth(): i32 {
121+
@inline getUTCMonth(): i32 {
122122
return this.month - 1;
123123
}
124124

125-
getUTCDate(): i32 {
125+
@inline getUTCDate(): i32 {
126126
return this.day;
127127
}
128128

129-
getUTCDay(): i32 {
129+
@inline getUTCDay(): i32 {
130130
return dayOfWeek(this.year, this.month, this.day);
131131
}
132132

@@ -209,6 +209,38 @@ export class Date {
209209
);
210210
}
211211

212+
toUTCString(): string {
213+
const weeks: StaticArray<string> = [
214+
"Sun, ", "Mon, ", "Tue, ", "Wed, ", "Thu, ", "Fri, ", "Sat, "
215+
];
216+
217+
const months: StaticArray<string> = [
218+
" Jan ", " Feb ", " Mar ", " Apr ", " May ", " Jun ",
219+
" Jul ", " Aug ", " Sep ", " Oct ", " Nov ", " Dec "
220+
];
221+
222+
var mo = this.month;
223+
var da = this.day;
224+
var yr = this.year;
225+
var wd = dayOfWeek(yr, mo, da);
226+
var year = abs(yr).toString().padStart(4, "0");
227+
if (yr < 0) year = "-" + year;
228+
229+
return (
230+
unchecked(weeks[wd]) +
231+
da.toString().padStart(2, "0") +
232+
unchecked(months[mo - 1]) +
233+
year +
234+
" " +
235+
this.getUTCHours().toString().padStart(2, "0") +
236+
":" +
237+
this.getUTCMinutes().toString().padStart(2, "0") +
238+
":" +
239+
this.getUTCSeconds().toString().padStart(2, "0") +
240+
" GMT"
241+
);
242+
}
243+
212244
toDateString(): string {
213245
// TODO: use u64 static data instead 4 chars
214246
// also use stream itoa variants.

std/assembly/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1758,6 +1758,7 @@ declare class Date {
17581758

17591759
toString(): string;
17601760
toISOString(): string;
1761+
toUTCString(): string;
17611762
toDateString(): string;
17621763
toTimeString(): string;
17631764
}

0 commit comments

Comments
 (0)