1use std::time::{Duration, UNIX_EPOCH};
2use json::{JsonValue, object};
3use crate::Field;
4use chrono::{DateTime, Local, NaiveDate, NaiveDateTime};
5
6pub struct Year {
7 pub require: bool,
8 pub field: String,
9 pub mode: String,
10 pub title: String,
11 pub def: u64,
12}
13
14impl Year {
15 pub fn new(require: bool, field: &str, title: &str, default: u64) -> Self {
16 Self {
17 field: field.to_string(),
18 mode: "year".to_string(),
19 title: title.to_string(),
20 def: default,
21 require,
22 }
23 }
24 pub fn year() -> String {
25 let now: DateTime<Local> = Local::now();
26 let dft = now.format("%Y");
27 dft.to_string()
28 }
29 pub fn timestamp_to_year(timestamp: i64) -> String {
30 let d = UNIX_EPOCH + Duration::from_secs(timestamp as u64);
31 let datetime = DateTime::<Local>::from(d);
32 let timestamp_str = datetime.format("%Y").to_string();
33 timestamp_str
34 }
35}
36
37impl Field for Year {
38 fn sql(&mut self, model: &str) -> String {
39 let mut sql = format!("{}", self.field);
40 sql = format!("{} year", sql.clone());
41 if self.require {
42 sql = format!("{} not null default '{}'", sql.clone(), self.def);
43 } else {
44 sql = format!("{} null", sql.clone())
45 }
46 match model {
47 "sqlite" => sql,
48 _ => format!("{} comment '{}|{}|{}|{}'", sql.clone(), self.title, self.mode, self.require, self.def)
49 }
50 }
51 fn field(&mut self) -> JsonValue {
52 let mut field = object! {};
53 field.insert("require", JsonValue::from(self.require.clone())).unwrap();
54 field.insert("field", JsonValue::from(self.field.clone())).unwrap();
55 field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
56 field.insert("title", JsonValue::from(self.title.clone())).unwrap();
57 field.insert("def", JsonValue::from(self.def.clone())).unwrap();
58 field
59 }
60}
61
62
63pub struct Datetime {
64 pub require: bool,
65 pub field: String,
66 pub mode: String,
67 pub title: String,
68 pub def: String,
69}
70
71impl Datetime {
72 pub fn new(require: bool, field: &str, title: &str, mut default: &str) -> Self {
73 if default == "" {
74 default = "0000-01-01 00:00:00"
75 }
76 Self {
77 field: field.to_string(),
78 mode: "datetime".to_string(),
79 title: title.to_string(),
80 def: default.to_string(),
81 require,
82 }
83 }
84 pub fn datetime() -> String {
85 let now: DateTime<Local> = Local::now();
86 let dft = now.format("%Y-%m-%d %H:%M:%S");
87 dft.to_string()
88 }
89 pub fn timestamp_to_datetime(timestamp: i64) -> String {
90 let d = UNIX_EPOCH + Duration::from_secs(timestamp as u64);
91 let datetime = DateTime::<Local>::from(d);
92 let timestamp_str = datetime.format("%Y-%m-%d %H:%M:%S").to_string();
93 timestamp_str
94 }
95}
96
97impl Field for Datetime {
98 fn sql(&mut self, model: &str) -> String {
99 let mut sql = format!("{}", self.field);
100 sql = format!("{} datetime", sql.clone());
101 if self.require {
102 sql = format!("{} not null default '{}'", sql.clone(), self.def);
103 } else {
104 sql = format!("{} null", sql.clone())
105 }
106 match model {
107 "sqlite" => sql,
108 _ => format!("{} comment '{}|{}|{}|{}'", sql.clone(), self.title, self.mode, self.require, self.def)
109 }
110 }
111 fn field(&mut self) -> JsonValue {
112 let mut field = object! {};
113 field.insert("require", JsonValue::from(self.require.clone())).unwrap();
114 field.insert("field", JsonValue::from(self.field.clone())).unwrap();
115 field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
116 field.insert("title", JsonValue::from(self.title.clone())).unwrap();
117 field.insert("def", JsonValue::from(self.def.clone())).unwrap();
118 field
119 }
120}
121
122
123pub struct Time {
124 pub require: bool,
125 pub field: String,
126 pub mode: String,
127 pub title: String,
128 pub def: String,
129}
130
131impl Time {
132 pub fn new(require: bool, field: &str, title: &str, default: &str) -> Self {
133 Self {
134 field: field.to_string(),
135 mode: "time".to_string(),
136 title: title.to_string(),
137 def: default.to_string(),
138 require,
139 }
140 }
141 pub fn time() -> String {
142 let now: DateTime<Local> = Local::now();
143 let dft = now.format("%H:%M:%S");
144 dft.to_string()
145 }
146}
147
148impl Field for Time {
149 fn sql(&mut self, model: &str) -> String {
150 let mut sql = format!("{}", self.field);
151 sql = format!("{} time", sql.clone());
152 if self.require {
153 sql = format!("{} not null default '{}'", sql.clone(), self.def);
154 } else {
155 sql = format!("{} null", sql.clone())
156 }
157 match model {
158 "sqlite" => sql,
159 _ => format!("{} comment '{}|{}|{}|{}'", sql.clone(), self.title, self.mode, self.require, self.def)
160 }
161 }
162 fn field(&mut self) -> JsonValue {
163 let mut field = object! {};
164 field.insert("require", JsonValue::from(self.require.clone())).unwrap();
165 field.insert("field", JsonValue::from(self.field.clone())).unwrap();
166 field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
167 field.insert("title", JsonValue::from(self.title.clone())).unwrap();
168 field.insert("def", JsonValue::from(self.def.clone())).unwrap();
169 field
170 }
171}
172
173
174pub struct Date {
175 pub require: bool,
176 pub field: String,
177 pub mode: String,
178 pub title: String,
179 pub def: String,
180}
181
182impl Date {
183 pub fn new(require: bool, field: &str, title: &str, default: &str) -> Self {
184 let def = {
185 if default == "" {
186 "0000-01-01"
187 } else {
188 default
189 }
190 };
191 Self {
192 field: field.to_string(),
193 mode: "date".to_string(),
194 title: title.to_string(),
195 def: def.parse().unwrap(),
196 require,
197 }
198 }
199 pub fn date() -> String {
201 let now: DateTime<Local> = Local::now();
202 let dft = now.format("%Y-%m-%d");
203 dft.to_string()
204 }
205 pub fn timestamp_to_date(timestamp: i64) -> String {
206 let d = UNIX_EPOCH + Duration::from_secs(timestamp as u64);
207 let datetime = DateTime::<Local>::from(d);
208 let timestamp_str = datetime.format("%Y-%m-%d").to_string();
209 timestamp_str
210 }
211}
212
213impl Field for Date {
214 fn sql(&mut self, model: &str) -> String {
215 let mut sql = format!("{}", self.field);
216 sql = format!("{} date", sql.clone());
217 if self.require {
218 sql = format!("{} not null default '{}'", sql.clone(), self.def);
219 } else {
220 sql = format!("{} null", sql.clone())
221 }
222 match model {
223 "sqlite" => sql,
224 _ => format!("{} comment '{}|{}|{}|{}'", sql.clone(), self.title, self.mode, self.require, self.def)
225 }
226 }
227 fn field(&mut self) -> JsonValue {
228 let mut field = object! {};
229 field.insert("require", JsonValue::from(self.require.clone())).unwrap();
230 field.insert("field", JsonValue::from(self.field.clone())).unwrap();
231 field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
232 field.insert("title", JsonValue::from(self.title.clone())).unwrap();
233 field.insert("def", JsonValue::from(self.def.clone())).unwrap();
234 field
235 }
236}
237
238
239pub struct Timestamp {
240 pub require: bool,
241 pub field: String,
242 pub mode: String,
243 pub title: String,
244 pub def: f64,
245 pub dec: i32,
246}
247
248impl Timestamp {
249 pub fn new(require: bool, field: &str, title: &str, dec: i32, default: f64) -> Self {
250 Self {
251 require,
252 field: field.to_string(),
253 mode: "timestamp".to_string(),
254 title: title.to_string(),
255 def: default,
256 dec,
257 }
258 }
259 pub fn timestamp() -> i64 {
261 return Local::now().timestamp();
262 }
263 pub fn timestamp_ms() -> i64 {
265 return Local::now().timestamp_millis();
266 }
267 pub fn timestamp_ms_f64() -> f64 {
269 return Local::now().timestamp_millis() as f64 / 1000.0;
270 }
271 pub fn timestamp_μs() -> i64 {
273 return Local::now().timestamp_micros();
274 }
275 pub fn timestamp_μs_f64() -> f64 {
277 return Local::now().timestamp_micros() as f64 / 1000.0 / 1000.0;
278 }
279 pub fn timestamp_ns() -> i64 {
281 return Local::now().timestamp_nanos();
282 }
283
284 pub fn date_to_timestamp(date: &str) -> i64 {
286 let t = NaiveDate::parse_from_str(date, "%Y-%m-%d").unwrap();
287 return t.and_hms_opt(0, 0, 0).unwrap().timestamp();
288 }
289 pub fn datetime_to_timestamp(datetime: &str) -> i64 {
291 let t = NaiveDateTime::parse_from_str(datetime, "%Y-%m-%d %H:%M:%S").unwrap();
292 return t.timestamp();
293 }
294}
295
296impl Field for Timestamp {
297 fn sql(&mut self, model: &str) -> String {
298 let max = 10 + self.dec;
299
300 let mut sql = format!("{}", self.field);
301 sql = format!("{} float({},{})", sql.clone(), max, self.dec);
302 if self.require {
303 sql = format!("{} not null", sql.clone())
304 };
305 self.def = format!("{0:.width$}", self.def, width = self.dec as usize).parse::<f64>().unwrap();
306 sql = format!("{} default {}", sql.clone(), self.def);
307 match model {
308 "sqlite" => sql,
309 _ => format!("{} comment '{}|{}|{}|{}|{}'", sql.clone(), self.title, self.mode, self.require, self.dec, self.def)
310 }
311 }
312 fn field(&mut self) -> JsonValue {
313 let mut field = object! {};
314 field.insert("require", JsonValue::from(self.require.clone())).unwrap();
315 field.insert("field", JsonValue::from(self.field.clone())).unwrap();
316 field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
317 field.insert("title", JsonValue::from(self.title.clone())).unwrap();
318 field.insert("def", JsonValue::from(self.def.clone())).unwrap();
319 field.insert("dec", JsonValue::from(self.dec.clone())).unwrap();
320 field
321 }
322}