temper_std/temporal/
mod.rs

1use temper_core::AnyValueTrait;
2use temper_core::AsAnyValue;
3use temper_core::Pair;
4use crate::json::JsonProducerTrait;
5mod support;
6pub use support::*;
7pub (crate) fn init() -> temper_core::Result<()> {
8    static INIT_ONCE: std::sync::OnceLock<temper_core::Result<()>> = std::sync::OnceLock::new();
9    INIT_ONCE.get_or_init(| |{
10            DAYS_IN_MONTH.set(std::sync::Arc::new(vec![0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31])).unwrap_or_else(| _ | panic!());
11            DAY_OF_WEEK_LOOKUP_TABLE_LEAPY.set(std::sync::Arc::new(vec![0, 0, 3, 4, 0, 2, 5, 0, 3, 6, 1, 4, 6])).unwrap_or_else(| _ | panic!());
12            DAY_OF_WEEK_LOOKUP_TABLE_NOT_LEAPY.set(std::sync::Arc::new(vec![0, 0, 3, 3, 6, 1, 4, 6, 2, 5, 0, 3, 5])).unwrap_or_else(| _ | panic!());
13            Ok(())
14    }).clone()
15}
16static DAYS_IN_MONTH: std::sync::OnceLock<temper_core::List<i32>> = std::sync::OnceLock::new();
17fn days_in_month() -> temper_core::List<i32> {
18    ( * DAYS_IN_MONTH.get().unwrap()).clone()
19}
20static DAY_OF_WEEK_LOOKUP_TABLE_LEAPY: std::sync::OnceLock<temper_core::List<i32>> = std::sync::OnceLock::new();
21fn day_of_week_lookup_table_leapy() -> temper_core::List<i32> {
22    ( * DAY_OF_WEEK_LOOKUP_TABLE_LEAPY.get().unwrap()).clone()
23}
24static DAY_OF_WEEK_LOOKUP_TABLE_NOT_LEAPY: std::sync::OnceLock<temper_core::List<i32>> = std::sync::OnceLock::new();
25fn day_of_week_lookup_table_not_leapy() -> temper_core::List<i32> {
26    ( * DAY_OF_WEEK_LOOKUP_TABLE_NOT_LEAPY.get().unwrap()).clone()
27}
28struct DateJsonAdapterStruct {}
29#[ derive(Clone)] pub (crate) struct DateJsonAdapter(std::sync::Arc<DateJsonAdapterStruct>);
30impl DateJsonAdapter {
31pub fn encode_to_json(& self, x__116: Date, p__117: crate::json::JsonProducer) {
32    x__116.encode_to_json(p__117.clone());
33}
34pub fn decode_from_json(& self, t__118: crate::json::JsonSyntaxTree, ic__119: crate::json::InterchangeContext) -> temper_core::Result<Date> {
35    let return__129: Date;
36    return__129 = Date::decode_from_json(t__118.clone(), ic__119.clone()) ? ;
37    return Ok(return__129.clone());
38}
39pub fn new() -> DateJsonAdapter {
40    let selfish = DateJsonAdapter(std::sync::Arc::new(DateJsonAdapterStruct {}));
41    return selfish;
42}
43}
44impl crate::json::JsonAdapterTrait<Date> for DateJsonAdapter {
45fn clone_boxed(& self) -> crate::json::JsonAdapter<Date> {
46    crate::json::JsonAdapter::new(self.clone())
47}
48fn encode_to_json(& self, x__116: Date, p__117: crate::json::JsonProducer) {
49    self.encode_to_json(x__116, p__117)
50}
51fn decode_from_json(& self, t__118: crate::json::JsonSyntaxTree, ic__119: crate::json::InterchangeContext) -> temper_core::Result<Date> {
52    self.decode_from_json(t__118, ic__119)
53}
54}
55temper_core::impl_any_value_trait!(DateJsonAdapter, [crate::json::JsonAdapter<Date>]);
56struct DateStruct {
57year: i32, month: i32, day: i32
58}
59#[ derive(Clone)] pub struct Date(std::sync::Arc<DateStruct>);
60#[ derive(Clone)] pub struct DateBuilder {
61pub year: i32, pub month: i32, pub day: i32
62}
63impl DateBuilder {
64pub fn build(self) -> temper_core::Result<Date> {
65    Date::new(self.year, self.month, self.day)
66}
67}
68impl Date {
69pub fn new(year__55: i32, month__56: i32, day__57: i32) -> temper_core::Result<Date> {
70    let year;
71    let month;
72    let day;
73    let mut t___343: i32;
74    let mut t___243: bool;
75    let mut t___245: bool;
76    let mut t___246: bool;
77    let mut t___247: bool;
78    let mut t___248: bool;
79    if Some(1) <= Some(month__56) {
80        if Some(month__56) <= Some(12) {
81            if Some(1) <= Some(day__57) {
82                if Some(month__56) != Some(2) {
83                    t___243 = true;
84                } else {
85                    t___243 = Some(day__57) != Some(29);
86                }
87                if t___243 {
88                    t___343 = temper_core::ListedTrait::get( & days_in_month(), month__56);
89                    t___245 = Some(day__57) <= Some(t___343);
90                } else {
91                    t___245 = isLeapYear__32(year__55);
92                }
93                t___246 = t___245;
94            } else {
95                t___246 = false;
96            }
97            t___247 = t___246;
98        } else {
99            t___247 = false;
100        }
101        t___248 = t___247;
102    } else {
103        t___248 = false;
104    }
105    if t___248 {
106        year = year__55;
107        month = month__56;
108        day = day__57;
109    } else {
110        return Err(temper_core::Error::new());
111    }
112    let selfish = Date(std::sync::Arc::new(DateStruct {
113                year, month, day
114    }));
115    return Ok(selfish);
116}
117pub fn to_string(& self) -> std::sync::Arc<String> {
118    let sb__61: std::sync::Arc<std::sync::RwLock<String>> = std::sync::Arc::new(std::sync::RwLock::new(String::new()));
119    padTo__33(4, self.0.year, sb__61.clone());
120    temper_core::string::builder::append( & sb__61, "-");
121    padTo__33(2, self.0.month, sb__61.clone());
122    temper_core::string::builder::append( & sb__61, "-");
123    padTo__33(2, self.0.day, sb__61.clone());
124    return temper_core::string::builder::to_string( & sb__61);
125}
126pub fn from_iso_string(isoString__63: impl temper_core::ToArcString) -> temper_core::Result<Date> {
127    let isoString__63 = isoString__63.to_arc_string();
128    let return__26: Date;
129    let mut t___327: i32;
130    let mut t___332: bool;
131    let mut t___222: bool;
132    let mut t___228: bool;
133    let end__65: usize = isoString__63.len();
134    let mut t___323: usize = temper_core::string::prev( & isoString__63, end__65);
135    let mut t___324: usize = temper_core::string::prev( & isoString__63, t___323);
136    let mut strIndex__66: usize = t___324;
137    let beforeDay__67: usize = strIndex__66;
138    let mut t___325: usize = temper_core::string::prev( & isoString__63, strIndex__66);
139    strIndex__66 = t___325;
140    let afterMonth__68: usize = strIndex__66;
141    if ! temper_core::string::has_index( & isoString__63, afterMonth__68) {
142        t___222 = true;
143    } else {
144        t___327 = temper_core::string::get( & isoString__63, strIndex__66);
145        t___222 = Some(t___327) != Some(45);
146    }
147    if t___222 {
148        return Err(temper_core::Error::new());
149    }
150    let mut t___328: usize = temper_core::string::prev( & isoString__63, strIndex__66);
151    let mut t___329: usize = temper_core::string::prev( & isoString__63, t___328);
152    strIndex__66 = t___329;
153    let beforeMonth__69: usize = strIndex__66;
154    let mut t___330: usize = temper_core::string::prev( & isoString__63, strIndex__66);
155    strIndex__66 = t___330;
156    if Some(temper_core::string::get( & isoString__63, strIndex__66)) != Some(45) {
157        t___228 = true;
158    } else {
159        t___332 = temper_core::string::has_at_least( & isoString__63, 0usize, strIndex__66, 4);
160        t___228 = ! t___332;
161    }
162    if t___228 {
163        return Err(temper_core::Error::new());
164    }
165    let day__70: i32;
166    day__70 = temper_core::string::to_int( & temper_core::string::slice( & isoString__63, beforeDay__67, end__65), Some(10)) ? ;
167    let month__71: i32;
168    month__71 = temper_core::string::to_int( & temper_core::string::slice( & isoString__63, beforeMonth__69, afterMonth__68), Some(10)) ? ;
169    let year__72: i32;
170    year__72 = temper_core::string::to_int( & temper_core::string::slice( & isoString__63, 0usize, strIndex__66), Some(10)) ? ;
171    return__26 = Date::new(year__72, month__71, day__70) ? ;
172    return Ok(return__26.clone());
173}
174pub fn years_between(start__74: Date, end__75: Date) -> i32 {
175    let mut t___320: i32;
176    let mut t___321: i32;
177    let mut t___213: bool;
178    let mut t___214: bool;
179    let mut t___215: i32;
180    let yearDelta__77: i32 = end__75.year().wrapping_sub(start__74.year());
181    let monthDelta__78: i32 = end__75.month().wrapping_sub(start__74.month());
182    if Some(monthDelta__78) < Some(0) {
183        t___214 = true;
184    } else {
185        if Some(monthDelta__78) == Some(0) {
186            t___320 = end__75.day();
187            t___321 = start__74.day();
188            t___213 = Some(t___320) < Some(t___321);
189        } else {
190            t___213 = false;
191        }
192        t___214 = t___213;
193    }
194    if t___214 {
195        t___215 = 1;
196    } else {
197        t___215 = 0;
198    }
199    return yearDelta__77.wrapping_sub(t___215);
200}
201pub fn day_of_week(& self) -> i32 {
202    let return__29: i32;
203    let mut t___196: i32;
204    let mut t___197: i32;
205    let y__83: i32 = self.0.year;
206    let c__84: i32;
207    if Some(y__83) >= Some(0) {
208        t___196 = y__83.wrapping_div(100);
209        c__84 = t___196;
210    } else {
211        t___197 = y__83.wrapping_neg().wrapping_div(100);
212        c__84 = t___197.wrapping_neg();
213    }
214    let yy__85: i32 = y__83.wrapping_sub(c__84.wrapping_mul(100));
215    let janFirst__86: i32 = (8 as i32).wrapping_add((5 as i32).wrapping_mul(yy__85.wrapping_add(3).wrapping_rem(4))).wrapping_add((3 as i32).wrapping_mul(yy__85.wrapping_sub(1))).wrapping_add((5 as i32).wrapping_mul(c__84.wrapping_rem(4))).wrapping_rem(7);
216    let table__87: temper_core::List<i32>;
217    if isLeapYear__32(y__83) {
218        table__87 = day_of_week_lookup_table_leapy().clone();
219    } else {
220        table__87 = day_of_week_lookup_table_not_leapy().clone();
221    }
222    let monthOffset__88: i32 = temper_core::ListedTrait::get( & table__87, self.0.month);
223    let gaussWeekday__89: i32 = janFirst__86.wrapping_add(self.0.day.wrapping_add(6)).wrapping_add(monthOffset__88).wrapping_rem(7);
224    if Some(gaussWeekday__89) == Some(0) {
225        return__29 = 7;
226    } else {
227        return__29 = gaussWeekday__89;
228    }
229    return return__29;
230}
231pub fn encode_to_json(& self, p__91: crate::json::JsonProducer) {
232    let mut t___313: std::sync::Arc<String> = self.to_string();
233    p__91.string_value(t___313.clone());
234}
235pub fn decode_from_json(t__94: crate::json::JsonSyntaxTree, ic__95: crate::json::InterchangeContext) -> temper_core::Result<Date> {
236    let return__31: Date;
237    let mut t___190: crate::json::JsonString;
238    t___190 = temper_core::cast::<crate::json::JsonString>(t__94.clone()).ok_or_else(| | temper_core::Error::new()) ? ;
239    return__31 = Date::from_iso_string(t___190.content()) ? ;
240    return Ok(return__31.clone());
241}
242pub fn year(& self) -> i32 {
243    return self.0.year;
244}
245pub fn month(& self) -> i32 {
246    return self.0.month;
247}
248pub fn day(& self) -> i32 {
249    return self.0.day;
250}
251pub fn json_adapter() -> crate::json::JsonAdapter<Date> {
252    return crate::json::JsonAdapter::new(DateJsonAdapter::new());
253}
254}
255temper_core::impl_any_value_trait!(Date, []);
256fn isLeapYear__32(year__41: i32) -> bool {
257let return__21: bool;
258let mut t___263: i32;
259if Some(year__41.wrapping_rem(4)) == Some(0) {
260    if Some(year__41.wrapping_rem(100)) != Some(0) {
261        return__21 = true;
262    } else {
263        t___263 = year__41.wrapping_rem(400);
264        return__21 = Some(t___263) == Some(0);
265    }
266} else {
267    return__21 = false;
268}
269return return__21;
270}
271fn padTo__33(minWidth__43: i32, num__44: i32, sb__45: std::sync::Arc<std::sync::RwLock<String>>) {
272let mut t___346: i32;
273let mut t___348: usize;
274let mut t___257: bool;
275let decimal__47: std::sync::Arc<String> = temper_core::int_to_string(num__44, Some(10));
276let mut decimalIndex__48: usize = 0usize;
277let decimalEnd__49: usize = decimal__47.len();
278if Some(decimalIndex__48) < Some(decimalEnd__49) {
279    t___346 = temper_core::string::get( & decimal__47, decimalIndex__48);
280    t___257 = Some(t___346) == Some(45);
281} else {
282    t___257 = false;
283}
284if t___257 {
285    temper_core::string::builder::append( & sb__45, "-");
286    t___348 = temper_core::string::next( & decimal__47, decimalIndex__48);
287    decimalIndex__48 = t___348;
288}
289let mut t___349: i32 = temper_core::string::count_between( & decimal__47, decimalIndex__48, decimalEnd__49);
290let mut nNeeded__50: i32 = minWidth__43.wrapping_sub(t___349);
291'loop___2678: while Some(nNeeded__50) > Some(0) {
292    temper_core::string::builder::append( & sb__45, "0");
293    nNeeded__50 = nNeeded__50.wrapping_sub(1);
294}
295temper_core::string::builder::append_between( & sb__45, decimal__47.clone(), decimalIndex__48, decimalEnd__49);
296}