mirror of
https://github.com/getrebuild/rebuild.git
synced 2025-03-13 15:44:26 +08:00
[better] date func
This commit is contained in:
parent
7ba44f5a28
commit
1e0bec9057
5 changed files with 57 additions and 50 deletions
2
pom.xml
2
pom.xml
|
@ -10,7 +10,7 @@
|
|||
</parent>
|
||||
<groupId>com.rebuild</groupId>
|
||||
<artifactId>rebuild</artifactId>
|
||||
<version>2.9.0-beta2</version>
|
||||
<version>2.9.0-beta3</version>
|
||||
<name>rebuild</name>
|
||||
<description>Building your business-systems freely!</description>
|
||||
<!-- UNCOMMENT USE TOMCAT -->
|
||||
|
|
|
@ -65,11 +65,11 @@ public class Application implements ApplicationListener<ApplicationStartedEvent>
|
|||
/**
|
||||
* Rebuild Version
|
||||
*/
|
||||
public static final String VER = "2.9.0-beta2";
|
||||
public static final String VER = "2.9.0-beta3";
|
||||
/**
|
||||
* Rebuild Build [MAJOR]{1}[MINOR]{2}[PATCH]{2}[BUILD]{2}
|
||||
*/
|
||||
public static final int BUILD = 2090002;
|
||||
public static final int BUILD = 2090003;
|
||||
|
||||
static {
|
||||
// Driver for DB
|
||||
|
|
|
@ -12,13 +12,14 @@ import cn.devezhao.commons.ObjectUtils;
|
|||
import com.googlecode.aviator.runtime.function.AbstractFunction;
|
||||
import com.googlecode.aviator.runtime.type.AviatorNil;
|
||||
import com.googlecode.aviator.runtime.type.AviatorObject;
|
||||
import org.apache.commons.lang.math.NumberUtils;
|
||||
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Usage: DATEADD(date, interval[H|D|M|Y])
|
||||
* Usage: DATEADD($date, $number, [H|D|M|Y])
|
||||
* Return: Date
|
||||
*
|
||||
* @author devezhao
|
||||
|
@ -29,36 +30,47 @@ public class DateAddFunction extends AbstractFunction {
|
|||
|
||||
@Override
|
||||
public AviatorObject call(Map<String, Object> env, AviatorObject arg1, AviatorObject arg2) {
|
||||
return call(env, arg1, arg2, AviatorNil.NIL);
|
||||
}
|
||||
|
||||
@Override
|
||||
public AviatorObject call(Map<String, Object> env, AviatorObject arg1, AviatorObject arg2, AviatorObject arg3) {
|
||||
Object o = arg1.getValue(env);
|
||||
Date date = o instanceof Date ? (Date) o : CalendarUtils.parse(o.toString());
|
||||
if (date == null) {
|
||||
final Date $date = o instanceof Date ? (Date) o : CalendarUtils.parse(o.toString());
|
||||
if ($date == null) {
|
||||
return AviatorNil.NIL;
|
||||
}
|
||||
|
||||
String interval = arg2.getValue(env).toString();
|
||||
|
||||
int unit4Interval = Calendar.DATE; // default
|
||||
|
||||
if (interval.endsWith(AviatorDate.DU_MINUTE)) {
|
||||
interval = interval.substring(0, interval.length() - 1);
|
||||
unit4Interval = Calendar.MINUTE;
|
||||
} else if (interval.endsWith(AviatorDate.DU_HOUR)) {
|
||||
interval = interval.substring(0, interval.length() - 1);
|
||||
unit4Interval = Calendar.HOUR_OF_DAY;
|
||||
} else if (interval.endsWith(AviatorDate.DU_DAY)) {
|
||||
interval = interval.substring(0, interval.length() - 1);
|
||||
} else if (interval.endsWith(AviatorDate.DU_MONTH)) {
|
||||
interval = interval.substring(0, interval.length() - 1);
|
||||
unit4Interval = Calendar.MONTH;
|
||||
} else if (interval.endsWith(AviatorDate.DU_YEAR)) {
|
||||
interval = interval.substring(0, interval.length() - 1);
|
||||
unit4Interval = Calendar.YEAR;
|
||||
String $number = arg2.getValue(env) == null ? null : arg2.getValue(env).toString();
|
||||
if ($number == null) {
|
||||
return AviatorNil.NIL;
|
||||
}
|
||||
|
||||
Date newDate = dateAdd(date, ObjectUtils.toInt(interval), unit4Interval);
|
||||
String $du = arg3.getValue(env) == null ? null : arg3.getValue(env).toString();
|
||||
|
||||
// compatible: v2.8
|
||||
String numberLast = $number.substring($number.length() - 1);
|
||||
if (!NumberUtils.isNumber(numberLast)) {
|
||||
$du = numberLast;
|
||||
$number = $number.substring(0, $number.length() - 1);
|
||||
}
|
||||
|
||||
int du4cal = Calendar.DATE; // default
|
||||
if (AviatorDate.DU_MINUTE.equalsIgnoreCase($du)) du4cal = Calendar.MINUTE;
|
||||
else if (AviatorDate.DU_HOUR.equalsIgnoreCase($du)) du4cal = Calendar.HOUR_OF_DAY;
|
||||
else if (AviatorDate.DU_MONTH.equalsIgnoreCase($du)) du4cal = Calendar.MONTH;
|
||||
else if (AviatorDate.DU_YEAR.equalsIgnoreCase($du)) du4cal = Calendar.YEAR;
|
||||
|
||||
Date newDate = dateAdd($date, ObjectUtils.toInt($number), du4cal);
|
||||
return new AviatorDate(newDate);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param date
|
||||
* @param interval
|
||||
* @param field
|
||||
* @return
|
||||
*/
|
||||
protected Date dateAdd(Date date, int interval, int field) {
|
||||
return CalendarUtils.add(date, interval, field);
|
||||
}
|
||||
|
|
|
@ -11,7 +11,6 @@ import cn.devezhao.commons.CalendarUtils;
|
|||
import cn.devezhao.commons.ObjectUtils;
|
||||
import cn.hutool.core.date.DateUnit;
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import com.googlecode.aviator.exception.ExpressionSyntaxErrorException;
|
||||
import com.googlecode.aviator.runtime.function.AbstractFunction;
|
||||
import com.googlecode.aviator.runtime.type.AviatorLong;
|
||||
import com.googlecode.aviator.runtime.type.AviatorNil;
|
||||
|
@ -24,7 +23,7 @@ import java.util.Date;
|
|||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Usage: DATEDIFF(date1, date2, [H|D|M|Y])
|
||||
* Usage: DATEDIFF($date1, $date2, [H|D|M|Y])
|
||||
* Return: Number
|
||||
*
|
||||
* @author devezhao
|
||||
|
@ -52,36 +51,32 @@ public class DateDiffFunction extends AbstractFunction {
|
|||
|
||||
@Override
|
||||
public AviatorObject call(Map<String, Object> env, AviatorObject arg1, AviatorObject arg2, AviatorObject arg3) {
|
||||
Object o1 = arg1.getValue(env);
|
||||
Date date1 = o1 instanceof Date ? (Date) o1 : CalendarUtils.parse(o1.toString());
|
||||
if (date1 == null) {
|
||||
Object o = arg1.getValue(env);
|
||||
Date $date1 = o instanceof Date ? (Date) o : CalendarUtils.parse(o.toString());
|
||||
if ($date1 == null) {
|
||||
return AviatorNil.NIL;
|
||||
}
|
||||
|
||||
Object o2 = arg2.getValue(env);
|
||||
Date date2 = o2 instanceof Date ? (Date) o2 : CalendarUtils.parse(o2.toString());
|
||||
if (date2 == null) {
|
||||
o = arg2.getValue(env);
|
||||
Date $date2 = o instanceof Date ? (Date) o : CalendarUtils.parse(o.toString());
|
||||
if ($date2 == null) {
|
||||
return AviatorNil.NIL;
|
||||
}
|
||||
|
||||
if (arg3.getValue(env) == null) {
|
||||
throw new ExpressionSyntaxErrorException("`dateUnit` cannot be null");
|
||||
}
|
||||
|
||||
String dateUnit = arg3.getValue(env).toString();
|
||||
final String $du = arg3.getValue(env) == null ? null : arg3.getValue(env).toString();
|
||||
|
||||
if (isUseMysql) {
|
||||
String mysqlUnit = "DAY";
|
||||
if (AviatorDate.DU_YEAR.equalsIgnoreCase(dateUnit)) mysqlUnit = "YEAR";
|
||||
else if (AviatorDate.DU_MONTH.equalsIgnoreCase(dateUnit)) mysqlUnit = "MONTH";
|
||||
else if (AviatorDate.DU_HOUR.equalsIgnoreCase(dateUnit)) mysqlUnit = "HOUR";
|
||||
else if (AviatorDate.DU_MINUTE.equalsIgnoreCase(dateUnit)) mysqlUnit = "MINUTE";
|
||||
if (AviatorDate.DU_YEAR.equalsIgnoreCase($du)) mysqlUnit = "YEAR";
|
||||
else if (AviatorDate.DU_MONTH.equalsIgnoreCase($du)) mysqlUnit = "MONTH";
|
||||
else if (AviatorDate.DU_HOUR.equalsIgnoreCase($du)) mysqlUnit = "HOUR";
|
||||
else if (AviatorDate.DU_MINUTE.equalsIgnoreCase($du)) mysqlUnit = "MINUTE";
|
||||
|
||||
// 利用 MySQL 计算,可预期
|
||||
String mysql = String.format("select TIMESTAMPDIFF(%s, '%s', '%s')",
|
||||
mysqlUnit,
|
||||
CalendarUtils.getUTCDateTimeFormat().format(date1),
|
||||
CalendarUtils.getUTCDateTimeFormat().format(date2));
|
||||
CalendarUtils.getUTCDateTimeFormat().format($date1),
|
||||
CalendarUtils.getUTCDateTimeFormat().format($date2));
|
||||
Object[] res = Application.getPersistManagerFactory().createNativeQuery(mysql).unique();
|
||||
|
||||
return AviatorLong.valueOf(ObjectUtils.toLong(res[0]));
|
||||
|
@ -90,11 +85,11 @@ public class DateDiffFunction extends AbstractFunction {
|
|||
|
||||
long res = 0;
|
||||
|
||||
if (AviatorDate.DU_YEAR.equalsIgnoreCase(dateUnit)) res = DateUtil.betweenYear(date1, date2, true);
|
||||
else if (AviatorDate.DU_MONTH.equalsIgnoreCase(dateUnit)) res = DateUtil.betweenMonth(date1, date2, true);
|
||||
else if (AviatorDate.DU_DAY.equalsIgnoreCase(dateUnit)) res = DateUtil.betweenDay(date1, date2, true);
|
||||
else if (AviatorDate.DU_HOUR.equalsIgnoreCase(dateUnit)) res = DateUtil.between(date1, date2, DateUnit.HOUR, false);
|
||||
else if (AviatorDate.DU_MINUTE.equalsIgnoreCase(dateUnit)) res = DateUtil.between(date1, date2, DateUnit.MINUTE, false);
|
||||
if (AviatorDate.DU_YEAR.equalsIgnoreCase($du)) res = DateUtil.betweenYear($date1, $date2, true);
|
||||
else if (AviatorDate.DU_MONTH.equalsIgnoreCase($du)) res = DateUtil.betweenMonth($date1, $date2, true);
|
||||
else if (AviatorDate.DU_DAY.equalsIgnoreCase($du)) res = DateUtil.betweenDay($date1, $date2, true);
|
||||
else if (AviatorDate.DU_HOUR.equalsIgnoreCase($du)) res = DateUtil.between($date1, $date2, DateUnit.HOUR, false);
|
||||
else if (AviatorDate.DU_MINUTE.equalsIgnoreCase($du)) res = DateUtil.between($date1, $date2, DateUnit.MINUTE, false);
|
||||
|
||||
return AviatorLong.valueOf(res);
|
||||
}
|
||||
|
|
|
@ -10,7 +10,7 @@ package com.rebuild.core.service.trigger.aviator;
|
|||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* Usage: DATESUB(date, interval[H|D|M|Y])
|
||||
* Usage: DATESUB($date, $number, [H|D|M|Y])
|
||||
* Return: Date
|
||||
*
|
||||
* @author devezhao
|
||||
|
|
Loading…
Reference in a new issue