1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
use core::fmt;
use std::str::FromStr;
use conversion::jdn_to_eth;
use time::{self, error};
mod conversion;
mod validator;
#[derive(Clone, Debug, PartialEq, Copy)]
pub enum Month {
Meskerem = 1,
Tikimit = 2,
Hedar = 3,
Tahasass = 4,
Tir = 5,
Yekatit = 6,
Megabit = 7,
Miyazia = 8,
Ginbot = 9,
Sene = 10,
Hamle = 11,
Nehase = 12,
Puagme = 13
}
impl TryFrom<u8> for Month {
type Error = ();
fn try_from(num: u8) -> Result<Self, Self::Error> {
match num {
1 => Ok(Self::Meskerem),
2 => Ok(Self::Tikimit),
3 => Ok(Self::Hedar),
4 => Ok(Self::Tahasass),
5 => Ok(Self::Tir),
6 => Ok(Self::Yekatit),
7 => Ok(Self::Megabit),
8 => Ok(Self::Miyazia),
9 => Ok(Self::Ginbot),
10 => Ok(Self::Sene),
11 => Ok(Self::Hamle),
12 => Ok(Self::Nehase),
13 => Ok(Self::Puagme),
_ => Err(()),
}
}
}
impl FromStr for Month {
type Err = ();
fn from_str(month_name: &str) -> Result<Self, Self::Err> {
match month_name {
"Meskerem" => Ok(Month::Meskerem),
"Tikimit" => Ok(Month::Tikimit),
"Hedar" => Ok(Month::Hedar),
"Tahasass" => Ok(Month::Tahasass),
"Yekatit" => Ok(Month::Yekatit),
"Megabit" => Ok(Month::Megabit),
"Miyazia" => Ok(Month::Miyazia),
"Sene" => Ok(Month::Sene),
"Hamle" => Ok(Month::Hamle),
"Nehase" => Ok(Month::Nehase),
"Puagme" => Ok(Month::Puagme),
_ => Err(()),
}
}
}
impl fmt::Display for Month {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Self::Meskerem => "መስከረም",
Self::Tikimit => "ጥቅምት",
Self::Hedar => "ኅዳር",
Self::Tahasass => "ታኅሣሥ",
Self::Tir => "ጥር",
Self::Yekatit => "የካቲት",
Self::Megabit => "መጋቢት",
Self::Miyazia => "ሚያዝያ",
Self::Ginbot => "ግንቦት",
Self::Sene => "ሰኔ",
Self::Hamle => "ሐምሌ",
Self::Nehase => "ነሐሴ",
Self::Puagme => "ጳጉሜ",
})
}
}
#[derive(Debug, PartialEq, Clone)]
pub struct Zemen {
year: i32,
month: Month,
day: u8,
}
impl fmt::Display for Zemen {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{} {}/{}", self.month, self.day, self.year)
}
}
impl Zemen {
fn new(year: i32, month: u8, day: u8) -> Result<Self, error::ComponentRange> {
let month = Month::try_from(month).expect("must between 1 and 13");
Ok(Zemen { year, month, day })
}
pub fn year(&self) -> i32 { self.year }
pub fn month(&self) -> Month {
self.month
}
pub fn day(&self) -> u8 { self.day }
pub fn today() -> Self {
let today = time::OffsetDateTime::now_utc().date();
conversion::gre_to_eth(
today.year(),
today.month() as u8,
today.day(),
).expect("Since `today` is valid we know this won't fail.")
}
pub fn from_gre_cal(year: i32, month: u8, day: u8) -> Result<Self, error::ComponentRange> {
conversion::gre_to_eth(
year, month, day
)
}
pub fn from_gre_date(gc_date: &time::Date) -> Self {
conversion::gre_to_eth(
gc_date.year(),
gc_date.month() as u8,
gc_date.day(),
).expect("Since `gc_date` is a valid date the returned date will also be valid")
}
pub fn from_eth_cal(year: i32, month: u8, day: u8) -> Result<Self, error::ComponentRange> {
Ok(Self::new(year, month, day)?)
}
pub fn to_gre(&self) -> time::Date {
conversion::eth_to_gre(
self.year,
self.month as u8,
self.day
).expect("Since we are able to create an instance of `Zemen` in the beginning. we dont need to return errors")
}
pub fn from_jdn(jdn: i32) -> Result<Self, error::ComponentRange> {
let (year, month, day) = jdn_to_eth(jdn);
Ok(Self::from_eth_cal(year, month, day)?)
}
pub fn to_jdn(&self) -> i32 {
conversion::eth_to_jdn(
self.year, self.month as i32, self.day as i32
)
}
}
#[cfg(test)]
mod tests {
use time::error;
use time;
use crate::Zemen;
use crate::Month;
#[test]
fn test_get_current_date_and_convert_to_gre() -> Result<(), error::ComponentRange> {
let zare = Zemen::today();
println!("Zare (initiated): {}", zare);
let converted_date = zare.to_gre();
println!("Today (converted): {}", converted_date);
let converted_zare = Zemen::from_gre_date(&converted_date);
println!("Zare (converted): {}", converted_zare);
Ok(())
}
#[test]
fn test_month_creating_and_basic_parsing() -> Result<(), ()> {
let wer_num = Month::try_from(13)?;
let wer_enum_pag = Month::Puagme;
assert_eq!(wer_enum_pag, wer_num);
let wer_enum_mesk = Month::Meskerem;
let wer_str: Month = "Meskerem".parse()?;
assert_eq!(wer_enum_mesk, wer_str);
println!("wer_num: {}", wer_num);
println!("wer_str: {}", wer_str);
Ok(())
}
}