/* ==================================================================== Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ==================================================================== */ package org.apache.poi.hssf.usermodel; import static java.util.Calendar.AUGUST; import static java.util.Calendar.FEBRUARY; import static java.util.Calendar.JANUARY; import static java.util.Calendar.JULY; import static java.util.Calendar.MARCH; import static java.util.Calendar.MAY; import static java.util.Calendar.OCTOBER; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import java.io.IOException; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.Locale; import java.util.TimeZone; import org.apache.poi.hssf.HSSFTestDataSamples; import org.apache.poi.hssf.model.InternalWorkbook; import org.apache.poi.ss.usermodel.DateUtil; import org.apache.poi.util.LocaleUtil; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; /** * Class TestHSSFDateUtil */ public class TestHSSFDateUtil { static TimeZone userTimeZone; @BeforeClass public static void setCEST() { userTimeZone = LocaleUtil.getUserTimeZone(); LocaleUtil.setUserTimeZone(TimeZone.getTimeZone("CEST")); } @AfterClass public static void resetTimeZone() { LocaleUtil.setUserTimeZone(userTimeZone); } /** * Checks the date conversion functions in the HSSFDateUtil class. */ @Test public void dateConversion() { // Iteratating over the hours exposes any rounding issues. Calendar cal = LocaleUtil.getLocaleCalendar(2002,JANUARY,1,0,1,1); for (int hour = 0; hour < 23; hour++) { double excelDate = HSSFDateUtil.getExcelDate(cal.getTime(), false); assertEquals("Checking hour = " + hour, cal.getTime().getTime(), HSSFDateUtil.getJavaDate(excelDate, false).getTime()); cal.add(Calendar.HOUR_OF_DAY, 1); } // check 1900 and 1904 date windowing conversions double excelDate = 36526.0; // with 1900 windowing, excelDate is Jan. 1, 2000 // with 1904 windowing, excelDate is Jan. 2, 2004 cal.set(2000,JANUARY,1,0,0,0); // Jan. 1, 2000 Date dateIf1900 = cal.getTime(); cal.add(Calendar.YEAR,4); // now Jan. 1, 2004 cal.add(Calendar.DATE,1); // now Jan. 2, 2004 Date dateIf1904 = cal.getTime(); // 1900 windowing assertEquals("Checking 1900 Date Windowing", dateIf1900.getTime(), HSSFDateUtil.getJavaDate(excelDate,false).getTime()); // 1904 windowing assertEquals("Checking 1904 Date Windowing", dateIf1904.getTime(), HSSFDateUtil.getJavaDate(excelDate,true).getTime()); } /** * Checks the conversion of a java.util.date to Excel on a day when * Daylight Saving Time starts. */ @Test public void excelConversionOnDSTStart() { Calendar cal = LocaleUtil.getLocaleCalendar(2004,MARCH,28,0,0,0); for (int hour = 0; hour < 24; hour++) { // Skip 02:00 CET as that is the Daylight change time // and Java converts it automatically to 03:00 CEST if (hour == 2) { continue; } cal.set(Calendar.HOUR_OF_DAY, hour); Date javaDate = cal.getTime(); double excelDate = HSSFDateUtil.getExcelDate(javaDate, false); double difference = excelDate - Math.floor(excelDate); int differenceInHours = (int) (difference * 24 * 60 + 0.5) / 60; assertEquals("Checking " + hour + " hour on Daylight Saving Time start date", hour, differenceInHours); assertEquals("Checking " + hour + " hour on Daylight Saving Time start date", javaDate.getTime(), HSSFDateUtil.getJavaDate(excelDate, false).getTime()); } } /** * Checks the conversion of an Excel date to a java.util.date on a day when * Daylight Saving Time starts. */ @Test public void javaConversionOnDSTStart() { Calendar cal = LocaleUtil.getLocaleCalendar(2004,MARCH,28,0,0,0); double excelDate = HSSFDateUtil.getExcelDate(cal.getTime(), false); double oneHour = 1.0 / 24; double oneMinute = oneHour / 60; for (int hour = 0; hour < 24; hour++, excelDate += oneHour) { // Skip 02:00 CET as that is the Daylight change time // and Java converts it automatically to 03:00 CEST if (hour == 2) { continue; } cal.set(Calendar.HOUR_OF_DAY, hour); Date javaDate = HSSFDateUtil.getJavaDate(excelDate, false); double actDate = HSSFDateUtil.getExcelDate(javaDate, false); assertEquals("Checking " + hour + " hours on Daylight Saving Time start date", excelDate, actDate, oneMinute); } } /** * Checks the conversion of a java.util.Date to Excel on a day when * Daylight Saving Time ends. */ @Test public void excelConversionOnDSTEnd() { Calendar cal = LocaleUtil.getLocaleCalendar(2004,OCTOBER,31,0,0,0); for (int hour = 0; hour < 24; hour++) { cal.set(Calendar.HOUR_OF_DAY, hour); Date javaDate = cal.getTime(); double excelDate = HSSFDateUtil.getExcelDate(javaDate, false); double difference = excelDate - Math.floor(excelDate); int differenceInHours = (int) (difference * 24 * 60 + 0.5) / 60; assertEquals("Checking " + hour + " hour on Daylight Saving Time end date", hour, differenceInHours); assertEquals("Checking " + hour + " hour on Daylight Saving Time start date", javaDate.getTime(), HSSFDateUtil.getJavaDate(excelDate, false).getTime()); } } /** * Checks the conversion of an Excel date to java.util.Date on a day when * Daylight Saving Time ends. */ @Test public void javaConversionOnDSTEnd() { Calendar cal = LocaleUtil.getLocaleCalendar(2004,OCTOBER,31,0,0,0); double excelDate = HSSFDateUtil.getExcelDate(cal.getTime(), false); double oneHour = 1.0 / 24; double oneMinute = oneHour / 60; for (int hour = 0; hour < 24; hour++, excelDate += oneHour) { cal.set(Calendar.HOUR_OF_DAY, hour); Date javaDate = HSSFDateUtil.getJavaDate(excelDate, false); assertEquals("Checking " + hour + " hours on Daylight Saving Time start date", excelDate, HSSFDateUtil.getExcelDate(javaDate, false), oneMinute); } } /** * Tests that we deal with time-zones properly */ @Test public void calendarConversion() { TimeZone userTZ = LocaleUtil.getUserTimeZone(); LocaleUtil.setUserTimeZone(TimeZone.getTimeZone("CET")); try { Calendar cal = LocaleUtil.getLocaleCalendar(2002,JANUARY,1,12,1,1); Date expected = cal.getTime(); // Iterating over the hours exposes any rounding issues. for (int hour = -12; hour <= 12; hour++) { String id = "GMT" + (hour < 0 ? "" : "+") + hour + ":00"; cal.setTimeZone(TimeZone.getTimeZone(id)); cal.set(Calendar.HOUR_OF_DAY, 12); double excelDate = HSSFDateUtil.getExcelDate(cal, false); Date javaDate = HSSFDateUtil.getJavaDate(excelDate); // Should match despite time-zone assertEquals("Checking timezone " + id, expected.getTime(), javaDate.getTime()); } // Check that the timezone aware getter works correctly TimeZone cet = TimeZone.getTimeZone("Europe/Copenhagen"); TimeZone ldn = TimeZone.getTimeZone("Europe/London"); // 12:45 on 27th April 2012 double excelDate = 41026.53125; // Same, no change assertEquals( HSSFDateUtil.getJavaDate(excelDate, false).getTime(), HSSFDateUtil.getJavaDate(excelDate, false, cet).getTime() ); // London vs Copenhagen, should differ by an hour Date cetDate = HSSFDateUtil.getJavaDate(excelDate, false); Date ldnDate = HSSFDateUtil.getJavaDate(excelDate, false, ldn); assertEquals(ldnDate.getTime() - cetDate.getTime(), 60*60*1000); } finally { LocaleUtil.setUserTimeZone(userTZ); } } /** * Tests that we correctly detect date formats as such */ @Test public void identifyDateFormats() { // First up, try with a few built in date formats short[] builtins = new short[] { 0x0e, 0x0f, 0x10, 0x16, 0x2d, 0x2e }; for(int i=0; i