home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 1998 February / VPR9802A.ISO / APP_DEMO / VC / MAIN.BIN / TimeZone.java < prev    next >
Text File  |  1997-10-27  |  14KB  |  346 lines

  1. /*
  2.  * @(#)TimeZone.java    1.17 97/01/29
  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.17 01/29/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.
  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 new SimpleTimeZone(-8*millisPerHour, "PST",
  140.             Calendar.APRIL, 1, Calendar.SUNDAY, 2*millisPerHour,
  141.             Calendar.OCTOBER, -1, Calendar.SUNDAY, 2*millisPerHour);
  142.         }
  143.     }
  144.  
  145.     /**
  146.      * Gets the available IDs according to the given time zone offset.
  147.      * @param rawOffset the given time zone GMT offset.
  148.      * @return an array of IDs, where the time zone for that ID has
  149.      * the specified GMT offset. For example, {"Phoenix", "Denver"},
  150.      * since both have GMT-07:00, but differ in daylight savings behavior.
  151.      */
  152.     public static synchronized String[] getAvailableIDs(int rawOffset) {
  153.         String[]    resultArray = new String[10]; // normally 2 ~ 3 IDs
  154.         int         count = 0;
  155.         for (int i = 0; i < timeZoneData.length; ++i)
  156.             if (rawOffset == timeZoneData[i].getRawOffset())
  157.                 resultArray[count++] = timeZoneData[i].getID();
  158.  
  159.         // copy into array of the right size and return
  160.         String[] finalResult = new String[count];
  161.         System.arraycopy(resultArray, 0, finalResult, 0, count);
  162.  
  163.         return finalResult;
  164.     }
  165.  
  166.     /**
  167.      * Gets all the available IDs supported.
  168.      * @return an array of IDs.
  169.      */
  170.     public static synchronized String[] getAvailableIDs() {
  171.         String[]    resultArray = new String[40];
  172.         int         count = 0;
  173.  
  174.         for (int i = 0; i < timeZoneData.length; ++i)
  175.             resultArray[count++] = timeZoneData[i].getID();
  176.  
  177.         // copy into array of the right size and return
  178.         String[] finalResult = new String[count];
  179.         System.arraycopy(resultArray, 0, finalResult, 0, count);
  180.  
  181.         return finalResult;
  182.     }
  183.  
  184.     /**
  185.      * Gets the default TimeZone for this host.
  186.      * @return a default TimeZone.
  187.      */
  188.     public static synchronized TimeZone getDefault() {
  189.         if (defaultZone == null) {
  190.             // get the ID from the system properties
  191.             String ID = System.getProperty("user.timezone", "GMT");
  192.             if (ID != null) {
  193.                 defaultZone = getTimeZone(ID);
  194.                 if (defaultZone != null)
  195.                     return defaultZone;
  196.             }
  197.  
  198.             // we couldn't get it from the properties, so use the
  199.             // offset from the native (host) system.
  200.             int rawOffset = -8*millisPerHour;
  201.  
  202.             // BRIAN FIXME
  203.             // reset rawOffset to be the system offset from GMT
  204.             String[] matches = getAvailableIDs(rawOffset);
  205.             if (matches != null) {
  206.                 defaultZone = getTimeZone(matches[0]);
  207.             }
  208.             if (defaultZone == null) {
  209.                 defaultZone = getTimeZone("PST");
  210.             }
  211.             return defaultZone;
  212.         }
  213.         else return defaultZone;
  214.     }
  215.  
  216.     /**
  217.      * Sets time zone to using the given TimeZone.
  218.      * @param zone the given time zone.
  219.      */
  220.     public static synchronized void setDefault(TimeZone zone)
  221.     {
  222.         defaultZone = zone;
  223.     }
  224.  
  225.     /**
  226.      * Overrides Cloneable
  227.      */
  228.     public Object clone()
  229.     {
  230.         try {
  231.             TimeZone other = (TimeZone) super.clone();
  232.             other.ID = ID;
  233.             return other;
  234.         } catch (CloneNotSupportedException e) {
  235.             throw new InternalError();
  236.         }
  237.     }
  238.  
  239.     // =======================privates===============================
  240.  
  241.     private String           ID;
  242.     private static TimeZone  defaultZone=null;
  243.     private static final int millisPerHour = 60*60*1000;
  244.  
  245.     private static final SimpleTimeZone[] timeZoneData = {
  246.         // GMT is the ID for Greenwich Mean Time time zone.
  247.         new SimpleTimeZone(0, "GMT",
  248.             Calendar.MARCH, -1, Calendar.SUNDAY, 2*millisPerHour,
  249.             Calendar.OCTOBER, 4, Calendar.SUNDAY, 2*millisPerHour),
  250.         // ECT is the ID for European Central Time time zone.
  251.         new SimpleTimeZone(1*millisPerHour, "ECT",
  252.             Calendar.MARCH, -1, Calendar.SUNDAY, 2*millisPerHour,
  253.             Calendar.SEPTEMBER, -1, Calendar.SUNDAY, 2*millisPerHour),
  254.         // EET is the ID for Eastern European Time time zone.
  255.         new SimpleTimeZone(2*millisPerHour, "EET",
  256.             Calendar.MARCH, -1, Calendar.SUNDAY, 2*millisPerHour,
  257.             Calendar.SEPTEMBER, -1, Calendar.SUNDAY, 2*millisPerHour),
  258.         // ART is the ID for (Arabic) Egypt Standard Time timezone.
  259.         new SimpleTimeZone(2*millisPerHour, "ART", // 5/1 ~ 10/1
  260.             Calendar.MAY, 1, Calendar.SUNDAY, 2*millisPerHour,
  261.             Calendar.OCTOBER, 1, Calendar.SUNDAY, 2*millisPerHour),
  262.         // EAT is the ID for Eastern African Time time zone.
  263.         new SimpleTimeZone(3*millisPerHour, "EAT"),
  264.         // MET is the ID for Middle East Time time zone.
  265.         new SimpleTimeZone((int)(3.5*millisPerHour), "MET"),
  266.         // NET is the ID for Near East Time time zone.
  267.         new SimpleTimeZone(4*millisPerHour, "NET"),
  268.         // PLT is the ID for Pakistan Lahore Time time zone.
  269.         new SimpleTimeZone(5*millisPerHour, "PLT"),
  270.         // IST is the ID for India Standard Time time zone.
  271.         new SimpleTimeZone((int)(5.5*millisPerHour), "IST"),
  272.         // BST is the ID for Bangladesh Standard Time time zone.
  273.         new SimpleTimeZone(6*millisPerHour, "BST"),
  274.         // VST is the ID for Vietnam Standard Time time zone.
  275.         new SimpleTimeZone(7*millisPerHour, "VST"),
  276.         // CTT is the ID for China Taiwan Time time zone.
  277.         new SimpleTimeZone(8*millisPerHour, "CTT"),
  278.         // JST is the ID for Japan Standard Time time zone.
  279.         new SimpleTimeZone(9*millisPerHour, "JST"),
  280.         // ACT is the ID for Australia Central Time time zone.
  281.         new SimpleTimeZone((int)(9.5*millisPerHour), "ACT",
  282.             Calendar.APRIL, 1, Calendar.SUNDAY, 2*millisPerHour,
  283.             Calendar.OCTOBER, -1, Calendar.SUNDAY, 2*millisPerHour),
  284.         // AET is the ID for Australia Eastern Time time zone.
  285.         new SimpleTimeZone(10*millisPerHour, "AET",
  286.             Calendar.APRIL, 1, Calendar.SUNDAY, 2*millisPerHour,
  287.             Calendar.OCTOBER, -1, Calendar.SUNDAY, 2*millisPerHour),
  288.         // SST is the ID for Solomon Standard Time time zone.
  289.         new SimpleTimeZone(11*millisPerHour, "SST"),
  290.         // NST is the ID for New Zealand Standard Time time zone.
  291.         new SimpleTimeZone(12*millisPerHour, "NST",
  292.             Calendar.APRIL, 1, Calendar.SUNDAY, 2*millisPerHour,
  293.             Calendar.OCTOBER, -1, Calendar.SUNDAY, 2*millisPerHour),
  294.         // MIT is the ID for Midway Islands Time time zone.
  295.         new SimpleTimeZone(-11*millisPerHour, "MIT"),
  296.         // HST is the ID for Hawaii Standard Time time zone.
  297.         new SimpleTimeZone(-10*millisPerHour, "HST",
  298.             Calendar.APRIL, 1, Calendar.SUNDAY, 2*millisPerHour,
  299.             Calendar.OCTOBER, -1, Calendar.SUNDAY, 2*millisPerHour),
  300.         // AST is the ID for Alaska Standard Time time zone.
  301.         new SimpleTimeZone(-9*millisPerHour, "AST",
  302.             Calendar.APRIL, 1, Calendar.SUNDAY, 2*millisPerHour,
  303.             Calendar.OCTOBER, -1, Calendar.SUNDAY, 2*millisPerHour),
  304.         // PST is the ID for Pacific Standard Time time zone.
  305.         new SimpleTimeZone(-8*millisPerHour, "PST",
  306.             Calendar.APRIL, 1, Calendar.SUNDAY, 2*millisPerHour,
  307.             Calendar.OCTOBER, -1, Calendar.SUNDAY, 2*millisPerHour),
  308.         // PNT is the ID for Phoenix Standard Time time zone.
  309.         new SimpleTimeZone(-7*millisPerHour, "PNT"),
  310.         // MST is the ID for Mountain Standard Time time zone.
  311.         new SimpleTimeZone(-7*millisPerHour, "MST",
  312.             Calendar.APRIL, 1, Calendar.SUNDAY, 2*millisPerHour,
  313.             Calendar.OCTOBER, -1, Calendar.SUNDAY, 2*millisPerHour),
  314.         // CST is the ID for Central Standard Time time zone.
  315.         new SimpleTimeZone(-6*millisPerHour, "CST",
  316.             Calendar.APRIL, 1, Calendar.SUNDAY, 2*millisPerHour,
  317.             Calendar.OCTOBER, -1, Calendar.SUNDAY, 2*millisPerHour),
  318.         // EST is the ID for Eastern Standard Time time zone.
  319.         new SimpleTimeZone(-5*millisPerHour, "EST",
  320.             Calendar.APRIL, 1, Calendar.SUNDAY, 2*millisPerHour,
  321.             Calendar.OCTOBER, -1, Calendar.SUNDAY, 2*millisPerHour),
  322.         // IET is the ID for Indiana Eastern Standard Time time zone.
  323.         new SimpleTimeZone(-5*millisPerHour, "IET"),
  324.         // PRT is the ID for Puerto Rico and US Virgin Islands Time time zone.
  325.         new SimpleTimeZone(-4*millisPerHour, "PRT"),
  326.         // CNT is the ID for Canada Newfoundland Time time zone.
  327.         new SimpleTimeZone((int)(-3.5*millisPerHour), "CNT",
  328.             Calendar.APRIL, 1, Calendar.SUNDAY, 2*millisPerHour,
  329.             Calendar.OCTOBER, -1, Calendar.SUNDAY, 2*millisPerHour),
  330.         // AGT is the ID for Argentina Standard Time time zone.
  331.         new SimpleTimeZone(-3*millisPerHour, "AGT"),
  332.         // BET is the ID for Brazil Eastern Time time zone.
  333.         new SimpleTimeZone(-3*millisPerHour, "BET",
  334.             Calendar.APRIL, 1, Calendar.SUNDAY, 2*millisPerHour,
  335.             Calendar.OCTOBER, -1, Calendar.SUNDAY, 2*millisPerHour),
  336.         // CAT is the ID for Central African Time time zone.
  337.         new SimpleTimeZone(-1*millisPerHour, "CAT")
  338.     };
  339.  
  340.     private static Hashtable lookup = new Hashtable(timeZoneData.length);
  341.     static {
  342.         for (int i = 0; i < timeZoneData.length; ++i)
  343.             lookup.put(timeZoneData[i].getID(), timeZoneData[i]);
  344.     }
  345. }
  346.