idate
Version:
Iranian Date constractor for Javascript
492 lines (442 loc) • 16.1 kB
JavaScript
(function webpackUniversalModuleDefinition(root, factory) {
if(typeof exports === 'object' && typeof module === 'object')
module.exports = factory();
else if(typeof define === 'function' && define.amd)
define([], factory);
else if(typeof exports === 'object')
exports["IDate"] = factory();
else
root["IDate"] = factory();
})(typeof self !== 'undefined' ? self : this, function() {
return /******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId]) {
/******/ return installedModules[moduleId].exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.l = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/
/******/ // define getter function for harmony exports
/******/ __webpack_require__.d = function(exports, name, getter) {
/******/ if(!__webpack_require__.o(exports, name)) {
/******/ Object.defineProperty(exports, name, {
/******/ configurable: false,
/******/ enumerable: true,
/******/ get: getter
/******/ });
/******/ }
/******/ };
/******/
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = function(module) {
/******/ var getter = module && module.__esModule ?
/******/ function getDefault() { return module['default']; } :
/******/ function getModuleExports() { return module; };
/******/ __webpack_require__.d(getter, 'a', getter);
/******/ return getter;
/******/ };
/******/
/******/ // Object.prototype.hasOwnProperty.call
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = 0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ (function(module, exports, __webpack_require__) {
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _utils = __webpack_require__(1);
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var methods = ['getHours', 'getMilliseconds', 'getMinutes', 'getSeconds', 'getTime', 'getTimezoneOffset', 'getUTCDate', 'getUTCDay', 'getUTCFullYear', 'getUTCHours', 'getUTCMilliseconds', 'getUTCMinutes', 'getUTCMonth', 'getUTCSeconds', 'now', 'parse', 'setHours', 'setMilliseconds', 'setMinutes', 'setSeconds', 'setTime', 'setUTCDate', 'setUTCFullYear', 'setUTCHours', 'setUTCMilliseconds', 'setUTCMinutes', 'setUTCMonth', 'setUTCSeconds', 'toDateString', 'toISOString', 'toJSON', 'toLocaleDateString', 'toLocaleTimeString', 'toLocaleString', 'toTimeString', 'toUTCString', 'UTC', 'valueOf'];
var DAY_NAMES = ['Shanbe', 'Yekshanbe', 'Doshanbe', 'Seshanbe', 'Chaharshanbe', 'Panjshanbe', 'Jom\'e'];
var PERSIAN_DAY_NAMES = ['شنبه', 'یکشنبه', 'دوشنبه', 'سهشنبه', 'چهارشنبه', 'پنجشنبه', 'جمعه'];
var MONTH_NAMES = ['Farvardin', 'Ordibehesht', 'Khordad', 'Tir', 'Mordad', 'Shahrivar', 'Mehr', 'Aban', 'Azar', 'Dey', 'Bahman', 'Esfand'];
var PERSIAN_MONTH_NAMES = ['فروردین', 'اردیبهشت', 'خرداد', 'تیر', 'مرداد', 'شهریور', 'مهر', 'آبان', 'آذر', 'دی', 'بهمن', 'اسفند'];
var PERSIAN_NUMBERS = ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹'];
var IDate = function (_Date) {
_inherits(IDate, _Date);
function IDate() {
_classCallCheck(this, IDate);
var _this = _possibleConstructorReturn(this, (IDate.__proto__ || Object.getPrototypeOf(IDate)).call(this));
var date = void 0;
var args = Array.from(arguments);
if (args.length === 0) {
date = Date.now();
} else if (args.length === 1) {
date = args[0] instanceof Date ? args[0].getTime() : args[0];
} else {
var fixed = (0, _utils.fixDate)(args[0], args[1] || 0, typeof args[2] === 'undefined' ? 1 : args[2]);
var _converted = (0, _utils.toGregorian)(fixed[0], fixed[1] + 1, fixed[2]);
date = [_converted.gy, _converted.gm - 1, _converted.gd].concat([args[3] || 0, args[4] || 0, args[5] || 0, args[6] || 0]);
}
if (Array.isArray(date)) {
_this.gdate = new (Function.prototype.bind.apply(Date, [null].concat(_toConsumableArray(date))))();
} else {
_this.gdate = new Date(date);
}
var converted = (0, _utils.toJalaali)(_this.gdate.getFullYear(), _this.gdate.getMonth() + 1, _this.gdate.getDate());
_this.jdate = [converted.jy, converted.jm - 1, converted.jd];
methods.forEach(function (method) {
IDate.prototype[method] = function () {
var _gdate;
return (_gdate = this.gdate)[method].apply(_gdate, arguments);
};
});
return _this;
}
_createClass(IDate, [{
key: 'getFullYear',
value: function getFullYear() {
return this.jdate[0];
}
}, {
key: 'setFullYear',
value: function setFullYear(value) {
this.jdate = (0, _utils.fixDate)(value, this.jdate[1], this.jdate[2]);
this.syncDate();
return this.gdate.getTime();
}
}, {
key: 'getMonth',
value: function getMonth() {
return this.jdate[1];
}
}, {
key: 'setMonth',
value: function setMonth(value) {
this.jdate = (0, _utils.fixDate)(this.jdate[0], value, this.jdate[2]);
this.syncDate();
return this.gdate.getTime();
}
}, {
key: 'getDate',
value: function getDate() {
return this.jdate[2];
}
}, {
key: 'setDate',
value: function setDate(value) {
this.jdate = (0, _utils.fixDate)(this.jdate[0], this.jdate[1], value);
this.syncDate();
return this.gdate.getTime();
}
}, {
key: 'getDay',
value: function getDay() {
return (this.gdate.getDay() + 1) % 7;
}
}, {
key: 'syncDate',
value: function syncDate() {
var converted = (0, _utils.toGregorian)(this.jdate[0], this.jdate[1] + 1, this.jdate[2]);
this.gdate.setFullYear(converted.gy);
this.gdate.setMonth(converted.gm - 1);
this.gdate.setDate(converted.gd);
}
}, {
key: 'toString',
value: function toString() {
var persianString = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
var replaceNums = function replaceNums(str) {
return str.replace(/./g, function (c) {
return PERSIAN_NUMBERS[c] || c;
});
};
var padNumber = function padNumber(num) {
return num.toString().length === 1 ? '0' + num : num.toString();
};
var time = padNumber(this.getHours()) + ':' + padNumber(this.getMinutes()) + ':' + padNumber(this.getSeconds());
if (persianString) {
return replaceNums(PERSIAN_DAY_NAMES[this.getDay()] + ' ' + this.getDate() + ' ' + PERSIAN_MONTH_NAMES[this.getMonth()] + ' ' + this.getFullYear() + ' \u0633\u0627\u0639\u062A ' + time);
}
return DAY_NAMES[this.getDay()] + ' ' + this.getDate() + ' ' + MONTH_NAMES[this.getMonth()] + ' ' + this.getFullYear() + ' ' + time;
}
}]);
return IDate;
}(Date);
exports.default = IDate;
module.exports = exports['default'];
/***/ }),
/* 1 */
/***/ (function(module, exports, __webpack_require__) {
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.toJalaali = toJalaali;
exports.toGregorian = toGregorian;
exports.monthLength = monthLength;
exports.fixDate = fixDate;
/*
Converts a Gregorian date to Jalaali.
*/
function toJalaali(gy, gm, gd) {
if (Object.prototype.toString.call(gy) === '[object Date]') {
gd = gy.getDate();
gm = gy.getMonth() + 1;
gy = gy.getFullYear();
}
return d2j(g2d(gy, gm, gd));
}
/*
Converts a Jalaali date to Gregorian.
*/
function toGregorian(jy, jm, jd) {
return d2g(j2d(jy, jm, jd));
}
// /*
// Checks whether a Jalaali date is valid or not.
// */
// function isValidJalaaliDate (jy, jm, jd) {
// return jy >= -61 && jy <= 3177 &&
// jm >= 1 && jm <= 12 &&
// jd >= 1 && jd <= monthLength(jy, jm)
// }
/*
Is this a leap year or not?
*/
function isLeapJalaaliYear(jy) {
return jalCal(jy).leap === 0;
}
/*
Number of days in a given month in a Jalaali year.
*/
function monthLength(jy, jm) {
if (jm <= 6) return 31;
if (jm <= 11) return 30;
if (isLeapJalaaliYear(jy)) return 30;
return 29;
}
/*
This function determines if the Jalaali (Persian) year is
leap (366-day long) or is the common year (365 days), and
finds the day in March (Gregorian calendar) of the first
day of the Jalaali year (jy).
@param jy Jalaali calendar year (-61 to 3177)
@return
leap: number of years since the last leap year (0 to 4)
gy: Gregorian year of the beginning of Jalaali year
march: the March day of Farvardin the 1st (1st day of jy)
@see: http://www.astro.uni.torun.pl/~kb/Papers/EMP/PersianC-EMP.htm
@see: http://www.fourmilab.ch/documents/calendar/
*/
function jalCal(jy) {
// Jalaali years starting the 33-year rule.
var breaks = [-61, 9, 38, 199, 426, 686, 756, 818, 1111, 1181, 1210, 1635, 2060, 2097, 2192, 2262, 2324, 2394, 2456, 3178];
var bl = breaks.length;
var gy = jy + 621;
var leapJ = -14;
var jp = breaks[0];
var jm;
var jump;
var leap;
var leapG;
var march;
var n;
var i;
if (jy < jp || jy >= breaks[bl - 1]) {
throw new Error('Invalid Jalaali year ' + jy);
}
// Find the limiting years for the Jalaali year jy.
for (i = 1; i < bl; i += 1) {
jm = breaks[i];
jump = jm - jp;
if (jy < jm) {
break;
}
leapJ = leapJ + div(jump, 33) * 8 + div(mod(jump, 33), 4);
jp = jm;
}
n = jy - jp;
// Find the number of leap years from AD 621 to the beginning
// of the current Jalaali year in the Persian calendar.
leapJ = leapJ + div(n, 33) * 8 + div(mod(n, 33) + 3, 4);
if (mod(jump, 33) === 4 && jump - n === 4) {
leapJ += 1;
}
// And the same in the Gregorian calendar (until the year gy).
leapG = div(gy, 4) - div((div(gy, 100) + 1) * 3, 4) - 150;
// Determine the Gregorian date of Farvardin the 1st.
march = 20 + leapJ - leapG;
// Find how many years have passed since the last leap year.
if (jump - n < 6) {
n = n - jump + div(jump + 4, 33) * 33;
}
leap = mod(mod(n + 1, 33) - 1, 4);
if (leap === -1) {
leap = 4;
}
return { leap: leap,
gy: gy,
march: march
};
}
/*
Converts a date of the Jalaali calendar to the Julian Day number.
@param jy Jalaali year (1 to 3100)
@param jm Jalaali month (1 to 12)
@param jd Jalaali day (1 to 29/31)
@return Julian Day number
*/
function j2d(jy, jm, jd) {
var r = jalCal(jy);
return g2d(r.gy, 3, r.march) + (jm - 1) * 31 - div(jm, 7) * (jm - 7) + jd - 1;
}
/*
Converts the Julian Day number to a date in the Jalaali calendar.
@param jdn Julian Day number
@return
jy: Jalaali year (1 to 3100)
jm: Jalaali month (1 to 12)
jd: Jalaali day (1 to 29/31)
*/
function d2j(jdn) {
var gy = d2g(jdn).gy; // Calculate Gregorian year (gy).
var jy = gy - 621;
var r = jalCal(jy);
var jdn1f = g2d(gy, 3, r.march);
var jd;
var jm;
var k;
// Find number of days that passed since 1 Farvardin.
k = jdn - jdn1f;
if (k >= 0) {
if (k <= 185) {
// The first 6 months.
jm = 1 + div(k, 31);
jd = mod(k, 31) + 1;
return { jy: jy,
jm: jm,
jd: jd
};
} else {
// The remaining months.
k -= 186;
}
} else {
// Previous Jalaali year.
jy -= 1;
k += 179;
if (r.leap === 1) {
k += 1;
}
}
jm = 7 + div(k, 30);
jd = mod(k, 30) + 1;
return { jy: jy,
jm: jm,
jd: jd
};
}
/*
Calculates the Julian Day number from Gregorian or Julian
calendar dates. This integer number corresponds to the noon of
the date (i.e. 12 hours of Universal Time).
The procedure was tested to be good since 1 March, -100100 (of both
calendars) up to a few million years into the future.
@param gy Calendar year (years BC numbered 0, -1, -2, ...)
@param gm Calendar month (1 to 12)
@param gd Calendar day of the month (1 to 28/29/30/31)
@return Julian Day number
*/
function g2d(gy, gm, gd) {
var d = div((gy + div(gm - 8, 6) + 100100) * 1461, 4) + div(153 * mod(gm + 9, 12) + 2, 5) + gd - 34840408;
d = d - div(div(gy + 100100 + div(gm - 8, 6), 100) * 3, 4) + 752;
return d;
}
/*
Calculates Gregorian and Julian calendar dates from the Julian Day number
(jdn) for the period since jdn=-34839655 (i.e. the year -100100 of both
calendars) to some millions years ahead of the present.
@param jdn Julian Day number
@return
gy: Calendar year (years BC numbered 0, -1, -2, ...)
gm: Calendar month (1 to 12)
gd: Calendar day of the month M (1 to 28/29/30/31)
*/
function d2g(jdn) {
var j, i, gd, gm, gy;
j = 4 * jdn + 139361631;
j = j + div(div(4 * jdn + 183187720, 146097) * 3, 4) * 4 - 3908;
i = div(mod(j, 1461), 4) * 5 + 308;
gd = div(mod(i, 153), 5) + 1;
gm = mod(div(i, 153), 12) + 1;
gy = div(j, 1461) - 100100 + div(8 - gm, 6);
return { gy: gy,
gm: gm,
gd: gd
};
}
/*
Utility helper functions.
*/
function div(a, b) {
return ~~(a / b);
}
function mod(a, b) {
return a - ~~(a / b) * b;
}
function fixDate(y, m, d) {
while (m > 11) {
y += 1;
m -= 12;
}
while (m < 0) {
y -= 1;
m += 12;
}
while (d > monthLength(y, m + 1)) {
d -= monthLength(y, m + 1);
m = m < 11 ? m + 1 : 0;
y = m === 0 ? y + 1 : y;
}
while (d <= 0) {
m = m > 0 ? m - 1 : 11;
y = m === 11 ? y - 1 : y;
d += monthLength(y, m + 1);
}
return [y, m, d];
}
/***/ })
/******/ ]);
});
//# sourceMappingURL=idate.js.map