VMTimeZone.java 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /* java.util.VMTimeZone
  2. Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2007
  3. Free Software Foundation, Inc.
  4. This file is part of GNU Classpath.
  5. GNU Classpath is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9. GNU Classpath is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GNU Classpath; see the file COPYING. If not, write to the
  15. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. 02110-1301 USA.
  17. Linking this library statically or dynamically with other modules is
  18. making a combined work based on this library. Thus, the terms and
  19. conditions of the GNU General Public License cover the whole
  20. combination.
  21. As a special exception, the copyright holders of this library give you
  22. permission to link this library with independent modules to produce an
  23. executable, regardless of the license terms of these independent
  24. modules, and to copy and distribute the resulting executable under
  25. terms of your choice, provided that you also meet, for each linked
  26. independent module, the terms and conditions of the license of that
  27. module. An independent module is a module which is not derived from
  28. or based on this library. If you modify this library, you may extend
  29. this exception to your version of the library, but you are not
  30. obligated to do so. If you do not wish to do so, delete this
  31. exception statement from your version. */
  32. package java.util;
  33. import gnu.classpath.Configuration;
  34. import gnu.classpath.SystemProperties;
  35. import gnu.java.util.ZoneInfo;
  36. import java.util.TimeZone;
  37. import java.io.*;
  38. /**
  39. *
  40. */
  41. final class VMTimeZone
  42. {
  43. static
  44. {
  45. if (Configuration.INIT_LOAD_LIBRARY)
  46. {
  47. System.loadLibrary("javautil");
  48. }
  49. }
  50. /**
  51. * This method returns a time zone id string which is in the form
  52. * (standard zone name) or (standard zone name)(GMT offset) or
  53. * (standard zone name)(GMT offset)(daylight time zone name). The
  54. * GMT offset can be in seconds, or where it is evenly divisible by
  55. * 3600, then it can be in hours. The offset must be the time to
  56. * add to the local time to get GMT. If a offset is given and the
  57. * time zone observes daylight saving then the (daylight time zone
  58. * name) must also be given (otherwise it is assumed the time zone
  59. * does not observe any daylight savings).
  60. * <p>
  61. * The result of this method is given to the method
  62. * TimeZone.getDefaultTimeZone(String) which tries to map the time
  63. * zone id to a known TimeZone. See that method on how the returned
  64. * String is mapped to a real TimeZone object.
  65. * <p>
  66. * The reference implementation which is made for GNU/Posix like
  67. * systems calls <code>System.getenv("TZ")</code>,
  68. * <code>readTimeZoneFile("/etc/timezone")</code>,
  69. * <code>ZoneInfo.readTZFile((String)null, "/etc/localtime")</code>
  70. * and finally <code>getSystemTimeZoneId()</code> till a supported
  71. * TimeZone is found through
  72. * <code>TimeZone.getDefaultTimeZone(String)</code>.
  73. * If every method fails <code>null</code> is returned (which means
  74. * the TimeZone code will fall back on GMT as default time zone).
  75. * <p>
  76. * Note that this method is called inside a
  77. * <code>AccessController.doPrivileged()</code> block and runs with
  78. * the priviliges of the java.util system classes. It will only be
  79. * called when the default time zone is not yet set, the system
  80. * property user.timezone isn't set and it is requested for the
  81. * first time.
  82. */
  83. static TimeZone getDefaultTimeZoneId()
  84. {
  85. TimeZone zone = null;
  86. // See if TZ environment variable is set and accessible.
  87. String tzid = System.getenv("TZ");
  88. if (tzid != null && !tzid.equals(""))
  89. zone = TimeZone.getDefaultTimeZone(tzid);
  90. // Try to parse /etc/timezone.
  91. if (zone == null)
  92. {
  93. tzid = readTimeZoneFile("/etc/timezone");
  94. if (tzid != null && !tzid.equals(""))
  95. zone = TimeZone.getDefaultTimeZone(tzid);
  96. }
  97. // Try to parse /etc/localtime
  98. if (zone == null)
  99. {
  100. zone = ZoneInfo.readTZFile((String) null, "/etc/localtime");
  101. if (zone != null)
  102. {
  103. // Try to find a more suitable ID for the /etc/localtime
  104. // timezone.
  105. // Sometimes /etc/localtime is a symlink to some
  106. // /usr/share/zoneinfo/ file.
  107. String id = null;
  108. try
  109. {
  110. id = new File("/etc/localtime").getCanonicalPath();
  111. if (id != null)
  112. {
  113. String zoneinfo_dir
  114. = SystemProperties.getProperty("gnu.java.util.zoneinfo.dir");
  115. if (zoneinfo_dir != null)
  116. zoneinfo_dir
  117. = new File(zoneinfo_dir
  118. + File.separatorChar).getCanonicalPath();
  119. if (zoneinfo_dir != null && id.startsWith(zoneinfo_dir))
  120. {
  121. int pos = zoneinfo_dir.length();
  122. while (pos < id.length()
  123. && id.charAt(pos) == File.separatorChar)
  124. pos++;
  125. if (pos < id.length())
  126. id = id.substring(pos);
  127. else
  128. id = null;
  129. }
  130. else
  131. id = null;
  132. }
  133. }
  134. catch (IOException ioe)
  135. {
  136. id = null;
  137. }
  138. if (id == null)
  139. id = readSysconfigClockFile("/etc/sysconfig/clock");
  140. if (id != null)
  141. zone.setID(id);
  142. }
  143. }
  144. // Try some system specific way
  145. if (zone == null)
  146. {
  147. tzid = getSystemTimeZoneId();
  148. if (tzid != null && !tzid.equals(""))
  149. zone = TimeZone.getDefaultTimeZone(tzid);
  150. }
  151. return zone;
  152. }
  153. /**
  154. * Tries to read the time zone name from a file. Only the first
  155. * consecutive letters, digits, slashes, dashes and underscores are
  156. * read from the file. If the file cannot be read or an IOException
  157. * occurs null is returned.
  158. * <p>
  159. * The /etc/timezone file is not standard, but a lot of systems have
  160. * it. If it exist the first line always contains a string
  161. * describing the timezone of the host of domain. Some systems
  162. * contain a /etc/TIMEZONE file which is used to set the TZ
  163. * environment variable (which is checked before /etc/timezone is
  164. * read).
  165. */
  166. private static String readTimeZoneFile(String file)
  167. {
  168. File f = new File(file);
  169. if (!f.exists())
  170. return null;
  171. InputStreamReader isr = null;
  172. try
  173. {
  174. FileInputStream fis = new FileInputStream(f);
  175. BufferedInputStream bis = new BufferedInputStream(fis);
  176. isr = new InputStreamReader(bis);
  177. StringBuffer sb = new StringBuffer();
  178. int i = isr.read();
  179. while (i != -1)
  180. {
  181. char c = (char) i;
  182. if (Character.isLetter(c) || Character.isDigit(c)
  183. || c == '/' || c == '-' || c == '_')
  184. {
  185. sb.append(c);
  186. i = isr.read();
  187. }
  188. else
  189. break;
  190. }
  191. return sb.toString();
  192. }
  193. catch (IOException ioe)
  194. {
  195. // Parse error, not a proper tzfile.
  196. return null;
  197. }
  198. finally
  199. {
  200. try
  201. {
  202. if (isr != null)
  203. isr.close();
  204. }
  205. catch (IOException ioe)
  206. {
  207. // Error while close, nothing we can do.
  208. }
  209. }
  210. }
  211. /**
  212. * Tries to read the time zone name from a file.
  213. * If the file cannot be read or an IOException occurs null is returned.
  214. * <p>
  215. * The /etc/sysconfig/clock file is not standard, but a lot of systems
  216. * have it. The file is included by shell scripts and the timezone
  217. * name is defined in ZONE variable.
  218. * This routine should grok it with or without quotes:
  219. * ZONE=America/New_York
  220. * or
  221. * ZONE="Europe/London"
  222. */
  223. private static String readSysconfigClockFile(String file)
  224. {
  225. BufferedReader br = null;
  226. try
  227. {
  228. FileInputStream fis = new FileInputStream(file);
  229. BufferedInputStream bis = new BufferedInputStream(fis);
  230. br = new BufferedReader(new InputStreamReader(bis));
  231. for (String line = br.readLine(); line != null; line = br.readLine())
  232. {
  233. line = line.trim();
  234. if (line.length() < 8 || !line.startsWith("ZONE="))
  235. continue;
  236. int posstart = 6;
  237. int posend;
  238. if (line.charAt(5) == '"')
  239. posend = line.indexOf('"', 6);
  240. else if (line.charAt(5) == '\'')
  241. posend = line.indexOf('\'', 6);
  242. else
  243. {
  244. posstart = 5;
  245. posend = line.length();
  246. }
  247. if (posend < 0)
  248. return null;
  249. return line.substring(posstart, posend);
  250. }
  251. return null;
  252. }
  253. catch (IOException ioe)
  254. {
  255. // Parse error, not a proper tzfile.
  256. return null;
  257. }
  258. finally
  259. {
  260. try
  261. {
  262. if (br != null)
  263. br.close();
  264. }
  265. catch (IOException ioe)
  266. {
  267. // Error while close, nothing we can do.
  268. }
  269. }
  270. }
  271. /**
  272. * Tries to get the system time zone id through native code.
  273. */
  274. private static native String getSystemTimeZoneId();
  275. }