Skip to content

Commit b622444

Browse files
refactor: simplified 'days in month' logic
1 parent 03e4d8b commit b622444

File tree

3 files changed

+1971
-2097
lines changed

3 files changed

+1971
-2097
lines changed

std/assembly/date.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ export class Date {
125125

126126
setUTCDate(value: i32): void {
127127
ymdFromEpochDays(i32(this.epochMillis / MILLIS_PER_DAY));
128-
throwIfNotInRange(value, 1, lastDayOfMonth(year, month));
128+
throwIfNotInRange(value, 1, daysInMonth(year, month));
129129
const mills = this.epochMillis % MILLIS_PER_DAY;
130130
this.epochMillis =
131131
i64(daysSinceEpoch(year, month, value)) * MILLIS_PER_DAY + mills;
@@ -205,15 +205,10 @@ function isLeap(y: i32): bool {
205205
return y % 4 == 0 && (y % 100 != 0 || y % 400 == 0);
206206
}
207207

208-
// http://howardhinnant.github.io/date_algorithms.html#last_day_of_month_common_year
209-
function lastDayOfMonthNonLeapYear(m: i32): i32 {
210-
const days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
211-
return days[m - 1];
212-
}
213-
214-
// http://howardhinnant.github.io/date_algorithms.html#last_day_of_month
215-
function lastDayOfMonth(y: i32, m: i32): i32 {
216-
return m != 2 || !isLeap(y) ? lastDayOfMonthNonLeapYear(m) : 29;
208+
function daysInMonth(year: i32, month: i32): i32 {
209+
return month == 2
210+
? 28 + i32(isLeap(year))
211+
: 30 + ((month + i32(month >= 8)) & 1);
217212
}
218213

219214
// ymdFromEpochDays returns values via globals to avoid allocations

0 commit comments

Comments
 (0)