react-native-hilal 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (85) hide show
  1. package/LICENSE +20 -0
  2. package/README.md +221 -2
  3. package/lib/module/calendar/Chevron.js +60 -0
  4. package/lib/module/calendar/Chevron.js.map +1 -0
  5. package/lib/module/calendar/DayCell.js +48 -0
  6. package/lib/module/calendar/DayCell.js.map +1 -0
  7. package/lib/module/calendar/HilalCalendar.js +288 -0
  8. package/lib/module/calendar/HilalCalendar.js.map +1 -0
  9. package/lib/module/calendar/index.js +5 -0
  10. package/lib/module/calendar/index.js.map +1 -0
  11. package/lib/module/calendar/month.js +178 -0
  12. package/lib/module/calendar/month.js.map +1 -0
  13. package/lib/module/calendar/styles.js +125 -0
  14. package/lib/module/calendar/styles.js.map +1 -0
  15. package/lib/module/calendar/theme.js +49 -0
  16. package/lib/module/calendar/theme.js.map +1 -0
  17. package/lib/module/calendar/types.js +4 -0
  18. package/lib/module/calendar/types.js.map +1 -0
  19. package/lib/module/core/convert.js +160 -0
  20. package/lib/module/core/convert.js.map +1 -0
  21. package/lib/module/core/days.js +61 -0
  22. package/lib/module/core/days.js.map +1 -0
  23. package/lib/module/core/format.js +37 -0
  24. package/lib/module/core/format.js.map +1 -0
  25. package/lib/module/core/index.js +6 -0
  26. package/lib/module/core/index.js.map +1 -0
  27. package/lib/module/core/locale.js +59 -0
  28. package/lib/module/core/locale.js.map +1 -0
  29. package/lib/module/core/types.js +2 -0
  30. package/lib/module/core/types.js.map +1 -0
  31. package/lib/module/core/umalqura-data.js +24 -0
  32. package/lib/module/core/umalqura-data.js.map +1 -0
  33. package/lib/module/index.js +5 -0
  34. package/lib/module/index.js.map +1 -0
  35. package/lib/module/package.json +1 -0
  36. package/lib/typescript/package.json +1 -0
  37. package/lib/typescript/src/calendar/Chevron.d.ts +13 -0
  38. package/lib/typescript/src/calendar/Chevron.d.ts.map +1 -0
  39. package/lib/typescript/src/calendar/DayCell.d.ts +22 -0
  40. package/lib/typescript/src/calendar/DayCell.d.ts.map +1 -0
  41. package/lib/typescript/src/calendar/HilalCalendar.d.ts +7 -0
  42. package/lib/typescript/src/calendar/HilalCalendar.d.ts.map +1 -0
  43. package/lib/typescript/src/calendar/index.d.ts +4 -0
  44. package/lib/typescript/src/calendar/index.d.ts.map +1 -0
  45. package/lib/typescript/src/calendar/month.d.ts +63 -0
  46. package/lib/typescript/src/calendar/month.d.ts.map +1 -0
  47. package/lib/typescript/src/calendar/styles.d.ts +119 -0
  48. package/lib/typescript/src/calendar/styles.d.ts.map +1 -0
  49. package/lib/typescript/src/calendar/theme.d.ts +31 -0
  50. package/lib/typescript/src/calendar/theme.d.ts.map +1 -0
  51. package/lib/typescript/src/calendar/types.d.ts +58 -0
  52. package/lib/typescript/src/calendar/types.d.ts.map +1 -0
  53. package/lib/typescript/src/core/convert.d.ts +54 -0
  54. package/lib/typescript/src/core/convert.d.ts.map +1 -0
  55. package/lib/typescript/src/core/days.d.ts +24 -0
  56. package/lib/typescript/src/core/days.d.ts.map +1 -0
  57. package/lib/typescript/src/core/format.d.ts +23 -0
  58. package/lib/typescript/src/core/format.d.ts.map +1 -0
  59. package/lib/typescript/src/core/index.d.ts +5 -0
  60. package/lib/typescript/src/core/index.d.ts.map +1 -0
  61. package/lib/typescript/src/core/locale.d.ts +21 -0
  62. package/lib/typescript/src/core/locale.d.ts.map +1 -0
  63. package/lib/typescript/src/core/types.d.ts +7 -0
  64. package/lib/typescript/src/core/types.d.ts.map +1 -0
  65. package/lib/typescript/src/core/umalqura-data.d.ts +15 -0
  66. package/lib/typescript/src/core/umalqura-data.d.ts.map +1 -0
  67. package/lib/typescript/src/index.d.ts +3 -0
  68. package/lib/typescript/src/index.d.ts.map +1 -0
  69. package/package.json +180 -4
  70. package/src/calendar/Chevron.tsx +55 -0
  71. package/src/calendar/DayCell.tsx +83 -0
  72. package/src/calendar/HilalCalendar.tsx +399 -0
  73. package/src/calendar/index.ts +8 -0
  74. package/src/calendar/month.ts +213 -0
  75. package/src/calendar/styles.ts +127 -0
  76. package/src/calendar/theme.ts +69 -0
  77. package/src/calendar/types.ts +61 -0
  78. package/src/core/convert.ts +192 -0
  79. package/src/core/days.ts +64 -0
  80. package/src/core/format.ts +65 -0
  81. package/src/core/index.ts +18 -0
  82. package/src/core/locale.ts +138 -0
  83. package/src/core/types.ts +6 -0
  84. package/src/core/umalqura-data.ts +34 -0
  85. package/src/index.tsx +2 -0
