thisweek_core/calendar/
arabic.rs

1use crate::language::str_to_vec;
2use crate::weekdays::convert_weekday;
3use crate::{language::Language, week_info::Date, week_info::DateView};
4use chrono::Datelike;
5use chrono::{DateTime, Local};
6use serde::Serialize;
7
8use crate::calendar::{Calendar, CalendarSpecificDateView, CalendarView, CALENDAR_ARABIC};
9
10use crate::calendar::calendar_names::*;
11use crate::month_names::*;
12use crate::season_names::*;
13use crate::weekday_names::*;
14
15#[derive(Debug, Serialize, Clone, PartialEq)]
16pub struct ArabicCalendar;
17
18impl CalendarSpecificDateView for ArabicCalendar {
19    fn new_date(datetime: DateTime<Local>) -> Date {
20        let day = datetime.day() as u8;
21        let month = datetime.month() as u8;
22        let year = datetime.year();
23        let weekday = convert_weekday(datetime.weekday()) as u32;
24        use icu::calendar::islamic::IslamicCivil;
25        let date_iso = icu::calendar::Date::try_new_iso_date(year, month, day)
26            .expect("Failed to initialize ISO Date instance.");
27        // Conversion into Indian calendar: 1914-08-02.
28        let date = date_iso.to_calendar(IslamicCivil);
29        let year = date.year().number;
30        let month = date.month().ordinal;
31        let day = date.day_of_month().0;
32        Date {
33            calendar: Calendar::Arabic(ArabicCalendar),
34            day,
35            month,
36            weekday,
37            year,
38        }
39    }
40
41    fn new_date_view(datetime: DateTime<Local>, lang: &Language) -> DateView {
42        let date = Self::new_date(datetime);
43        let day = date.day.to_string();
44        let day = lang.change_numbers_language(&day);
45        let month0 = (date.month - 1) as usize;
46        let month = match lang {
47            Language::Arabic => ARABIC_MONTH_NAME_AR[month0],
48            Language::Farsi => ARABIC_MONTH_NAME_FA[month0],
49            _ => ARABIC_MONTH_NAME_EN[month0],
50        };
51        let month = month.to_string();
52        let year = date.year.to_string();
53        let year = lang.change_numbers_language(&year);
54
55        let weekday = date.weekday as usize;
56        let full_format = match lang {
57            Language::Arabic => format!(
58                "{}، {} {} {}",
59                WEEKDAY_NAME_FULL_AR[weekday], day, month, year
60            ),
61            Language::Farsi => format!(
62                "{}، {} {} {}",
63                WEEKDAY_NAME_FULL_FA[weekday], day, month, year
64            ),
65            _ => format!(
66                "{}, {} {} {}",
67                WEEKDAY_NAME_FULL_EN[weekday], day, month, year
68            ),
69        }
70        .to_string();
71        let weekday = match lang {
72            Language::Arabic => WEEKDAY_NAME_FULL_AR[weekday],
73            Language::Farsi => WEEKDAY_NAME_FULL_FA[weekday],
74            _ => WEEKDAY_NAME_HALF_CAP_EN[weekday],
75        }
76        .to_string();
77
78        DateView {
79            unix_day: 0,
80            day,
81            month,
82            weekday,
83            year,
84            full_format,
85        }
86    }
87
88    fn get_calendar_view(lang: &Language) -> CalendarView {
89        let months_names: Vec<String> = match lang {
90            Language::Arabic => str_to_vec(&ARABIC_MONTH_NAME_AR),
91            Language::Farsi => str_to_vec(&ARABIC_MONTH_NAME_FA),
92            _ => str_to_vec(&ARABIC_MONTH_NAME_EN),
93        };
94        let seasons_names: Vec<String> = match lang {
95            Language::Arabic => str_to_vec(&SEASON_NAME_AR),
96            Language::Farsi => str_to_vec(&SEASON_NAME_FA),
97            _ => str_to_vec(&SEASON_NAME_EN),
98        };
99        let calendar_name: String = match lang {
100            Language::Arabic => ARABIC_CALENDAR_NAME_AR.into(),
101            Language::Farsi => ARABIC_CALENDAR_NAME_FA.into(),
102            _ => ARABIC_CALENDAR_NAME_EN.into(),
103        };
104        CalendarView {
105            calendar: CALENDAR_ARABIC,
106            calendar_name,
107            language: lang.clone().into(),
108            direction: lang.default_direction(),
109            months_names,
110            seasons_names,
111        }
112    }
113}