import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Locale;
+import java.util.TimeZone;
import org.apache.poi.ss.formula.eval.ErrorEval;
import org.apache.poi.ss.formula.eval.EvaluationException;
/**
* Implementation for the Excel function DATE
- *
- * @author Pavel Krupets (pkrupets at palmtreebusiness dot com)
*/
public final class DateFunc extends Fixed3ArgFunction {
+ /**
+ * Excel doesn't store TimeZone information in the file, so if in doubt,
+ * use UTC to perform calculations
+ */
+ private static final TimeZone DEFAULT_TIMEZONE = TimeZone.getTimeZone("UTC");
public static final Function instance = new DateFunc();
}
// Turn this into a Java date
- Calendar c = new GregorianCalendar(Locale.ROOT);
+ Calendar c = new GregorianCalendar(DEFAULT_TIMEZONE, Locale.ROOT);
c.set(year, month, day, 0, 0, 0);
c.set(Calendar.MILLISECOND, 0);
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Locale;
+import java.util.TimeZone;
import org.apache.poi.ss.formula.eval.NumberEval;
import org.apache.poi.ss.formula.eval.ValueEval;
/**
* Implementation of Excel TODAY() Function<br/>
- *
- * @author Frank Taffelt
*/
public final class Today extends Fixed0ArgFunction {
+ /**
+ * Excel doesn't store TimeZone information in the file, so if in doubt,
+ * use UTC to perform calculations
+ */
+ private static final TimeZone DEFAULT_TIMEZONE = TimeZone.getTimeZone("UTC");
public ValueEval evaluate(int srcRowIndex, int srcColumnIndex) {
-
- Calendar now = new GregorianCalendar(Locale.ROOT);
+ Calendar now = new GregorianCalendar(DEFAULT_TIMEZONE, Locale.ROOT);
now.set(now.get(Calendar.YEAR), now.get(Calendar.MONTH), now.get(Calendar.DATE),0,0,0);
now.set(Calendar.MILLISECOND, 0);
return new NumberEval(DateUtil.getExcelDate(now.getTime()));
// elapsed time patterns: [h],[m] and [s]
private static final Pattern date_ptrn4 = Pattern.compile("^\\[([hH]+|[mM]+|[sS]+)\\]");
- // only get this static info once (because operations are not really cheap)
+ /**
+ * Excel doesn't store TimeZone information in the file, so if in doubt,
+ * use UTC to perform calculations
+ */
private static final TimeZone TIMEZONE_UTC = TimeZone.getTimeZone("UTC");
* @param use1904windowing Should 1900 or 1904 date windowing be used?
*/
public static double getExcelDate(Date date, boolean use1904windowing) {
- Calendar calStart = new GregorianCalendar(Locale.ROOT);
+ Calendar calStart = new GregorianCalendar(TIMEZONE_UTC, Locale.ROOT);
calStart.setTime(date); // If date includes hours, minutes, and seconds, set them to 0
return internalGetExcelDate(calStart, use1904windowing);
}
if (timeZone != null) {
calendar = new GregorianCalendar(timeZone, Locale.ROOT);
} else {
- calendar = new GregorianCalendar(Locale.ROOT); // using default time-zone
+ calendar = new GregorianCalendar(TIMEZONE_UTC, Locale.ROOT); // using default time-zone
}
setCalendar(calendar, wholeDays, millisecondsInDay, use1904windowing, roundSeconds);
return calendar;