Skip to content

Commit

Permalink
Add support for Date, Time and DateTime types
Browse files Browse the repository at this point in the history
  • Loading branch information
AmrDeveloper committed Sep 9, 2023
1 parent 0d93e27 commit 9b1bb36
Show file tree
Hide file tree
Showing 9 changed files with 298 additions and 2 deletions.
152 changes: 152 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions crates/gitql-ast/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ license = "MIT"

[dependencies]
lazy_static = "1.4.0"
chrono = "0.4.28"
43 changes: 43 additions & 0 deletions crates/gitql-ast/src/date_utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
extern crate chrono;

use chrono::NaiveDateTime;
use chrono::TimeZone;
use chrono::Utc;

static CHRONO_TIME_FORMAT: &str = "%H:%M:%S";
static CHRONO_DATE_FORMAT: &str = "%Y-%m-%d";
static CHRONO_DATE_TIME_FORMAT: &str = "%Y-%m-%d %H:%M:%S";

pub fn get_unix_timestamp_ms() -> i64 {
let now = Utc::now();
return now.timestamp();
}

pub fn time_stamp_to_date(time_stamp: i64) -> String {
let utc = NaiveDateTime::from_timestamp_opt(time_stamp, 0).unwrap();
let datetime = Utc.from_utc_datetime(&utc);
let date_str = datetime.format(CHRONO_DATE_FORMAT).to_string();
return date_str;
}

pub fn time_stamp_to_time(time_stamp: i64) -> String {
let utc = NaiveDateTime::from_timestamp_opt(time_stamp, 0).unwrap();
let datetime = Utc.from_utc_datetime(&utc);
let time_str = datetime.format(CHRONO_TIME_FORMAT).to_string();
return time_str;
}

pub fn time_stamp_to_date_time(time_stamp: i64) -> String {
let utc = NaiveDateTime::from_timestamp_opt(time_stamp, 0).unwrap();
let datetime = Utc.from_utc_datetime(&utc);
let date_time_str = datetime.format(CHRONO_DATE_TIME_FORMAT).to_string();
return date_time_str;
}

pub fn date_time_to_time_stamp(date: &str) -> i64 {
let date_time = NaiveDateTime::parse_from_str(date, CHRONO_DATE_TIME_FORMAT);
if date_time.is_err() {
return 0;
}
return date_time.ok().unwrap().timestamp();
}
54 changes: 54 additions & 0 deletions crates/gitql-ast/src/function.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::date_utils;
use crate::types::DataType;
use crate::value::Value;

Expand Down Expand Up @@ -28,13 +29,19 @@ lazy_static! {
map.insert("left", text_left);
map.insert("datalength", text_datalength);
map.insert("char", text_char);

// Date functions
map.insert("current_date", date_current_date);
map.insert("current_time", date_current_time);
map.insert("current_timestamp", date_current_timestamp);
map
};
}

lazy_static! {
pub static ref PROTOTYPES: HashMap<&'static str, Prototype> = {
let mut map: HashMap<&'static str, Prototype> = HashMap::new();
// String functions
map.insert(
"lower",
Prototype {
Expand Down Expand Up @@ -126,10 +133,37 @@ lazy_static! {
result: DataType::Text,
},
);

// Date functions
map.insert(
"current_date",
Prototype {
parameters: vec![],
result: DataType::Date,
},
);

map.insert(
"current_time",
Prototype {
parameters: vec![],
result: DataType::Time,
},
);

map.insert(
"current_timestamp",
Prototype {
parameters: vec![],
result: DataType::DateTime,
},
);
map
};
}

// String functions

fn text_lowercase(inputs: Vec<Value>) -> Value {
return Value::Text(inputs[0].as_text().to_lowercase());
}
Expand Down Expand Up @@ -207,3 +241,23 @@ fn text_char(inputs: Vec<Value>) -> Value {
}
return Value::Text("".to_string());
}

// Date functions

fn date_current_date(_inputs: Vec<Value>) -> Value {
let time_stamp = date_utils::get_unix_timestamp_ms();
let time = date_utils::time_stamp_to_date(time_stamp);
return Value::Text(time);
}

fn date_current_time(_inputs: Vec<Value>) -> Value {
let time_stamp = date_utils::get_unix_timestamp_ms();
let date = date_utils::time_stamp_to_time(time_stamp);
return Value::Text(date);
}

fn date_current_timestamp(_inputs: Vec<Value>) -> Value {
let time_stamp = date_utils::get_unix_timestamp_ms();
let date_time = date_utils::time_stamp_to_date_time(time_stamp);
return Value::Text(date_time);
}
1 change: 1 addition & 0 deletions crates/gitql-ast/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ pub mod scope;
pub mod statement;
pub mod types;
pub mod value;
pub mod date_utils;
6 changes: 5 additions & 1 deletion crates/gitql-ast/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ pub enum DataType {
Number,
Boolean,
Date,
Time,
DateTime,
Undefined,
Null,
}
Expand All @@ -20,6 +22,8 @@ impl DataType {
DataType::Number => "Number",
DataType::Boolean => "Boolean",
DataType::Date => "Date",
DataType::Time => "Time",
DataType::DateTime => "DateTime",
DataType::Undefined => "Undefined",
DataType::Null => "Null",
};
Expand All @@ -39,7 +43,7 @@ lazy_static! {
map.insert("files_changed", DataType::Number);
map.insert("email", DataType::Text);
map.insert("type", DataType::Text);
map.insert("time", DataType::Date);
map.insert("time", DataType::DateTime);
map.insert("is_head", DataType::Boolean);
map.insert("is_remote", DataType::Boolean);
map.insert("commit_count", DataType::Number);
Expand Down
Loading

0 comments on commit 9b1bb36

Please sign in to comment.