1pub mod math;
2pub mod date;
3
4use zenocore::Engine;
5
6pub fn register_std_slots(engine: &mut Engine) {
7 math::register(engine);
8 date::register(engine);
9}
10
11pub(crate) fn translate_layout_to_chrono(layout: &str) -> String {
16 let layout_lower = layout.to_lowercase();
17 match layout_lower.as_str() {
18 "human" => return "%d %b %Y %H:%M".to_string(),
19 "date" => return "%Y-%m-%d".to_string(),
20 "time" => return "%H:%M:%S".to_string(),
21 "rfc3339" => return "%Y-%m-%dT%H:%M:%S%:z".to_string(),
22 "full" => return "%A, %d %B %Y %H:%M:%S".to_string(),
23 _ => {}
24 }
25
26 let mut res = layout.to_string();
27 res = res.replace("2006-01-02 15:04:05", "%Y-%m-%d %H:%M:%S");
28 res = res.replace("2006-01-02", "%Y-%m-%d");
29 res = res.replace("15:04:05", "%H:%M:%S");
30 res = res.replace("2006", "%Y");
31 res = res.replace("06", "%y");
32 res = res.replace("01", "%m");
33 res = res.replace("02", "%d");
34 res = res.replace("15", "%H");
35 res = res.replace("04", "%M");
36 res = res.replace("05", "%S");
37
38 res = res.replace("yyyy", "%Y");
39 res = res.replace("yy", "%y");
40 res = res.replace("MMMM", "%B");
41 res = res.replace("MMM", "%b");
42 res = res.replace("MM", "%m");
43 res = res.replace("dddd", "%A");
44 res = res.replace("ddd", "%a");
45 res = res.replace("dd", "%d");
46 res = res.replace("HH", "%H");
47 res = res.replace("hh", "%I");
48 res = res.replace("mm", "%M");
49 res = res.replace("ss", "%S");
50 res = res.replace("tt", "%p");
51
52 res
53}
54
55pub(crate) fn parse_flex_date(s: &str) -> Option<chrono::DateTime<chrono::Local>> {
56 use chrono::TimeZone;
57 if let Ok(dt) = chrono::DateTime::parse_from_rfc3339(s) {
58 return Some(dt.with_timezone(&chrono::Local));
59 }
60 let formats = [
61 "%Y-%m-%d %H:%M:%S",
62 "%Y-%m-%d",
63 "%d-%m-%Y",
64 "%d/%m/%Y",
65 "%d %b %Y",
66 ];
67 for fmt in &formats {
68 if fmt.contains("%H") {
69 if let Ok(ndt) = chrono::NaiveDateTime::parse_from_str(s, fmt) {
70 if let Some(dt) = chrono::Local.from_local_datetime(&ndt).earliest() {
71 return Some(dt);
72 }
73 }
74 } else {
75 if let Ok(nd) = chrono::NaiveDate::parse_from_str(s, fmt) {
76 if let Some(ndt) = nd.and_hms_opt(0, 0, 0) {
77 if let Some(dt) = chrono::Local.from_local_datetime(&ndt).earliest() {
78 return Some(dt);
79 }
80 }
81 }
82 }
83 }
84 None
85}
86
87pub(crate) fn shift_datetime(dt: &chrono::DateTime<chrono::Local>, dur_str: &str) -> Option<chrono::DateTime<chrono::Local>> {
88 let s = dur_str.trim();
89 if s.is_empty() {
90 return Some(*dt);
91 }
92
93 let (is_negative, rest) = if s.starts_with('-') {
94 (true, &s[1..])
95 } else if s.starts_with('+') {
96 (false, &s[1..])
97 } else {
98 (false, s)
99 };
100
101 let mut num_str = String::new();
102 let mut unit = String::new();
103 for c in rest.chars() {
104 if c.is_ascii_digit() {
105 num_str.push(c);
106 } else {
107 unit.push(c);
108 }
109 }
110
111 let val = num_str.parse::<i64>().ok()?;
112 let val_signed = if is_negative { -val } else { val };
113
114 match unit.trim().to_lowercase().as_str() {
115 "s" => Some(*dt + chrono::Duration::seconds(val_signed)),
116 "m" | "min" => Some(*dt + chrono::Duration::minutes(val_signed)),
117 "h" | "hr" | "hour" | "hours" => Some(*dt + chrono::Duration::hours(val_signed)),
118 "d" | "day" | "days" => Some(*dt + chrono::Duration::days(val_signed)),
119 _ => None,
120 }
121}
122
123#[cfg(test)]
124mod tests {
125 use super::*;
126 use zenocore::{Engine, Scope, Value, parse_string, register_logic_slots, Context};
127
128 #[test]
129 fn test_math_calc_slot() {
130 let mut engine = Engine::new();
131 register_logic_slots(&mut engine);
132 register_std_slots(&mut engine);
133
134 let code = r#"
135 var: $total {
136 val: 25
137 }
138 math.calc: "ceil($total / 10)" {
139 as: $pages
140 }
141 math.calc: "pow(2, 3)" {
142 as: $powered
143 }
144 math.calc: "max(1, 5, 3)" {
145 as: $maximum
146 }
147 "#;
148 let root = parse_string(code, "test.zl").unwrap();
149 let mut ctx = Context::new();
150 let scope = Scope::new(None);
151 engine.execute(&mut ctx, &root, &scope).unwrap();
152
153 assert_eq!(scope.get("pages").unwrap(), Value::Float(3.0));
154 assert_eq!(scope.get("powered").unwrap(), Value::Float(8.0));
155 assert_eq!(scope.get("maximum").unwrap(), Value::Float(5.0));
156 }
157
158 #[test]
159 fn test_money_calc_slot() {
160 let mut engine = Engine::new();
161 register_logic_slots(&mut engine);
162 register_std_slots(&mut engine);
163
164 let code = r#"
165 var: $harga {
166 val: "100.50"
167 }
168 var: $qty {
169 val: 3
170 }
171 var: $diskon {
172 val: "10.25"
173 }
174 money.calc: "($harga * $qty) - $diskon" {
175 as: $total
176 }
177 "#;
178 let root = parse_string(code, "test.zl").unwrap();
179 let mut ctx = Context::new();
180 let scope = Scope::new(None);
181 engine.execute(&mut ctx, &root, &scope).unwrap();
182
183 assert_eq!(scope.get("total").unwrap(), Value::String("291.25".to_string()));
184 }
185
186 #[test]
187 fn test_date_slots() {
188 let mut engine = Engine::new();
189 register_logic_slots(&mut engine);
190 register_std_slots(&mut engine);
191
192 let code = r#"
193 date.now: {
194 layout: "yyyy-MM-dd"
195 as: $today
196 }
197 date.parse: "2023-12-25 15:30:00" {
198 layout: "yyyy-MM-dd HH:mm:ss"
199 as: $parsed
200 }
201 date.format: $parsed {
202 layout: "date"
203 as: $formatted
204 }
205 date.add: $parsed {
206 duration: "2h"
207 as: $shifted
208 }
209 "#;
210 let root = parse_string(code, "test.zl").unwrap();
211 let mut ctx = Context::new();
212 let scope = Scope::new(None);
213 engine.execute(&mut ctx, &root, &scope).unwrap();
214
215 let today_str = scope.get("today").unwrap().to_string_coerce();
216 assert_eq!(today_str.len(), 10);
217 assert!(today_str.contains('-'));
218
219 assert_eq!(scope.get("formatted").unwrap(), Value::String("2023-12-25".to_string()));
220
221 let shifted_str = scope.get("shifted").unwrap().to_string_coerce();
222 assert!(shifted_str.contains("17:30:00"));
223 }
224}