gitql_std/datetime/
mod.rs

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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
extern crate chrono;

use gitql_ast::types::any::AnyType;
use gitql_ast::types::boolean::BoolType;
use gitql_ast::types::date::DateType;
use gitql_ast::types::datetime::DateTimeType;
use gitql_ast::types::integer::IntType;
use gitql_ast::types::text::TextType;
use gitql_ast::types::time::TimeType;
use gitql_ast::types::variant::VariantType;
use gitql_core::signature::Signature;
use gitql_core::signature::StandardFunction;
use gitql_core::values::base::Value;
use gitql_core::values::boolean::BoolValue;
use gitql_core::values::date::DateValue;
use gitql_core::values::datetime::DateTimeValue;
use gitql_core::values::integer::IntValue;
use gitql_core::values::text::TextValue;
use gitql_core::values::time::TimeValue;

use std::collections::HashMap;

use chrono::DateTime;
use chrono::Datelike;
use chrono::NaiveDate;
use chrono::TimeZone;
use chrono::Timelike;
use chrono::Utc;
use chrono::Weekday;

#[inline(always)]
pub fn register_std_datetime_functions(map: &mut HashMap<&'static str, StandardFunction>) {
    map.insert("date", date_extract_date);
    map.insert("current_date", date_current_date);
    map.insert("current_time", date_current_time);
    map.insert("current_timestamp", date_current_timestamp);
    map.insert("now", date_current_timestamp);
    map.insert("makedate", date_make_date);
    map.insert("maketime", date_make_time);
    map.insert("day", date_day);
    map.insert("dayname", date_dayname);
    map.insert("monthname", date_monthname);
    map.insert("hour", date_hour);
    map.insert("minute", date_minute);
    map.insert("isdate", date_is_date);
    map.insert("dayofweek", date_day_of_week);
    map.insert("dayofmonth", date_day_of_month);
    map.insert("dayofyear", date_day_of_year);
    map.insert("weekofyear", date_week_of_year);
    map.insert("quarter", date_quarter);
    map.insert("year", date_year);
    map.insert("month", date_month);
    map.insert("weekday", date_weekday);
    map.insert("to_days", date_to_days);
    map.insert("last_day", date_last_day);
    map.insert("yearweek", date_year_and_week);
}

