56 |
|
|
57 |
#include "java_util_VMTimeZone.h" |
#include "java_util_VMTimeZone.h" |
58 |
|
|
59 |
static size_t jint_to_charbuf (char* bufend, jint num); |
static size_t jint_to_charbuf (char *bufend, jint num); |
60 |
|
|
61 |
/** |
/** |
62 |
* This method returns a time zone id string which is in the form |
* This method returns a time zone id string which is in the form |
75 |
* TimeZone object. |
* TimeZone object. |
76 |
*/ |
*/ |
77 |
JNIEXPORT jstring JNICALL |
JNIEXPORT jstring JNICALL |
78 |
Java_java_util_VMTimeZone_getSystemTimeZoneId(JNIEnv *env, |
Java_java_util_VMTimeZone_getSystemTimeZoneId (JNIEnv * env, |
79 |
jclass clazz |
jclass clazz |
80 |
__attribute__ ((__unused__))) |
__attribute__ ((__unused__))) |
81 |
{ |
{ |
82 |
struct tm tim; |
struct tm tim; |
83 |
#ifndef HAVE_LOCALTIME_R |
#ifndef HAVE_LOCALTIME_R |
94 |
char *tzid; |
char *tzid; |
95 |
jstring retval; |
jstring retval; |
96 |
|
|
97 |
time(¤t_time); |
time (¤t_time); |
98 |
#ifdef HAVE_LOCALTIME_R |
#ifdef HAVE_LOCALTIME_R |
99 |
localtime_r(¤t_time, &tim); |
localtime_r (¤t_time, &tim); |
100 |
#else |
#else |
101 |
/* Fall back on non-thread safe localtime. */ |
/* Fall back on non-thread safe localtime. */ |
102 |
lt_tim = localtime(¤t_time); |
lt_tim = localtime (¤t_time); |
103 |
memcpy(&tim, lt_tim, sizeof (struct tm)); |
memcpy (&tim, lt_tim, sizeof (struct tm)); |
104 |
#endif |
#endif |
105 |
mktime(&tim); |
mktime (&tim); |
106 |
|
|
107 |
#ifdef HAVE_STRUCT_TM_TM_ZONE |
#ifdef HAVE_STRUCT_TM_TM_ZONE |
108 |
/* We will cycle through the months to make sure we hit dst. */ |
/* We will cycle through the months to make sure we hit dst. */ |
111 |
while (tz1 == NULL || tz2 == NULL) |
while (tz1 == NULL || tz2 == NULL) |
112 |
{ |
{ |
113 |
if (tim.tm_isdst > 0) |
if (tim.tm_isdst > 0) |
114 |
tz2 = tim.tm_zone; |
tz2 = tim.tm_zone; |
115 |
else if (tz1 == NULL) |
else if (tz1 == NULL) |
116 |
{ |
{ |
117 |
tz1 = tim.tm_zone; |
tz1 = tim.tm_zone; |
118 |
month = tim.tm_mon; |
month = tim.tm_mon; |
119 |
} |
} |
120 |
|
|
121 |
if (tz1 == NULL || tz2 == NULL) |
if (tz1 == NULL || tz2 == NULL) |
122 |
{ |
{ |
123 |
tim.tm_mon++; |
tim.tm_mon++; |
124 |
tim.tm_mon %= 12; |
tim.tm_mon %= 12; |
125 |
} |
} |
126 |
|
|
127 |
if (tim.tm_mon == month && tz2 == NULL) |
if (tim.tm_mon == month && tz2 == NULL) |
128 |
tz2 = ""; |
tz2 = ""; |
129 |
else |
else |
130 |
mktime(&tim); |
mktime (&tim); |
131 |
} |
} |
132 |
/* We want to make sure the tm struct we use later on is not dst. */ |
/* We want to make sure the tm struct we use later on is not dst. */ |
133 |
tim.tm_mon = month; |
tim.tm_mon = month; |
134 |
mktime(&tim); |
mktime (&tim); |
135 |
#elif defined (HAVE_TZNAME) |
#elif defined (HAVE_TZNAME) |
136 |
/* If dst is never used, tzname[1] is the empty string. */ |
/* If dst is never used, tzname[1] is the empty string. */ |
137 |
tzset(); |
tzset (); |
138 |
tz1 = tzname[0]; |
tz1 = tzname[0]; |
139 |
tz2 = tzname[1]; |
tz2 = tzname[1]; |
140 |
#else |
#else |
153 |
tzoffset = (long) _timezone; |
tzoffset = (long) _timezone; |
154 |
#elif HAVE_TIMEZONE |
#elif HAVE_TIMEZONE |
155 |
/* timezone is secs WEST of UTC. */ |
/* timezone is secs WEST of UTC. */ |
156 |
tzoffset = timezone; |
tzoffset = timezone; |
157 |
#else |
#else |
158 |
/* FIXME: there must be another global if neither tm_gmtoff nor timezone |
/* FIXME: there must be another global if neither tm_gmtoff nor timezone |
159 |
is available, esp. if tzname is valid. |
is available, esp. if tzname is valid. |
166 |
if ((tzoffset % 3600) == 0) |
if ((tzoffset % 3600) == 0) |
167 |
tzoffset = tzoffset / 3600; |
tzoffset = tzoffset / 3600; |
168 |
|
|
169 |
tz1_len = strlen(tz1); |
tz1_len = strlen (tz1); |
170 |
tz2_len = strlen(tz2); |
tz2_len = strlen (tz2); |
171 |
tzoff_len = jint_to_charbuf (tzoff + 11, tzoffset); |
tzoff_len = jint_to_charbuf (tzoff + 11, tzoffset); |
172 |
tzid = (char*) malloc (tz1_len + tz2_len + tzoff_len + 1); /* FIXME alloc */ |
tzid = (char *) malloc (tz1_len + tz2_len + tzoff_len + 1); /* FIXME alloc */ |
173 |
memcpy (tzid, tz1, tz1_len); |
memcpy (tzid, tz1, tz1_len); |
174 |
memcpy (tzid + tz1_len, tzoff + 11 - tzoff_len, tzoff_len); |
memcpy (tzid + tz1_len, tzoff + 11 - tzoff_len, tzoff_len); |
175 |
memcpy (tzid + tz1_len + tzoff_len, tz2, tz2_len); |
memcpy (tzid + tz1_len + tzoff_len, tz2, tz2_len); |
191 |
*/ |
*/ |
192 |
|
|
193 |
static size_t |
static size_t |
194 |
jint_to_charbuf (char* bufend, jint num) |
jint_to_charbuf (char *bufend, jint num) |
195 |
{ |
{ |
196 |
register char* ptr = bufend; |
register char *ptr = bufend; |
197 |
jboolean isNeg; |
jboolean isNeg; |
198 |
if (num < 0) |
if (num < 0) |
199 |
{ |
{ |
200 |
isNeg = JNI_TRUE; |
isNeg = JNI_TRUE; |
201 |
num = -(num); |
num = -(num); |
202 |
if (num < 0) |
if (num < 0) |
203 |
{ |
{ |
204 |
/* Must be MIN_VALUE, so handle this special case. |
/* Must be MIN_VALUE, so handle this special case. |
205 |
FIXME use 'unsigned jint' for num. */ |
FIXME use 'unsigned jint' for num. */ |
206 |
*--ptr = '8'; |
*--ptr = '8'; |
207 |
num = 214748364; |
num = 214748364; |
208 |
} |
} |
209 |
} |
} |
210 |
else |
else |
211 |
isNeg = JNI_FALSE; |
isNeg = JNI_FALSE; |
212 |
|
|
213 |
do |
do |
214 |
{ |
{ |
215 |
*--ptr = (char) ((int) '0' + (num % 10)); |
*--ptr = (char) ((int) '0' + (num % 10)); |
216 |
num /= 10; |
num /= 10; |
217 |
} |
} |
218 |
while (num > 0); |
while (num > 0); |
219 |
|
|
220 |
if (isNeg) |
if (isNeg) |
221 |
*--ptr = '-'; |
*--ptr = '-'; |
222 |
return bufend - ptr; |
return bufend - ptr; |
223 |
} |
} |