Skip to content

Commit

Permalink
Merge pull request #69 from mobley-trent/hour
Browse files Browse the repository at this point in the history
feat: Added HOUR function
  • Loading branch information
AmrDeveloper authored Jan 5, 2024
2 parents 0b9a6ff + a760037 commit 06b68a6
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
7 changes: 7 additions & 0 deletions crates/gitql-ast/src/date_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use chrono::NaiveDate;
use chrono::NaiveDateTime;
use chrono::NaiveTime;
use chrono::TimeZone;
use chrono::Timelike;
use chrono::Utc;
use chrono::Weekday;

Expand Down Expand Up @@ -58,6 +59,12 @@ pub fn date_time_to_time_stamp(date: &str) -> i64 {
date_time.ok().unwrap().timestamp()
}

pub fn date_time_to_hour(date: i64) -> i64 {
let date_time = NaiveDateTime::from_timestamp_opt(date, 0);
let dt = date_time.unwrap().time();
dt.hour() as i64
}

pub fn date_to_day_name(date: i64) -> String {
let parsed_date = NaiveDateTime::from_timestamp_opt(date, 0).unwrap();

Expand Down
14 changes: 14 additions & 0 deletions crates/gitql-ast/src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ lazy_static! {
map.insert("maketime", date_make_time);
map.insert("dayname", date_dayname);
map.insert("monthname", date_monthname);
map.insert("hour", date_hour);

// Numeric functions
map.insert("abs", numeric_abs);
Expand Down Expand Up @@ -298,6 +299,13 @@ lazy_static! {
result: DataType::Text,
}
);
map.insert(
"hour",
Prototype {
parameters: vec![DataType::DateTime],
result: DataType::Integer,
}
);
// Numeric functions
map.insert(
"abs",
Expand Down Expand Up @@ -700,6 +708,12 @@ fn date_monthname(inputs: &[Value]) -> Value {
Value::Text(month_str)
}

fn date_hour(inputs: &[Value]) -> Value {
let date = inputs[0].as_date_time();
let hour = date_utils::date_time_to_hour(date);
Value::Integer(hour)
}

// Numeric functions

fn numeric_abs(inputs: &[Value]) -> Value {
Expand Down
2 changes: 2 additions & 0 deletions docs/function/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ SELECT UNICODE("AmrDeveloper")
| NOW | | DateTime | Return current date time in `YYYY-MM-DD HH:MM:SS` format. |
| DAYNAME | Date | Text | Returns the name of the day given a timestamp. |
| MONTHNAME | Date | Text | Returns the name of the month given a timestamp. |
| HOUR | DateTime | Integer | Returns the hour part of a datetime. |

### Date functions samples

Expand All @@ -78,6 +79,7 @@ SELECT MAKETIME(12, 59, 59)
SELECT NOW()
SELECT DAYNAME(CURRENT_DATE())
SELECT MONTHNAME(CURRENT_DATE())
SELECT HOUR(NOW())
```

### Numeric Functions
Expand Down

0 comments on commit 06b68a6

Please sign in to comment.