[Mulgara-svn] r1265 - trunk/src/jar/util/java/org/mulgara/util

pag at mulgara.org pag at mulgara.org
Tue Sep 23 04:30:13 UTC 2008


Author: pag
Date: 2008-09-22 21:30:12 -0700 (Mon, 22 Sep 2008)
New Revision: 1265

Modified:
   trunk/src/jar/util/java/org/mulgara/util/Timezone.java
Log:
Added methods to convert to java.util.TimeZone and to output as a string

Modified: trunk/src/jar/util/java/org/mulgara/util/Timezone.java
===================================================================
--- trunk/src/jar/util/java/org/mulgara/util/Timezone.java	2008-09-23 04:28:20 UTC (rev 1264)
+++ trunk/src/jar/util/java/org/mulgara/util/Timezone.java	2008-09-23 04:30:12 UTC (rev 1265)
@@ -15,6 +15,7 @@
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
+import java.util.TimeZone;
 
 /**
  * Maps from a known hour:minute offset for a timezone into a 6 bit code, and back.
@@ -29,6 +30,9 @@
   /** A mask for restricting bit patterns to a byte (removing sign extension) */
   private static final int BYTE_MASK = 0xFF;
 
+  /** The name of the timezone for Zulu time 00:00 */
+  private static final String ZULU_NAME = "UTC";
+
   /** The construct that holds the offset details */
   private HourMinute hm;
 
@@ -60,6 +64,14 @@
     internalCode = c;
   }
 
+  /**
+   * A convenience factory method to construct a ZULU timezone.
+   * @return a Timezone for ZULU time.
+   */
+  public static Timezone newZuluTimezone() {
+    return new Timezone(ZULU_CODE);
+  }
+
   /** Gets the hour for this timezone. */
   public int getHour() {
     return hm.hour;
@@ -75,6 +87,23 @@
     return (byte)(internalCode << 2);
   }
 
+  /** Indicates if this code represents Zulu. */
+  public boolean isZulu() {
+    return getCode() == ZULU_CODE;
+  }
+
+  /** @see java.lang.Object#toString() */
+  public String toString() {
+    return hm == ZULU ? "Z" : hm.toString();
+  }
+
+  /** Convert to a {@linkplain java.util.TimeZone} */
+  public TimeZone asJavaTimeZone() {
+    TimeZone tz = TimeZone.getTimeZone(ZULU_NAME);
+    tz.setRawOffset(hm.getOffset());
+    return tz;
+  }
+
   /** Gets the code for the ZULU timezone. */
   public static byte getZuluCode() {
     return ZULU_CODE;
@@ -114,8 +143,16 @@
    * A private structure for associating an hour and minute together.
    */
   private static class HourMinute {
+
+    /** Number of milliseconds in a minute */
+    private static final int MINUTE_MILLIS = 60 * 1000;
+
+    /** Number of milliseconds in an hour */
+    private static final int HOUR_MILLIS = 60 * MINUTE_MILLIS;
+
     /** The hour value. */
     public final int hour;
+
     /** The minute value. */
     public final int minute;
 
@@ -125,6 +162,11 @@
       minute = m;
     }
 
+    /** Calculates a millisecond offset for this hour/minute pair */
+    public int getOffset() {
+      return hour * HOUR_MILLIS + minute * MINUTE_MILLIS;
+    }
+
     /** @inheritDoc */
     public int hashCode() {
       return (hour * 4) + (minute / 15);
@@ -137,8 +179,8 @@
 
     /** @inheritDoc */
     public String toString() {
-      if (hour == 0 && minute == 0) return "00:00";
-      return String.format("%02d:%02d", hour, minute);
+      if (hour == 0 && minute == 0) return "+00:00";
+      return String.format("%+03d:%02d", hour, minute);
     }
   }
 




More information about the Mulgara-svn mailing list