thisweek_core/calendar/
arabic.rs1use crate::language::str_to_vec;
2use crate::weekdays::{convert_weekday, is_weekday_weekend_holiday};
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 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 weekend_holiday = is_weekday_weekend_holiday(weekday.into());
57 let full_format = match lang {
58 Language::Arabic => format!(
59 "{}، {} {} {}",
60 WEEKDAY_NAME_FULL_AR[weekday], day, month, year
61 ),
62 Language::Farsi => format!(
63 "{}، {} {} {}",
64 WEEKDAY_NAME_FULL_FA[weekday], day, month, year
65 ),
66 _ => format!(
67 "{}, {} {} {}",
68 WEEKDAY_NAME_FULL_EN[weekday], day, month, year
69 ),
70 }
71 .to_string();
72 let weekday = match lang {
73 Language::Arabic => WEEKDAY_NAME_FULL_AR[weekday],
74 Language::Farsi => WEEKDAY_NAME_FULL_FA[weekday],
75 _ => WEEKDAY_NAME_HALF_CAP_EN[weekday],
76 }
77 .to_string();
78
79 DateView {
80 unix_day: 0,
81 day,
82 month,
83 weekday,
84 year,
85 full_format,
86 weekend_holiday,
87 }
88 }
89
90 fn get_calendar_view(lang: &Language) -> CalendarView {
91 let months_names: Vec<String> = match lang {
92 Language::Arabic => str_to_vec(&ARABIC_MONTH_NAME_AR),
93 Language::Farsi => str_to_vec(&ARABIC_MONTH_NAME_FA),
94 _ => str_to_vec(&ARABIC_MONTH_NAME_EN),
95 };
96 let seasons_names: Vec<String> = match lang {
97 Language::Arabic => str_to_vec(&SEASON_NAME_AR),
98 Language::Farsi => str_to_vec(&SEASON_NAME_FA),
99 _ => str_to_vec(&SEASON_NAME_EN),
100 };
101 let calendar_name: String = match lang {
102 Language::Arabic => ARABIC_CALENDAR_NAME_AR.into(),
103 Language::Farsi => ARABIC_CALENDAR_NAME_FA.into(),
104 _ => ARABIC_CALENDAR_NAME_EN.into(),
105 };
106 CalendarView {
107 calendar: CALENDAR_ARABIC,
108 calendar_name,
109 language: lang.clone().into(),
110 direction: lang.default_direction(),
111 months_names,
112 seasons_names,
113 }
114 }
115}