summaryrefslogtreecommitdiffstats
path: root/libkcal/libical/vzic-1.3/test-vzic.c
blob: 9da1abe1b2d972d1342eab468dad3cc05f58ad97 (plain)
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/*
 * Vzic - a program to convert Olson timezone database files into VZTIMEZONE
 * files compatible with the iCalendar specification (RFC2445).
 *
 * Copyright (C) 2000-2001 Ximian, Inc.
 * Copyright (C) 2003 Damon Chaplin.
 *
 * Author: Damon Chaplin <damon@gnome.org>
 *
 * This program 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 of the License, or
 * (at your option) any later version.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
 */

/*
 * test-vzic.c - test vzic + libical against mktime() and friends.
 *
 * Note that when we output VCALENDAR data compatible with Outlook the
 * results aren't all correct.
 *
 * We have to modify some RRULEs which makes these timezones incorrect:
 *
 *   Africa/Cairo
 *   America/Godthab
 *   America/Santiago
 *   Antarctica/Palmer
 *   Asia/Baghdad
 *   Asia/Damascus
 *   Asia/Jerusalem
 *
 * Also, we can only output one RDATE or a pair of RRULEs which may make some
 * other timezones incorrect sometimes (e.g. if they change).
 */

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>

#include <ical.h>
/*#include <evolution/ical.h>*/

#define CHANGES_MAX_YEAR 2030

/* These are the years between which we test against the Unix timezone
   functions, inclusive. When using 'vzic --pure' you can test the full
   range from 1970 to 2037 and it should match against mktime() etc.
   (assuming you are using the same Olson timezone data for both).

   But when using VTIMEZONE's that are compatible with Outlook, it is only
   worth testing times in the future. There will be lots of differences in
   the past, since we can't include any historical changes in the files. */
#if 1
#define DUMP_START_YEAR 2003
#define DUMP_END_YEAR	2037
#else
#define DUMP_START_YEAR 1970
#define DUMP_END_YEAR	2037
#endif

/* The maximum size of any complete pathname. */
#define PATHNAME_BUFFER_SIZE	1024

#ifndef	FALSE
#define	FALSE	(0)
#endif

#ifndef	TRUE
#define	TRUE	(!FALSE)
#endif

int VzicDumpChanges		= FALSE;

/* We output beneath the current directory for now. */
char *directory = "test-output";

static void	usage				(void);
static int	parse_zone_name			(char		*name,
						 char	       **directory,
						 char	       **subdirectory,
						 char	       **filename);
static void	ensure_directory_exists		(char		*directory);
static void	dump_local_times		(icaltimezone	*zone,
						 FILE		*fp);


int main(int argc, char* argv[])
{
  icalarray *zones;
  icaltimezone *zone;
  char *zone_directory, *zone_subdirectory, *zone_filename, *location;
  char output_directory[PATHNAME_BUFFER_SIZE];
  char filename[PATHNAME_BUFFER_SIZE];
  FILE *fp;
  int i;
  int skipping = TRUE;

  /*
   * Command-Line Option Parsing.
   */
  for (i = 1; i < argc; i++) {
    /* --dump-changes: Dumps a list of times when each timezone changed,
       and the new local time offset from UTC. */
    if (!strcmp (argv[i], "--dump-changes"))
      VzicDumpChanges = TRUE;

    else
      usage ();
  }


  zones = icaltimezone_get_builtin_timezones ();

  ensure_directory_exists (directory);

  for (i = 0; i < zones->num_elements; i++) {
    zone = icalarray_element_at (zones, i);

    location = icaltimezone_get_location (zone);

#if 0
    /* Use this to start at a certain zone. */
    if (skipping && strcmp (location, "America/Boise"))
      continue;
#endif

    skipping = FALSE;

    /* Use this to only output data for certain timezones. */
#if 0
    if (strcmp (location, "America/Cancun")
	&& strcmp (location, "Asia/Baku")
	&& strcmp (location, "Asia/Nicosia")
	&& strcmp (location, "Asia/Novosibirsk")
	&& strcmp (location, "Asia/Samarkand")
	&& strcmp (location, "Asia/Tashkent")
	&& strcmp (location, "Asia/Tbilisi")
	&& strcmp (location, "Asia/Yerevan")
	&& strcmp (location, "Australia/Broken_Hill")
	&& strcmp (location, "Europe/Simferopol")
	&& strcmp (location, "Europe/Tallinn")
	&& strcmp (location, "Europe/Zaporozhye")
	)
      continue;
#endif

#if 0
    printf ("%s\n", location);
#endif

    parse_zone_name (location, &zone_directory, &zone_subdirectory,
		     &zone_filename);

    sprintf (output_directory, "%s/%s", directory, zone_directory);
    ensure_directory_exists (output_directory);
    sprintf (filename, "%s/%s", output_directory, zone_filename);

    if (zone_subdirectory) {
      sprintf (output_directory, "%s/%s/%s", directory, zone_directory,
	       zone_subdirectory);
      ensure_directory_exists (output_directory);
      sprintf (filename, "%s/%s", output_directory, zone_filename);
    }

    fp = fopen (filename, "w");
    if (!fp) {
      fprintf (stderr, "Couldn't create file: %s\n", filename);
      exit (1);
    }

    /* We can run 2 different tests - output all changes for each zone, or
       test against mktime()/localtime(). Should have a command-line option
       or something. */
    if (VzicDumpChanges)
      icaltimezone_dump_changes (zone, CHANGES_MAX_YEAR, fp);
    else
      dump_local_times (zone, fp);

    if (ferror (fp)) {
      fprintf (stderr, "Error writing file: %s\n", filename);
      exit (1);
    }

    fclose (fp);
  }

  return 0;
}


