aboutsummaryrefslogtreecommitdiff
path: root/libjava/java/util
diff options
context:
space:
mode:
authorTom Tromey <tromey@redhat.com>2002-05-13 20:10:37 +0000
committerTom Tromey <tromey@gcc.gnu.org>2002-05-13 20:10:37 +0000
commit86397868159ba8c7ba20d56fdf49f551a512a1f1 (patch)
tree8d55a2017b3cd999dc25fd24a3e201558e07756e /libjava/java/util
parent16133d00586de86b5d77c3b5f5833c6485ca697c (diff)
downloadgcc-86397868159ba8c7ba20d56fdf49f551a512a1f1.zip
gcc-86397868159ba8c7ba20d56fdf49f551a512a1f1.tar.gz
gcc-86397868159ba8c7ba20d56fdf49f551a512a1f1.tar.bz2
re PR libgcj/6389 (System.getProperty("") should always throw an IllegalArgumentException)
Fixes PR libgcj/6389: * Makefile.in: Rebuilt. * Makefile.am (nat_source_files): Added natTimeZone.cc. * java/util/natTimeZone.cc: New file. * java/util/TimeZone.java (getDefaultTimeZoneId): New method. * java/lang/System.java: Merged with Classpath. * java/lang/Runtime.java: Merged with Classpath. * java/lang/natSystem.cc (setErr0): Renamed from setErr; don't run security check. (setIn0): Renamed from setIn; don't run security check. (setOut0): Renamed from setOut; don't run security check. (file_encoding, getpwuid_adaptor, getSystemTimeZone, init_properties): Moved to natRuntime.cc. Moved many includes to natRuntime.cc. (isWordsBigEndian): New method. * java/lang/natRuntime.cc: Include Long.h, also other includes previously in natSystem.cc. (maxMemory): New function. (exitInternal): Renamed from `_exit'. (exit): Removed. (init): Don't set finalize_on_exit. (exitInternal): Use `finalizeOnExit'. (file_encoding, getpwuid_adaptor): New functions from natSystem.cc. (insertSystemProperties): New method, renamed from System::init_properties. Don't set user.timezone. (_load): Don't call checkLink. (execInternal): New method. (availableProcessors): Likewise. (nativeGetLibname): Likewise. From-SVN: r53429
Diffstat (limited to 'libjava/java/util')
-rw-r--r--libjava/java/util/TimeZone.java23
-rw-r--r--libjava/java/util/natTimeZone.cc154
2 files changed, 174 insertions, 3 deletions
diff --git a/libjava/java/util/TimeZone.java b/libjava/java/util/TimeZone.java
index 974065a..eba2236 100644
--- a/libjava/java/util/TimeZone.java
+++ b/libjava/java/util/TimeZone.java
@@ -1,5 +1,5 @@
/* java.util.TimeZone
- Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
+ Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
This file is part of GNU Classpath.
@@ -38,6 +38,7 @@ exception statement from your version. */
package java.util;
import java.text.DateFormatSymbols;
+import gnu.classpath.Configuration;
/**
* This class represents a time zone offset and handles daylight savings.
@@ -753,16 +754,32 @@ public abstract class TimeZone implements java.io.Serializable, Cloneable
/* Look up default timezone */
static
{
- // System.loadLibrary("javautil");
-
+ if (Configuration.INIT_LOAD_LIBRARY)
+ {
+ System.loadLibrary("javautil");
+ }
String tzid = System.getProperty("user.timezone");
if (tzid == null)
+ tzid = getDefaultTimeZoneId();
+
+ if (tzid == null)
tzid = "GMT";
defaultZone = getTimeZone(tzid);
}
+ /* This method returns us a time zone id string which is in the
+ form <standard zone name><GMT offset><daylight time zone name>.
+ The GMT offset is in seconds, except where it is evenly divisible
+ by 3600, then it is in hours. If the zone does not observe
+ daylight time, then the daylight zone name is omitted. Examples:
+ in Chicago, the timezone would be CST6CDT. In Indianapolis
+ (which does not have Daylight Savings Time) the string would
+ be EST5
+ */
+ private static native String getDefaultTimeZoneId();
+
/**
* Gets the time zone offset, for current date, modified in case of
* daylight savings. This is the offset to add to UTC to get the local
diff --git a/libjava/java/util/natTimeZone.cc b/libjava/java/util/natTimeZone.cc
new file mode 100644
index 0000000..007f689
--- /dev/null
+++ b/libjava/java/util/natTimeZone.cc
@@ -0,0 +1,154 @@
+// natTimeZone.cc -- Native side of TimeZone class.
+
+/* Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation
+
+ This file is part of libgcj.
+
+This software is copyrighted work licensed under the terms of the
+Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
+details. */
+
+#include <config.h>
+
+#include <gcj/cni.h>
+#include <jvm.h>
+
+#include <java/util/TimeZone.h>
+#include <java/lang/Character.h>
+#include <java/lang/Integer.h>
+
+/*
+ * This method returns a time zone string that is used by init_properties
+ * to set the default timezone property 'user.timezone'. That value is
+ * used by default as a key into the timezone table used by the
+ * java::util::TimeZone class.
+ */
+static jstring
+getSystemTimeZone (void)
+{
+ struct tm *tim;
+ time_t current_time;
+ long tzoffset;
+ const char *tz1, *tz2;
+ char *tzid;
+
+ current_time = time(0);
+
+ mktime(tim = localtime(&current_time));
+#ifdef STRUCT_TM_HAS_GMTOFF
+ // tm_gmtoff is secs EAST of UTC.
+ tzoffset = -(tim->tm_gmtoff) + tim->tm_isdst * 3600L;
+#elif HAVE_UNDERSCORE_TIMEZONE
+ tzoffset = _timezone;
+#elif HAVE_TIMEZONE
+ // timezone is secs WEST of UTC.
+ tzoffset = timezone;
+#else
+ // FIXME: there must be another global if neither tm_gmtoff nor timezone
+ // is available, esp. if tzname is valid.
+ // Richard Earnshaw <rearnsha@arm.com> has suggested using difftime to
+ // calculate between gmtime and localtime (and accounting for possible
+ // daylight savings time) as an alternative.
+ tzoffset = 0L;
+#endif
+
+#ifdef HAVE_TM_ZONE
+ tz1 = tim->tm_zone;
+ tz2 = "";
+#elif defined (HAVE_TZNAME)
+ tz1 = tzname[0];
+ tz2 = strcmp (tzname[0], tzname[1]) ? tzname[1] : "";
+#else
+ // Some targets have no concept of timezones.
+ tz1 = "???";
+ tz2 = tz1;
+#endif
+
+ if ((tzoffset % 3600) == 0)
+ tzoffset = tzoffset / 3600;
+
+ tzid = (char*) _Jv_Malloc (strlen(tz1) + strlen(tz2) + 6);
+ sprintf(tzid, "%s%ld%s", tz1, tzoffset, tz2);
+ jstring retval = JvNewStringUTF (tzid);
+ _Jv_Free (tzid);
+
+ return retval;
+}
+
+// Get the System Timezone as reported by the OS. It should be in
+// the form PST8PDT so we'll need to parse it and check that it's valid.
+// FIXME: Using the code from Classpath for generating the System
+// Timezone IMO is suboptimal because it ignores whether the rules for
+// DST match up.
+jstring
+java::util::TimeZone::getDefaultTimeZoneId ()
+{
+ jstring sysTimeZoneId = getSystemTimeZone ();
+
+ using namespace java::lang;
+
+ // Check if this is a valid timezone. Make sure the IDs match
+ // since getTimeZone returns GMT if no match is found.
+ TimeZone *tz = TimeZone::getTimeZone (sysTimeZoneId);
+ if (tz->getID ()->equals (sysTimeZoneId))
+ return sysTimeZoneId;
+
+ // Check if the base part of sysTimeZoneId is a valid timezone that
+ // matches with daylight usage and rawOffset. Make sure the IDs match
+ // since getTimeZone returns GMT if no match is found.
+ // First find start of GMT offset info and any Daylight zone name.
+ int startGMToffset = 0;
+ int sysTimeZoneIdLength = sysTimeZoneId->length();
+ for (int i = 0; i < sysTimeZoneIdLength && startGMToffset == 0; i++)
+ {
+ if (Character::isDigit (sysTimeZoneId->charAt (i)))
+ startGMToffset = i;
+ }
+
+ int startDaylightZoneName = 0;
+ jboolean usesDaylight = false;
+ for (int i = sysTimeZoneIdLength - 1;
+ i >= 0 && !Character::isDigit (sysTimeZoneId->charAt (i)); --i)
+ {
+ startDaylightZoneName = i;
+ }
+ if (startDaylightZoneName > 0)
+ usesDaylight = true;
+
+ int GMToffset
+ = Integer::parseInt (startDaylightZoneName == 0 ?
+ sysTimeZoneId->substring (startGMToffset) :
+ sysTimeZoneId->substring (startGMToffset,
+ startDaylightZoneName));
+
+ // Offset could be in hours or seconds. Convert to millis.
+ if (GMToffset < 24)
+ GMToffset *= 60 * 60;
+ GMToffset *= -1000;
+
+ jstring tzBasename = sysTimeZoneId->substring (0, startGMToffset);
+ tz = TimeZone::getTimeZone (tzBasename);
+ if (tz->getID ()->equals (tzBasename) && tz->getRawOffset () == GMToffset)
+ {
+ jboolean tzUsesDaylight = tz->useDaylightTime ();
+ if (usesDaylight && tzUsesDaylight || !usesDaylight && !tzUsesDaylight)
+ return tzBasename;
+ }
+
+ // If no match, see if a valid timezone has the same attributes as this
+ // and then use it instead.
+ jstringArray IDs = TimeZone::getAvailableIDs (GMToffset);
+ jstring *elts = elements (IDs);
+ for (int i = 0; i < IDs->length; ++i)
+ {
+ // FIXME: The daylight savings rules may not match the rules
+ // for the desired zone.
+ jboolean IDusesDaylight =
+ TimeZone::getTimeZone (elts[i])->useDaylightTime ();
+ if (usesDaylight && IDusesDaylight || !usesDaylight && !IDusesDaylight)
+ return elts[i];
+ }
+
+ // If all else fails, return null.
+ return NULL;
+}