This is the mail archive of the java-patches@sourceware.cygnus.com mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Patch: Date.toString()


I'm checking in the appended patch (plus I'm removing natDate.cc,
which isn't shown in the patch).  This changes Date.toString() to be
correct according to the JCL book; the current implementation has at
least two bugs (and is hard to fix in its current form).  This fixes
PR java.util/47.

1999-09-24  Tom Tromey  <tromey@cygnus.com>

	Fix for PR java.util/47:
	* configure, include/config.h: Rebuilt.
	* configure.in: Don't look for ctime or ctime_r.
	* Makefile.in: Rebuilt.
	* Makefile.am (nat_source_files): Don't mention natDate.cc.
	* java/util/natDate.cc: Removed.
	* java/util/TimeZone.java (tzIDs, rawOffsets, timeZones): New
	static fields.
	(getAvailableIDs): Rewrote.
	(getTimeZone): Rewrote.
	* java/util/Date.java (toGMTString): New method.
	(toLocaleString): New method.
	(toString): Rewrote.

Tom

Index: Makefile.am
===================================================================
RCS file: /cvs/java/libgcj/libjava/Makefile.am,v
retrieving revision 1.31
diff -u -r1.31 Makefile.am
--- Makefile.am	1999/09/10 22:03:04	1.31
+++ Makefile.am	1999/09/24 18:59:56
@@ -802,7 +802,6 @@
 java/net/natPlainDatagramSocketImpl.cc \
 java/net/natPlainSocketImpl.cc \
 java/text/natCollator.cc \
-java/util/natDate.cc \
 java/util/natGregorianCalendar.cc \
 java/util/zip/natDeflater.cc \
 java/util/zip/natInflater.cc
Index: configure.in
===================================================================
RCS file: /cvs/java/libgcj/libjava/configure.in,v
retrieving revision 1.32
diff -u -r1.32 configure.in
--- configure.in	1999/09/23 19:38:11	1.32
+++ configure.in	1999/09/24 19:00:06
@@ -282,7 +282,6 @@
    AC_DEFINE(HAVE_MEMMOVE)
    AC_DEFINE(HAVE_MEMCPY)
    AC_DEFINE(HAVE_STRERROR)
-   AC_DEFINE(HAVE_CTIME_R)
    AC_DEFINE(HAVE_GMTIME_R)
    AC_DEFINE(HAVE_LOCALTIME_R)
    dnl This is only for POSIX threads.
@@ -303,7 +302,6 @@
    fi
 else
    AC_CHECK_FUNCS(strerror ioctl select fstat open fsync sleep)
-   AC_CHECK_FUNCS(ctime_r ctime, break)
    AC_CHECK_FUNCS(gmtime_r localtime_r readdir_r getpwuid_r)
    AC_CHECK_FUNCS(access stat mkdir rename rmdir unlink realpath)
    AC_CHECK_FUNCS(inet_aton inet_addr, break)
Index: java/util/Date.java
===================================================================
RCS file: /cvs/java/libgcj/libjava/java/util/Date.java,v
retrieving revision 1.1.1.1
diff -u -r1.1.1.1 Date.java
--- Date.java	1999/04/07 14:52:41	1.1.1.1
+++ Date.java	1999/09/24 19:00:07
@@ -445,10 +445,33 @@
 	      + cal.get(Calendar.DST_OFFSET)/(60*1000));
   }
 
-  public native String toString ();
+  public String toString ()
+  {
+    // This is slow, but does it matter?  There is no particularly
+    // fast way to do it, because we need the timezone offset, which
+    // we don't store.  Unix ctime() doesn't provide this information.
+    SimpleDateFormat fmt = new SimpleDateFormat ("E MMM dd HH:mm:ss z yyyy",
+						 Locale.US);
+    fmt.setTimeZone(TimeZone.getDefault());
+    return fmt.format(this);
+  }
 
-  // TODO: toLocaleString
-  // TODO: toGMTString
+  public String toGMTString ()
+  {
+    // This method is deprecated.  We don't care if it is very slow.
+    SimpleDateFormat fmt = new SimpleDateFormat ("d MMM yyyy HH:mm:ss 'GMT'",
+						 Locale.US);
+    fmt.setTimeZone(TimeZone.zoneGMT);
+    return fmt.format(this);
+  }
+
+  public String toLocaleString ()
+  {
+    // This method is deprecated.  We don't care if it is very slow.
+    DateFormat fmt = DateFormat.getDateTimeInstance();
+    fmt.setTimeZone(TimeZone.getDefault());
+    return fmt.format(this);
+  }
 
   public static long UTC (int year, int month, int date,
 			  int hours, int minutes, int seconds)
