* intrinsic.texi: Various minor cleanups.
[official-gcc.git] / libjava / java / util / natVMTimeZone.cc
blobd45ebf97853f5ba2fb2ba5450a5e097c7af6a739
1 // natVMTimeZone.cc -- Native side of VMTimeZone class.
3 /* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2006
4 Free Software Foundation
6 This file is part of libgcj.
8 This software is copyrighted work licensed under the terms of the
9 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
10 details. */
12 #include <config.h>
13 #include <platform.h>
15 #include <gcj/cni.h>
16 #include <jvm.h>
18 #include <java/util/VMTimeZone.h>
19 #include <java/lang/Character.h>
20 #include <java/lang/Integer.h>
22 #include <stdio.h>
24 #if TIME_WITH_SYS_TIME
25 # include <sys/time.h>
26 # include <time.h>
27 #else
28 # if HAVE_SYS_TIME_H
29 # include <sys/time.h>
30 # else
31 # include <time.h>
32 # endif
33 #endif
35 #include <string.h>
37 /**
38 * This method returns a time zone id string which is in the form
39 * (standard zone name) or (standard zone name)(GMT offset) or
40 * (standard zone name)(GMT offset)(daylight time zone name). The
41 * GMT offset can be in seconds, or where it is evenly divisible by
42 * 3600, then it can be in hours. The offset must be the time to
43 * add to the local time to get GMT. If a offset is given and the
44 * time zone observes daylight saving then the (daylight time zone
45 * name) must also be given (otherwise it is assumed the time zone
46 * does not observe any daylight savings).
47 * <p>
48 * The result of this method is given to getDefaultTimeZone(String)
49 * which tries to map the time zone id to a known TimeZone. See
50 * that method on how the returned String is mapped to a real
51 * TimeZone object.
53 jstring
54 java::util::VMTimeZone::getSystemTimeZoneId()
56 struct tm tim;
57 #if !defined(HAVE_LOCALTIME_R) || !defined(_POSIX_PTHREAD_SEMANTICS)
58 struct tm *lt_tim;
59 #endif
60 #ifdef HAVE_TM_ZONE
61 int month;
62 #endif
63 time_t current_time;
64 long tzoffset;
65 const char *tz1, *tz2;
66 char *tzid;
68 time(&current_time);
69 #if defined(HAVE_LOCALTIME_R) && defined(_POSIX_PTHREAD_SEMANTICS)
70 localtime_r(&current_time, &tim);
71 #else
72 /* Fall back on non-thread safe localtime. */
73 lt_tim = localtime(&current_time);
74 memcpy(&tim, lt_tim, sizeof (struct tm));
75 #endif
76 mktime(&tim);
78 #ifdef HAVE_TM_ZONE
79 /* We will cycle through the months to make sure we hit dst. */
80 month = tim.tm_mon;
81 tz1 = tz2 = NULL;
82 while (tz1 == NULL || tz2 == NULL)
84 if (tim.tm_isdst > 0)
85 tz2 = tim.tm_zone;
86 else if (tz1 == NULL)
88 tz1 = tim.tm_zone;
89 month = tim.tm_mon;
92 if (tz1 == NULL || tz2 == NULL)
94 tim.tm_mon++;
95 tim.tm_mon %= 12;
98 if (tim.tm_mon == month && tz2 == NULL)
99 tz2 = "";
100 else
101 mktime(&tim);
103 /* We want to make sure the tm struct we use later on is not dst. */
104 tim.tm_mon = month;
105 mktime(&tim);
106 #elif defined (HAVE_TZNAME)
107 /* If dst is never used, tzname[1] is the empty string. */
108 tzset();
109 tz1 = tzname[0];
110 tz2 = tzname[1];
111 #else
112 /* Some targets have no concept of timezones. Assume GMT without dst. */
113 tz1 = "GMT";
114 tz2 = "";
115 #endif
117 #ifdef STRUCT_TM_HAS_GMTOFF
118 /* tm_gmtoff is the number of seconds that you must add to GMT to get
119 local time, we need the number of seconds to add to the local time
120 to get GMT. */
121 tzoffset = -1L * tim.tm_gmtoff;
122 #elif HAVE_UNDERSCORE_TIMEZONE
123 tzoffset = _timezone;
124 #elif HAVE_TIMEZONE
125 /* timezone is secs WEST of UTC. */
126 tzoffset = timezone;
127 #else
128 /* FIXME: there must be another global if neither tm_gmtoff nor timezone
129 is available, esp. if tzname is valid.
130 Richard Earnshaw <rearnsha@arm.com> has suggested using difftime to
131 calculate between gmtime and localtime (and accounting for possible
132 daylight savings time) as an alternative. */
133 tzoffset = 0L;
134 #endif
136 if ((tzoffset % 3600) == 0)
137 tzoffset = tzoffset / 3600;
139 tzid = (char*) _Jv_Malloc (strlen(tz1) + strlen(tz2) + 6);
140 sprintf(tzid, "%s%ld%s", tz1, tzoffset, tz2);
141 jstring retval = JvNewStringUTF (tzid);
142 _Jv_Free (tzid);
144 return retval;