Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions api/src/main/java/org/eclipse/daanse/olap/api/Evaluator.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

package org.eclipse.daanse.olap.api;

import java.util.Date;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Locale;
import java.util.Map;
Expand Down Expand Up @@ -66,7 +66,7 @@ public interface Evaluator{
/**
* Returns the start time of the current query.
*/
Date getQueryStartTime();
LocalDateTime getQueryStartTime();

/**
* Creates a savepoint encapsulating the current state of the evalutor.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@

package org.eclipse.daanse.olap.api.calc;

import java.util.Date;
import java.time.LocalDateTime;


public interface DateTimeCalc extends Calc<Date> {
public interface DateTimeCalc extends Calc<LocalDateTime> {

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@

package org.eclipse.daanse.olap.calc.base.nested;

import java.util.Date;
import java.time.LocalDateTime;

import org.eclipse.daanse.olap.api.calc.Calc;
import org.eclipse.daanse.olap.api.calc.DateTimeCalc;
import org.eclipse.daanse.olap.api.type.Type;
import org.eclipse.daanse.olap.calc.base.AbstractProfilingNestedCalc;

public abstract class AbstractProfilingNestedDateTimeCalc extends AbstractProfilingNestedCalc<Date>
public abstract class AbstractProfilingNestedDateTimeCalc extends AbstractProfilingNestedCalc<LocalDateTime>
implements DateTimeCalc {

protected AbstractProfilingNestedDateTimeCalc(Type type, Calc<?>... calcs) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

package org.eclipse.daanse.olap.calc.base.type.datetime;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

import org.eclipse.daanse.olap.api.Evaluator;
Expand All @@ -28,14 +30,17 @@ public UnknownToDateTimeCalc(Type type, Calc<?> calc) {
}

@Override
public Date evaluateInternal(Evaluator evaluator) {
public LocalDateTime evaluateInternal(Evaluator evaluator) {
Object o = getFirstChildCalc().evaluate(evaluator);
if (o == null) {
return null;
} else if (o instanceof LocalDateTime ldt) {
return ldt;
} else if (o instanceof Date d) {
return d;
// Support legacy Date conversion
return LocalDateTime.ofInstant(d.toInstant(), ZoneId.systemDefault());
}

throw evaluator.newEvalException(null, "expected Date was " + o);
throw evaluator.newEvalException(null, "expected LocalDateTime was " + o);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import static org.eclipse.daanse.olap.common.Util.DOUBLE_NULL;
import static org.eclipse.daanse.olap.common.Util.newInternal;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -642,6 +644,8 @@ public static int compareValues( Object value0, Object value1 ) {
( (Number) value1 ).doubleValue() );
} else if ( value0 instanceof Date ) {
return ( (Date) value0 ).compareTo( (Date) value1 );
} else if ( value0 instanceof LocalDateTime ) {
return ( (LocalDateTime) value0 ).compareTo( (LocalDateTime) value1 );
} else if ( value0 instanceof OrderKey ) {
return ( (OrderKey) value0 ).compareTo( value1 );
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
*/
package org.eclipse.daanse.olap.function.def.udf.currentdatemember;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;
import java.util.Locale;
Expand Down Expand Up @@ -79,9 +81,11 @@ public Object evaluateInternal(Evaluator evaluator) {

/*
* Package private function created for proper testing.
* Returns Date for Format compatibility.
*/
Date getDate(Evaluator evaluator) {
return evaluator.getQueryStartTime();
LocalDateTime ldt = evaluator.getQueryStartTime();
return Date.from(ldt.atZone(ZoneId.systemDefault()).toInstant());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
*/
package org.eclipse.daanse.olap.function.def.udf.currentdatestring;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.Locale;

Expand All @@ -31,11 +33,13 @@ protected CurrentDateStringCalc(Type type, final StringCalc stringCalc) {
@Override
public String evaluateInternal(Evaluator evaluator) {
StringCalc stringCalc = getChildCalc(0, StringCalc.class);

final Locale locale = Locale.getDefault();
final Format format = new Format(stringCalc.evaluate(evaluator), locale);
Date currDate = evaluator.getQueryStartTime();
return format.format(currDate);
LocalDateTime currDate = evaluator.getQueryStartTime();
// Convert LocalDateTime to Date for Format compatibility
Date legacyDate = Date.from(currDate.atZone(ZoneId.systemDefault()).toInstant());
return format.format(legacyDate);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@
*/
package org.eclipse.daanse.olap.function.def.vba.cdate;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import org.eclipse.daanse.olap.api.Evaluator;
import org.eclipse.daanse.olap.api.calc.Calc;
Expand All @@ -26,42 +30,61 @@

public class CDateCalc extends AbstractProfilingNestedDateTimeCalc {

private static final List<DateTimeFormatter> TIME_FORMATTERS = List.of(
DateTimeFormatter.ofPattern("HH:mm:ss"),
DateTimeFormatter.ofPattern("H:mm:ss"),
DateTimeFormatter.ofPattern("h:mm:ss a", Locale.ENGLISH),
DateTimeFormatter.ofPattern("hh:mm:ss a", Locale.ENGLISH),
DateTimeFormatter.ofPattern("h:mm:ss a"),
DateTimeFormatter.ofPattern("hh:mm:ss a"));

protected CDateCalc(Type type, Calc<?> doubleCalc) {
super(type, doubleCalc);
}

@Override
public Date evaluateInternal(Evaluator evaluator) {
public LocalDateTime evaluateInternal(Evaluator evaluator) {
Object expression = getChildCalc(0, Calc.class).evaluate(evaluator);
String str = String.valueOf(expression);
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
if (expression instanceof Date date) {
return date;
if (expression instanceof LocalDateTime ldt) {
return ldt;
} else if (expression instanceof Date date) {
return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
} else if (expression == null) {
return null;
} else {
// note that this currently only supports a limited set of dates and
// times
// "October 19, 1962"
// "4:35:47 PM"
// Try parsing as time with various formats
LocalTime time = parseTime(str);
if (time != null) {
return time.atDate(java.time.LocalDate.of(1970, 1, 1));
}
// Try parsing as ISO LocalDateTime
try {
return sdf.parse(str);
} catch (ParseException ex0) {
return LocalDateTime.parse(str);
} catch (DateTimeParseException ex1) {
// Try parsing as ISO LocalDate
try {
return DateFormat.getDateTimeInstance().parse(str);
} catch (ParseException ex1) {
try {
return DateFormat.getDateInstance().parse(str);
} catch (ParseException ex2) {
throw new InvalidArgumentException(
new StringBuilder("Invalid parameter. ")
.append("expression parameter of CDate function must be ")
.append("formatted correctly (")
.append(String.valueOf(expression)).append(")").toString());
}
return java.time.LocalDate.parse(str).atStartOfDay();
} catch (DateTimeParseException ex2) {
throw new InvalidArgumentException(
new StringBuilder("Invalid parameter. ")
.append("expression parameter of CDate function must be ")
.append("formatted correctly (")
.append(String.valueOf(expression)).append(")").toString());
}
}
}
}

private LocalTime parseTime(String str) {
for (DateTimeFormatter formatter : TIME_FORMATTERS) {
try {
return LocalTime.parse(str, formatter);
} catch (DateTimeParseException e) {
// Try next formatter
}
}
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
*/
package org.eclipse.daanse.olap.function.def.vba.date;

import java.util.Calendar;
import java.util.Date;
import java.time.LocalDate;
import java.time.LocalDateTime;

import org.eclipse.daanse.olap.api.Evaluator;
import org.eclipse.daanse.olap.api.type.Type;
Expand All @@ -27,14 +27,9 @@ protected DateCalc(Type type) {
}

@Override
public Date evaluateInternal(Evaluator evaluator) {
Calendar calendar = Calendar.getInstance();
calendar.clear();
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
return calendar.getTime();
public LocalDateTime evaluateInternal(Evaluator evaluator) {
// Returns current date at midnight
return LocalDate.now().atStartOfDay();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
*/
package org.eclipse.daanse.olap.function.def.vba.dateadd;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.Date;

import org.eclipse.daanse.olap.api.Evaluator;
import org.eclipse.daanse.olap.api.calc.DateTimeCalc;
Expand All @@ -26,31 +28,35 @@
public class DateAddCalc extends AbstractProfilingNestedDateTimeCalc {

public static final long MILLIS_IN_A_DAY = 24L * 60 * 60 * 1000;

protected DateAddCalc(Type type, StringCalc stringCalc, DoubleCalc doubleCalc, DateTimeCalc dateTimeCalc ) {
super(type, stringCalc, doubleCalc, dateTimeCalc);
}

@Override
public Date evaluateInternal(Evaluator evaluator) {
public LocalDateTime evaluateInternal(Evaluator evaluator) {
String intervalName = getChildCalc(0, StringCalc.class).evaluate(evaluator);
Double number = getChildCalc(1, DoubleCalc.class).evaluate(evaluator);
Date date = getChildCalc(2, DateTimeCalc.class).evaluate(evaluator);
LocalDateTime dateTime = getChildCalc(2, DateTimeCalc.class).evaluate(evaluator);

Interval interval = Interval.valueOf(intervalName);
final double floor = Math.floor(number);

// Convert LocalDateTime to Calendar for Interval processing
ZoneId zone = ZoneId.systemDefault();
long millis = dateTime.atZone(zone).toInstant().toEpochMilli();

// We use the local calendar here. This method will therefore return
// different results in different locales: it depends whether the
// initial date and the final date are in DST.
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.setTimeInMillis(millis);
if (floor != number) {
final double ceil = Math.ceil(number);
interval.add(calendar, (int) ceil);
final long ceilMillis = calendar.getTimeInMillis();

calendar.setTime(date);
calendar.setTimeInMillis(millis);
interval.add(calendar, (int) floor);
final long floorMillis = calendar.getTimeInMillis();

Expand All @@ -66,7 +72,8 @@ public Date evaluateInternal(Evaluator evaluator) {
} else {
interval.add(calendar, (int) floor);
}
return calendar.getTime();
// Convert Calendar back to LocalDateTime
return LocalDateTime.ofInstant(Instant.ofEpochMilli(calendar.getTimeInMillis()), zone);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
*/
package org.eclipse.daanse.olap.function.def.vba.datediff;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.Date;

import org.eclipse.daanse.olap.api.Evaluator;
import org.eclipse.daanse.olap.api.calc.DateTimeCalc;
Expand All @@ -32,31 +33,32 @@ protected DateDiffCalc(Type type, StringCalc stringCalc, DateTimeCalc dateTimeCa
@Override
public Long evaluateInternal(Evaluator evaluator) {
String intervalName = getChildCalc(0, StringCalc.class).evaluate(evaluator);
Date date1 = getChildCalc(1, DateTimeCalc.class).evaluate(evaluator);
Date date2 = getChildCalc(2, DateTimeCalc.class).evaluate(evaluator);
LocalDateTime dateTime1 = getChildCalc(1, DateTimeCalc.class).evaluate(evaluator);
LocalDateTime dateTime2 = getChildCalc(2, DateTimeCalc.class).evaluate(evaluator);
int firstDayOfWeek = getChildCalc(3, IntegerCalc.class).evaluate(evaluator);
int fwofy = getChildCalc(4, IntegerCalc.class).evaluate(evaluator);
FirstWeekOfYear firstWeekOfYear = FirstWeekOfYear.values()[fwofy];
return dateDiff(
intervalName, date1, date2,
intervalName, dateTime1, dateTime2,
firstDayOfWeek, firstWeekOfYear);
}

private static long dateDiff(
String intervalName, Date date1, Date date2,
String intervalName, LocalDateTime dateTime1, LocalDateTime dateTime2,
int firstDayOfWeek, FirstWeekOfYear firstWeekOfYear)
{
Interval interval = Interval.valueOf(intervalName);
if (interval == Interval.d) {
// MONDRIAN-2319
interval = Interval.y;
}
// Convert LocalDateTime to Calendar for Interval processing
Calendar calendar1 = Calendar.getInstance();
firstWeekOfYear.apply(calendar1);
calendar1.setTime(date1);
calendar1.setTimeInMillis(dateTime1.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());
Calendar calendar2 = Calendar.getInstance();
firstWeekOfYear.apply(calendar2);
calendar2.setTime(date2);
calendar2.setTimeInMillis(dateTime2.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());
return interval.diff(calendar1, calendar2, firstDayOfWeek);
}

Expand Down
Loading
Loading