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
154
155
156
157
158
159
160
161
162
163
164
165
|
/* -*- Mode: C -*-
======================================================================
The contents of this file are subject to the Mozilla Public License
Version 1.0 (the "License"); you may not use this file except in
compliance with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS"
basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
the License for the specific language governing rights and
limitations under the License.
======================================================================*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <libical/ical.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
icalarray *timezones = icaltimezone_get_builtin_timezones();
int i;
int ret = 0;
unsigned int total_failed = 0;
unsigned int total_okay = 0;
unsigned int percent_failed = 0;
int verbose = 0;
icaltimezone *utc_zone = icaltimezone_get_utc_timezone();
/* for all known time zones... */
for (i = 0; i < timezones->num_elements; i++) {
icaltimezone *zone = icalarray_element_at(timezones, i);
const char *zone_location = icaltimezone_get_location(zone);
int day;
time_t start_time;
struct tm start_tm;
time_t curr_time;
struct tm curr_tm;
struct icaltimetype curr_tt;
int failed = 0;
int curr_failed;
int zonedef_printed = 0;
/*
* select this location for glibc: needs support for TZ=<location>
* which is not POSIX
*/
#if defined(HAVE_SETENV)
setenv("TZ", zone_location, 1);
#else
static new_tz[256];
new_tz[0] = '\0';
strncat(new_tz, "TZ=", 255);
strncat(new_tz, zone_location, 255);
putenv(new_tz);
#endif
tzset();
/*
* determine current local time and date: always use midday in
* the current zone and first day of first month in the year
*/
start_time = time(NULL);
localtime_r(&start_time, &start_tm);
start_tm.tm_hour = 12;
start_tm.tm_min = 0;
start_tm.tm_sec = 0;
start_tm.tm_mday = 1;
start_tm.tm_mon = 0;
start_time = mktime(&start_tm);
/* check time conversion for the next 365 days */
for (day = 0, curr_time = start_time;
day < 365;
day++, curr_time += 24 * 60 * 60) {
/* determine date/time with glibc */
localtime_r(&curr_time, &curr_tm);
/* determine date/time with libical */
curr_tt = icaltime_from_timet_with_zone(curr_time, 0, utc_zone);
curr_tt.zone = utc_zone; /* workaround: icaltime_from_timet_with_zone() should do this, but doesn't! */
curr_tt = icaltime_convert_to_zone(curr_tt, zone);
/* compare... */
curr_failed =
curr_tm.tm_year + 1900 != curr_tt.year ||
curr_tm.tm_mon + 1 != curr_tt.month ||
curr_tm.tm_mday != curr_tt.day ||
curr_tm.tm_hour != curr_tt.hour ||
curr_tm.tm_min != curr_tt.minute ||
curr_tm.tm_sec != curr_tt.second;
/* only print first failed day and first day which is okay again */
if (verbose || curr_failed != failed) {
struct tm utc_tm;
gmtime_r(&curr_time, &utc_tm);
printf("%s: day %03d: %s: %04d-%02d-%02d %02d:%02d:%02d UTC = libc %04d-%02d-%02d %02d:%02d:%02d dst %d",
zone_location,
day,
verbose ?
(curr_failed ? "failed" : "okay") :
(curr_failed ? "first failed" : "okay again"),
utc_tm.tm_year + 1900,
utc_tm.tm_mon + 1,
utc_tm.tm_mday,
utc_tm.tm_hour,
utc_tm.tm_min,
utc_tm.tm_sec,
curr_tm.tm_year + 1900,
curr_tm.tm_mon + 1,
curr_tm.tm_mday,
curr_tm.tm_hour,
curr_tm.tm_min,
curr_tm.tm_sec,
curr_tm.tm_isdst);
if (curr_failed) {
printf(" != libical %04d-%02d-%02d %02d:%02d:%02d dst %d",
curr_tt.year,
curr_tt.month,
curr_tt.day,
curr_tt.hour,
curr_tt.minute,
curr_tt.second,
curr_tt.is_daylight);
ret = 1;
}
printf("\n");
failed = curr_failed;
if (!zonedef_printed) {
icalcomponent *comp = icaltimezone_get_component(zone);
if (comp) {
printf("%s\n", icalcomponent_as_ical_string(comp));
}
zonedef_printed = 1;
}
}
if (curr_failed) {
total_failed++;
} else {
total_okay++;
}
}
}
if (total_failed || total_okay) {
percent_failed = total_failed * 100 / (total_failed + total_okay);
printf(" *** Summary: %d zones tested, %u days failed, %u okay => %u%% failed ***\n",
timezones->num_elements,
total_failed,
total_okay,
percent_failed);
}
return ret;
}
|