|
| 1 | +/** |
| 2 | + * Amazigh Calendar System |
| 3 | + * |
| 4 | + * @file AmazighCalendarSystem.js |
| 5 | + * @project dayjs-calendarsystems |
| 6 | + * @license see LICENSE file included in the project |
| 7 | + * @author Calidy.com, Amir Moradi (https://calidy.com/) |
| 8 | + * @description see README.md file included in the project |
| 9 | + * |
| 10 | + */ |
| 11 | + |
| 12 | +import CalendarSystemBase from "./CalendarSystemBase"; |
| 13 | +import * as CalendarUtils from "../calendarUtils/fourmilabCalendar"; |
| 14 | +import { generateMonthNames } from "../calendarUtils/IntlUtils"; |
| 15 | + |
| 16 | +export default class AmazighCalendarSystem extends CalendarSystemBase { |
| 17 | + constructor(locale = "en") { |
| 18 | + super(); |
| 19 | + this.firstDayOfWeek = 1; // Monday |
| 20 | + this.locale = locale; |
| 21 | + this.intlCalendar = "gregory"; // Using Gregorian as base for month names |
| 22 | + this.firstMonthNameEnglish = "Yennayer"; |
| 23 | + this.monthNamesLocalized = generateMonthNames( |
| 24 | + locale, |
| 25 | + "amazigh", |
| 26 | + "Yennayer" |
| 27 | + ); |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Converts a Julian Day Number to an Amazigh date. |
| 32 | + * @param {number} jdn - The Julian Day Number. |
| 33 | + * @returns {Object} An object containing the Amazigh year, month, and day. |
| 34 | + */ |
| 35 | + convertFromJulian(jdn) { |
| 36 | + // Constants for JDN of the Julian calendar start and the Amazigh calendar start year |
| 37 | + const JDN_JULIAN_START = 2299160.5; // October 15, 1582, Gregorian calendar start (end of Julian calendar) |
| 38 | + const AMZ_YEAR_START = 950; // Amazigh calendar start year in BC |
| 39 | + const DAYS_IN_YEAR = 365.25; // Average days in a year accounting for leap years in Julian calendar |
| 40 | + const GREGORIAN_START_YEAR = 1582; // Year the Gregorian calendar starts |
| 41 | + const YENNAYER_JDN_OFFSET = 13; // Offset for Yennayer in the Gregorian calendar as of the 21st century |
| 42 | + |
| 43 | + // Calculate the Gregorian year for the given JDN |
| 44 | + let year = GREGORIAN_START_YEAR + Math.floor((jdn - JDN_JULIAN_START) / DAYS_IN_YEAR); |
| 45 | + // Adjust the year based on the Amazigh calendar start year |
| 46 | + let amazighYear = year + (AMZ_YEAR_START - (year < 0 ? 1 : 0)); // Adjust for no year 0 in historical counting |
| 47 | + |
| 48 | + // Calculate the JDN for January 1st of the given year |
| 49 | + let jdnJan1 = jdn - ((jdn - JDN_JULIAN_START) % DAYS_IN_YEAR); |
| 50 | + // Calculate the day of the year from JDN |
| 51 | + let dayOfYear = jdn - jdnJan1 + 1; // +1 since January 1st is day 1 |
| 52 | + |
| 53 | + // Adjust dayOfYear based on the Yennayer offset |
| 54 | + dayOfYear -= YENNAYER_JDN_OFFSET; |
| 55 | + |
| 56 | + // Correct the year and dayOfYear if the adjustment crosses into the previous year |
| 57 | + if (dayOfYear <= 0) { |
| 58 | + amazighYear -= 1; |
| 59 | + dayOfYear += DAYS_IN_YEAR; // Add the days in a year to the negative dayOfYear |
| 60 | + } |
| 61 | + |
| 62 | + // Determine the month and day from dayOfYear |
| 63 | + let month = 0, day = dayOfYear; |
| 64 | + const daysInMonths = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; // Days in each month for Julian calendar |
| 65 | + while (day > daysInMonths[month]) { |
| 66 | + day -= daysInMonths[month]; |
| 67 | + month += 1; |
| 68 | + } |
| 69 | + |
| 70 | + // Adjust for the Amazigh calendar specifics if necessary |
| 71 | + // Note: This example uses a simplified approach and might need adjustments for leap years and accurate month lengths |
| 72 | + |
| 73 | + return [ |
| 74 | + amazighYear, |
| 75 | + month + 1, // +1 to make the month 1-based |
| 76 | + day |
| 77 | + ]; |
| 78 | +} |
| 79 | + |
| 80 | + |
| 81 | + // Convert Amazigh date to Julian Day |
| 82 | + convertToJulian(calendarYear, calendarMonth, calendarDay) { |
| 83 | + // Convert Amazigh year to Gregorian year |
| 84 | + const gregorianYear = calendarYear + 950; |
| 85 | + // Adjusting for Yennayer starting on January 14th in the Gregorian calendar |
| 86 | + const isBeforeYennayer = calendarMonth === 0 && calendarDay < 14; |
| 87 | + const adjustedYear = gregorianYear - (isBeforeYennayer ? 1 : 0); |
| 88 | + const adjustedMonth = isBeforeYennayer ? 12 : calendarMonth + 1; // Adjust month to 1-based for calculation |
| 89 | + const adjustedDay = calendarDay + (isBeforeYennayer ? 18 : 13); // Adjust days for Yennayer start, considering the current 13-day discrepancy |
| 90 | + |
| 91 | + // Convert adjusted Gregorian date to Julian Day |
| 92 | + return CalendarUtils.gregorian_to_jd(adjustedYear, adjustedMonth, adjustedDay); |
| 93 | + } |
| 94 | + |
| 95 | + |
| 96 | + // Convert from Gregorian date to Amazigh date |
| 97 | + convertFromGregorian(date) { |
| 98 | + const julianDay = CalendarUtils.gregorian_to_jd(date.getFullYear(), date.getMonth() + 1, date.getDate()); |
| 99 | + const gregorianYear = date.getFullYear(); |
| 100 | + const gregorianMonth = date.getMonth() + 1; // 1-based month |
| 101 | + const gregorianDay = date.getDate(); |
| 102 | + |
| 103 | + // Calculate the Amazigh year |
| 104 | + let amazighYear = gregorianYear - 950; |
| 105 | + if (gregorianMonth < 1 || (gregorianMonth === 1 && gregorianDay < 14)) { |
| 106 | + amazighYear -= 1; // Adjust for Yennayer |
| 107 | + } |
| 108 | + |
| 109 | + // Convert Julian day back to Gregorian to adjust for Yennayer offset |
| 110 | + const { year, month, day } = CalendarUtils.jd_to_gregorian(julianDay - 13); |
| 111 | + |
| 112 | + return { |
| 113 | + year: year - 950, |
| 114 | + month: month - 1, // Convert to 0-based month index |
| 115 | + day: day, |
| 116 | + }; |
| 117 | + } |
| 118 | + |
| 119 | + convertToGregorian(calendarYear, calendarMonth, calendarDay) { |
| 120 | + // Calculate the Gregorian year for the given Amazigh year. |
| 121 | + const baseYear = -950; // Starting point of the Amazigh calendar in the Gregorian calendar (950 BC). |
| 122 | + let gregorianYear = calendarYear + baseYear; |
| 123 | + |
| 124 | + // Adjust for the current discrepancy between the Julian and Gregorian calendars. |
| 125 | + const discrepancyDays = 13; // As of the 21st century, there's a 13-day difference between the calendars. |
| 126 | + const yennayerGregorianDate = new Date(gregorianYear, 0, 14 + discrepancyDays); // January 14th + discrepancy in days. |
| 127 | + |
| 128 | + // Calculate the Julian Day Number for Yennayer of the given Gregorian year. |
| 129 | + let julianDayYennayer = this.convertToJulian(yennayerGregorianDate.getFullYear(), yennayerGregorianDate.getMonth(), yennayerGregorianDate.getDate()); |
| 130 | + |
| 131 | + // Considering the Amazigh calendar follows the Julian calendar with months having the same length, |
| 132 | + // we calculate the total number of days since Yennayer to the Amazigh date. |
| 133 | + let daysSinceYennayer = 0; |
| 134 | + for (let month = 0; month < calendarMonth; month++) { |
| 135 | + daysSinceYennayer += month === 1 ? 28 : (month < 7 ? (month % 2 === 0 ? 31 : 30) : (month % 2 === 0 ? 30 : 31)); |
| 136 | + } |
| 137 | + daysSinceYennayer += calendarDay - 1; // Subtract one since Yennayer is considered day 1. |
| 138 | + |
| 139 | + // Calculate the total Julian Day and convert it back to a Gregorian date. |
| 140 | + let julianDay = julianDayYennayer + daysSinceYennayer; |
| 141 | + const gregorianDateArray = CalendarUtils.jd_to_gregorian(julianDay); |
| 142 | + return { |
| 143 | + year: gregorianDateArray[0], |
| 144 | + month: gregorianDateArray[1] - 1, // -1 because the Gregorian month is 0-based |
| 145 | + day: gregorianDateArray[2], |
| 146 | + }; |
| 147 | +} |
| 148 | + |
| 149 | + isLeapYear(year) { |
| 150 | + // Adjust if Amazigh leap year rules differ, using Gregorian as placeholder |
| 151 | + const adjustedYear = year + 950; |
| 152 | + return (adjustedYear % 4 === 0 && adjustedYear % 100 !== 0) || adjustedYear % 400 === 0; |
| 153 | + } |
| 154 | + monthNames( |
| 155 | + locale = "en", |
| 156 | + calendar = "amazigh", |
| 157 | + firstMonthName = "Yennayer" |
| 158 | + ) { |
| 159 | + return generateMonthNames(locale, calendar, firstMonthName); |
| 160 | + } |
| 161 | + |
| 162 | + getLocalizedMonthName(monthIndex) { |
| 163 | + return this.monthNamesLocalized[monthIndex]; |
| 164 | + } |
| 165 | +} |
| 166 | + |
0 commit comments