UNPKG

@phensley/cldr-core

Version:
180 lines 6.56 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); var calendar_1 = require("./calendar"); var utils_1 = require("./utils"); // TODO: helpers to compute fields from partial information // export class GregorianInfo { // daysInYear(year: number): number { // const isLeap = leapGregorian(year); // const m = MONTH_COUNT[11]; // return m[isLeap ? 1 : 0] + m[isLeap ? 3 : 2]; // } // monthsInYear(year: number): number { // return MONTH_COUNT.length; // } // daysInMonth(year: number, month: number): number { // const i = clamp(month - 1, 0, 11); // const isLeap = leapGregorian(year); // return MONTH_COUNT[month][isLeap ? 1 : 0]; // } // } /** * Construct a date using the rules of the Gregorian calendar. * * type: gregory */ var GregorianDate = /** @class */ (function (_super) { __extends(GregorianDate, _super); function GregorianDate(type, firstDay, minDays) { return _super.call(this, type, firstDay, minDays) || this; } GregorianDate.prototype.add = function (fields) { var zoneId = fields.zoneId || this.timeZoneId(); var _a = this._add(fields), jd = _a[0], ms = _a[1]; return new GregorianDate('gregory', this._firstDay, this._minDays).initFromJD(jd, ms, zoneId); }; GregorianDate.prototype.toString = function () { return this._toString('Gregorian'); }; GregorianDate.fromUnixEpoch = function (epoch, zoneId, firstDay, minDays) { if (firstDay === void 0) { firstDay = 1; } if (minDays === void 0) { minDays = 1; } return new GregorianDate('gregory', firstDay, minDays).initFromUnixEpoch(epoch, zoneId); }; GregorianDate.prototype.initFromUnixEpoch = function (epoch, zoneId) { _super.prototype.initFromUnixEpoch.call(this, epoch, zoneId); return this.initGregorian(); }; GregorianDate.prototype.initFromJD = function (jd, msDay, zoneId) { _super.prototype.initFromJD.call(this, jd, msDay, zoneId); return this.initGregorian(); }; GregorianDate.prototype.initGregorian = function () { var f = this._fields; if (f[1 /* JULIAN_DAY */] >= 2299161 /* JD_GREGORIAN_CUTOVER */) { computeGregorianFields(f); } else { // We use Julian calendar for dates before the Gregorian cutover computeJulianFields(f); } // Set era and year based on extended year var year = f[3 /* EXTENDED_YEAR */]; var era = 1; // AD if (year < 1) { era = 0; year = 1 - year; } f[2 /* ERA */] = era; f[4 /* YEAR */] = year; return this; }; GregorianDate.prototype.monthStart = function (eyear, month, useMonth) { var isLeap = eyear % 4 === 0; var y = eyear - 1; var jd = 365 * y + floor(y / 4) + (1721426 /* JD_GREGORIAN_EPOCH */ - 3); if (eyear >= 1582 /* JD_GREGORIAN_CUTOVER_YEAR */) { isLeap = isLeap && ((eyear % 100 !== 0) || (eyear % 400 === 0)); jd += floor(y / 400) - floor(y / 100) + 2; } if (month !== 0) { jd += MONTH_COUNT[month][isLeap ? 3 : 2]; } return jd; }; return GregorianDate; }(calendar_1.CalendarDate)); exports.GregorianDate = GregorianDate; var floor = Math.floor; var MONTH_COUNT = [ [31, 31, 0, 0], [28, 29, 31, 31], [31, 31, 59, 60], [30, 30, 90, 91], [31, 31, 120, 121], [30, 30, 151, 152], [31, 31, 181, 182], [31, 31, 212, 213], [30, 30, 243, 244], [31, 31, 273, 274], [30, 30, 304, 305], [31, 31, 334, 335] // Dec ]; /** * Compute fields for dates on or after the Gregorian cutover. */ var computeGregorianFields = function (f) { var ged = f[1 /* JULIAN_DAY */] - 1721426 /* JD_GREGORIAN_EPOCH */; var rem = [0]; var n400 = utils_1.floorDiv(ged, 146097, rem); var n100 = utils_1.floorDiv(rem[0], 36524, rem); var n4 = utils_1.floorDiv(rem[0], 1461, rem); var n1 = utils_1.floorDiv(rem[0], 365, rem); var year = 400 * n400 + 100 * n100 + 4 * n4 + n1; var doy = rem[0]; // 0-based day of year if (n100 === 4 || n1 === 4) { doy = 365; } else { ++year; } var isLeap = leapGregorian(year); var corr = 0; var mar1 = isLeap ? 60 : 59; if (doy >= mar1) { corr = isLeap ? 1 : 2; } var month = floor((12 * (doy + corr) + 6) / 367); var dom = doy - MONTH_COUNT[month][isLeap ? 3 : 2] + 1; f[3 /* EXTENDED_YEAR */] = year; f[7 /* MONTH */] = month + 1; f[10 /* DAY_OF_MONTH */] = dom; f[9 /* DAY_OF_YEAR */] = doy + 1; f[21 /* IS_LEAP */] = isLeap ? 1 : 0; }; /** * Compute fields for dates before the Gregorian cutover using the proleptic * Julian calendar. Any Gregorian date before October 15, 1582 is really a * date on the proleptic Julian calendar, with leap years every 4 years. */ var computeJulianFields = function (f) { var jed = f[1 /* JULIAN_DAY */] - (1721426 /* JD_GREGORIAN_EPOCH */ - 2); var eyear = floor((4 * jed + 1464) / 1461); var jan1 = 365 * (eyear - 1) + floor((eyear - 1) / 4); var doy = jed - jan1; var isLeap = eyear % 4 === 0; var corr = 0; var mar1 = isLeap ? 60 : 59; if (doy >= mar1) { corr = isLeap ? 1 : 2; } var month = floor((12 * (doy + corr) + 6) / 367); var dom = doy - MONTH_COUNT[month][isLeap ? 3 : 2] + 1; f[3 /* EXTENDED_YEAR */] = eyear; f[7 /* MONTH */] = month + 1; f[10 /* DAY_OF_MONTH */] = dom; f[9 /* DAY_OF_YEAR */] = doy + 1; f[21 /* IS_LEAP */] = isLeap ? 1 : 0; }; /** * Return true if the given year is a leap year in the Gregorian calendar; false otherwise. * Note that we switch to the Julian calendar at the Gregorian cutover year. */ var leapGregorian = function (y) { var r = y % 4 === 0; if (y >= 1582 /* JD_GREGORIAN_CUTOVER_YEAR */) { r = r && ((y % 100 !== 0) || (y % 400 === 0)); } return r; }; //# sourceMappingURL=gregorian.js.map