diff --git a/Cargo.lock b/Cargo.lock index a379da7..a629c7f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -56,7 +56,7 @@ checksum = "572f695136211188308f16ad2ca5c851a712c464060ae6974944458eb83880ba" [[package]] name = "calcit_std" -version = "0.2.4" +version = "0.2.5" dependencies = [ "chrono", "cirru_edn", @@ -105,9 +105,9 @@ dependencies = [ [[package]] name = "cirru_edn" -version = "0.6.6" +version = "0.6.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "51e76586e1d99e801ffa01acedffbcf7bff79f2463ce9ecf09b72dd25c306df5" +checksum = "0c14fc3c1e5507efa83722494e0bca13d83e2bf00a64ab5199c0185491c7f217" dependencies = [ "bincode", "cirru_parser", diff --git a/Cargo.toml b/Cargo.toml index 3dcd507..db20d26 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "calcit_std" -version = "0.2.4" +version = "0.2.5" authors = ["jiyinyiyong "] edition = "2021" @@ -17,7 +17,8 @@ crate-type = ["dylib"] # Creates dynamic lib serde = { version = "1.0.202", features = ["derive"] } serde_json = "1.0.117" chrono = "0.4.38" -cirru_edn = "0.6.6" +cirru_edn = "0.6.7" +# cirru_edn = { path = "/Users/chenyong/repo/cirru/edn.rs" } cirru_parser = "0.1.29" nanoid = "0.4.0" rand = "0.8.5" diff --git a/calcit.cirru b/calcit.cirru index 8780a92..73dc3bf 100644 --- a/calcit.cirru +++ b/calcit.cirru @@ -1,6 +1,6 @@ {} (:package |calcit.std) - :configs $ {} (:init-fn |calcit.std.test/main!) (:port 6001) (:reload-fn |calcit.std.test/reload!) (:version |0.2.4) + :configs $ {} (:init-fn |calcit.std.test/main!) (:port 6001) (:reload-fn |calcit.std.test/reload!) (:version |0.2.5) :modules $ [] :entries $ {} :files $ {} diff --git a/compact.cirru b/compact.cirru index 5d26d74..ec83336 100644 --- a/compact.cirru +++ b/compact.cirru @@ -1,6 +1,6 @@ {} (:package |calcit.std) - :configs $ {} (:init-fn |calcit.std.test/main!) (:reload-fn |calcit.std.test/reload!) (:version |0.2.4) + :configs $ {} (:init-fn |calcit.std.test/main!) (:reload-fn |calcit.std.test/reload!) (:version |0.2.5) :modules $ [] :entries $ {} :files $ {} diff --git a/src/date.rs b/src/date.rs index f079842..f147325 100644 --- a/src/date.rs +++ b/src/date.rs @@ -1,21 +1,11 @@ -//! TODO, need to store offset as well, -//! currently lack of offset, prefer using Local time - -use std::{ - collections::HashMap, - sync::{Arc, RwLock}, -}; +/// DateTime is used to store time internally +/// +use std::{collections::HashMap, sync::Arc}; use chrono::{DateTime, Datelike, Duration, FixedOffset, Local, LocalResult, NaiveDate, TimeZone, Timelike, Weekday}; -use cirru_edn::{Edn, EdnAnyRef, EdnTupleView}; +use cirru_edn::{Edn, EdnTupleView}; use std::ops::Add; -/// an alias generating Edn from DateTime -/// DateTime is used to store time internally -fn date_to_edn(d: &DateTime) -> Edn { - Edn::AnyRef(EdnAnyRef(Arc::new(RwLock::new(d.fixed_offset())))) -} - /// calcit represents DateTime in f64 /// nil for no format #[no_mangle] @@ -23,11 +13,11 @@ pub fn parse_time(args: Vec) -> Result { if args.len() == 2 { match (&args[0], &args[1]) { (Edn::Str(s), Edn::Nil) => match DateTime::parse_from_rfc3339(s) { - Ok(time) => Ok(date_to_edn(&time)), + Ok(time) => Ok(Edn::any_ref(time.fixed_offset())), Err(e) => Err(format!("parse-time failed, {e}")), }, (Edn::Str(s), Edn::Str(f)) => match DateTime::parse_from_str(s, f) { - Ok(time) => Ok(date_to_edn(&time)), + Ok(time) => Ok(Edn::any_ref(time.fixed_offset())), Err(e) => Err(format!("parse-time failed, {s} {f} {e}")), }, (_, _) => Err(format!("parse-time expected 2 arguments, got: {args:?}")), @@ -39,7 +29,7 @@ pub fn parse_time(args: Vec) -> Result { #[no_mangle] pub fn now_bang(_args: Vec) -> Result { - Ok(date_to_edn(&Local::now())) + Ok(Edn::any_ref(Local::now().fixed_offset())) } /// TODO currently only return self, no offset involved yet @@ -141,11 +131,11 @@ pub fn from_ymd(args: Vec) -> Result { })), LocalResult::Single(d) => Ok(Edn::Tuple(EdnTupleView { tag: Arc::new(Edn::tag("single")), - extra: vec![date_to_edn(&d)], + extra: vec![Edn::any_ref(d.fixed_offset())], })), LocalResult::Ambiguous(d, d2) => Ok(Edn::Tuple(EdnTupleView { tag: Arc::new(Edn::tag("ambiguous")), - extra: vec![date_to_edn(&d), date_to_edn(&d2)], + extra: vec![Edn::any_ref(d.fixed_offset()), Edn::any_ref(d2.fixed_offset())], })), } } @@ -185,11 +175,11 @@ pub fn from_ywd(args: Vec) -> Result { })), LocalResult::Single(d) => Ok(Edn::Tuple(EdnTupleView { tag: Arc::new(Edn::tag("single")), - extra: vec![date_to_edn(&d)], + extra: vec![Edn::any_ref(d.fixed_offset())], })), LocalResult::Ambiguous(d, d2) => Ok(Edn::Tuple(EdnTupleView { tag: Arc::new(Edn::tag("single")), - extra: vec![date_to_edn(&d), date_to_edn(&d2)], + extra: vec![Edn::any_ref(d.fixed_offset()), Edn::any_ref(d2.fixed_offset())], })), }, None => Err(format!("from-ywd got invalid args: {y} {w} {weekday}")), @@ -209,12 +199,12 @@ pub fn add_duration(args: Vec) -> Result { (Edn::AnyRef(d), Edn::Number(n), Edn::Tag(k)) => { if let Some(time) = d.0.read().unwrap().downcast_ref::>() { match k.ref_str() { - "week" | "weeks" => Ok(date_to_edn(&time.add(Duration::weeks(*n as i64)))), - "day" | "days" => Ok(date_to_edn(&time.add(Duration::days(*n as i64)))), - "h" | "hour" | "hours" => Ok(date_to_edn(&time.add(Duration::hours(*n as i64)))), - "min" | "minute" | "minutes" => Ok(date_to_edn(&time.add(Duration::minutes(*n as i64)))), - "second" | "seconds" => Ok(date_to_edn(&time.add(Duration::seconds(*n as i64)))), - "milli" | "millisecond" | "milliseconds" => Ok(date_to_edn(&time.add(Duration::milliseconds(*n as i64)))), + "week" | "weeks" => Ok(Edn::any_ref(time.add(Duration::weeks(*n as i64)).fixed_offset())), + "day" | "days" => Ok(Edn::any_ref(time.add(Duration::days(*n as i64)).fixed_offset())), + "h" | "hour" | "hours" => Ok(Edn::any_ref(time.add(Duration::hours(*n as i64)).fixed_offset())), + "min" | "minute" | "minutes" => Ok(Edn::any_ref(time.add(Duration::minutes(*n as i64)).fixed_offset())), + "second" | "seconds" => Ok(Edn::any_ref(time.add(Duration::seconds(*n as i64)).fixed_offset())), + "milli" | "millisecond" | "milliseconds" => Ok(Edn::any_ref(time.add(Duration::milliseconds(*n as i64)).fixed_offset())), a => Err(format!("unknown duration unit: {a}")), } } else {