@phensley/cldr-core
Version:
Core library for @phensley/cldr
100 lines • 3.59 kB
JavaScript
;
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");
/**
* Construct a date using the rules of the Persian calendar.
*
* type: persian
*
* @alpha
*/
var PersianDate = /** @class */ (function (_super) {
__extends(PersianDate, _super);
function PersianDate(firstDay, minDays) {
return _super.call(this, 'persian', firstDay, minDays) || this;
}
PersianDate.prototype.relatedYear = function () {
return this._fields[3 /* EXTENDED_YEAR */] + 622;
};
PersianDate.prototype.add = function (fields) {
var zoneId = fields.zoneId || this.timeZoneId();
var _a = this._add(fields), jd = _a[0], ms = _a[1];
return new PersianDate(this._firstDay, this._minDays).initFromJD(jd, ms, zoneId);
};
PersianDate.prototype.toString = function () {
return this._toString('Persian');
};
PersianDate.fromUnixEpoch = function (epoch, zoneId, firstDay, minDays) {
return new PersianDate(firstDay, minDays).initFromUnixEpoch(epoch, zoneId);
};
PersianDate.prototype.initFromUnixEpoch = function (epoch, zoneId) {
_super.prototype.initFromUnixEpoch.call(this, epoch, zoneId);
computePersianFields(this._fields);
return this;
};
PersianDate.prototype.initFromJD = function (jd, msDay, zoneId) {
_super.prototype.initFromJD.call(this, jd, msDay, zoneId);
computePersianFields(this._fields);
return this;
};
PersianDate.prototype.monthStart = function (eyear, month, useMonth) {
var jd = 1948320 /* JD_PERSIAN_EPOCH */ - 1 + 365 * (eyear - 1) + floor((8 * eyear + 21) / 33);
if (month !== 0) {
jd += MONTH_COUNT[month][2];
}
return jd;
};
return PersianDate;
}(calendar_1.CalendarDate));
exports.PersianDate = PersianDate;
var floor = Math.floor;
var MONTH_COUNT = [
[31, 31, 0],
[31, 31, 31],
[31, 31, 62],
[31, 31, 93],
[31, 31, 124],
[31, 31, 155],
[30, 30, 186],
[30, 30, 216],
[30, 30, 246],
[30, 30, 276],
[30, 30, 306],
[29, 30, 336] // Esfand
];
var computePersianFields = function (f) {
var jd = f[1 /* JULIAN_DAY */];
var days = jd - 1948320 /* JD_PERSIAN_EPOCH */;
var year = 1 + floor((33 * days + 3) / 12053);
var favardin1 = 365 * (year - 1) + floor((8 * year + 21) / 33);
var doy = days - favardin1;
var month = floor(doy < 216 ? (doy / 31) : ((doy - 6) / 30));
var dom = doy - MONTH_COUNT[month][2] + 1;
f[2 /* ERA */] = 0;
f[4 /* YEAR */] = year;
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 */] = leapPersian(year) ? 1 : 0;
};
/**
* Return true if the given year is a leap year in the Persian calendar; false otherwise;
*/
var leapPersian = function (y) {
var rem = [0];
utils_1.floorDiv(25 * y + 11, 33, rem);
return rem[0] < 8;
};
//# sourceMappingURL=persian.js.map