#[inline(always)]
pub fn register_std_datetime_function_signatures(map: &mut HashMap<&'static str, Signature>) {
    map.insert(
        "date",
        Signature {
            parameters: vec![Box::new(VariantType {
                variants: vec![Box::new(DateType), Box::new(DateTimeType)],
            })],
            return_type: Box::new(DateType),
        },
    );
    map.insert(
        "current_date",
        Signature {
            parameters: vec![],
            return_type: Box::new(DateType),
        },
    );
    map.insert(
        "current_time",
        Signature {
            parameters: vec![],
            return_type: Box::new(TimeType),
        },
    );
    map.insert(
        "current_timestamp",
        Signature {
            parameters: vec![],
            return_type: Box::new(DateTimeType),
        },
    );
    map.insert(
        "now",
        Signature {
            parameters: vec![],
            return_type: Box::new(DateTimeType),
        },
    );
    map.insert(
        "makedate",
        Signature {
            parameters: vec![Box::new(IntType), Box::new(IntType)],
            return_type: Box::new(DateType),
        },
    );
    map.insert(
        "maketime",
        Signature {
            parameters: vec![Box::new(IntType), Box::new(IntType), Box::new(IntType)],
            return_type: Box::new(TimeType),
        },
    );
    map.insert(
        "dayname",
        Signature {
            parameters: vec![Box::new(DateType)],
            return_type: Box::new(TextType),
        },
    );
    map.insert(
        "day",
        Signature {
            parameters: vec![Box::new(DateType)],
            return_type: Box::new(IntType),
        },
    );
    map.insert(
        "monthname",
        Signature {
            parameters: vec![Box::new(DateType)],
            return_type: Box::new(TextType),
        },
    );
    map.insert(
        "hour",
        Signature {
            parameters: vec![Box::new(DateTimeType)],
            return_type: Box::new(IntType),
        },
    );
    map.insert(
        "minute",
        Signature {
            parameters: vec![Box::new(DateTimeType)],
            return_type: Box::new(IntType),
        },
    );
    map.insert(
        "isdate",
        Signature {
            parameters: vec![Box::new(AnyType)],
            return_type: Box::new(BoolType),
        },
    );
    map.insert(
        "dayofweek",
        Signature {
            parameters: vec![Box::new(DateType)],
            return_type: Box::new(IntType),
        },
    );
    map.insert(
        "dayofmonth",
        Signature {
            parameters: vec![Box::new(DateType)],
            return_type: Box::new(IntType),
        },
    );
    map.insert(
        "dayofyear",
        Signature {
            parameters: vec![Box::new(DateType)],
            return_type: Box::new(IntType),
        },
    );
    map.insert(
        "weekofyear",
        Signature {
            parameters: vec![Box::new(DateType)],
            return_type: Box::new(IntType),
        },
    );
    map.insert(
        "quarter",
        Signature {
            parameters: vec![Box::new(DateType)],
            return_type: Box::new(IntType),
        },
    );
    map.insert(
        "year",
        Signature {
            parameters: vec![Box::new(DateType)],
            return_type: Box::new(IntType),
        },
    );
    map.insert(
        "month",
        Signature {
            parameters: vec![Box::new(DateType)],
            return_type: Box::new(IntType),
        },
    );
    map.insert(
        "weekday",
        Signature {
            parameters: vec![Box::new(DateType)],
            return_type: Box::new(IntType),
        },
    );
    map.insert(
        "to_days",
        Signature {
            parameters: vec![Box::new(DateType)],
            return_type: Box::new(IntType),
        },
    );
    map.insert(
        "last_day",
        Signature {
            parameters: vec![Box::new(DateType)],
            return_type: Box::new(DateType),
        },
    );
    map.insert(
        "yearweek",
        Signature {
            parameters: vec![Box::new(DateType)],
            return_type: Box::new(TextType),
        },
    );
}

pub fn date_extract_date(inputs: &[Box<dyn Value>]) -> Box<dyn Value> {
    let argument_type = inputs[0].data_type();
    if argument_type.is_date() {
        return inputs[0].clone();
    }
    let timestamp = inputs[0].as_date_time().unwrap();
    Box::new(DateValue { value: timestamp })
}

pub fn date_current_date(_inputs: &[Box<dyn Value>]) -> Box<dyn Value> {
    let timestamp = Utc::now().timestamp();
    Box::new(DateValue { value: timestamp })
}

pub fn date_current_time(_inputs: &[Box<dyn Value>]) -> Box<dyn Value> {
    let time_stamp = Utc::now().timestamp();
    let datetime = DateTime::from_timestamp(time_stamp, 0).unwrap();
    let time = datetime.format("%H:%M:%S").to_string();
    Box::new(TimeValue { value: time })
}

pub fn date_current_timestamp(_inputs: &[Box<dyn Value>]) -> Box<dyn Value> {
    let timestamp = Utc::now().timestamp();
    Box::new(DateTimeValue { value: timestamp })
}

pub fn date_make_date(inputs: &[Box<dyn Value>]) -> Box<dyn Value> {
    let year = inputs[0].as_int().unwrap() as i32;
    let day_of_year = inputs[1].as_int().unwrap() as u32;
    let date = NaiveDate::from_yo_opt(year, day_of_year).unwrap();
    let datetime = date.and_hms_opt(0, 0, 0).unwrap();
    let timestamp = Utc.from_utc_datetime(&datetime).timestamp();
    Box::new(DateValue { value: timestamp })
}

pub fn date_make_time(inputs: &[Box<dyn Value>]) -> Box<dyn Value> {
    let hour = inputs[0].as_int().unwrap();
    let minute = inputs[1].as_int().unwrap();
    let second = inputs[2].as_int().unwrap();
    let time = format!("{}:{:02}:{:02}", hour, minute, second);
    Box::new(TimeValue { value: time })
}

