Skip to content

Commit

Permalink
feat: Implemnet DAYOFWEEK and DAYOFMONTH Date functions
Browse files Browse the repository at this point in the history
  • Loading branch information
AmrDeveloper committed Feb 23, 2024
1 parent 0638c95 commit efe8144
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
7 changes: 6 additions & 1 deletion crates/gitql-ast/src/date_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,17 @@ pub fn date_time_to_hour(date: i64) -> i64 {
dt.hour() as i64
}

pub fn date_to_day_number_in_week(date: i64) -> u32 {
let parsed_date = NaiveDateTime::from_timestamp_opt(date, 0).unwrap();
parsed_date.weekday().number_from_sunday()
}

pub fn date_to_day_number_in_month(date: i64) -> u32 {
let parsed_date = NaiveDateTime::from_timestamp_opt(date, 0).unwrap();
parsed_date.day()
}

pub fn date_to_day_of_the_year(date: i64) -> u32 {
pub fn date_to_day_number_in_year(date: i64) -> u32 {
let parsed_date = NaiveDateTime::from_timestamp_opt(date, 0).unwrap();
parsed_date.ordinal()
}
Expand Down
29 changes: 28 additions & 1 deletion crates/gitql-ast/src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ lazy_static! {
map.insert("monthname", date_monthname);
map.insert("hour", date_hour);
map.insert("isdate", date_is_date);

map.insert("dayofweek", date_day_of_week);
map.insert("dayofmonth", date_day_of_month);
map.insert("dayofyear", date_day_of_year);

// Numeric functions
Expand Down Expand Up @@ -336,6 +339,20 @@ lazy_static! {
result: DataType::Boolean,
}
);
map.insert(
"dayofweek",
Prototype {
parameters: vec![DataType::Date],
result: DataType::Integer,
}
);
map.insert(
"dayofmonth",
Prototype {
parameters: vec![DataType::Date],
result: DataType::Integer,
}
);
map.insert(
"dayofyear",
Prototype {
Expand Down Expand Up @@ -787,9 +804,19 @@ fn date_is_date(inputs: &[Value]) -> Value {
Value::Boolean(inputs[0].data_type().is_date())
}

fn date_day_of_week(inputs: &[Value]) -> Value {
let date = inputs[0].as_date();
Value::Integer(date_utils::date_to_day_number_in_week(date).into())
}

fn date_day_of_month(inputs: &[Value]) -> Value {
let date = inputs[0].as_date();
Value::Integer(date_utils::date_to_day_number_in_month(date).into())
}

fn date_day_of_year(inputs: &[Value]) -> Value {
let date = inputs[0].as_date();
Value::Integer(date_utils::date_to_day_of_the_year(date).into())
Value::Integer(date_utils::date_to_day_number_in_year(date).into())
}

// Numeric functions
Expand Down
2 changes: 2 additions & 0 deletions docs/function/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ SELECT UNICODE("AmrDeveloper")
| MONTHNAME | Date | Text | Returns the name of the month given a timestamp. |
| HOUR | DateTime | Integer | Returns the hour part of a datetime. |
| ISDATE | Any | Boolean | Return TRUE if the argument type is Date. |
| DAYOFWEEK | Date | Integer | Returns the day of the week for a given date (a number from 1 to 7) |
| DAYOFMONTH | Date | Integer | Returns the day of the month for a given date (a number from 1 to 31) |
| DAYOFYEAR | Date | Integer | Returns the day of the year for a given date (a number from 1 to 366) |

### Date functions samples
Expand Down

0 comments on commit efe8144

Please sign in to comment.