static void
usage				(void)
{
  fprintf (stderr, "Usage: test-vzic [--dump-changes]\n");

  exit (1);
}


/* This checks that the Zone name only uses the characters in [-+_/a-zA-Z0-9],
   and outputs a warning if it isn't. */
static int
parse_zone_name			(char		*name,
				 char	       **directory,
				 char	       **subdirectory,
				 char	       **filename)
{
  static int invalid_zone_num = 1;

  char *p, ch, *first_slash_pos = NULL, *second_slash_pos = NULL;
  int invalid = FALSE;

  for (p = name; (ch = *p) != 0; p++) {
    if ((ch < 'a' || ch > 'z') && (ch < 'A' || ch > 'Z')
	&& (ch < '0' || ch > '9') && ch != '/' && ch != '_'
	&& ch != '-' && ch != '+') {
      fprintf (stderr, "Warning: Unusual Zone name: %s\n", name);
      invalid = TRUE;
      break;
    }

    if (ch == '/') {
      if (!first_slash_pos) {
	first_slash_pos = p;
      } else if (!second_slash_pos) {
	second_slash_pos = p;
      } else {
	fprintf (stderr, "Warning: More than 2 '/' characters in Zone name: %s\n", name);
	invalid = TRUE;
	break;
      }
    }
  }

  if (!first_slash_pos) {
	fprintf (stderr, "No '/' character in Zone name: %s. Skipping.\n", name);
	return FALSE;
  }

  if (invalid) {
    fprintf (stderr, "Invalid zone name: %s\n", name);
    exit (0);
  } else {
    *first_slash_pos = '\0';
    *directory = icalmemory_strdup (name);
    *first_slash_pos = '/';

    if (second_slash_pos) {
      *second_slash_pos = '\0';
      *subdirectory = icalmemory_strdup (first_slash_pos + 1);
      *second_slash_pos = '/';

      *filename = icalmemory_strdup (second_slash_pos + 1);
    } else {
      *subdirectory = NULL;
      *filename = icalmemory_strdup (first_slash_pos + 1);
    }
  }
}


static void
ensure_directory_exists		(char		*directory)
{
  struct stat filestat;

  if (stat (directory, &filestat) != 0) {
    /* If the directory doesn't exist, try to create it. */
    if (errno == ENOENT) {
      if (mkdir (directory, 0777) != 0) {
	fprintf (stderr, "Can't create directory: %s\n", directory);
	exit (1);
      }
    } else {
      fprintf (stderr, "Error calling stat() on directory: %s\n", directory);
      exit (1);
    }
  } else if (!S_ISDIR (filestat.st_mode)) {
    fprintf (stderr, "Can't create directory, already exists: %s\n",
	     directory);
    exit (1);
  }
}


