diff --git a/.gitignore b/.gitignore index deb846a..053f511 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ node_modules -calendars -locales -dist +/calendars +/locales +/dist test.js diff --git a/index.d.ts b/index.d.ts index d37ac05..9864af0 100644 --- a/index.d.ts +++ b/index.d.ts @@ -551,6 +551,14 @@ declare module "react-date-object/calendars/julian" { export = julian; } +declare module "react-date-object/calendars/real-persian" { + import type { Calendar } from "react-date-object"; + + const realPersian: Omit; + + export default realPersian; +} + declare module "react-date-object/locales/gregorian_en" { import type { Locale } from "react-date-object"; diff --git a/rollup.config.js b/rollup.config.js index ca3685f..14b6edc 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -41,8 +41,32 @@ export default [ }, ...build("calendars"), ...build("locales"), + ...buildCustom("calendars"), ]; +function buildCustom(path) { + const customPath = `./src/${path}`; + + if (!fs.existsSync(customPath)) return []; + + return fs + .readdirSync(customPath) + .filter((file) => file.endsWith(".js")) + .map((file) => file.replace(/\.js$/, "")) + .map((name) => ({ + input: `${customPath}/${name}.js`, + output: [ + { + file: `${path}/${name}.js`, + format: "cjs", + exports: "default", + plugins: [terser()], + }, + ], + plugins: [commonjs()], + })); +} + function build(path) { const nodePath = `./node_modules/date-object/${path}`; const array = fs diff --git a/src/calendars/real-persian.js b/src/calendars/real-persian.js new file mode 100644 index 0000000..4a121cf --- /dev/null +++ b/src/calendars/real-persian.js @@ -0,0 +1,50 @@ +"use strict"; + +var persian = require("../../node_modules/date-object/calendars/cjs/persian"); + +// The "Old Persian" (Imperial/Shahanshahi) calendar uses the same +// solar calendar as the modern Persian (Solar Hijri) calendar, +// but counts years from the founding of the Achaemenid Empire +// by Cyrus the Great (559 BCE) instead of the Islamic Hijra (622 CE). + +var HIJRI_EPOCH_CE = 622; +var ACHAEMENID_EPOCH_BCE = 559; +var YEAR_OFFSET = HIJRI_EPOCH_CE + ACHAEMENID_EPOCH_BCE - 1; // 1180 + +var realPersian = { + name: "real-persian", + startYear: persian.startYear + YEAR_OFFSET, + yearLength: persian.yearLength, + epoch: persian.epoch, + century: Math.ceil((persian.century * 100 + YEAR_OFFSET) / 100), + weekStartDayIndex: persian.weekStartDayIndex, + getMonthLengths: function (isLeap) { + return persian.getMonthLengths(isLeap); + }, + isLeap: function (year) { + return persian.isLeap(year - YEAR_OFFSET); + }, + getLeaps: function (currentYear) { + var persianYear = currentYear - YEAR_OFFSET; + var leaps = persian.getLeaps(persianYear); + if (!leaps) return leaps; + return leaps.map(function (y) { + return y + YEAR_OFFSET; + }); + }, + getDayOfYear: function (date) { + return persian.getDayOfYear(date); + }, + getAllDays: function (date) { + return persian.getAllDays.call(persian, { + year: date.year - YEAR_OFFSET, + month: date.month, + day: date.day, + }); + }, + guessYear: function (days, currentYear) { + return persian.guessYear(days, currentYear) + YEAR_OFFSET; + }, +}; + +module.exports = realPersian;