pub fn date_day(inputs: &[Box<dyn Value>]) -> Box<dyn Value> {
    let date = inputs[0].as_date().unwrap();
    let parsed_date = DateTime::from_timestamp(date, 0).unwrap();
    Box::new(IntValue {
        value: parsed_date.day().into(),
    })
}

pub fn date_dayname(inputs: &[Box<dyn Value>]) -> Box<dyn Value> {
    let date = inputs[0].as_date().unwrap();
    let parsed_date = DateTime::from_timestamp(date, 0).unwrap();
    let day_name = match parsed_date.weekday() {
        Weekday::Mon => "Monday",
        Weekday::Tue => "Tuesday",
        Weekday::Wed => "Wednesday",
        Weekday::Thu => "Thursday",
        Weekday::Fri => "Friday",
        Weekday::Sat => "Saturday",
        Weekday::Sun => "Sunday",
    }
    .to_string();
    Box::new(TextValue { value: day_name })
}

pub fn date_monthname(inputs: &[Box<dyn Value>]) -> Box<dyn Value> {
    let date = inputs[0].as_date().unwrap();
    let parsed_date = DateTime::from_timestamp(date, 0).unwrap();
    let month_name = match parsed_date.month() {
        1 => "January",
        2 => "February",
        3 => "March",
        4 => "April",
        5 => "May",
        6 => "June",
        7 => "July",
        8 => "August",
        9 => "September",
        10 => "October",
        11 => "November",
        12 => "December",
        _ => "",
    }
    .to_string();
    Box::new(TextValue { value: month_name })
}

pub fn date_hour(inputs: &[Box<dyn Value>]) -> Box<dyn Value> {
    let date = inputs[0].as_date_time().unwrap();
    let date_time = DateTime::from_timestamp(date, 0);
    let dt = date_time.unwrap().time();
    Box::new(IntValue {
        value: dt.hour() as i64,
    })
}

pub fn date_minute(inputs: &[Box<dyn Value>]) -> Box<dyn Value> {
    let date = inputs[0].as_date_time().unwrap();
    let date_time = DateTime::from_timestamp(date, 0);
    let dt = date_time.unwrap().time();
    Box::new(IntValue {
        value: dt.minute() as i64,
    })
}

pub fn date_is_date(inputs: &[Box<dyn Value>]) -> Box<dyn Value> {
    let is_date = inputs[0].data_type().is_date();
    Box::new(BoolValue { value: is_date })
}

pub fn date_day_of_week(inputs: &[Box<dyn Value>]) -> Box<dyn Value> {
    let date = inputs[0].as_date().unwrap();
    let parsed_date = DateTime::from_timestamp(date, 0).unwrap();
    let value = parsed_date.weekday().number_from_sunday().into();
    Box::new(IntValue { value })
}

pub fn date_day_of_month(inputs: &[Box<dyn Value>]) -> Box<dyn Value> {
    let date = inputs[0].as_date().unwrap();
    let parsed_date = DateTime::from_timestamp(date, 0).unwrap();
    Box::new(IntValue {
        value: parsed_date.day().into(),
    })
}

pub fn date_day_of_year(inputs: &[Box<dyn Value>]) -> Box<dyn Value> {
    let date = inputs[0].as_date().unwrap();
    let parsed_date = DateTime::from_timestamp(date, 0).unwrap();
    Box::new(IntValue {
        value: parsed_date.ordinal().into(),
    })
}

pub fn date_week_of_year(inputs: &[Box<dyn Value>]) -> Box<dyn Value> {
    let date = inputs[0].as_date().unwrap();

    let parsed_date = DateTime::from_timestamp(date, 0).unwrap();
    let native_date = parsed_date.date_naive();
    let first_day_of_year = NaiveDate::from_ymd_opt(native_date.year(), 1, 1).unwrap();
    let days_diff = native_date
        .signed_duration_since(first_day_of_year)
        .num_days();
    let week_offset = match first_day_of_year.weekday() {
        Weekday::Mon => 0,
        _ => 1,
    };

    let days_with_offset = days_diff + week_offset;
    let value = ((days_with_offset / 7) as u32 + 1).into();
    Box::new(IntValue { value })
}

