synthphonia/expr/ops/
date.rs

1use chrono::NaiveDate;
2use chrono::Datelike;
3
4use std::cmp::min;
5use std::ops::Not;
6
7use bumpalo::collections::CollectIn;
8use derive_more::DebugCustom;
9use crate::galloc::{AllocForStr, AllocForExactSizeIter, TryAllocForExactSizeIter, AllocForIter, AllocForCharIter};
10use crate::new_op2_opt;
11use crate::{new_op1, new_op2, impl_op3, impl_op3_opt, impl_op2_opt, new_op1_opt};
12use itertools::izip;
13
14
15use super::list::to_index;
16use super::{Op1, Op3, Op2};
17
18
19
20
21
22new_op1_opt!(AsMonth, "date.month",
23    Int -> Int { |s1| {
24        let p = i32::try_from(*s1).ok();
25        p?;
26        NaiveDate::from_num_days_from_ce_opt(p.unwrap()).map(|date| date.month() as i64)
27    }}
28);
29
30new_op1_opt!(AsDay, "date.day",
31    Int -> Int { |s1| {
32        let p = i32::try_from(*s1).ok();
33        p?;
34        NaiveDate::from_num_days_from_ce_opt(p.unwrap()).map(|date| date.day() as i64)
35    }}
36);
37
38new_op1_opt!(AsYear, "date.year",
39    Int -> Int { |s1| {
40        let p = i32::try_from(*s1).ok();
41        p?;
42        NaiveDate::from_num_days_from_ce_opt(p.unwrap()).map(|date| date.year() as i64)
43    }}
44);
45
46new_op1_opt!(AsWeekDay, "date.weekday",
47    Int -> Int { |s1| {
48        let p = i32::try_from(*s1).ok();
49        p?;
50        NaiveDate::from_num_days_from_ce_opt(p.unwrap()).map(|date| date.weekday().number_from_sunday() as i64)
51    }}
52);
53
54new_op2_opt!(TimeFloor, "time.floor",
55    (Int, Int) -> Int { |(s1, s2)| {
56        if *s2 != 0 {
57            Some(s1.div_floor(*s2) * *s2)
58        } else { None }
59    }}
60);
61
62new_op2!(TimeAdd, "time.+",
63    (Int, Int) -> Int { |(s1, s2)| {
64        (s1 + s2) % (60 * 60 * 60)
65    }}
66);
67
68new_op2!(TimeMul, "time.*",
69    (Int, Int) -> Int { |(s1, s2)| {
70        (s1 * s2) % (60 * 60 * 60)
71    }}
72);