import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.io.InputStreamReader; import java.util.Calendar; import java.util.Date; import java.util.SimpleTimeZone; import java.util.TimeZone; final class TimeZoneTest { public static void main (String[] args) { long errorcount = 0; if (!new File("/usr/share/zoneinfo").isDirectory() || !new File("/usr/sbin/zdump").exists()) System.exit (0); TimeZone utc = (TimeZone) new SimpleTimeZone(0, "GMT"); TimeZone.setDefault(utc); String[] zones = TimeZone.getAvailableIDs(); for (int i = 0; i < zones.length; i++) { if (!new File("/usr/share/zoneinfo/" + zones[i]).exists()) continue; // These two timezones have different definitions between // tzdata and JDK. In JDK EST is EST5EDT, while in tzdata // just EST5, similarly for MST. if (zones[i].equals("EST") || zones[i].equals("MST")) continue; TimeZone tz = TimeZone.getTimeZone(zones[i]); if (tz == null) { System.out.println ("getTimeZone(" + zones[i] + ") failed"); errorcount++; continue; } Calendar cal = Calendar.getInstance(tz); BufferedReader br = null; Process process = null; try { process = Runtime.getRuntime().exec("/usr/sbin/zdump -v " + zones[i]); br = new BufferedReader(new InputStreamReader(process.getInputStream())); for (String line = br.readLine(); line != null; line = br.readLine()) { int end1 = line.indexOf(" UTC = "); if (end1 < 0) continue; int start1 = line.indexOf(" "); if (start1 < 0 || start1 >= end1) continue; int start2 = line.indexOf(" isdst="); int start3 = line.indexOf(" gmtoff="); if (start2 <= end1 || start3 <= start2) continue; Date d = new Date(line.substring(start1 + 2, end1 + 4)); cal.setTime(d); int isdst = Integer.parseInt(line.substring(start2 + 7, start3)); int gmtoff = Integer.parseInt(line.substring(start3 + 8, line.length())); if (tz.inDaylightTime(d) != (isdst != 0)) { System.out.println ("Zone " + zones[i] + " " + d + " isdst=" + isdst + " inDaylightTime=" + tz.inDaylightTime(d)); errorcount++; } if (tz.getOffset(d.getTime()) != gmtoff * 1000) { System.out.println ("Zone " + zones[i] + " " + d + " gmtoff=" + gmtoff + " getOffset=" + tz.getOffset(d.getTime())); errorcount++; } if (cal.get(Calendar.DST_OFFSET) + cal.get(Calendar.ZONE_OFFSET) != gmtoff * 1000) { System.out.println ("Zone " + zones[i] + " " + d + " gmtoff=" + gmtoff + " DST_OFFSET+ZONE_OFFSET=" + (cal.get(Calendar.DST_OFFSET) + cal.get(Calendar.ZONE_OFFSET))); errorcount++; } } } catch (IOException ioe) { } finally { try { if (br != null) br.close(); if (process != null) { process.waitFor(); process.exitValue(); } } catch (IOException ioe) { } catch (InterruptedException ine) { } } } if (errorcount > 0) { System.out.println(errorcount + " errors encountered"); System.exit(1); } System.out.println("All OK"); System.exit(0); } }