Skip to content

Optimize util for calculation year, month, day in Date #2397

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Aug 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 28 additions & 22 deletions std/assembly/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ import { Date as Date_binding } from "./bindings/dom";
MILLIS_PER_DAY = 1000 * 60 * 60 * 24,
MILLIS_PER_HOUR = 1000 * 60 * 60,
MILLIS_PER_MINUTE = 1000 * 60,
MILLIS_PER_SECOND = 1000;
MILLIS_PER_SECOND = 1000,

YEARS_PER_EPOCH = 400,
DAYS_PER_EPOCH = 146097,
EPOCH_OFFSET = 719468, // Jan 1, 1970
MILLIS_LIMIT = 8640000000000000;

// ymdFromEpochDays returns values via globals to avoid allocations
// @ts-ignore: decorator
Expand Down Expand Up @@ -94,7 +99,7 @@ export class Date {
// instead throwing exception.
if (invalidDate(epochMillis)) throw new RangeError(E_INVALIDDATE);

this.year = ymdFromEpochDays(i32(floorDiv(epochMillis, MILLIS_PER_DAY)));
this.year = dateFromEpoch(epochMillis);
this.month = _month;
this.day = _day;
}
Expand All @@ -107,7 +112,7 @@ export class Date {
if (invalidDate(time)) throw new RangeError(E_INVALIDDATE);

this.epochMillis = time;
this.year = ymdFromEpochDays(i32(floorDiv(time, MILLIS_PER_DAY)));
this.year = dateFromEpoch(time);
this.month = _month;
this.day = _day;

Expand Down Expand Up @@ -306,7 +311,7 @@ function epochMillis(

// @ts-ignore: decorator
@inline function floorDiv<T extends number>(a: T, b: T): T {
return (a >= 0 ? a : a - b + 1) / b as T;
return (a - (a < 0 ? b - 1 : 0)) / b as T;
}

// @ts-ignore: decorator
Expand All @@ -317,41 +322,42 @@ function epochMillis(

function invalidDate(millis: i64): bool {
// @ts-ignore
return (millis < -8640000000000000) | (millis > 8640000000000000);
return (millis < -MILLIS_LIMIT) | (millis > MILLIS_LIMIT);
}

// see: http://howardhinnant.github.io/date_algorithms.html#civil_from_days
function ymdFromEpochDays(z: i32): i32 {
z += 719468;
var era = <u32>floorDiv(z, 146097);
var doe = <u32>z - era * 146097; // [0, 146096]
var yoe = (doe - doe / 1460 + doe / 36524 - doe / 146096) / 365; // [0, 399]
var year = yoe + era * 400;
var doy = doe - (365 * yoe + yoe / 4 - yoe / 100); // [0, 365]
var mo = (5 * doy + 2) / 153; // [0, 11]
_day = doy - (153 * mo + 2) / 5 + 1; // [1, 31]
mo += mo < 10 ? 3 : -9; // [1, 12]
_month = mo;
year += u32(mo <= 2);
// Based on "Euclidean Affine Functions and Applications to Calendar Algorithms"
// Paper: https://arxiv.org/pdf/2102.06959.pdf
function dateFromEpoch(ms: i64): i32 {
var da = (<i32>floorDiv(ms, MILLIS_PER_DAY) * 4 + EPOCH_OFFSET * 4) | 3;
var q0 = floorDiv(da, DAYS_PER_EPOCH); // [0, 146096]
var r1 = <u32>da - q0 * DAYS_PER_EPOCH;
var u1 = u64(r1 | 3) * 2939745;
var dm1 = <u32>u1 / 11758980;
var n1 = 2141 * dm1 + 197913;
var year = 100 * q0 + i32(u1 >>> 32);
var mo = n1 >>> 16;
_day = (n1 & 0xFFFF) / 2141 + 1; // [1, 31]
if (dm1 >= 306) { mo -= 12; ++year; }
_month = mo; // [1, 12]
return year;
}

// http://howardhinnant.github.io/date_algorithms.html#days_from_civil
function daysSinceEpoch(y: i32, m: i32, d: i32): i32 {
y -= i32(m <= 2);
var era = <u32>floorDiv(y, 400);
var yoe = <u32>y - era * 400; // [0, 399]
var era = <u32>floorDiv(y, YEARS_PER_EPOCH);
var yoe = <u32>y - era * YEARS_PER_EPOCH; // [0, 399]
var doy = <u32>(153 * (m + (m > 2 ? -3 : 9)) + 2) / 5 + d - 1; // [0, 365]
var doe = yoe * 365 + yoe / 4 - yoe / 100 + doy; // [0, 146096]
return era * 146097 + doe - 719468;
return era * 146097 + doe - EPOCH_OFFSET;
}

// TomohikoSakamoto algorithm from https://en.wikipedia.org/wiki/Determination_of_the_day_of_the_week
function dayOfWeek(year: i32, month: i32, day: i32): i32 {
const tab = memory.data<u8>([0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4]);

year -= i32(month < 3);
year += floorDiv(year, 4) - floorDiv(year, 100) + floorDiv(year, 400);
year += floorDiv(year, 4) - floorDiv(year, 100) + floorDiv(year, YEARS_PER_EPOCH);
month = <i32>load<u8>(tab + month - 1);
return euclidRem(year + month + day, 7);
}
Loading