-
Notifications
You must be signed in to change notification settings - Fork 876
Description
interface Reminder {
time: string; // Format: "HH:mm"
subject: string;
}
interface Day {
date: string; // Format: "yyyy/MM/dd" (Jalali)
reminders: Reminder[];
}
class PersianCalendar {
private days: Record<string, Day> = {};
constructor(year: number, month: number) {
this.generateMonth(year, month);
}
// Generate all days for the given Jalali month
private generateMonth(year: number, month: number) {
const daysInMonth = this.getJalaliMonthDays(year, month);
for (let day = 1; day <= daysInMonth; day++) {
const dateKey = ${year}/${this.pad(month)}/${this.pad(day)};
this.days[dateKey] = { date: dateKey, reminders: [] };
}
}
// Add a reminder for a specific day and time
addReminder(date: string, time: string, subject: string) {
if (!this.days[date]) {
this.days[date] = { date, reminders: [] };
}
this.days[date].reminders.push({ time, subject });
}
// Get reminders for a specific day
getReminders(date: string): Reminder[] {
return this.days[date]?.reminders || [];
}
// Helper: Pad numbers with leading zeros
private pad(n: number): string {
return n < 10 ? '0' + n : n.toString();
}
// Helper: Determine number of days in a Jalali month
private getJalaliMonthDays(year: number, month: number): number {
if (month <= 6) return 31;
if (month <= 11) return 30;
// Simple leap year check for Jalali calendar (not fully accurate)
return this.isJalaliLeapYear(year) ? 30 : 29;
}
// Simple algorithm for Jalali leap year (can be improved)
private isJalaliLeapYear(year: number): boolean {
const rem = (year - (474)) % 2820 + 474 + 38;
return rem * 682 % 2816 < 682;
}
}
// Example usage:
const calendar = new PersianCalendar(1403, 5); // مرداد ۱۴۰۳
calendar.addReminder("1403/05/10", "14:00", "جلسه با تیم توسعه");
calendar.addReminder("1403/05/10", "18:00", "ورزش");
calendar.addReminder("1403/05/15", "09:00", "یادآوری پرداخت قبض آب");
console.log(calendar.getReminders("1403/05/10"));