From: Nick Burch Date: Tue, 1 Sep 2015 19:11:20 +0000 (+0000) Subject: Fix some Forbidden APIs errors X-Git-Tag: REL_3_13_FINAL~58 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=5c4777ba49425e15013ceac7b71177274591e6d5;p=poi.git Fix some Forbidden APIs errors git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1700645 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/java/org/apache/poi/ss/formula/functions/DateFunc.java b/src/java/org/apache/poi/ss/formula/functions/DateFunc.java index cd64505d56..f767d1574b 100644 --- a/src/java/org/apache/poi/ss/formula/functions/DateFunc.java +++ b/src/java/org/apache/poi/ss/formula/functions/DateFunc.java @@ -20,6 +20,7 @@ package org.apache.poi.ss.formula.functions; 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; @@ -30,10 +31,13 @@ import org.apache.poi.ss.usermodel.DateUtil; /** * 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(); @@ -87,7 +91,7 @@ public final class DateFunc extends Fixed3ArgFunction { } // 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); diff --git a/src/java/org/apache/poi/ss/formula/functions/Today.java b/src/java/org/apache/poi/ss/formula/functions/Today.java index 906cbda1ff..60cdedb1bd 100644 --- a/src/java/org/apache/poi/ss/formula/functions/Today.java +++ b/src/java/org/apache/poi/ss/formula/functions/Today.java @@ -20,6 +20,7 @@ package org.apache.poi.ss.formula.functions; 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; @@ -27,14 +28,16 @@ import org.apache.poi.ss.usermodel.DateUtil; /** * Implementation of Excel TODAY() Function
- * - * @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())); diff --git a/src/java/org/apache/poi/ss/usermodel/DateUtil.java b/src/java/org/apache/poi/ss/usermodel/DateUtil.java index 31b33eabc4..ff8ab4db2e 100644 --- a/src/java/org/apache/poi/ss/usermodel/DateUtil.java +++ b/src/java/org/apache/poi/ss/usermodel/DateUtil.java @@ -61,7 +61,10 @@ public class DateUtil { // 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"); @@ -84,7 +87,7 @@ public class DateUtil { * @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); } @@ -322,7 +325,7 @@ public class DateUtil { 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;