@@ -0,0 +1,127 @@
1
+ import { StyleSheet } from 'react-native';
2
+
3
+ import type { HilalTheme } from './theme';
4
+
5
+ export type CalendarStyles = ReturnType<typeof createStyles>;
6
+
7
+ export function createStyles(theme: HilalTheme) {
8
+ return StyleSheet.create({
9
+ container: {
10
+ backgroundColor: theme.background,
11
+ borderRadius: 20,
12
+ padding: 12,
13
+ },
14
+ toggle: {
15
+ alignSelf: 'center',
16
+ borderColor: theme.border,
17
+ borderRadius: 999,
18
+ borderWidth: 1,
19
+ marginBottom: 12,
20
+ padding: 3,
21
+ },
22
+ toggleOption: {
23
+ borderRadius: 999,
24
+ paddingHorizontal: 16,
25
+ paddingVertical: 6,
26
+ },
27
+ toggleOptionActive: {
28
+ backgroundColor: theme.primary,
29
+ },
30
+ toggleText: {
31
+ color: theme.textSecondary,
32
+ fontSize: 14,
33
+ fontWeight: '600',
34
+ },
35
+ toggleTextActive: {
36
+ color: theme.onPrimary,
37
+ },
38
+ header: {
39
+ alignItems: 'center',
40
+ marginBottom: 8,
41
+ },
42
+ titles: {
43
+ alignItems: 'center',
44
+ flex: 1,
45
+ },
46
+ title: {
47
+ color: theme.text,
48
+ fontSize: 18,
49
+ fontWeight: '700',
50
+ },
51
+ subtitle: {
52
+ color: theme.textSecondary,
53
+ fontSize: 13,
54
+ marginTop: 2,
55
+ },
56
+ arrow: {
57
+ alignItems: 'center',
58
+ borderRadius: 999,
59
+ height: 40,
60
+ justifyContent: 'center',
61
+ width: 40,
62
+ },
63
+ arrowPressed: {
64
+ backgroundColor: theme.border,
65
+ },
66
+ weekdays: {
67
+ marginBottom: 4,
68
+ },
69
+ weekday: {
70
+ color: theme.textSecondary,
71
+ flex: 1,
72
+ fontSize: 12,
73
+ fontWeight: '600',
74
+ textAlign: 'center',
75
+ },
76
+ week: {},
77
+ slot: {
78
+ alignItems: 'center',
79
+ flex: 1,
80
+ // Keeps a gap between neighbouring days when the grid is narrower
81
+ // than 7 full-size cells, so today's ring never touches the
82
+ // selected day.
83
+ paddingHorizontal: 3,
84
+ paddingVertical: 2,
85
+ },
86
+ day: {
87
+ alignItems: 'center',
88
+ borderColor: 'transparent',
89
+ borderRadius: theme.radius,
90
+ borderWidth: 1.5,
91
+ height: 48,
92
+ justifyContent: 'center',
93
+ maxWidth: 48,
94
+ width: '100%',
95
+ },
96
+ today: {
97
+ borderColor: theme.today,
98
+ },
99
+ selected: {
100
+ backgroundColor: theme.primary,
101
+ borderColor: theme.primary,
102
+ },
103
+ pressed: {
104
+ backgroundColor: theme.border,
105
+ },
106
+ dayText: {
107
+ color: theme.text,
108
+ fontSize: 16,
109
+ fontWeight: '600',
110
+ },
111
+ secondaryText: {
112
+ color: theme.textSecondary,
113
+ fontSize: 10,
114
+ marginTop: 1,
115
+ },
116
+ selectedText: {
117
+ color: theme.onPrimary,
118
+ },
119
+ selectedSecondaryText: {
120
+ color: theme.onPrimary,
121
+ opacity: 0.8,
122
+ },
123
+ disabledText: {
124
+ color: theme.disabled,
125
+ },
126
+ });
127
+ }
@@ -0,0 +1,69 @@
1
+ /** Colors and shape of the calendar. */
2
+ export type HilalTheme = {
3
+ /** Calendar background. */
4
+ background: string;
5
+ /** Day numbers, month title, arrows. */
6
+ text: string;
7
+ /** Secondary-calendar dates and weekday names. */
8
+ textSecondary: string;
9
+ /** Selected day and active calendar toggle. */
10
+ primary: string;
11
+ /** Text on `primary`. */
12
+ onPrimary: string;
13
+ /** Ring around today's date. */
14
+ today: string;
15
+ /** Days that can't be selected, and disabled arrows. */
16
+ disabled: string;
17
+ /** Toggle outline. */
18
+ border: string;
19
+ /** Corner radius of a day cell. */
20
+ radius: number;
21
+ };
22
+
23
+ export type ThemeProp = 'light' | 'dark' | 'auto' | Partial<HilalTheme>;
24
+
25
+ export const lightTheme: HilalTheme = {
26
+ background: '#FFFFFF',
27
+ text: '#1B1F24',
28
+ textSecondary: '#6B7280',
29
+ primary: '#0F766E',
30
+ onPrimary: '#FFFFFF',
31
+ today: '#0F766E',
32
+ disabled: '#C5CAD1',
33
+ border: '#E3E6EA',
34
+ radius: 12,
35
+ };
36
+
37
+ export const darkTheme: HilalTheme = {
38
+ background: '#14171B',
39
+ text: '#F2F4F7',
40
+ textSecondary: '#9AA3AF',
41
+ primary: '#2DD4BF',
42
+ onPrimary: '#042F2C',
43
+ today: '#2DD4BF',
44
+ disabled: '#4A525C',
45
+ border: '#2A3038',
46
+ radius: 12,
47
+ };
48
+
49
+ /**
50
+ * The theme to render with. 'auto' follows the system color scheme. A
51
+ * partial theme is merged over the 'auto' theme, so a custom `primary`
52
+ * color works in both light and dark mode.
53
+ */
54
+ export function resolveTheme(
55
+ theme: ThemeProp,
56
+ colorScheme: string | null | undefined
57
+ ): HilalTheme {
58
+ const system = colorScheme === 'dark' ? darkTheme : lightTheme;
59
+ if (theme === 'light') {
60
+ return lightTheme;
61
+ }
62
+ if (theme === 'dark') {
63
+ return darkTheme;
64
+ }
65
+ if (theme === 'auto') {
66
+ return system;
67
+ }
68
+ return { ...system, ...theme };
69
+ }
@@ -0,0 +1,61 @@
1
+ import type { StyleProp, ViewStyle } from 'react-native';
2
+
3
+ import type { CalendarType, Digits, Locale } from '../core/locale';
4
+ import type { HijriDate } from '../core/types';
5
+ import type { ThemeProp } from './theme';
6
+
7
+ /** A day picked in the calendar, in both calendars. */
8
+ export type SelectedDate = {
9
+ /** Local midnight of the day. */
10
+ gregorian: Date;
11
+ hijri: HijriDate;
12
+ /** Both dates as text in the calendar's locale and digits. */
13
+ formatted: { gregorian: string; hijri: string };
14
+ };
15
+
16
+ export type HilalCalendarProps = {
17
+ /** Selected day (controlled). Only the local calendar day is used. */
18
+ value?: Date;
19
+ /** Initially selected day (uncontrolled). */
20
+ defaultValue?: Date;
21
+ /** Called when the user picks a day. */
22
+ onChange?: (date: SelectedDate) => void;
23
+
24
+ /** Calendar of the month grid. Default 'hijri'. */
25
+ calendar?: CalendarType;
26
+ /** Called when the user switches calendars with the toggle. */
27
+ onCalendarChange?: (calendar: CalendarType) => void;
28
+ /** Show the Hijri / Gregorian toggle. Default true. */
29
+ showCalendarToggle?: boolean;
30
+ /** Show the other calendar's day under each day. Default true. */
31
+ showSecondaryDate?: boolean;
32
+
33
+ /** Language of names and labels. Default 'ar'. */
34
+ locale?: Locale;
35
+ /** Default 'arabic' when locale is 'ar', otherwise 'latin'. */
36
+ digits?: Digits;
37
+ /**
38
+ * Layout direction. 'auto' (default) is right-to-left when the app runs
39
+ * right-to-left (`I18nManager.isRTL`) or the locale is 'ar'.
40
+ */
41
+ direction?: 'rtl' | 'ltr' | 'auto';
42
+ /** 0 = Sunday (default), 1 = Monday, 6 = Saturday. */
43
+ firstDayOfWeek?: 0 | 1 | 6;
44
+
45
+ /** Earliest selectable day. */
46
+ minDate?: Date;
47
+ /** Latest selectable day. */
48
+ maxDate?: Date;
49
+ /** Return true to disable a day. */
50
+ isDateDisabled?: (date: SelectedDate) => boolean;
51
+
52
+ /**
53
+ * 'light', 'dark', 'auto' (default, follows the system color scheme),
54
+ * or a partial theme merged over the 'auto' theme.
55
+ */
56
+ theme?: ThemeProp;
57
+ /** Style of the outer container. */
58
+ style?: StyleProp<ViewStyle>;
59
+ /** Style of every day cell (the pressable circle). */
60
+ dayStyle?: StyleProp<ViewStyle>;
61
+ };
@@ -0,0 +1,192 @@
1
+ import {
2
+ civilFromDays,
3
+ epochDayOfLocalDate,
4
+ localDateOfEpochDay,
5
+ } from './days';
6
+ import type { HijriDate } from './types';
7
+ import {
8
+ UMALQURA_MAX_YEAR,
9
+ UMALQURA_MIN_YEAR,
10
+ UMALQURA_MONTH_LENGTH_MASKS,
11
+ UMALQURA_START_EPOCH_DAY,
12
+ } from './umalqura-data';
13
+
14
+ /**
15
+ * Epoch day of the first day of every month in the table, plus one extra
16
+ * entry for the day after the last month. Index `(year - MIN) * 12 +
17
+ * (month - 1)`.
18
+ */
19
+ const MONTH_STARTS: readonly number[] = (() => {
20
+ const starts = [UMALQURA_START_EPOCH_DAY];
21
+ let epochDay = UMALQURA_START_EPOCH_DAY;
22
+ for (const mask of UMALQURA_MONTH_LENGTH_MASKS) {
23
+ for (let month = 0; month < 12; month++) {
24
+ // eslint-disable-next-line no-bitwise -- the table is a bit mask
25
+ epochDay += (mask >> month) & 1 ? 30 : 29;
26
+ starts.push(epochDay);
27
+ }
28
+ }
29
+ return starts;
30
+ })();
31
+
32
+ /** First and last epoch day of the supported range (both inclusive). */
33
+ export const FIRST_DAY = MONTH_STARTS[0]!;
34
+ export const LAST_DAY = MONTH_STARTS[MONTH_STARTS.length - 1]! - 1;
35
+
36
+ function monthIndex(year: number, month: number) {
37
+ return (year - UMALQURA_MIN_YEAR) * 12 + (month - 1);
38
+ }
39
+
40
+ function formatIso({ year, month, day }: HijriDate) {
41
+ const pad = (n: number) => String(n).padStart(2, '0');
42
+ return `${year}-${pad(month)}-${pad(day)}`;
43
+ }
44
+
45
+ function supportedRangeText() {
46
+ const { min, max } = getSupportedRange();
47
+ return (
48
+ `${formatIso(min)} to ${formatIso(max)} AH ` +
49
+ `(${formatIso(civilFromDays(FIRST_DAY))} to ` +
50
+ `${formatIso(civilFromDays(LAST_DAY))})`
51
+ );
52
+ }
53
+
54
+ /** First and last Hijri dates the table covers. */
55
+ export function getSupportedRange(): { min: HijriDate; max: HijriDate } {
56
+ return {
57
+ min: { year: UMALQURA_MIN_YEAR, month: 1, day: 1 },
58
+ max: {
59
+ year: UMALQURA_MAX_YEAR,
60
+ month: 12,
61
+ day: getHijriMonthLength(UMALQURA_MAX_YEAR, 12),
62
+ },
63
+ };
64
+ }
65
+
66
+ /**
67
+ * Number of days in a Hijri month.
68
+ *
69
+ * @throws {RangeError} if the year is outside the supported range or the
70
+ * month is not an integer from 1 to 12.
71
+ */
72
+ export function getHijriMonthLength(year: number, month: number): 29 | 30 {
73
+ if (
74
+ !Number.isInteger(year) ||
75
+ !Number.isInteger(month) ||
76
+ year < UMALQURA_MIN_YEAR ||
77
+ year > UMALQURA_MAX_YEAR ||
78
+ month < 1 ||
79
+ month > 12
80
+ ) {
81
+ throw new RangeError(
82
+ `getHijriMonthLength: ${year}-${month} is not a month in the ` +
83
+ `supported range (${UMALQURA_MIN_YEAR}-${UMALQURA_MAX_YEAR} AH)`
84
+ );
85
+ }
86
+ const index = monthIndex(year, month);
87
+ return MONTH_STARTS[index + 1]! - MONTH_STARTS[index]! === 30 ? 30 : 29;
88
+ }
89
+
90
+ /** Whether `hijri` is a real day within the supported range. */
91
+ export function isValidHijri(hijri: HijriDate): boolean {
92
+ if (typeof hijri !== 'object' || hijri === null) {
93
+ return false;
94
+ }
95
+ const { year, month, day } = hijri;
96
+ if (
97
+ !Number.isInteger(year) ||
98
+ !Number.isInteger(month) ||
99
+ !Number.isInteger(day) ||
100
+ year < UMALQURA_MIN_YEAR ||
101
+ year > UMALQURA_MAX_YEAR ||
102
+ month < 1 ||
103
+ month > 12 ||
104
+ day < 1
105
+ ) {
106
+ return false;
107
+ }
108
+ return day <= getHijriMonthLength(year, month);
109
+ }
110
+
111
+ /**
112
+ * Hijri date of an epoch day. Internal: the public entry point is
113
+ * `toHijri`.
114
+ *
115
+ * @throws {RangeError} if the day is outside the supported range.
116
+ */
117
+ export function hijriOfEpochDay(epochDay: number, caller = 'toHijri') {
118
+ if (
119
+ !Number.isInteger(epochDay) ||
120
+ epochDay < FIRST_DAY ||
121
+ epochDay > LAST_DAY
122
+ ) {
123
+ throw new RangeError(
124
+ `${caller}: ${formatIso(civilFromDays(epochDay))} is outside the ` +
125
+ `supported range ${supportedRangeText()}`
126
+ );
127
+ }
128
+
129
+ // Last month that starts on or before `epochDay`.
130
+ let low = 0;
131
+ let high = MONTH_STARTS.length - 2;
132
+ while (low < high) {
133
+ const mid = (low + high + 1) >> 1; // eslint-disable-line no-bitwise
134
+ if (MONTH_STARTS[mid]! <= epochDay) {
135
+ low = mid;
136
+ } else {
137
+ high = mid - 1;
138
+ }
139
+ }
140
+
141
+ return {
142
+ year: UMALQURA_MIN_YEAR + Math.floor(low / 12),
143
+ month: (low % 12) + 1,
144
+ day: epochDay - MONTH_STARTS[low]! + 1,
145
+ };
146
+ }
147
+
148
+ /**
149
+ * Epoch day of a Hijri date. Internal: the public entry point is
150
+ * `toGregorian`.
151
+ *
152
+ * @throws {RangeError} if `hijri` is not a valid date in the supported
153
+ * range.
154
+ */
155
+ export function epochDayOfHijri(hijri: HijriDate, caller = 'toGregorian') {
156
+ if (!isValidHijri(hijri)) {
157
+ const text =
158
+ typeof hijri === 'object' && hijri !== null
159
+ ? `${hijri.year}-${hijri.month}-${hijri.day}`
160
+ : String(hijri);
161
+ throw new RangeError(
162
+ `${caller}: ${text} is not a valid Hijri date in the supported ` +
163
+ `range ${supportedRangeText()}`
164
+ );
165
+ }
166
+ return MONTH_STARTS[monthIndex(hijri.year, hijri.month)]! + hijri.day - 1;
167
+ }
168
+
169
+ /**
170
+ * Converts the local calendar day of `date` to a Hijri date. The time of
171
+ * day and the time zone offset are ignored: 23:59 and 00:00 on the same
172
+ * local day give the same result.
173
+ *
174
+ * @throws {RangeError} if `date` is invalid or outside the supported range.
175
+ */
176
+ export function toHijri(date: Date): HijriDate {
177
+ if (!(date instanceof Date) || Number.isNaN(date.getTime())) {
178
+ throw new RangeError('toHijri: expected a valid Date');
179
+ }
180
+ return hijriOfEpochDay(epochDayOfLocalDate(date));
181
+ }
182
+
183
+ /**
184
+ * Converts a Hijri date to a `Date` at local midnight of the matching
185
+ * Gregorian day.
186
+ *
187
+ * @throws {RangeError} if `hijri` is not a valid date in the supported
188
+ * range.
189
+ */
190
+ export function toGregorian(hijri: HijriDate): Date {
191
+ return localDateOfEpochDay(epochDayOfHijri(hijri));
192
+ }
@@ -0,0 +1,64 @@
1
+ /**
2
+ * Calendar-day arithmetic on plain year/month/day numbers.
3
+ *
4
+ * Days are counted from 1970-01-01 in the proleptic Gregorian calendar
5
+ * ("epoch days"). The algorithms are Howard Hinnant's `days_from_civil`
6
+ * and `civil_from_days`, which use integer math only and never touch a
7
+ * time zone.
8
+ */
9
+
10
+ export type CivilDate = { year: number; month: number; day: number };
11
+
12
+ /** Epoch day of a Gregorian year/month/day (month is 1-12). */
13
+ export function daysFromCivil(year: number, month: number, day: number) {
14
+ const y = month <= 2 ? year - 1 : year;
15
+ const era = Math.floor(y / 400);
16
+ const yearOfEra = y - era * 400;
17
+ const shiftedMonth = (month + 9) % 12; // March = 0
18
+ const dayOfYear = Math.floor((153 * shiftedMonth + 2) / 5) + day - 1;
19
+ const dayOfEra =
20
+ yearOfEra * 365 +
21
+ Math.floor(yearOfEra / 4) -
22
+ Math.floor(yearOfEra / 100) +
23
+ dayOfYear;
24
+ return era * 146097 + dayOfEra - 719468;
25
+ }
26
+
27
+ /** Gregorian year/month/day (month is 1-12) of an epoch day. */
28
+ export function civilFromDays(epochDay: number): CivilDate {
29
+ const z = epochDay + 719468;
30
+ const era = Math.floor(z / 146097);
31
+ const dayOfEra = z - era * 146097;
32
+ const yearOfEra = Math.floor(
33
+ (dayOfEra -
34
+ Math.floor(dayOfEra / 1460) +
35
+ Math.floor(dayOfEra / 36524) -
36
+ Math.floor(dayOfEra / 146096)) /
37
+ 365
38
+ );
39
+ const dayOfYear =
40
+ dayOfEra -
41
+ (365 * yearOfEra + Math.floor(yearOfEra / 4) - Math.floor(yearOfEra / 100));
42
+ const shiftedMonth = Math.floor((5 * dayOfYear + 2) / 153);
43
+ const day = dayOfYear - Math.floor((153 * shiftedMonth + 2) / 5) + 1;
44
+ const month = shiftedMonth < 10 ? shiftedMonth + 3 : shiftedMonth - 9;
45
+ const year = yearOfEra + era * 400 + (month <= 2 ? 1 : 0);
46
+ return { year, month, day };
47
+ }
48
+
49
+ /** Epoch day of the local calendar day `date` falls on. */
50
+ export function epochDayOfLocalDate(date: Date) {
51
+ return daysFromCivil(date.getFullYear(), date.getMonth() + 1, date.getDate());
52
+ }
53
+
54
+ /** A `Date` at local midnight (start of day) of an epoch day. */
55
+ export function localDateOfEpochDay(epochDay: number) {
56
+ const { year, month, day } = civilFromDays(epochDay);
57
+ return new Date(year, month - 1, day);
58
+ }
59
+
60
+ /** Day of the week of an epoch day, 0 = Sunday (like `Date#getDay()`). */
61
+ export function weekdayOfEpochDay(epochDay: number) {
62
+ // 1970-01-01 was a Thursday.
63
+ return (((epochDay + 4) % 7) + 7) % 7;
64
+ }
@@ -0,0 +1,65 @@
1
+ import { isValidHijri } from './convert';
2
+ import {
3
+ defaultDigits,
4
+ formatNumber,
5
+ getMonthNames,
6
+ type Digits,
7
+ type Locale,
8
+ } from './locale';
9
+ import type { CivilDate } from './days';
10
+ import type { HijriDate } from './types';
11
+
12
+ export type FormatHijriOptions = {
13
+ /** Default: 'ar'. */
14
+ locale?: Locale;
15
+ /** Default: 'arabic' when locale is 'ar', 'latin' otherwise. */
16
+ digits?: Digits;
17
+ };
18
+
19
+ const ERA: Record<Locale, string> = { ar: 'هـ', en: 'AH' };
20
+
21
+ /**
22
+ * Formats a Hijri date as "day month year era", for example
23
+ * "١ رمضان ١٤٤٦ هـ" or "1 Ramadan 1446 AH".
24
+ *
25
+ * @throws {RangeError} if `hijri` is not a valid date in the supported
26
+ * range.
27
+ */
28
+ export function formatHijri(
29
+ hijri: HijriDate,
30
+ options: FormatHijriOptions = {}
31
+ ): string {
32
+ if (!isValidHijri(hijri)) {
33
+ throw new RangeError(
34
+ `formatHijri: ${hijri.year}-${hijri.month}-${hijri.day} is not a ` +
35
+ 'valid Hijri date in the supported range'
36
+ );
37
+ }
38
+ const locale = options.locale ?? 'ar';
39
+ const digits = options.digits ?? defaultDigits(locale);
40
+ const month = getMonthNames('hijri', locale)[hijri.month - 1];
41
+ return [
42
+ formatNumber(hijri.day, digits),
43
+ month,
44
+ formatNumber(hijri.year, digits),
45
+ ERA[locale],
46
+ ].join(' ');
47
+ }
48
+
49
+ /**
50
+ * Formats a Gregorian date as "day month year", for example
51
+ * "١١ مارس ٢٠٢٤" or "11 March 2024". Internal: used by the calendar.
52
+ */
53
+ export function formatGregorian(
54
+ date: CivilDate,
55
+ options: FormatHijriOptions = {}
56
+ ): string {
57
+ const locale = options.locale ?? 'ar';
58
+ const digits = options.digits ?? defaultDigits(locale);
59
+ const month = getMonthNames('gregorian', locale)[date.month - 1];
60
+ return [
61
+ formatNumber(date.day, digits),
62
+ month,
63
+ formatNumber(date.year, digits),
64
+ ].join(' ');
65
+ }
@@ -0,0 +1,18 @@
1
+ export {
2
+ getHijriMonthLength,
3
+ getSupportedRange,
4
+ isValidHijri,
5
+ toGregorian,
6
+ toHijri,
7
+ } from './convert';
8
+ export { formatHijri, type FormatHijriOptions } from './format';
9
+ export {
10
+ getMonthNames,
11
+ getWeekdayNames,
12
+ toArabicDigits,
13
+ type CalendarType,
14
+ type Digits,
15
+ type Locale,
16
+ type WeekdayFormat,
17
+ } from './locale';
18
+ export type { HijriDate } from './types';