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
use std::fmt;
#[derive(PartialEq,PartialOrd)]
pub struct UTCDatetime{
year:u16,
month:u8,
day:u8,
hour:u8,
minute:u8,
second:u8,
}
#[derive(Debug, Clone)]
pub enum IllegalTimeError{
YearNumberError,
MonthNumberError,
DayNumberError,
HourNumberError,
MinuteNumberError,
SecondNumberError,
TimeStringError
}
impl fmt::Display for IllegalTimeError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self{
IllegalTimeError::YearNumberError=>write!(f, "Year Number Error"),
IllegalTimeError::MonthNumberError=>write!(f, "Month Number Error"),
IllegalTimeError::DayNumberError=>write!(f, "Day Number Error"),
IllegalTimeError::HourNumberError=>write!(f, "Hour Number Error"),
IllegalTimeError::MinuteNumberError=>write!(f, "Minute Number Error"),
IllegalTimeError::SecondNumberError=>write!(f, "Second Number Error"),
IllegalTimeError::TimeStringError=>write!(f,"The format of the input time string is not standardized")
}
}
}
impl UTCDatetime{
pub fn new(year:u16,month:u8,day:u8,hour:u8,minute:u8,second:u8)->Result<UTCDatetime, IllegalTimeError>{
if month==0 || month >12{
return Err(IllegalTimeError::MonthNumberError)
}
if day==0 || day >31{
return Err(IllegalTimeError::DayNumberError)
}
if hour >23{
return Err(IllegalTimeError::HourNumberError)
}
if minute>59{
return Err(IllegalTimeError::MinuteNumberError)
}
if second>59{
return Err(IllegalTimeError::SecondNumberError)
}
Ok(UTCDatetime{year,month,day,hour,minute,second})
}
pub fn get_timestamp(&self)->Result<u32,IllegalTimeError>{
if self.year<1970{
return Err(IllegalTimeError::YearNumberError)
}
let second=self.second as u32;
let minute=self.minute as u32;
let hour=self.hour as u32;
let day =self.day as u32;
let seconds_this_month=second+60*minute+60*60*hour+60*60*24*(day-1);
let mut seconds_past_years:u32=0;
for i in 1970..self.year{
if is_leap_year(i){
seconds_past_years+=366*24*60*60;
}else{
seconds_past_years+=365*24*60*60;
}
}
let mut seconds_past_months=0;
for i in 1..self.month{
let days_num=days_of_the_month(self.year, i) as u32;
seconds_past_months+=days_num*24*60*60;
}
Ok(seconds_past_years+seconds_past_months+seconds_this_month)
}
pub fn day_of_the_week(&self)->u8{
let ts=self.get_timestamp().unwrap();
let this_week_seconds=ts%(7*24*3600);
let this_week_days=this_week_seconds/(24*3600);
let week_number=(4+this_week_days)%7;
week_number as u8
}
pub fn equals(&self,sd:&UTCDatetime)->bool{
if self.year!=sd.year
||self.month!=sd.month
||self.day!=sd.day
||self.hour!=sd.hour
||self.minute!=sd.minute
||self.second!=sd.second{
return false
}
true
}
fn compare_to(&self,sd:&UTCDatetime)->bool{
if self.year>sd.year{
return true
}else if self.year<sd.year{
return false
}
if self.month>sd.month{
return true
}else if self.month<sd.month{
return false
}
if self.day>sd.day{
return true
}else if self.day<sd.day{
return false
}
if self.hour>sd.hour{
return true
}else if self.hour<sd.hour{
return false
}
if self.minute>sd.minute{
return true
}else if self.minute<sd.minute{
return false
}
if self.second>sd.second{
return true
}else if self.second<sd.second{
return false
}
false
}
pub fn from_string(time_str:&str)->Result<UTCDatetime, IllegalTimeError>{
let time_string_array:Vec<&str>=time_str.split(|x|(x=='-' || x==':' ||x==' ')).collect();
if time_string_array.len()!=6{
return Err(IllegalTimeError::TimeStringError)
}
let year=time_string_array[0].parse::<u16>().unwrap();
let month=time_string_array[1].parse::<u8>().unwrap();
let day=time_string_array[2].parse::<u8>().unwrap();
let hour=time_string_array[3].parse::<u8>().unwrap();
let minute=time_string_array[4].parse::<u8>().unwrap();
let second=time_string_array[5].parse::<u8>().unwrap();
UTCDatetime::new(year,month,day,hour,minute,second)
}
}
impl fmt::Display for UTCDatetime{
fn fmt(&self,f: &mut fmt::Formatter)->fmt::Result{
write!(f,"{}-{:02}-{:02} {:02}:{:02}:{:02}",self.year,self.month,self.day,self.hour,self.minute,self.second)
}
}
fn is_leap_year(year:u16)->bool{
if (year%4==0 && year%100!=0)||year%400==0{
return true
}
false
}
fn days_of_the_month(year:u16,month:u8)->u8{
match month{
1|3|5|7|8|10|12=>31,
4|6|9|11=>30,
2=>{
if is_leap_year(year){
return 29
}
28
}
_=>{0}
}
}
fn main() ->Result<(),IllegalTimeError> {
let a_time= UTCDatetime::new(2019, 1, 1, 1, 1, 0)?;
let another_time = UTCDatetime::new(2019,1,1,1,1,0)?;
println!("{}",a_time>=another_time);
Ok(())
}