pub fn date_year_and_week(inputs: &[Box<dyn Value>]) -> Box<dyn Value> {
    let date = inputs[0].as_date().unwrap();
    let parsed_date = DateTime::from_timestamp(date, 0).unwrap();
    let year = parsed_date.year();
    let parsed_date = DateTime::from_timestamp(date, 0).unwrap();
    let native_date = parsed_date.date_naive();
    let first_day_of_year = NaiveDate::from_ymd_opt(native_date.year(), 1, 1).unwrap();
    let days_diff = native_date
        .signed_duration_since(first_day_of_year)
        .num_days();
    let week_offset = match first_day_of_year.weekday() {
        Weekday::Mon => 0,
        _ => 1,
    };

    let days_with_offset = days_diff + week_offset;
    let week_number = (days_with_offset / 7) as u32 + 1;
    Box::new(TextValue {
        value: format!("{}{}", year, week_number),
    })
}

pub fn date_quarter(inputs: &[Box<dyn Value>]) -> Box<dyn Value> {
    let date = inputs[0].as_date().unwrap();
    let parsed_date = DateTime::from_timestamp(date, 0).unwrap();
    let month = parsed_date.month() as i64;
    Box::new(IntValue {
        value: (month - 1) / 3 + 1,
    })
}

pub fn date_year(inputs: &[Box<dyn Value>]) -> Box<dyn Value> {
    let date = inputs[0].as_date().unwrap();
    let parsed_date = DateTime::from_timestamp(date, 0).unwrap();
    Box::new(IntValue {
        value: parsed_date.year().into(),
    })
}

pub fn date_month(inputs: &[Box<dyn Value>]) -> Box<dyn Value> {
    let date = inputs[0].as_date().unwrap();
    let parsed_date = DateTime::from_timestamp(date, 0).unwrap();
    Box::new(IntValue {
        value: parsed_date.month().into(),
    })
}

pub fn date_weekday(inputs: &[Box<dyn Value>]) -> Box<dyn Value> {
    let date = inputs[0].as_date().unwrap();
    let parsed_date = DateTime::from_timestamp(date, 0).unwrap();
    let value = (parsed_date.weekday().number_from_monday() - 1) as i64;
    Box::new(IntValue { value })
}

pub fn date_to_days(inputs: &[Box<dyn Value>]) -> Box<dyn Value> {
    let date = inputs[0].as_date().unwrap();
    let parsed_date = DateTime::from_timestamp(date, 0).unwrap();
    let days_since_year_0 = parsed_date.ordinal0() as i64;
    let year = parsed_date.year() as i64;
    let leap_years = (year - 1) / 4 - (year - 1) / 100 + (year - 1) / 400;
    let non_leap_years = year - leap_years;
    let days = 365 * non_leap_years + 366 * leap_years + days_since_year_0;
    Box::new(IntValue { value: days })
}

pub fn date_last_day(inputs: &[Box<dyn Value>]) -> Box<dyn Value> {
    let date = inputs[0].as_date().unwrap();
    let parsed_date = DateTime::from_timestamp(date, 0).unwrap();
    let (year, month) = (parsed_date.year(), parsed_date.month());

    let curr_month_start = NaiveDate::from_ymd_opt(year, month, 1).unwrap();
    let next_month_start = if month < 12 {
        NaiveDate::from_ymd_opt(year, month + 1, 1)
    } else {
        NaiveDate::from_ymd_opt(year + 1, 1, 1)
    }
    .unwrap();
    let days_in_month = next_month_start - curr_month_start;

    let parsed_date = parsed_date.with_day(1).unwrap();
    let last_day = parsed_date + days_in_month - chrono::Duration::days(1);

    Box::new(DateValue {
        value: last_day.timestamp(),
    })
}