jalali-ts
Version:
Parse and interact with jalali date
186 lines (185 loc) • 7.11 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Utils = void 0;
/**
* Methods of Utils class grabbed from here:
* https://github.com/jalaali/jalaali-js
* Thanks to all contributors
* https://github.com/jalaali
*/
var Utils = /** @class */ (function () {
function Utils() {
}
Utils.toJalali = function (arg1, arg2, arg3) {
var gregorian = arg1 instanceof Date ? arg1 : null;
var year = gregorian ? gregorian.getFullYear() : arg1;
var month = gregorian ? gregorian.getMonth() + 1 : arg2;
var date = gregorian ? gregorian.getDate() : arg3;
var julian = this.gregorianToJulian(year, month, date);
return this.julianToJalali(julian);
};
Utils.toGregorian = function (year, month, date) {
var julian = this.jalaliToJulian(year, month, date);
return this.julianToGregorian(julian);
};
Utils.isValid = function (year, month, date, hours, minutes, seconds, ms) {
if (hours === void 0) { hours = 0; }
if (minutes === void 0) { minutes = 0; }
if (seconds === void 0) { seconds = 0; }
if (ms === void 0) { ms = 0; }
return year >= -61 && year <= 3177 &&
month >= 1 && month <= 12 &&
date >= 1 && date <= this.monthLength(year, month) &&
hours >= 0 && hours <= 23 &&
minutes >= 0 || minutes <= 59 &&
seconds >= 0 || seconds <= 59 &&
ms >= 0 || ms <= 999;
};
Utils.isLeapYear = function (year) {
return this.calculateLeap(year) === 0;
};
Utils.monthLength = function (year, month) {
if (month <= 6)
return 31;
if (month <= 11)
return 30;
if (this.isLeapYear(year))
return 30;
return 29;
};
Utils.calculateLeap = function (year, calculated) {
var bl = this.breaks.length;
var jp = calculated ? calculated.jp : this.breaks[0];
var jump = calculated ? calculated.jump : 0;
if (!calculated) {
if (year < jp || year >= this.breaks[bl - 1]) {
throw new Error("Invalid Jalali year ".concat(year));
}
for (var i = 1; i < bl; i++) {
var jm = this.breaks[i];
jump = jm - jp;
if (year < jm)
break;
jp = jm;
}
}
var n = year - jp;
if (jump - n < 6) {
n = n - jump + this.div(jump + 4, 33) * 33;
}
var leap = this.mod(this.mod(n + 1, 33) - 1, 4);
if (leap === -1) {
leap = 4;
}
return leap;
};
Utils.calculateJalali = function (year, calculateLeap) {
if (calculateLeap === void 0) { calculateLeap = true; }
var bl = this.breaks.length;
var gregorianYear = year + 621;
var leapJ = -14;
var jp = this.breaks[0];
if (year < jp || year >= this.breaks[bl - 1]) {
throw new Error("Invalid Jalali year ".concat(year));
}
var jump = 0;
for (var i = 1; i < bl; i++) {
var jm = this.breaks[i];
jump = jm - jp;
if (year < jm)
break;
leapJ = leapJ + this.div(jump, 33) * 8 + this.div(this.mod(jump, 33), 4);
jp = jm;
}
var n = year - jp;
leapJ = leapJ + this.div(n, 33) * 8 + this.div(this.mod(n, 33) + 3, 4);
if (this.mod(jump, 33) === 4 && (jump - n) === 4) {
leapJ += 1;
}
var leapG = this.div(gregorianYear, 4) - this.div((this.div(gregorianYear, 100) + 1) * 3, 4) - 150;
var march = 20 + leapJ - leapG;
return {
gregorianYear: gregorianYear,
march: march,
leap: calculateLeap ? this.calculateLeap(year, { jp: jp, jump: jump }) : -1
};
};
Utils.jalaliToJulian = function (year, month, date) {
var r = this.calculateJalali(year, false);
return this.gregorianToJulian(r.gregorianYear, 3, r.march) + (month - 1) * 31 - this.div(month, 7) * (month - 7) + date - 1;
};
Utils.julianToJalali = function (julian) {
var gregorian = this.julianToGregorian(julian);
var year = gregorian.year - 621;
var r = this.calculateJalali(year);
var julian1F = this.gregorianToJulian(gregorian.year, 3, r.march);
var k = julian - julian1F;
if (k >= 0) {
if (k <= 185) {
return {
year: year,
month: 1 + this.div(k, 31),
date: this.mod(k, 31) + 1
};
}
else {
k -= 186;
}
}
else {
year -= 1;
k += 179;
if (r.leap === 1)
k += 1;
}
return {
year: year,
month: 7 + this.div(k, 30),
date: this.mod(k, 30) + 1
};
};
Utils.gregorianToJulian = function (year, month, date) {
var julian = this.div((year + this.div(month - 8, 6) + 100100) * 1461, 4)
+ this.div(153 * this.mod(month + 9, 12) + 2, 5)
+ date - 34840408;
return (julian - this.div(this.div(year + 100100 + this.div(month - 8, 6), 100) * 3, 4) + 752);
};
Utils.julianToGregorian = function (julian) {
var j = 4 * julian + 139361631;
j = j + this.div(this.div(4 * julian + 183187720, 146097) * 3, 4) * 4 - 3908;
var i = this.div(this.mod(j, 1461), 4) * 5 + 308;
var date = this.div(this.mod(i, 153), 5) + 1;
var month = this.mod(this.div(i, 153), 12) + 1;
var year = this.div(j, 1461) - 100100 + this.div(8 - month, 6);
return { year: year, month: month, date: date };
};
Utils.jalaliWeek = function (year, month, date) {
var dayOfWeek = this.toDate(year, month, date).getDay();
var startDayDifference = dayOfWeek === 6 ? 0 : -(dayOfWeek + 1);
var endDayDifference = 6 + startDayDifference;
return {
saturday: this.julianToJalali(this.jalaliToJulian(year, month, date + startDayDifference)),
friday: this.julianToJalali(this.jalaliToJulian(year, month, date + endDayDifference))
};
};
Utils.toDate = function (year, month, date, hours, minutes, seconds, ms) {
if (hours === void 0) { hours = 0; }
if (minutes === void 0) { minutes = 0; }
if (seconds === void 0) { seconds = 0; }
if (ms === void 0) { ms = 0; }
var gregorian = this.toGregorian(year, month, date);
return new Date(gregorian.year, gregorian.month - 1, gregorian.date, hours, minutes, seconds, ms);
};
Utils.div = function (a, b) {
return ~~(a / b);
};
Utils.mod = function (a, b) {
return a - ~~(a / b) * b;
};
Utils.breaks = [
-61, 9, 38, 199, 426, 686, 756, 818, 1111, 1181, 1210,
1635, 2060, 2097, 2192, 2262, 2324, 2394, 2456, 3178
];
return Utils;
}());
exports.Utils = Utils;