Skip to content

Commit

Permalink
Added datetime helper functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Tushar-Naik committed Jan 9, 2024
1 parent 9c1efe6 commit 97ec625
Show file tree
Hide file tree
Showing 12 changed files with 390 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2019. Santanu Sinha
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing permissions and limitations
* under the License.
*/

package io.appform.hope.core.functions.impl.date;

import io.appform.hope.core.functions.FunctionImplementation;
import io.appform.hope.core.functions.HopeFunction;
import io.appform.hope.core.values.NumericValue;
import io.appform.hope.core.visitors.Evaluator;

import java.time.LocalDateTime;

/**
* returns the current day of the month as a NumericValue.
*/
@FunctionImplementation("datetime.day_of_month")
public class DayOfMonth extends HopeFunction<NumericValue> {
@Override
public NumericValue apply(Evaluator.EvaluationContext evaluationContext) {
return new NumericValue(LocalDateTime.now().getDayOfMonth());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2019. Santanu Sinha
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing permissions and limitations
* under the License.
*/

package io.appform.hope.core.functions.impl.date;

import io.appform.hope.core.functions.FunctionImplementation;
import io.appform.hope.core.functions.HopeFunction;
import io.appform.hope.core.values.NumericValue;
import io.appform.hope.core.visitors.Evaluator;

import java.time.LocalDateTime;

/**
* returns the current day of the week as a NumericValue.
*/
@FunctionImplementation("datetime.day_of_week")
public class DayOfWeek extends HopeFunction<NumericValue> {
@Override
public NumericValue apply(Evaluator.EvaluationContext evaluationContext) {
return new NumericValue(LocalDateTime.now().getDayOfWeek().getValue());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2019. Santanu Sinha
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing permissions and limitations
* under the License.
*/

package io.appform.hope.core.functions.impl.date;

import io.appform.hope.core.functions.FunctionImplementation;
import io.appform.hope.core.functions.HopeFunction;
import io.appform.hope.core.values.NumericValue;
import io.appform.hope.core.visitors.Evaluator;

import java.time.LocalDateTime;

/**
* returns the current day of the year as a NumericValue.
*/
@FunctionImplementation("datetime.day_of_year")
public class DayOfYear extends HopeFunction<NumericValue> {
@Override
public NumericValue apply(Evaluator.EvaluationContext evaluationContext) {
return new NumericValue(LocalDateTime.now().getDayOfYear());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2019. Santanu Sinha
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing permissions and limitations
* under the License.
*/

package io.appform.hope.core.functions.impl.date;

import io.appform.hope.core.functions.FunctionImplementation;
import io.appform.hope.core.functions.HopeFunction;
import io.appform.hope.core.values.NumericValue;
import io.appform.hope.core.visitors.Evaluator;

import java.time.LocalDateTime;

/**
* Returns the current hour of the day as a NumericValue.
*/
@FunctionImplementation("datetime.hour_of_day")
public class HourOfDay extends HopeFunction<NumericValue> {
@Override
public NumericValue apply(Evaluator.EvaluationContext evaluationContext) {
return new NumericValue(LocalDateTime.now().getHour());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2019. Santanu Sinha
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing permissions and limitations
* under the License.
*/

package io.appform.hope.core.functions.impl.date;

import io.appform.hope.core.functions.FunctionImplementation;
import io.appform.hope.core.functions.HopeFunction;
import io.appform.hope.core.values.NumericValue;
import io.appform.hope.core.visitors.Evaluator;

import java.time.LocalDateTime;

/**
* Returns the current minute of the hour as a NumericValue.
*/
@FunctionImplementation("datetime.minute_of_hour")
public class MinuteOfHour extends HopeFunction<NumericValue> {
@Override
public NumericValue apply(Evaluator.EvaluationContext evaluationContext) {
return new NumericValue(LocalDateTime.now().getMinute());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2019. Santanu Sinha
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing permissions and limitations
* under the License.
*/

package io.appform.hope.core.functions.impl.date;

import io.appform.hope.core.functions.FunctionImplementation;
import io.appform.hope.core.functions.HopeFunction;
import io.appform.hope.core.values.NumericValue;
import io.appform.hope.core.visitors.Evaluator;

import java.time.LocalDateTime;
import java.time.temporal.WeekFields;
import java.util.Locale;

/**
* returns the current month of the year as a NumericValue.
*/
@FunctionImplementation("datetime.month_of_year")
public class MonthOfYear extends HopeFunction<NumericValue> {
@Override
public NumericValue apply(Evaluator.EvaluationContext evaluationContext) {
return new NumericValue(LocalDateTime.now().getMonth().getValue());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright 2019. Santanu Sinha
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing permissions and limitations
* under the License.
*/

package io.appform.hope.core.functions.impl.date;

import io.appform.hope.core.functions.FunctionImplementation;
import io.appform.hope.core.functions.HopeFunction;
import io.appform.hope.core.values.NumericValue;
import io.appform.hope.core.visitors.Evaluator;

/**
* Returns current UNIX epoch time {@link NumericValue} in milliseconds.
*/
@FunctionImplementation("datetime.now")
public class Now extends HopeFunction<NumericValue> {
@Override
public NumericValue apply(Evaluator.EvaluationContext evaluationContext) {
return new NumericValue(System.currentTimeMillis());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2019. Santanu Sinha
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing permissions and limitations
* under the License.
*/

package io.appform.hope.core.functions.impl.date;

import io.appform.hope.core.functions.FunctionImplementation;
import io.appform.hope.core.functions.HopeFunction;
import io.appform.hope.core.values.NumericValue;
import io.appform.hope.core.visitors.Evaluator;

import java.time.LocalDateTime;

/**
* Returns the current second of the minute as a NumericValue.
*/
@FunctionImplementation("datetime.second_of_minute")
public class SecondOfMinute extends HopeFunction<NumericValue> {
@Override
public NumericValue apply(Evaluator.EvaluationContext evaluationContext) {
return new NumericValue(LocalDateTime.now().getSecond());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2019. Santanu Sinha
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing permissions and limitations
* under the License.
*/

package io.appform.hope.core.functions.impl.date;

import io.appform.hope.core.functions.FunctionImplementation;
import io.appform.hope.core.functions.HopeFunction;
import io.appform.hope.core.values.NumericValue;
import io.appform.hope.core.visitors.Evaluator;

import java.time.LocalDateTime;
import java.time.temporal.WeekFields;
import java.util.Locale;

/**
* returns the current week of the month as a NumericValue.
*/
@FunctionImplementation("datetime.week_of_month")
public class WeekOfMonth extends HopeFunction<NumericValue> {
@Override
public NumericValue apply(Evaluator.EvaluationContext evaluationContext) {
return new NumericValue(LocalDateTime.now().get(WeekFields.of(Locale.getDefault()).weekOfMonth()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2019. Santanu Sinha
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing permissions and limitations
* under the License.
*/

package io.appform.hope.core.functions.impl.date;

import io.appform.hope.core.functions.FunctionImplementation;
import io.appform.hope.core.functions.HopeFunction;
import io.appform.hope.core.values.NumericValue;
import io.appform.hope.core.visitors.Evaluator;

import java.time.LocalDateTime;
import java.time.temporal.WeekFields;
import java.util.Locale;

/**
* returns the current week of the year as a NumericValue.
*/
@FunctionImplementation("datetime.week_of_year")
public class WeekOfYear extends HopeFunction<NumericValue> {
@Override
public NumericValue apply(Evaluator.EvaluationContext evaluationContext) {
return new NumericValue(LocalDateTime.now().get(WeekFields.of(Locale.getDefault()).weekOfYear()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2019. Santanu Sinha
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing permissions and limitations
* under the License.
*/

package io.appform.hope.core.functions.impl.date;

import io.appform.hope.core.functions.FunctionImplementation;
import io.appform.hope.core.functions.HopeFunction;
import io.appform.hope.core.values.NumericValue;
import io.appform.hope.core.visitors.Evaluator;

import java.time.LocalDateTime;

/**
* returns the current year as a NumericValue.
*/
@FunctionImplementation("datetime.year")
public class Year extends HopeFunction<NumericValue> {
@Override
public NumericValue apply(Evaluator.EvaluationContext evaluationContext) {
return new NumericValue(LocalDateTime.now().getYear());
}
}
Loading

0 comments on commit 97ec625

Please sign in to comment.