Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
node_modules
calendars
locales
dist
/calendars
/locales
/dist
test.js
8 changes: 8 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Calendar, "leapsLength">;

export default realPersian;
}

declare module "react-date-object/locales/gregorian_en" {
import type { Locale } from "react-date-object";

Expand Down
24 changes: 24 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
50 changes: 50 additions & 0 deletions src/calendars/real-persian.js
Original file line number Diff line number Diff line change
@@ -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;