Skip to content

Commit

Permalink
any_ref with alias; tag 0.2.5
Browse files Browse the repository at this point in the history
  • Loading branch information
tiye committed May 19, 2024
1 parent 727d7d9 commit 81edb56
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 34 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "calcit_std"
version = "0.2.4"
version = "0.2.5"
authors = ["jiyinyiyong <[email protected]>"]
edition = "2021"

Expand All @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion calcit.cirru

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion compact.cirru
Original file line number Diff line number Diff line change
@@ -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 $ {}
Expand Down
44 changes: 17 additions & 27 deletions src/date.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,23 @@
//! TODO, need to store offset as well,
//! currently lack of offset, prefer using Local time
use std::{
collections::HashMap,
sync::{Arc, RwLock},
};
/// DateTime<FixedOffset> 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<FixedOffset> is used to store time internally
fn date_to_edn<T: TimeZone>(d: &DateTime<T>) -> Edn {
Edn::AnyRef(EdnAnyRef(Arc::new(RwLock::new(d.fixed_offset()))))
}

/// calcit represents DateTime in f64
/// nil for no format
#[no_mangle]
pub fn parse_time(args: Vec<Edn>) -> Result<Edn, String> {
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:?}")),
Expand All @@ -39,7 +29,7 @@ pub fn parse_time(args: Vec<Edn>) -> Result<Edn, String> {

#[no_mangle]
pub fn now_bang(_args: Vec<Edn>) -> Result<Edn, String> {
Ok(date_to_edn(&Local::now()))
Ok(Edn::any_ref(Local::now().fixed_offset()))
}

/// TODO currently only return self, no offset involved yet
Expand Down Expand Up @@ -141,11 +131,11 @@ pub fn from_ymd(args: Vec<Edn>) -> Result<Edn, String> {
})),
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())],
})),
}
}
Expand Down Expand Up @@ -185,11 +175,11 @@ pub fn from_ywd(args: Vec<Edn>) -> Result<Edn, String> {
})),
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}")),
Expand All @@ -209,12 +199,12 @@ pub fn add_duration(args: Vec<Edn>) -> Result<Edn, String> {
(Edn::AnyRef(d), Edn::Number(n), Edn::Tag(k)) => {
if let Some(time) = d.0.read().unwrap().downcast_ref::<DateTime<FixedOffset>>() {
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 {
Expand Down

0 comments on commit 81edb56

Please sign in to comment.