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
18 #include <java/util/VMTimeZone.h>
19 #include <java/lang/Character.h>
20 #include <java/lang/Integer.h>
24 #if TIME_WITH_SYS_TIME
25 # include <sys/time.h>
29 # include <sys/time.h>
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).
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
54 java::util::VMTimeZone::getSystemTimeZoneId()
57 #if !defined(HAVE_LOCALTIME_R) || !defined(_POSIX_PTHREAD_SEMANTICS)
65 const char *tz1
, *tz2
;
69 #if defined(HAVE_LOCALTIME_R) && defined(_POSIX_PTHREAD_SEMANTICS)
70 localtime_r(¤t_time
, &tim
);
72 /* Fall back on non-thread safe localtime. */
73 lt_tim
= localtime(¤t_time
);
74 memcpy(&tim
, lt_tim
, sizeof (struct tm
));
79 /* We will cycle through the months to make sure we hit dst. */
82 while (tz1
== NULL
|| tz2
== NULL
)
92 if (tz1
== NULL
|| tz2
== NULL
)
98 if (tim
.tm_mon
== month
&& tz2
== NULL
)
103 /* We want to make sure the tm struct we use later on is not dst. */
106 #elif defined (HAVE_TZNAME)
107 /* If dst is never used, tzname[1] is the empty string. */
112 /* Some targets have no concept of timezones. Assume GMT without dst. */
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
121 tzoffset
= -1L * tim
.tm_gmtoff
;
122 #elif HAVE_UNDERSCORE_TIMEZONE
123 tzoffset
= _timezone
;
125 /* timezone is secs WEST of UTC. */
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. */
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
);