Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added date helper functions #37

Merged
merged 8 commits into from
Jan 11, 2024
Prev Previous commit
Next Next commit
Renamed "datetime" function implementations to "date"
Tushar-Naik committed Jan 10, 2024
commit 171e5097f4fc78252de534145b5e7ac96326514c
Original file line number Diff line number Diff line change
@@ -24,7 +24,7 @@
/**
* returns the current day of the month as a NumericValue.
*/
@FunctionImplementation("datetime.day_of_month")
@FunctionImplementation("date.day_of_month")
public class DayOfMonth extends HopeFunction<NumericValue> {
@Override
public NumericValue apply(Evaluator.EvaluationContext evaluationContext) {
Original file line number Diff line number Diff line change
@@ -24,7 +24,7 @@
/**
* returns the current day of the week as a NumericValue.
*/
@FunctionImplementation("datetime.day_of_week")
@FunctionImplementation("date.day_of_week")
public class DayOfWeek extends HopeFunction<NumericValue> {
@Override
public NumericValue apply(Evaluator.EvaluationContext evaluationContext) {
Original file line number Diff line number Diff line change
@@ -24,7 +24,7 @@
/**
* returns the current day of the year as a NumericValue.
*/
@FunctionImplementation("datetime.day_of_year")
@FunctionImplementation("date.day_of_year")
public class DayOfYear extends HopeFunction<NumericValue> {
@Override
public NumericValue apply(Evaluator.EvaluationContext evaluationContext) {
Original file line number Diff line number Diff line change
@@ -24,7 +24,7 @@
/**
* Returns the current hour of the day as a NumericValue.
*/
@FunctionImplementation("datetime.hour_of_day")
@FunctionImplementation("date.hour_of_day")
public class HourOfDay extends HopeFunction<NumericValue> {
@Override
public NumericValue apply(Evaluator.EvaluationContext evaluationContext) {
Original file line number Diff line number Diff line change
@@ -24,7 +24,7 @@
/**
* Returns the current minute of the hour as a NumericValue.
*/
@FunctionImplementation("datetime.minute_of_hour")
@FunctionImplementation("date.minute_of_hour")
public class MinuteOfHour extends HopeFunction<NumericValue> {
@Override
public NumericValue apply(Evaluator.EvaluationContext evaluationContext) {
Original file line number Diff line number Diff line change
@@ -26,7 +26,7 @@
/**
* returns the current month of the year as a NumericValue.
*/
@FunctionImplementation("datetime.month_of_year")
@FunctionImplementation("date.month_of_year")
public class MonthOfYear extends HopeFunction<NumericValue> {
@Override
public NumericValue apply(Evaluator.EvaluationContext evaluationContext) {
Original file line number Diff line number Diff line change
@@ -22,7 +22,7 @@
/**
* Returns current UNIX epoch time {@link NumericValue} in milliseconds.
*/
@FunctionImplementation("datetime.now")
@FunctionImplementation("date.now")
public class Now extends HopeFunction<NumericValue> {
@Override
public NumericValue apply(Evaluator.EvaluationContext evaluationContext) {
Original file line number Diff line number Diff line change
@@ -24,7 +24,7 @@
/**
* Returns the current second of the minute as a NumericValue.
*/
@FunctionImplementation("datetime.second_of_minute")
@FunctionImplementation("date.second_of_minute")
public class SecondOfMinute extends HopeFunction<NumericValue> {
@Override
public NumericValue apply(Evaluator.EvaluationContext evaluationContext) {
Original file line number Diff line number Diff line change
@@ -26,7 +26,7 @@
/**
* returns the current week of the month as a NumericValue.
*/
@FunctionImplementation("datetime.week_of_month")
@FunctionImplementation("date.week_of_month")
public class WeekOfMonth extends HopeFunction<NumericValue> {
@Override
public NumericValue apply(Evaluator.EvaluationContext evaluationContext) {
Original file line number Diff line number Diff line change
@@ -26,7 +26,7 @@
/**
* returns the current week of the year as a NumericValue.
*/
@FunctionImplementation("datetime.week_of_year")
@FunctionImplementation("date.week_of_year")
public class WeekOfYear extends HopeFunction<NumericValue> {
@Override
public NumericValue apply(Evaluator.EvaluationContext evaluationContext) {
Original file line number Diff line number Diff line change
@@ -24,7 +24,7 @@
/**
* returns the current year as a NumericValue.
*/
@FunctionImplementation("datetime.year")
@FunctionImplementation("date.year")
public class Year extends HopeFunction<NumericValue> {
@Override
public NumericValue apply(Evaluator.EvaluationContext evaluationContext) {
Original file line number Diff line number Diff line change
@@ -242,17 +242,17 @@ private static Stream<Arguments> rules() {
Arguments.of("{ \"array\" : [1,2,3, 4,8,16] }","arr.len(\"/array\") == 6", true),
Arguments.of("{ \"array\" : [1,2,3, 4,8,16] }","arr.len('/array') == 6", true),

Arguments.of("{}", "math.sub(datetime.now(), %d) <= 1000 && math.sub(datetime.now(), %d) >= 0".formatted(epochMilli, epochMilli), true),
Arguments.of("{}", "math.sub(datetime.second_of_minute(), %d) <= 1 && math.sub(datetime.second_of_minute(), %d) >= 0".formatted(dateTime.getSecond(), dateTime.getSecond()), true),
Arguments.of("{}", "math.sub(datetime.minute_of_hour(), %d) <= 1 && math.sub(datetime.minute_of_hour(), %d) >= 0".formatted(dateTime.getMinute(), dateTime.getMinute()), true),
Arguments.of("{}", "math.sub(datetime.hour_of_day(), %d) <= 1 && math.sub(datetime.hour_of_day(), %d) >= 0".formatted(dateTime.getHour(), dateTime.getHour()), true),
Arguments.of("{}", "math.sub(datetime.day_of_week(), %d) <= 1 && math.sub(datetime.day_of_week(), %d) >= 0".formatted(dateTime.getDayOfWeek().getValue(), dateTime.getDayOfWeek().getValue()), true),
Arguments.of("{}", "math.sub(datetime.day_of_month(), %d) <= 1 && math.sub(datetime.day_of_month(), %d) >= 0".formatted(dateTime.getDayOfMonth(), dateTime.getDayOfMonth()), true),
Arguments.of("{}", "math.sub(datetime.day_of_year(), %d) <= 1 && math.sub(datetime.day_of_year(), %d) >= 0".formatted(dateTime.getDayOfYear(), dateTime.getDayOfYear()), true),
Arguments.of("{}", "math.sub(datetime.week_of_month(), %d) <= 1 && math.sub(datetime.week_of_month(), %d) >= 0".formatted(weekOfMonth, weekOfMonth), true),
Arguments.of("{}", "math.sub(datetime.week_of_year(), %d) <= 1 && math.sub(datetime.week_of_year(), %d) >= 0".formatted(weekOfYear, weekOfYear), true),
Arguments.of("{}", "math.sub(datetime.month_of_year(), %d) <= 1 && math.sub(datetime.month_of_year(), %d) >= 0".formatted(dateTime.getMonth().getValue(), dateTime.getMonth().getValue()), true),
Arguments.of("{}", "math.sub(datetime.year(), %d) <= 1 && math.sub(datetime.year(), %d) >= 0".formatted(dateTime.getYear(), dateTime.getYear()), true)
Arguments.of("{}", "math.sub(date.now(), %d) <= 2000 && math.sub(date.now(), %d) >= 0".formatted(epochMilli, epochMilli), true),
Arguments.of("{}", "math.sub(date.second_of_minute(), %d) <= 1 && math.sub(date.second_of_minute(), %d) >= 0".formatted(dateTime.getSecond(), dateTime.getSecond()), true),
Arguments.of("{}", "math.sub(date.minute_of_hour(), %d) <= 1 && math.sub(date.minute_of_hour(), %d) >= 0".formatted(dateTime.getMinute(), dateTime.getMinute()), true),
Arguments.of("{}", "math.sub(date.hour_of_day(), %d) <= 1 && math.sub(date.hour_of_day(), %d) >= 0".formatted(dateTime.getHour(), dateTime.getHour()), true),
Arguments.of("{}", "math.sub(date.day_of_week(), %d) <= 1 && math.sub(date.day_of_week(), %d) >= 0".formatted(dateTime.getDayOfWeek().getValue(), dateTime.getDayOfWeek().getValue()), true),
Arguments.of("{}", "math.sub(date.day_of_month(), %d) <= 1 && math.sub(date.day_of_month(), %d) >= 0".formatted(dateTime.getDayOfMonth(), dateTime.getDayOfMonth()), true),
Arguments.of("{}", "math.sub(date.day_of_year(), %d) <= 1 && math.sub(date.day_of_year(), %d) >= 0".formatted(dateTime.getDayOfYear(), dateTime.getDayOfYear()), true),
Arguments.of("{}", "math.sub(date.week_of_month(), %d) <= 1 && math.sub(date.week_of_month(), %d) >= 0".formatted(weekOfMonth, weekOfMonth), true),
Arguments.of("{}", "math.sub(date.week_of_year(), %d) <= 1 && math.sub(date.week_of_year(), %d) >= 0".formatted(weekOfYear, weekOfYear), true),
Arguments.of("{}", "math.sub(date.month_of_year(), %d) <= 1 && math.sub(date.month_of_year(), %d) >= 0".formatted(dateTime.getMonth().getValue(), dateTime.getMonth().getValue()), true),
Arguments.of("{}", "math.sub(date.year(), %d) <= 1 && math.sub(date.year(), %d) >= 0".formatted(dateTime.getYear(), dateTime.getYear()), true)

);
}