1 /* java.util.VMTimeZone
2 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004
3 Free Software Foundation, Inc.
5 This file is part of GNU Classpath.
7 GNU Classpath is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU Classpath is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Classpath; see the file COPYING. If not, write to the
19 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 Linking this library statically or dynamically with other modules is
23 making a combined work based on this library. Thus, the terms and
24 conditions of the GNU General Public License cover the whole
27 As a special exception, the copyright holders of this library give you
28 permission to link this library with independent modules to produce an
29 executable, regardless of the license terms of these independent
30 modules, and to copy and distribute the resulting executable under
31 terms of your choice, provided that you also meet, for each linked
32 independent module, the terms and conditions of the license of that
33 module. An independent module is a module which is not derived from
34 or based on this library. If you modify this library, you may extend
35 this exception to your version of the library, but you are not
36 obligated to do so. If you do not wish to do so, delete this
37 exception statement from your version. */
42 import gnu
.classpath
.Configuration
;
49 final class VMTimeZone
53 if (Configuration
.INIT_LOAD_LIBRARY
)
55 System
.loadLibrary("javautil");
60 * This method returns a time zone id string which is in the form
61 * (standard zone name) or (standard zone name)(GMT offset) or
62 * (standard zone name)(GMT offset)(daylight time zone name). The
63 * GMT offset can be in seconds, or where it is evenly divisible by
64 * 3600, then it can be in hours. The offset must be the time to
65 * add to the local time to get GMT. If a offset is given and the
66 * time zone observes daylight saving then the (daylight time zone
67 * name) must also be given (otherwise it is assumed the time zone
68 * does not observe any daylight savings).
70 * The result of this method is given to the method
71 * TimeZone.getDefaultTimeZone(String) which tries to map the time
72 * zone id to a known TimeZone. See that method on how the returned
73 * String is mapped to a real TimeZone object.
75 * The reference implementation which is made for GNU/Posix like
76 * systems calls <code>System.getenv("TZ")</code>,
77 * <code>readTimeZoneFile("/etc/timezone")</code>,
78 * <code>readtzFile("/etc/localtime")</code> and finally
79 * <code>getSystemTimeZoneId()</code> till a supported TimeZone is
80 * found through <code>TimeZone.getDefaultTimeZone(String)</code>.
81 * If every method fails <code>null</code> is returned (which means
82 * the TimeZone code will fall back on GMT as default time zone).
84 * Note that this method is called inside a
85 * <code>AccessController.doPrivileged()</code> block and runs with
86 * the priviliges of the java.util system classes. It will only be
87 * called when the default time zone is not yet set, the system
88 * property user.timezone isn't set and it is requested for the
91 static TimeZone
getDefaultTimeZoneId()
95 // See if TZ environment variable is set and accessible.
96 String tzid
= System
.getenv("TZ");
97 if (tzid
!= null && !tzid
.equals(""))
98 zone
= TimeZone
.getDefaultTimeZone(tzid
);
100 // Try to parse /etc/timezone.
103 tzid
= readTimeZoneFile("/etc/timezone");
104 if (tzid
!= null && !tzid
.equals(""))
105 zone
= TimeZone
.getDefaultTimeZone(tzid
);
108 // Try to parse /etc/localtime
111 tzid
= readtzFile("/etc/localtime");
112 if (tzid
!= null && !tzid
.equals(""))
113 zone
= TimeZone
.getDefaultTimeZone(tzid
);
116 // Try some system specific way
119 tzid
= getSystemTimeZoneId();
120 if (tzid
!= null && !tzid
.equals(""))
121 zone
= TimeZone
.getDefaultTimeZone(tzid
);
128 * Tries to read the time zone name from a file. Only the first
129 * consecutive letters, digits, slashes, dashes and underscores are
130 * read from the file. If the file cannot be read or an IOException
131 * occurs null is returned.
133 * The /etc/timezone file is not standard, but a lot of systems have
134 * it. If it exist the first line always contains a string
135 * describing the timezone of the host of domain. Some systems
136 * contain a /etc/TIMEZONE file which is used to set the TZ
137 * environment variable (which is checked before /etc/timezone is
140 private static String
readTimeZoneFile(String file
)
142 File f
= new File(file
);
146 InputStreamReader isr
= null;
149 FileInputStream fis
= new FileInputStream(f
);
150 BufferedInputStream bis
= new BufferedInputStream(fis
);
151 isr
= new InputStreamReader(bis
);
153 StringBuffer sb
= new StringBuffer();
158 if (Character
.isLetter(c
) || Character
.isDigit(c
)
159 || c
== '/' || c
== '-' || c
== '_')
167 return sb
.toString();
169 catch (IOException ioe
)
171 // Parse error, not a proper tzfile.
181 catch (IOException ioe
)
183 // Error while close, nothing we can do.
189 * Tries to read a file as a "standard" tzfile and return a time
190 * zone id string as expected by <code>getDefaultTimeZone(String)</code>.
191 * If the file doesn't exist, an IOException occurs or it isn't a tzfile
192 * that can be parsed null is returned.
194 * The tzfile structure (as also used by glibc) is described in the Olson
195 * tz database archive as can be found at
196 * <code>ftp://elsie.nci.nih.gov/pub/</code>.
198 * At least the following platforms support the tzdata file format
199 * and /etc/localtime (GNU/Linux, Darwin, Solaris and FreeBSD at
200 * least). Some systems (like Darwin) don't start the file with the
201 * required magic bytes 'TZif', this implementation can handle
204 private static String
readtzFile(String file
)
206 File f
= new File(file
);
210 DataInputStream dis
= null;
213 FileInputStream fis
= new FileInputStream(f
);
214 BufferedInputStream bis
= new BufferedInputStream(fis
);
215 dis
= new DataInputStream(bis
);
217 // Make sure we are reading a tzfile.
218 byte[] tzif
= new byte[4];
220 if (tzif
[0] == 'T' && tzif
[1] == 'Z'
221 && tzif
[2] == 'i' && tzif
[3] == 'f')
222 // Reserved bytes, ttisgmtcnt, ttisstdcnt and leapcnt
223 skipFully(dis
, 16 + 3 * 4);
225 // Darwin has tzdata files that don't start with the TZif marker
226 skipFully(dis
, 16 + 3 * 4 - 4);
228 int timecnt
= dis
.readInt();
229 int typecnt
= dis
.readInt();
232 int charcnt
= dis
.readInt();
233 // Transition times plus indexed transition times.
234 skipFully(dis
, timecnt
* (4 + 1));
236 // Get last gmt_offset and dst/non-dst time zone names.
238 int dst_abbrind
= -1;
240 while (typecnt
-- > 0)
243 int offset
= dis
.readInt();
244 int dst
= dis
.readByte();
247 abbrind
= dis
.readByte();
251 dst_abbrind
= dis
.readByte();
254 // gmt_offset is the offset you must add to UTC/GMT to
255 // get the local time, we need the offset to add to
256 // the local time to get UTC/GMT.
259 // Turn into hours if possible.
260 if (gmt_offset
% 3600 == 0)
265 byte[] names
= new byte[charcnt
];
266 dis
.readFully(names
);
268 while (j
< charcnt
&& names
[j
] != 0)
271 String zonename
= new String(names
, abbrind
, j
- abbrind
,
275 if (dst_abbrind
>= 0)
278 while (j
< charcnt
&& names
[j
] != 0)
280 dst_zonename
= new String(names
, dst_abbrind
,
281 j
- dst_abbrind
, "ASCII");
286 // Only use gmt offset when necessary.
287 // Also special case GMT+/- timezones.
288 String offset_string
;
289 if ("".equals(dst_zonename
)
291 || zonename
.startsWith("GMT+")
292 || zonename
.startsWith("GMT-")))
295 offset_string
= Integer
.toString(gmt_offset
);
297 String id
= zonename
+ offset_string
+ dst_zonename
;
303 // Something didn't match while reading the file.
306 catch (IOException ioe
)
308 // Parse error, not a proper tzfile.
318 catch(IOException ioe
)
320 // Error while close, nothing we can do.
326 * Skips the requested number of bytes in the given InputStream.
327 * Throws EOFException if not enough bytes could be skipped.
328 * Negative numbers of bytes to skip are ignored.
330 private static void skipFully(InputStream is
, long l
) throws IOException
336 throw new EOFException();
342 * Tries to get the system time zone id through native code.
344 private static native String
getSystemTimeZoneId();