Index: java/util/TimeZone.java
===================================================================
RCS file: /cvs/java/libgcj/libjava/java/util/TimeZone.java,v
retrieving revision 1.1.1.1
diff -u -r1.1.1.1 TimeZone.java
--- TimeZone.java	1999/04/07 14:52:41	1.1.1.1
+++ TimeZone.java	1999/09/24 19:00:07
@@ -68,21 +68,57 @@
 
   public abstract boolean inDaylightTime (Date date);
 
-  public static TimeZone getTimeZone (String ID)
+  public static synchronized TimeZone getTimeZone (String ID)
   {
-    return zoneGMT;  // FIXME
+    int i;
+    for (i = 0; i < tzIDs.length; ++i)
+      {
+	if (ID.equals(tzIDs[i]))
+	  break;
+      }
+    if (i == tzIDs.length)
+      return null;
+
+    if (timeZones[i] == null)
+      {
+	if (ID.equals("GMT"))
+	  timeZones[i] = zoneGMT;
+	else
+	  timeZones[i] = new SimpleTimeZone (rawOffsets[i], tzIDs[i]);
+      }
+
+    return timeZones[i];
   }
 
   public static String[] getAvailableIDs()
-  { // FIXME - only knows about GMT
-    String[] zones = new String[1];
-    zones[0] = "GMT";
-    return zones;
+  {
+    return (String[]) tzIDs.clone();
   }
 
   public static String[] getAvailableIDs(int rawOffset)
   {
-    return rawOffset == 0 ? getAvailableIDs() : new String[0];  // FIXME
+    int first, last;
+
+    for (first = 0; first < rawOffsets.length; ++first)
+      {
+	if (rawOffset == rawOffsets[first])
+	  break;
+      }
+    if (first == rawOffsets.length)
+      return new String[0];
+    for (last = first + 1; last < rawOffsets.length; ++last)
+      {
+	if (rawOffset != rawOffsets[last])
+	  break;
+      }
+
+    String[] r = new String[last - first];
+    for (int i = first; i < last; ++i)
+      {
+	r[i - first] = tzIDs[i];
+      }
+
+    return r;
   }
 
   private static synchronized TimeZone setDefault()
@@ -117,4 +153,31 @@
   }
 
   // public Object clone ();
+
+  // Names of timezones.  This array is kept in parallel with
+  // rawOffsets.  This list comes from the JCL 1.1 book.
+  private static final String[] tzIDs =
+  {
+    "MIT", "HST", "AST", "PST", "PNT",
+    "MST", "CST", "EST", "IET", "PRT",
+    "CNT", "AGT", "BET", "CAT", "GMT",
+    "ECT", "EET", "ART", "EAT", "MET",
+    "NET", "PLT", "IST", "BST", "VST",
+    "CTT", "JST", "ACT", "AET", "SST",
+    "NST"
+  };
+  // This holds raw offsets in milliseconds.
+  // 3600000 == 60 * 60 * 1000
+  private static final int[] rawOffsets =
+  {
+    -11 * 3600000, -10 * 3600000, -9 * 3600000, -8 * 3600000, -7 * 3600000,
+    -7 * 3600000, -6 * 3600000, -5 * 3600000, -5 * 3600000, -4 * 3600000,
+    -35 * 360000, -3 * 3600000, -3 * 3600000, -1 * 3600000, 0,
+    1 * 3600000, 1 * 3600000, 2 * 3600000, 3 * 3600000, 35 * 360000,
+    4 * 3600000, 5 * 3600000, 55 * 360000, 6 * 3600000, 7 * 3600000,
+    8 * 3600000, 9 * 3600000, 95 * 360000, 10 * 3600000, 11 * 3600000,
+    12 * 3600000
+  };
+  // This caches all the corresponding zone objects.
+  private static TimeZone[] timeZones = new TimeZone[tzIDs.length];
 }

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]