Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4154652

SimpleTimeZone works wrong

XMLWordPrintable



      Name: avC70361 Date: 07/06/98



        java.util.SimpleTimeZone.getOffset works wrong for some dates. inDaylightTime
      for that date returns true, but getOffset returns offset which is not equal to
      GMT offset + DST saving offset.

      Here is a test demonstrating the bug.
      ---------------
      import java.util.Calendar;
      import java.util.Date;
      import java.util.GregorianCalendar;
      import java.util.SimpleTimeZone;
      import java.util.TimeZone;

      public class GetOffsetTest {

        public static void main(String args[]) {
          SimpleTimeZone tz = new SimpleTimeZone(
            60 * 60 * 1000, "stz",
            Calendar.JANUARY, 1, 0, 0, /* DST start day: January, 1, 0:00 */
            Calendar.APRIL, 1, 0, 0, /* DST end day: April, 1, 0:00 */
            60 * 60 * 1000
          );

          Calendar calendar = new GregorianCalendar(TimeZone.getTimeZone("GMT"));

      TimeZone.setDefault(TimeZone.getTimeZone("GMT"));

          // setting a date just after end rule using GMT zone
          calendar.clear();
          calendar.set(Calendar.ERA, GregorianCalendar.AD);
          calendar.set(Calendar.YEAR, 1998);
          calendar.set(Calendar.MONTH, Calendar.MARCH);
          calendar.set(Calendar.DATE, 31);
          calendar.set(Calendar.HOUR, 22);
          calendar.set(Calendar.MINUTE, 1);

          Date date = calendar.getTime();

          int expectedOffset;

          boolean inDST = tz.inDaylightTime(date);

          if (inDST) {
            expectedOffset = tz.getRawOffset() + tz.getDSTSavings();
          } else {
            expectedOffset = tz.getRawOffset();
          }

          calendar = new GregorianCalendar(tz);

          calendar.setTime(date);

          int offset = tz.getOffset(
            calendar.get(Calendar.ERA),
            calendar.get(Calendar.YEAR),
            calendar.get(Calendar.MONTH),
            calendar.get(Calendar.DATE),
            calendar.get(Calendar.DAY_OF_WEEK),
            calendar.get(Calendar.HOUR) * 3600000
            + calendar.get(Calendar.MINUTE) * 60000
            + calendar.get(Calendar.SECOND) * 1000
            + calendar.get(Calendar.MILLISECOND)
            -(inDST ? tz.getDSTSavings() : 0) /* we should use standard local time */
          );

          if (offset != expectedOffset) {
            System.out.println(
              "tz = " + tz + "\n" +
              "date = " + date + "\n" +
              "in DST = " + tz.inDaylightTime(date) + "\n" +
              "expected offset = " + expectedOffset + "\n" +
              "Failed. getOffset() returns " + offset
            );
          } else {
            System.out.println("Passed");
          }
        }
      }

      ---------The test output--------
      > java GetOffsetTest
      tz = java.util.SimpleTimeZone[id=stz,offset=3600000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=1,startMonth=0,startDay=1,startDayOfWeek=0,startTime=0,endMode=1,endMonth=3,endDay=1,endDayOfWeek=0,endTime=0]
      date = Tue Mar 31 22:01:00 GMT 1998
      in DST = true
      expected offset = 7200000
      Failed. getOffset() returns 3600000

      ======================================================================

            aliusunw Alan Liu (Inactive)
            ovlasov Oleksandr Vlasov (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: