File: zdump.java

package info (click to toggle)
mauve 20120103-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 28,504 kB
  • sloc: java: 250,155; sh: 2,834; xml: 208; makefile: 66
file content (153 lines) | stat: -rw-r--r-- 4,137 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Copyright (C) 2007 Red Hat, Inc.
// Written by Gary Benson <gbenson@redhat.com>
// Based on code by Jakub Jelinek <jakub@redhat.com>

// This file is part of Mauve.

// Mauve is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2, or (at your option)
// any later version.

// Mauve is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Mauve; see the file COPYING.  If not, write to
// the Free Software Foundation, 59 Temple Place - Suite 330,
// Boston, MA 02111-1307, USA.

// Tags: JDK1.4

package gnu.testlet.java.util.TimeZone;

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;

import gnu.testlet.Testlet;
import gnu.testlet.TestHarness;

public class zdump implements Testlet
{
  public static final String zdump = "/usr/sbin/zdump";
  
  public void test(TestHarness harness)
  {
    String zoneinfodir = System.getProperty("gnu.java.util.zoneinfo.dir");
    if (zoneinfodir == null)
      return;

    if (!new File(zdump).exists() || !new File(zoneinfodir).isDirectory())
      return;

    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(zoneinfodir, 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;

	checkZone(harness, zones[i]);
      }
  }

  public static void checkZone(TestHarness harness, String zone)
  {
    harness.checkPoint(zone);
    
    TimeZone tz = TimeZone.getTimeZone(zone);
    if (tz == null)
      {
	harness.check(false);
	return;
      }

    Calendar cal = Calendar.getInstance(tz);

    BufferedReader br = null;
    Process process = null;
    try
      {
	process = Runtime.getRuntime().exec(zdump + " -v " + zone);
	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()));

	    harness.debug("Zone " + zone + " " + d +
			  " isdst=" + isdst +
			  " inDaylightTime=" + tz.inDaylightTime(d));
	    harness.check(tz.inDaylightTime(d) == (isdst != 0));

	    harness.debug("Zone " + zone + " " + d +
			  " gmtoff=" + gmtoff +
			  " getOffset=" + tz.getOffset(d.getTime()));
	    harness.check(tz.getOffset(d.getTime()) == gmtoff * 1000);

	    int offset = cal.get(Calendar.DST_OFFSET) +
	      cal.get(Calendar.ZONE_OFFSET);
	    
	    harness.debug("Zone " + zone + " " + d +
			  " gmtoff=" + gmtoff +
			  " DST_OFFSET+ZONE_OFFSET=" + offset);
	    harness.check(offset == gmtoff * 1000);
	  }
      }
    catch (IOException ioe)
      {
      }
    finally
      {
	try
	  {
	    if (br != null)
	      br.close();
	    if (process != null)
	      {
		process.waitFor();
		process.exitValue();
	      }
	  }
	catch (IOException ioe)
	  {
	  }
	catch (InterruptedException ine)
	  {
	  }
      }
  }
}