static void
dump_local_times (icaltimezone *zone, FILE *fp)
{
  static char *months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
			    "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
  icaltimezone *utc_timezone;
  struct icaltimetype tt, tt_copy;
  struct tm tm, local_tm;
  time_t t;
  char tzstring[256], *location;
  int last_year_output = 0;
  int total_error = 0, total_error2 = 0;

  utc_timezone = icaltimezone_get_utc_timezone ();

  /* This is our UTC time that we will use to iterate over the period. */
  tt.year = DUMP_START_YEAR;
  tt.month = 1;
  tt.day = 1;
  tt.hour = 0;
  tt.minute = 0;
  tt.second = 0;
  tt.is_utc = 0;
  tt.is_date = 0;
  tt.zone = "";

  tm.tm_year = tt.year - 1900;
  tm.tm_mon = tt.month - 1;
  tm.tm_mday = tt.day;
  tm.tm_hour = tt.hour;
  tm.tm_min = tt.minute;
  tm.tm_sec = tt.second;
  tm.tm_isdst = -1;

  /* Convert it to a time_t by saying it is in UTC. */
  putenv ("TZ=UTC");
  t = mktime (&tm);

  location = icaltimezone_get_location (zone);
  sprintf (tzstring, "TZ=%s", location);

  /*printf ("Zone: %s\n", location);*/
  putenv (tzstring);

  /* Loop around converting the UTC time to local time, outputting it, and
     then adding on 15 minutes to the UTC time. */
  while (tt.year <= DUMP_END_YEAR) {
    if (tt.year > last_year_output) {
      last_year_output = tt.year;
#if 0
      printf ("  %i\n", last_year_output);
      fprintf (fp, "  %i\n", last_year_output);
#endif
    }

#if 1
    /* First use the Unix functions. */
    /* Now convert it to a local time in the given timezone. */
    local_tm = *localtime (&t);
#endif

#if 1
    /* Now use libical. */
    tt_copy = tt;
    icaltimezone_convert_time (&tt_copy, utc_timezone, zone);
#endif

#if 1
    if (local_tm.tm_year + 1900 != tt_copy.year
	|| local_tm.tm_mon + 1 != tt_copy.month
	|| local_tm.tm_mday != tt_copy.day
	|| local_tm.tm_hour != tt_copy.hour
	|| local_tm.tm_min != tt_copy.minute
	|| local_tm.tm_sec != tt_copy.second) {

      /* The error format is:

     ERROR: Original-UTC-Time  Local-Time-From-mktime  Local-Time-From-Libical

      */

      total_error++;

      fprintf (fp, "ERROR:%2i %s %04i %2i:%02i:%02i UTC",
	       tt.day, months[tt.month - 1], tt.year,
	       tt.hour, tt.minute, tt.second);
      fprintf (fp, " ->%2i %s %04i %2i:%02i:%02i",
	       local_tm.tm_mday, months[local_tm.tm_mon],
	       local_tm.tm_year + 1900,
	       local_tm.tm_hour, local_tm.tm_min, local_tm.tm_sec);
      fprintf (fp, " Us:%2i %s %04i %2i:%02i:%02i\n",
	       tt_copy.day, months[tt_copy.month - 1], tt_copy.year,
	       tt_copy.hour, tt_copy.minute, tt_copy.second);
    }
#endif

    /* Now convert it back, and check we get the original time. */
    icaltimezone_convert_time (&tt_copy, zone, utc_timezone);
    if (tt.year != tt_copy.year
	|| tt.month != tt_copy.month
	|| tt.day != tt_copy.day
	|| tt.hour != tt_copy.hour
	|| tt.minute != tt_copy.minute
	|| tt.second != tt_copy.second) {

      total_error2++;

      fprintf (fp, "ERROR 2: %2i %s %04i %2i:%02i:%02i UTC",
	       tt.day, months[tt.month - 1], tt.year,
	       tt.hour, tt.minute, tt.second);
      fprintf (fp, " Us:%2i %s %04i %2i:%02i:%02i UTC\n",
	       tt_copy.day, months[tt_copy.month - 1], tt_copy.year,
	       tt_copy.hour, tt_copy.minute, tt_copy.second);
    }


    /* Increment the time. */
    icaltime_adjust (&tt, 0, 0, 15, 0);

    /* We assume leap seconds are not included in time_t values, which should
       be true on POSIX systems. */
    t += 15 * 60;
  }

  printf ("Zone: %40s  Errors: %i (%i)\n", icaltimezone_get_location (zone),
	  total_error, total_error2);
}