/* Test TimeStampLTZ */ public void testTimeStampLTZ() { int year = 2000, month = 3, date = 21, hour = 11, minute = 45, second = 50; Integer tsId = null; Calendar originalCal = null, dbCal = null; String zoneId = "America/Los_Angeles"; EntityManager em = createEntityManager("timestamptz"); beginTransaction(em); try { TStamp ts = new TStamp(); originalCal = Calendar.getInstance(TimeZone.getTimeZone(zoneId)); originalCal.set(Calendar.AM_PM, Calendar.AM); originalCal.set(year, month, date, hour, minute, second); originalCal.set(Calendar.MILLISECOND, 0); ts.setTsLTZ(originalCal); em.persist(ts); em.flush(); tsId = ts.getId(); commitTransaction(em); } catch (Exception e) { e.printStackTrace(); rollbackTransaction(em); } finally { clearCache(); dbCal = em.find(TStamp.class, tsId).getTsLTZ(); closeEntityManager(em); } assertTrue("The year is not macth", year == dbCal.get(java.util.Calendar.YEAR)); assertTrue("The month is not match", month == dbCal.get(java.util.Calendar.MONTH)); assertTrue("The date is not match", date == dbCal.get(java.util.Calendar.DATE)); int hourDiffFromDB = dbCal.get(Calendar.HOUR_OF_DAY) - originalCal.get(Calendar.HOUR_OF_DAY); int hourDiffFromZone = (dbCal.get(Calendar.ZONE_OFFSET) - originalCal.get(Calendar.ZONE_OFFSET)) / 3600000; assertTrue("The hour is not match", hourDiffFromDB == hourDiffFromZone); assertTrue("The minute is not match", minute == dbCal.get(java.util.Calendar.MINUTE)); assertTrue("The second is not match", second == dbCal.get(java.util.Calendar.SECOND)); }
/* Test TimeStampTZ with daylightsaving time*/ public void testTimeStampTZDST() { int year = 2008, month = 2, date = 10, hour = 11, minute = 0, second = 0; Integer tsId = null; Calendar originalCal = null, dbCal = null; String zoneIdRemote = "Europe/London"; EntityManager em = createEntityManager("timestamptz"); beginTransaction(em); try { TStamp ts = new TStamp(); originalCal = Calendar.getInstance(TimeZone.getTimeZone(zoneIdRemote)); originalCal.set(Calendar.AM_PM, Calendar.AM); originalCal.set(year, month, date, hour, minute, second); originalCal.set(Calendar.MILLISECOND, 0); ts.setTsLTZ(originalCal); em.persist(ts); em.flush(); tsId = ts.getId(); commitTransaction(em); closeEntityManager(em); clearCache(); em = createEntityManager("timestamptz"); dbCal = em.find(TStamp.class, tsId).getTsLTZ(); int hourDiffFromDB = dbCal.get(Calendar.HOUR_OF_DAY) - originalCal.get(Calendar.HOUR_OF_DAY); int hourDiffFromZone = (dbCal.get(Calendar.ZONE_OFFSET) - originalCal.get(Calendar.ZONE_OFFSET)) / 3600000; assertTrue( "The yhour is not macth", (hourDiffFromZone + dbCal.get(Calendar.DST_OFFSET) / 3600000) == hourDiffFromDB); } finally { if (isTransactionActive(em)) { rollbackTransaction(em); } closeEntityManager(em); } }