@northguild/gmt
Version:
Date and time library for JavaScript and TypeScript, built on TC39 Temporal: time zones, DST, business days, ISO 8601 strings in and out. Never throws.
106 lines (104 loc) • 4.75 kB
JavaScript
import { isoYearString } from "./isoYearString.js";
// src/internal/epochDay.ts
/** Days in an ISO week. */
const ISO_WEEKDAYS = 7;
/**
* TC39 `ISODateWithinLimits`: -271821-04-19 and +275760-09-13, counted in days from 1970-01-01,
* are the first and last dates a PlainDate holds.
*/
const MIN_EPOCH_DAY = -100000001;
const MAX_EPOCH_DAY = 1e8;
/** 1970-01-01 was a Thursday, ISO weekday 4. */
const EPOCH_WEEKDAY_OFFSET = 3;
/** Days in a 400-year cycle of the proleptic Gregorian calendar. */
const DAYS_PER_400_YEARS = 146097;
/** Days from 0000-03-01, the start of a March-based year, to 1970-01-01. */
const DAYS_FROM_MARCH_0000_TO_EPOCH = 719468;
/**
* Days from 1970-01-01 to an ISO date, in the proleptic Gregorian calendar of ECMA-262 §21.4.1
* that Temporal's ISO calendar extends. Years run from March so the leap day ends each one.
*/
function epochDayOf(year, month, day) {
const marchYear = month <= 2 ? year - 1 : year;
const cycle = Math.floor(marchYear / 400);
const yearOfCycle = marchYear - cycle * 400;
const dayOfYear = Math.floor((153 * ((month + 9) % 12) + 2) / 5) + day - 1;
const dayOfCycle = yearOfCycle * 365 + Math.floor(yearOfCycle / 4) - Math.floor(yearOfCycle / 100) + dayOfYear;
return cycle * DAYS_PER_400_YEARS + dayOfCycle - DAYS_FROM_MARCH_0000_TO_EPOCH;
}
/** The ISO year, month and day `epochDay` days from 1970-01-01 — `epochDayOf` inverted. */
function isoFieldsOf(epochDay) {
const shifted = epochDay + DAYS_FROM_MARCH_0000_TO_EPOCH;
const cycle = Math.floor(shifted / DAYS_PER_400_YEARS);
const dayOfCycle = shifted - cycle * DAYS_PER_400_YEARS;
const yearOfCycle = Math.floor((dayOfCycle - Math.floor(dayOfCycle / 1460) + Math.floor(dayOfCycle / 36524) - Math.floor(dayOfCycle / 146096)) / 365);
const dayOfYear = dayOfCycle - (365 * yearOfCycle + Math.floor(yearOfCycle / 4) - Math.floor(yearOfCycle / 100));
const monthFromMarch = Math.floor((5 * dayOfYear + 2) / 153);
const day = dayOfYear - Math.floor((153 * monthFromMarch + 2) / 5) + 1;
const month = monthFromMarch < 10 ? monthFromMarch + 3 : monthFromMarch - 9;
return [
yearOfCycle + cycle * 400 + (month <= 2 ? 1 : 0),
month,
day
];
}
/** `Temporal.PlainDate#toString` for an ISO date: the format `mergeCalendars` returns its holidays in. */
function isoDateString(epochDay) {
const [year, month, day] = isoFieldsOf(epochDay);
return `${isoYearString(year)}-${String(month).padStart(2, "0")}-${String(day).padStart(2, "0")}`;
}
/**
* Days from 1970-01-01 to `date`, an ISO-calendar date. Any other calendar throws a
* `RangeError`, which callers turn into their sentinel: its year, month and day are not ISO
* fields, so the arithmetic below would silently walk from the wrong day.
*/
function epochDayOfDate(date) {
if (date.calendarId !== "iso8601") throw new RangeError("business-day walks take an ISO-calendar date");
return epochDayOf(date.year, date.month, date.day);
}
/**
* ISO weekday of an epoch day, Monday 1 to Sunday 7. Follows ECMA-262 `WeekDay` (day 0, 1970-01-01,
* is a Thursday) renumbered as Temporal's `ISODayOfWeek` does, Sunday 7 rather than 0.
*/
function isoWeekdayOf(epochDay) {
return ((epochDay + EPOCH_WEEKDAY_OFFSET) % 7 + 7) % 7 + 1;
}
/** Index of the first element of `days` (sorted ascending) that is >= `epochDay`. */
function lowerBound(days, epochDay) {
let low = 0;
let high = days.length;
while (low < high) {
const middle = low + high >>> 1;
if (days[middle] < epochDay) low = middle + 1;
else high = middle;
}
return low;
}
/** Members of `days` (sorted ascending) within [from, to]; 0 when from > to. */
function countWithin(days, from, to) {
if (from > to) return 0;
return lowerBound(days, to + 1) - lowerBound(days, from);
}
/** True when `epochDay` is a member of `days` (sorted ascending). */
function hasEpochDay(days, epochDay) {
const index = lowerBound(days, epochDay);
return index < days.length && days[index] === epochDay;
}
/**
* `days` ascending with repeats removed. Returns `days` itself when it is already strictly
* ascending, so a list written in date order costs one pass and no copy; otherwise sorts a copy
* numerically.
*/
function sortedUnique(days) {
let ascending = true;
for (let index = 1; index < days.length; index++) if (days[index] <= days[index - 1]) {
ascending = false;
break;
}
if (ascending) return days;
const sorted = days.slice().sort();
let kept = 0;
for (let index = 0; index < sorted.length; index++) if (index === 0 || sorted[index] !== sorted[index - 1]) sorted[kept++] = sorted[index];
return sorted.slice(0, kept);
}
export { ISO_WEEKDAYS, MAX_EPOCH_DAY, MIN_EPOCH_DAY, countWithin, epochDayOf, epochDayOfDate, hasEpochDay, isoDateString, isoFieldsOf, isoWeekdayOf, lowerBound, sortedUnique };