home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 July & August / Pcwk78a98.iso / Internet / Javadraw / DATA.Z / TimeZone.java < prev    next >
Text File  |  1997-08-30  |  15KB  |  341 lines

  1. /*
  2.  * @(#)TimeZone.java    1.19 97/07/24
  3.  *
  4.  * (C) Copyright Taligent, Inc. 1996 - All Rights Reserved
  5.  * (C) Copyright IBM Corp. 1996 - All Rights Reserved
  6.  *
  7.  * Portions copyright (c) 1996 Sun Microsystems, Inc. All Rights Reserved.
  8.  *
  9.  *   The original version of this source code and documentation is copyrighted
  10.  * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These
  11.  * materials are provided under terms of a License Agreement between Taligent
  12.  * and Sun. This technology is protected by multiple US and International
  13.  * patents. This notice and attribution to Taligent may not be removed.
  14.  *   Taligent is a registered trademark of Taligent, Inc.
  15.  *
  16.  * Permission to use, copy, modify, and distribute this software
  17.  * and its documentation for NON-COMMERCIAL purposes and without
  18.  * fee is hereby granted provided that this copyright notice
  19.  * appears in all copies. Please refer to the file "copyright.html"
  20.  * for further important copyright and licensing information.
  21.  *
  22.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  23.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  24.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  25.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  26.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  27.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  28.  *
  29.  */
  30.  
  31. package java.util;
  32. import java.io.Serializable;
  33.  
  34. /**
  35.  * <code>TimeZone</code> represents a time zone offset, and also figures out daylight
  36.  * savings.
  37.  *
  38.  * <p>
  39.  * Typically, you get a <code>TimeZone</code> using <code>getDefault</code>
  40.  * which creates a <code>TimeZone</code> based on the time zone where the program
  41.  * is running. For example, for a program running in Japan, <code>getDefault</code>
  42.  * creates a <code>TimeZone</code> object based on Japanese Standard Time.
  43.  *
  44.  * <p>
  45.  * You can also get a <code>TimeZone</code> using <code>getTimeZone</code> along
  46.  * with a time zone ID. For instance, the time zone ID for the Pacific
  47.  * Standard Time zone is "PST". So, you can get a PST <code>TimeZone</code> object
  48.  * with:
  49.  * <blockquote>
  50.  * <pre>
  51.  * TimeZone tz = TimeZone.getTimeZone("PST");
  52.  * </pre>
  53.  * </blockquote>
  54.  * You can use <code>getAvailableIDs</code> method to iterate through
  55.  * all the supported time zone IDs. You can then choose a
  56.  * supported ID to get a favorite <code>TimeZone</code>.
  57.  *
  58.  * @see          Calendar
  59.  * @see          GregorianCalendar
  60.  * @see          SimpleTimeZone
  61.  * @version      1.19 07/24/97
  62.  * @author       Mark Davis, David Goldsmith, Chen-Lieh Huang
  63.  */
  64. abstract public class TimeZone implements Serializable, Cloneable {
  65.     /**
  66.      * Gets the time zone offset, for current date, modified in case of
  67.      * daylight savings. This is the offset to add *to* UTC to get local time.
  68.      * @param era the era of the given date.
  69.      * @param year the year in the given date.
  70.      * @param month the month in the given date.
  71.      * Month is 0-based. e.g., 0 for January.
  72.      * @param day the day-in-month of the given date.
  73.      * @param dayOfWeek the day-of-week of the given date.
  74.      * @param milliseconds the millis in day in <em>standard</em> local time.
  75.      * @return the offset to add *to* GMT to get local time.
  76.      */
  77.     abstract public int getOffset(int era, int year, int month, int day,
  78.                                   int dayOfWeek, int milliseconds);
  79.  
  80.     /**
  81.      * Sets the base time zone offset to GMT.
  82.      * This is the offset to add *to* UTC to get local time.
  83.      * @param offsetMillis the given base time zone offset to GMT.
  84.      */
  85.     abstract public void setRawOffset(int offsetMillis);
  86.  
  87.     /**
  88.      * Gets unmodified offset, NOT modified in case of daylight savings.
  89.      * This is the offset to add *to* UTC to get local time.
  90.      * @return the unmodified offset to add *to* UTC to get local time.
  91.      */
  92.     abstract public int getRawOffset();
  93.  
  94.     /**
  95.      * Gets the ID of this time zone.
  96.      * @return the ID of this time zone.
  97.      */
  98.     public String getID()
  99.     {
  100.         return ID;
  101.     }
  102.  
  103.     /**
  104.      * Sets the time zone ID. This does not change any other data in
  105.      * the time zone object.
  106.      * @param ID the new time zone ID.
  107.      */
  108.     public void setID(String ID)
  109.     {
  110.         this.ID = ID;
  111.     }
  112.  
  113.     /**
  114.      * Queries if this time zone uses Daylight Savings Time.
  115.      * @return true if this time zone uses Daylight Savings Time,
  116.      * false, otherwise.
  117.      */
  118.     abstract public boolean useDaylightTime();
  119.  
  120.     /**
  121.      * Queries if the given date is in Daylight Savings Time in
  122.      * this time zone.
  123.      * @param date the given Date.
  124.      * @return true if the given date is in Daylight Savings Time,
  125.      * false, otherwise.
  126.      */
  127.     abstract public boolean inDaylightTime(Date date);
  128.  
  129.     /**
  130.      * Gets the TimeZone for the given ID.
  131.      * @param ID the given ID.
  132.      * @return a TimeZone.
  133.      */
  134.     public static synchronized TimeZone getTimeZone(String ID)
  135.     {
  136.         try {
  137.             return (SimpleTimeZone) lookup.get(ID);
  138.         } catch (MissingResourceException e) {
  139.             return (SimpleTimeZone) lookup.get("GMT");
  140.         }
  141.     }
  142.  
  143.     /**
  144.      * Gets the available IDs according to the given time zone offset.
  145.      * @param rawOffset the given time zone GMT offset.
  146.      * @return an array of IDs, where the time zone for that ID has
  147.      * the specified GMT offset. For example, {"Phoenix", "Denver"},
  148.      * since both have GMT-07:00, but differ in daylight savings behavior.
  149.      */
  150.     public static synchronized String[] getAvailableIDs(int rawOffset) {
  151.         String[]    resultArray = new String[10]; // normally 2 ~ 3 IDs
  152.         int         count = 0;
  153.         for (int i = 0; i < timeZoneData.length; ++i)
  154.             if (rawOffset == timeZoneData[i].getRawOffset())
  155.                 resultArray[count++] = timeZoneData[i].getID();
  156.  
  157.         // copy into array of the right size and return
  158.         String[] finalResult = new String[count];
  159.         System.arraycopy(resultArray, 0, finalResult, 0, count);
  160.  
  161.         return finalResult;
  162.     }
  163.  
  164.     /**
  165.      * Gets all the available IDs supported.
  166.      * @return an array of IDs.
  167.      */
  168.     public static synchronized String[] getAvailableIDs() {
  169.         String[]    resultArray = new String[40];
  170.         int         count = 0;
  171.  
  172.         for (int i = 0; i < timeZoneData.length; ++i)
  173.             resultArray[count++] = timeZoneData[i].getID();
  174.  
  175.         // copy into array of the right size and return
  176.         String[] finalResult = new String[count];
  177.         System.arraycopy(resultArray, 0, finalResult, 0, count);
  178.  
  179.         return finalResult;
  180.     }
  181.  
  182.     /**
  183.      * Gets the default TimeZone for this host.
  184.      * @return a default TimeZone.
  185.      */
  186.     public static synchronized TimeZone getDefault() {
  187.         if (defaultZone == null) {
  188.             // get the ID from the system properties
  189.             String ID = System.getProperty("user.timezone", "GMT");
  190.             if (ID != null) {
  191.                 defaultZone = getTimeZone(ID);
  192.                 if (defaultZone != null)
  193.                     return defaultZone;
  194.             }
  195.  
  196.             // There used to be code here that pretended to get a time zone
  197.             // for the host's system's offset, but since we don't have the
  198.             // offset, it basically hardwired to PST. PST is a bad default,
  199.             // so we now return GMT. We should improve our time zone name mapping
  200.             // mechanism so that we get here far less often than we do now.
  201.  
  202.             return getTimeZone("GMT");
  203.         }
  204.         else return defaultZone;
  205.     }
  206.  
  207.     /**
  208.      * Sets time zone to using the given TimeZone.
  209.      * @param zone the given time zone.
  210.      */
  211.     public static synchronized void setDefault(TimeZone zone)
  212.     {
  213.         defaultZone = zone;
  214.     }
  215.  
  216.     /**
  217.      * Overrides Cloneable
  218.      */
  219.     public Object clone()
  220.     {
  221.         try {
  222.             TimeZone other = (TimeZone) super.clone();
  223.             other.ID = ID;
  224.             return other;
  225.         } catch (CloneNotSupportedException e) {
  226.             throw new InternalError();
  227.         }
  228.     }
  229.  
  230.     // =======================privates===============================
  231.  
  232.     private String           ID;
  233.     private static TimeZone  defaultZone=null;
  234.     private static final int millisPerHour = 60*60*1000;
  235.  
  236.     private static final SimpleTimeZone[] timeZoneData = {
  237.         // GMT is the ID for Greenwich Mean Time time zone.
  238.         // Since Java treats GMT and UTC as synonyms, the GMT
  239.         // time zone can't have daylight savings time.
  240.         // ??? We need a different zone for Britain.
  241.         new SimpleTimeZone(0, "GMT"),
  242.         // ECT is the ID for European Central Time time zone.
  243.         new SimpleTimeZone(1 * millisPerHour, "ECT",
  244.                 Calendar.MARCH, -1, Calendar.SUNDAY, 2 * millisPerHour,
  245.                 Calendar.OCTOBER, -1, Calendar.SUNDAY, 2 * millisPerHour),
  246.         // EET is the ID for Eastern European Time time zone.
  247.         new SimpleTimeZone(2 * millisPerHour, "EET",
  248.                 Calendar.MARCH, -1, Calendar.SUNDAY, 0 * millisPerHour,
  249.                 Calendar.OCTOBER, -1, Calendar.SUNDAY, 0 * millisPerHour),
  250.         // ART is the ID for (Arabic) Egypt Standard Time timezone.
  251.         new SimpleTimeZone(2 * millisPerHour, "ART",
  252.                 Calendar.APRIL, -1, Calendar.FRIDAY, 0 * millisPerHour,
  253.                 Calendar.SEPTEMBER, -1, Calendar.FRIDAY, 0 * millisPerHour),
  254.         // EAT is the ID for Eastern African Time time zone.
  255.         new SimpleTimeZone(3 * millisPerHour, "EAT"),
  256.         // MET is the ID for Middle East Time time zone.
  257.         // ??? Apparently, Iran switches on March 4 and September 4 at midnight.
  258.         // We can't express that yet, so no daylight savings time for now.
  259.         new SimpleTimeZone((int)(3.5 * millisPerHour), "MET"),
  260.         // NET is the ID for Near East Time time zone.
  261.         new SimpleTimeZone(4 * millisPerHour, "NET"),
  262.         // PLT is the ID for Pakistan Lahore Time time zone.
  263.         new SimpleTimeZone(5 * millisPerHour, "PLT"),
  264.         // IST is the ID for India Standard Time time zone.
  265.         new SimpleTimeZone((int)(5.5 * millisPerHour), "IST"),
  266.         // BST is the ID for Bangladesh Standard Time time zone.
  267.         new SimpleTimeZone(6 * millisPerHour, "BST"),
  268.         // VST is the ID for Vietnam Standard Time time zone.
  269.         new SimpleTimeZone(7 * millisPerHour, "VST"),
  270.         // CTT is the ID for China Taiwan Time time zone.
  271.         new SimpleTimeZone(8 * millisPerHour, "CTT"),
  272.         // JST is the ID for Japan Standard Time time zone.
  273.         new SimpleTimeZone(9 * millisPerHour, "JST"),
  274.         // ACT is the ID for Australia Central Time time zone.
  275.         new SimpleTimeZone((int)(9.5 * millisPerHour), "ACT",
  276.                 Calendar.OCTOBER, -1, Calendar.SUNDAY, 2 * millisPerHour,
  277.                 Calendar.MARCH, -1, Calendar.SUNDAY, 3 * millisPerHour),
  278.         // AET is the ID for Australia Eastern Time time zone.
  279.         new SimpleTimeZone(10 * millisPerHour, "AET",
  280.                 Calendar.OCTOBER, -1, Calendar.SUNDAY, 2 * millisPerHour,
  281.                 Calendar.MARCH, -1, Calendar.SUNDAY, 3 * millisPerHour),
  282.         // SST is the ID for Solomon Standard Time time zone.
  283.         new SimpleTimeZone(11 * millisPerHour, "SST"),
  284.         // NST is the ID for New Zealand Standard Time time zone.
  285.         new SimpleTimeZone(12 * millisPerHour, "NST",
  286.                 Calendar.OCTOBER, 1, Calendar.SUNDAY, 2 * millisPerHour,
  287.                 Calendar.MARCH, 3, Calendar.SUNDAY, 3 * millisPerHour),
  288.         // MIT is the ID for Midway Islands Time time zone.
  289.         new SimpleTimeZone(-11 * millisPerHour, "MIT"),
  290.         // HST is the ID for Hawaii Standard Time time zone.
  291.         new SimpleTimeZone(-10 * millisPerHour, "HST"),
  292.         // AST is the ID for Alaska Standard Time time zone.
  293.         new SimpleTimeZone(-9 * millisPerHour, "AST",
  294.                 Calendar.APRIL, 1, Calendar.SUNDAY, 2 * millisPerHour,
  295.                 Calendar.OCTOBER, -1, Calendar.SUNDAY, 2 * millisPerHour),
  296.         // PST is the ID for Pacific Standard Time time zone.
  297.         new SimpleTimeZone(-8 * millisPerHour, "PST",
  298.                 Calendar.APRIL, 1, Calendar.SUNDAY, 2 * millisPerHour,
  299.                 Calendar.OCTOBER, -1, Calendar.SUNDAY, 2 * millisPerHour),
  300.         // PNT is the ID for Phoenix Standard Time time zone.
  301.         new SimpleTimeZone(-7 * millisPerHour, "PNT"),
  302.         // MST is the ID for Mountain Standard Time time zone.
  303.         new SimpleTimeZone(-7 * millisPerHour, "MST",
  304.                 Calendar.APRIL, 1, Calendar.SUNDAY, 2 * millisPerHour,
  305.                 Calendar.OCTOBER, -1, Calendar.SUNDAY, 2 * millisPerHour),
  306.         // CST is the ID for Central Standard Time time zone.
  307.         new SimpleTimeZone(-6 * millisPerHour, "CST",
  308.                 Calendar.APRIL, 1, Calendar.SUNDAY, 2 * millisPerHour,
  309.                 Calendar.OCTOBER, -1, Calendar.SUNDAY, 2 * millisPerHour),
  310.         // EST is the ID for Eastern Standard Time time zone.
  311.         new SimpleTimeZone(-5 * millisPerHour, "EST",
  312.                 Calendar.APRIL, 1, Calendar.SUNDAY, 2 * millisPerHour,
  313.                 Calendar.OCTOBER, -1, Calendar.SUNDAY, 2 * millisPerHour),
  314.         // IET is the ID for Indiana Eastern Standard Time time zone.
  315.         new SimpleTimeZone(-5 * millisPerHour, "IET"),
  316.         // PRT is the ID for Puerto Rico and US Virgin Islands Time time zone.
  317.         new SimpleTimeZone(-4 * millisPerHour, "PRT"),
  318.         // CNT is the ID for Canada Newfoundland Time time zone.
  319.         new SimpleTimeZone((int)(-3.5 * millisPerHour), "CNT",
  320.                 Calendar.APRIL, 1, Calendar.SUNDAY, 2 * millisPerHour,
  321.                 Calendar.OCTOBER, -1, Calendar.SUNDAY, 2 * millisPerHour),
  322.         // AGT is the ID for Argentina Standard Time time zone.
  323.         new SimpleTimeZone(-3 * millisPerHour, "AGT"),
  324.         // BET is the ID for Brazil Eastern Time time zone.
  325.         // ??? The correct rule apparently is the first Sunday >= Feb 11,
  326.         // but we don't support that yet. 3rd Sunday works for 1997/1998.
  327.         new SimpleTimeZone(-3 * millisPerHour, "BET",
  328.                 Calendar.OCTOBER, 1, Calendar.SUNDAY, 0 * millisPerHour,
  329.                 Calendar.FEBRUARY, 3, Calendar.SUNDAY, 0 * millisPerHour),
  330.         // CAT is the ID for Central African Time time zone.
  331.         // ??? GMT-1 for Central Africa? Or is this the Azores?
  332.         new SimpleTimeZone(-1 * millisPerHour, "CAT")
  333.     };
  334.  
  335.     private static Hashtable lookup = new Hashtable(timeZoneData.length);
  336.     static {
  337.         for (int i = 0; i < timeZoneData.length; ++i)
  338.             lookup.put(timeZoneData[i].getID(), timeZoneData[i]);
  339.     }
  340. }
  341.