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
use core::any::{Any, TypeId};
use core::cell::RefCell;
use alloc::string::ToString;
use alloc::string::String;
use alloc::sync::Arc;
use chrono::{Datelike, Duration, Local, NaiveDate};
use crate::app::Session;
use crate::compiler::duration::DurationItem;
use crate::config::SmartCalcConfig;
use crate::formatter::{MONTH, YEAR, get_month_info, left_padding, uppercase_first_letter};
use crate::types::{TokenType, TimeOffset};
use super::{DataItem, OperationType, UnaryType};
#[derive(Debug)]
pub struct DateItem(pub NaiveDate, pub TimeOffset);
impl DateItem {
pub fn get_date(&self) -> NaiveDate {
self.0.clone()
}
pub fn get_tz(&self) -> TimeOffset {
self.1.clone()
}
fn get_month_from_duration(&self, duration: Duration) -> i64 {
duration.num_seconds().abs() / MONTH
}
fn get_year_from_duration(&self, duration: Duration) -> i64 {
duration.num_seconds().abs() / YEAR
}
}
impl DataItem for DateItem {
fn as_token_type(&self) -> TokenType {
TokenType::Date(self.0, self.1.clone())
}
fn is_same<'a>(&self, other: &'a dyn Any) -> bool {
match other.downcast_ref::<NaiveDate>() {
Some(l_value) => l_value == &self.0,
None => false
}
}
fn as_any(&self) -> &dyn Any { self }
fn calculate(&self, _: &SmartCalcConfig, _: bool, other: &dyn DataItem, operation_type: OperationType) -> Option<Arc<dyn DataItem>> {
if other.type_name() != "DURATION" {
return None;
}
let mut date = self.0;
let mut duration = other.as_any().downcast_ref::<DurationItem>().unwrap().get_duration();
return match operation_type {
OperationType::Add => {
match self.get_year_from_duration(duration) {
0 => (),
n => {
let years_diff = date.year() + n as i32;
date = NaiveDate::from_ymd(years_diff as i32, date.month() as u32, date.day());
duration = Duration::seconds(duration.num_seconds() - (YEAR * n))
}
};
match self.get_month_from_duration(duration) {
0 => (),
n => {
let years_diff = (date.month() + n as u32) / 12;
let month = (date.month() + n as u32) % 12;
date = NaiveDate::from_ymd(date.year() + years_diff as i32, month as u32, date.day());
duration = Duration::seconds(duration.num_seconds() - (MONTH * n))
}
};
Some(Arc::new(DateItem(date + duration, self.1.clone())))
},
OperationType::Sub => {
match self.get_year_from_duration(duration) {
0 => (),
n => {
let years_diff = date.year() - n as i32;
date = NaiveDate::from_ymd(years_diff as i32, date.month() as u32, date.day());
duration = Duration::seconds(duration.num_seconds() - (YEAR * n))
}
};
match self.get_month_from_duration(duration) {
0 => (),
n => {
let years = date.year() - (n as i32 / 12);
let mut months = date.month() as i32 - (n as i32 % 12);
if months < 0 {
months += 12;
}
date = NaiveDate::from_ymd(years as i32, months as u32, date.day());
duration = Duration::seconds(duration.num_seconds() - (MONTH * n))
}
};
Some(Arc::new(DateItem(date - duration, self.1.clone())))
},
_ => None
};
}
fn get_number(&self, _: &dyn DataItem) -> f64 {
self.get_underlying_number()
}
fn get_underlying_number(&self) -> f64 { 0.0 }
fn type_name(&self) -> &'static str { "DATE" }
fn type_id(&self) -> TypeId { TypeId::of::<DateItem>() }
fn print(&self, config: &SmartCalcConfig, session: &RefCell<Session>) -> String {
let format = match config.format.get( &session.borrow().get_language()) {
Some(formats) => formats,
_ => match config.format.get( "en") {
Some(formats) => formats,
_ => return "".to_string()
}
};
let date_format = match self.0.year() == Local::now().date().year() {
true => format.date.get("current_year"),
false => format.date.get("full_date")
};
match date_format {
Some(data) => {
match get_month_info(config, &format.language, self.0.month() as u8) {
Some(month_info) => data.clone()
.replace("{day}", &self.0.day().to_string())
.replace("{month}", &self.0.month().to_string())
.replace("{day_pad}", &left_padding(self.0.day().into(), 2))
.replace("{month_pad}", &left_padding(self.0.month().into(), 2))
.replace("{month_long}", &uppercase_first_letter(&month_info.long))
.replace("{month_short}", &uppercase_first_letter(&month_info.short))
.replace("{year}", &self.0.year().to_string())
.replace("{timezone}", &self.1.name),
None => self.0.to_string()
}
},
None => self.0.to_string()
}
}
fn unary(&self, _: UnaryType) -> Arc<dyn DataItem> {
Arc::new(Self(self.0, self.1.clone()))
}
}
#[cfg(test)]
#[test]
fn date_test() {
use crate::compiler::date::DateItem;
use crate::compiler::duration::DurationItem;
use crate::config::SmartCalcConfig;
let config = SmartCalcConfig::default();
let session = RefCell::new(Session::default());
assert_eq!(DateItem(NaiveDate::from_ymd(2020, 1, 1), config.get_time_offset()).print(&config, &session), "1 Jan 2020".to_string());
let left = DateItem(NaiveDate::from_ymd(2020, 1, 1), config.get_time_offset());
let right = DateItem(NaiveDate::from_ymd(2020, 1, 1), config.get_time_offset());
let result = left.calculate(&config, true, &right, OperationType::Sub);
assert!(result.is_none());
let left = DateItem(NaiveDate::from_ymd(2020, 1, 1), config.get_time_offset());
let right = DurationItem(Duration::hours(24 * 20));
let result = left.calculate(&config, true, &right, OperationType::Add);
assert!(result.is_some());
assert_eq!(result.unwrap().print(&config, &session), "21 Jan 2020".to_string());
}