ASF Bugzilla – Attachment 17688 Details for
Bug 38641
[PATCH] Use the calendar's timezone in HSSFCell.setCellValue(Calendar)
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
Remember
[x]
|
Forgot Password
Login:
[x]
[patch]
Patch HSSFCell.setCellValue(Calendar) to use the calendar's timezone
hssfcell-setcalendar.patch.txt (text/plain), 4.83 KB, created by
Hack Kampbjorn
on 2006-02-14 14:36:02 UTC
(
hide
)
Description:
Patch HSSFCell.setCellValue(Calendar) to use the calendar's timezone
Filename:
MIME Type:
Creator:
Hack Kampbjorn
Created:
2006-02-14 14:36:02 UTC
Size:
4.83 KB
patch
obsolete
>Index: src/java/org/apache/poi/hssf/usermodel/HSSFDateUtil.java >=================================================================== >--- src/java/org/apache/poi/hssf/usermodel/HSSFDateUtil.java (revision 377681) >+++ src/java/org/apache/poi/hssf/usermodel/HSSFDateUtil.java (working copy) >@@ -63,7 +63,25 @@ > > calStart.setTime( > date); // If date includes hours, minutes, and seconds, set them to 0 >- if (calStart.get(Calendar.YEAR) < 1900) >+ return internalGetExcelDate(calStart); >+ } >+ >+ /** >+ * Given a Date, converts it into a double representing its internal Excel representation, >+ * which is the number of days since 1/1/1900. Fractional days represent hours, minutes, and seconds. >+ * >+ * @return Excel representation of Date (-1 if error - test for error by checking for less than 0.1) >+ * @param date the Date >+ */ >+ public static double getExcelDate(Calendar date) >+ { >+ return internalGetExcelDate((Calendar) date.clone()); >+ } >+ >+ // The time will be change to the daystart >+ private static double internalGetExcelDate(Calendar date) >+ { >+ if (date.get(Calendar.YEAR) < 1900) > { > return BAD_DATE; > } >@@ -76,14 +94,14 @@ > // be 4 hours. > // E.g. 2004-03-28 04:00 CEST - 2004-03-28 00:00 CET is 3 hours > // and 2004-10-31 04:00 CET - 2004-10-31 00:00 CEST is 5 hours >- double fraction = (((calStart.get(Calendar.HOUR_OF_DAY) * 60 >- + calStart.get(Calendar.MINUTE) >- ) * 60 + calStart.get(Calendar.SECOND) >- ) * 1000 + calStart.get(Calendar.MILLISECOND) >+ double fraction = (((date.get(Calendar.HOUR_OF_DAY) * 60 >+ + date.get(Calendar.MINUTE) >+ ) * 60 + date.get(Calendar.SECOND) >+ ) * 1000 + date.get(Calendar.MILLISECOND) > ) / ( double ) DAY_MILLISECONDS; >- calStart = dayStart(calStart); >+ date = dayStart(date); > >- return fraction + ( double ) absoluteDay(calStart) >+ return fraction + ( double ) absoluteDay(date) > - CAL_1900_ABSOLUTE; > } > } >Index: src/java/org/apache/poi/hssf/usermodel/HSSFCell.java >=================================================================== >--- src/java/org/apache/poi/hssf/usermodel/HSSFCell.java (revision 377681) >+++ src/java/org/apache/poi/hssf/usermodel/HSSFCell.java (working copy) >@@ -518,13 +518,20 @@ > * set a date value for the cell. Excel treats dates as numeric so you will need to format the cell as > * a date. > * >+ * This will set the cell value based on the Calendar's timezone as Excel >+ * does not support timezones this means that both 20:00+03:00 and >+ * 20:00-03:00 will be reported as the same value (20:00) even that there >+ * are 6 hours difference between the two times. This difference can be >+ * preserved by using <code>setCellValue(value.getTime())</code> which will >+ * automatically shift the times to the default timezone. >+ * > * @param value the date value to set this cell to. For formulas we'll set the > * precalculated value, for numerics we'll set its value. For othertypes we > * will change the cell to a numeric cell and set its value. > */ > public void setCellValue(Calendar value) > { >- setCellValue(value.getTime()); >+ setCellValue(HSSFDateUtil.getExcelDate(value)); > } > > /** >Index: src/testcases/org/apache/poi/hssf/usermodel/TestHSSFDateUtil.java >=================================================================== >--- src/testcases/org/apache/poi/hssf/usermodel/TestHSSFDateUtil.java (revision 377681) >+++ src/testcases/org/apache/poi/hssf/usermodel/TestHSSFDateUtil.java (working copy) >@@ -182,6 +182,25 @@ > } > } > >+ /** >+ * Checks the conversion of a calendar uses the Calendar's TimeZone >+ */ >+ public void testCalendarConversion() { >+ GregorianCalendar date = new GregorianCalendar(2002, 0, 1, 12, 1, 1); >+ Date expected = date.getTime(); >+ >+ // Iteratating over the hours exposes any rounding issues. >+ for (int hour = -12; hour <= 12; hour++) >+ { >+ String id = "GMT" + (hour < 0 ? "" : "+") + hour + ":00"; >+ date.setTimeZone(TimeZone.getTimeZone(id)); >+ date.set(Calendar.HOUR_OF_DAY, 12); >+ double excelDate = HSSFDateUtil.getExcelDate(date); >+ Date javaDate = HSSFDateUtil.getJavaDate(excelDate); >+ assertEquals("Checking timezone " + id, expected.getTime(), javaDate.getTime()); >+ } >+ } >+ > public static void main(String [] args) { > System.out > .println("Testing org.apache.poi.hssf.usermodel.TestHSSFDateUtil");
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
Attachments on
bug 38641
: 17688