-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathPersianCalendarSystem.js
More file actions
143 lines (133 loc) · 3.89 KB
/
PersianCalendarSystem.js
File metadata and controls
143 lines (133 loc) · 3.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/**
* Persian Calendar System
*
* @file PersianCalendarSystem.js
* @project dayjs-calendarsystems
* @license see LICENSE file included in the project
* @author Calidy.com, Amir Moradi (https://calidy.com/)
* @description see README.md file included in the project
*
*/
import CalendarSystemBase from "./CalendarSystemBase";
import * as CalendarUtils from "../calendarUtils/fourmilabCalendar";
import { generateMonthNames } from "../calendarUtils/IntlUtils";
export default class PersianCalendarSystem extends CalendarSystemBase {
constructor(locale = "en") {
super();
this.firstDayOfWeek = 6; // Saturday
this.locale = locale;
this.intlCalendar = "persian";
this.firstMonthNameEnglish = "Farvardin";
this.monthNamesLocalized = generateMonthNames(
locale,
"persian",
"Farvardin"
);
// months: 'ژانویه_فوریه_مارس_آوریل_مه_ژوئن_ژوئیه_اوت_سپتامبر_اکتبر_نوامبر_دسامبر'.split('_'),
}
// Expects a zero-based month index
// The Julian Day starts at noon, not at midnight.
// So, when you convert a Gregorian date to a Julian Day number,
// the result is the Julian Day number for the noon of that day.
// If the time of the date is noon or later, the Julian Day number will be for the next day.
convertToJulian(
calendarYear,
calendarMonth,
calendarDay,
hour = 0,
minute = 0,
second = 0
) {
// calendarMonth = calendarMonth+1 because the *_to_jd function month is 1-based
return (
CalendarUtils.persiana_to_jd(calendarYear, calendarMonth + 1, calendarDay) +
// We adjust the time to midnight. (noon -> midnight , diff 0.5)
0.5 +
Math.floor(second + 60 * (minute + 60 * hour) + 0.5) / 86400.0
);
}
convertFromGregorian(date) {
date = this.validateDate(date);
const julianDay =
CalendarUtils.gregorian_to_jd(
date.getFullYear(),
date.getMonth() + 1,
date.getDate()
) +
Math.floor(
date.getSeconds() +
60 * (date.getMinutes() + 60 * date.getHours()) +
0.5
) /
86400.0 -
0.5;
const convertedDateArray = CalendarUtils.jd_to_persiana(julianDay);
return {
year: convertedDateArray[0],
month: convertedDateArray[1] - 1, // -1 because the month is 0-based
day: convertedDateArray[2],
};
}
// Returns a zero-based month index
// Expects a zero-based month index
convertToGregorian(
calendarYear,
calendarMonth,
calendarDay,
hour = 0,
minute = 0,
second = 0,
millisecond = 0
) {
const julianDay = this.convertToJulian(
calendarYear,
calendarMonth,
calendarDay,
hour,
minute,
second,
millisecond
);
const gregorianDateArray = CalendarUtils.jd_to_gregorian(julianDay);
return {
year: gregorianDateArray[0],
month: gregorianDateArray[1] - 1, // -1 because the Gregorian month is 0-based
day: gregorianDateArray[2],
};
}
isLeapYear(year=null) {
if(year === null) {
year = this.$y;
}
return CalendarUtils.leap_persiana(year);
}
/**
* Get the number of days in a Persian calendar month
*
* @param {number} year - Persian year
* @param {number} month - Month (0-based, 0 = Farvardin)
* @returns {number} Number of days in the month
*/
daysInMonth(year, month) {
// First 6 months (Farvardin-Shahrivar): 31 days
if (month < 6) {
return 31;
}
// Last month (Esfand): 29 days (30 in leap years)
if (month === 11) {
return this.isLeapYear(year) ? 30 : 29;
}
// Months 6-10 (Mehr-Bahman): 30 days
return 30;
}
monthNames(
locale = "en",
calendar = "persian",
firstMonthName = "Farvardin"
) {
return generateMonthNames(locale, calendar, firstMonthName);
}
getLocalizedMonthName(monthIndex) {
return this.monthNamesLocalized[monthIndex];
}
}