forked from libical/vzic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vzic-output.c
2556 lines (2122 loc) · 73.6 KB
/
vzic-output.c
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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 <[email protected]>
*
* 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.
*/
/* ALGORITHM:
*
* First we expand all the Rule arrays, so that each element only represents 1
* year. If a Rule extends to infinity we expand it up to a few years past the
* maximum UNTIL year used in any of the timezones. We do this to make sure
* that the last of the expanded Rules (which may be infinite) is only used
* in the last of the time periods (i.e. the last Zone line).
*
* The Rule arrays are also sorted by the start time (FROM + IN + ON + AT).
* Doing all this makes it much easier to find which rules apply to which
* periods.
*
* For each timezone (i.e. ZoneData element), we step through each of the
* time periods, the ZoneLineData elements (which represent each Zone line
* from the Olson file.)
*
* We calculate the start & end time of the period.
* - For the first line the start time is -infinity.
* - For the last line the end time is +infinity.
* - The end time of each line is also the start time of the next.
*
* We create an array of time changes which occur in this period, including
* the one implied by the Zone line itself (though this is later taken out
* if it is found to be at exactly the same time as the first Rule).
*
* Now we iterate over the time changes, outputting them as STANDARD or
* DAYLIGHT components. We also try to merge them together into RRULEs or
* use RDATEs.
*/
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <sys/stat.h>
#include "vzic.h"
#include "vzic-output.h"
#include "vzic-dump.h"
/* These come from the Makefile. See the comments there. */
char *ProductID = PRODUCT_ID;
char *TZIDPrefix = TZID_PREFIX;
/* We expand the TZIDPrefix, replacing %D with the date, in here. */
char TZIDPrefixExpanded[1024];
/* We only use RRULEs if there are at least MIN_RRULE_OCCURRENCES occurrences,
since otherwise RDATEs are more efficient. Actually, I've set this high
so we only use RRULEs for infinite recurrences. Since expanding RRULEs is
very time-consuming, this seems sensible. */
#define MIN_RRULE_OCCURRENCES 2
/* The year we go up to when dumping the list of timezone changes (used
for testing & debugging). */
#define MAX_CHANGES_YEAR 2030
/* This is the maximum year that time_t value can typically hold on 32-bit
systems. */
#define MAX_TIME_T_YEAR 2038
/* The year we use to start RRULEs. */
#define RRULE_START_YEAR 1970
/* The year we use for RDATEs. */
#define RDATE_YEAR 1970
static char *WeekDays[] = { "SU", "MO", "TU", "WE", "TH", "FR", "SA" };
static int DaysInMonth[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
char *CurrentZoneName;
typedef struct _VzicTime VzicTime;
struct _VzicTime
{
/* Normal years, e.g. 2001. */
int year;
/* 0 (Jan) to 11 (Dec). */
int month;
/* The day, either a simple month day number, 1-31, or a rule such as
the last Sunday, or the first Monday on or after the 8th. */
DayCode day_code;
int day_number; /* 1 to 31. */
int day_weekday; /* 0 (Sun) to 6 (Sat). */
/* The time, in seconds from midnight. The code specifies whether the
time is a wall clock time, local standard time, or universal time. */
int time_seconds;
TimeCode time_code;
/* The offset from UTC for local standard time. */
int stdoff;
/* The offset from UTC for local wall clock time. If this is different to
stdoff then this is a DAYLIGHT component. This is TZOFFSETTO. */
int walloff;
/* TRUE if the time change recurs every year to infinity. */
gboolean is_infinite;
/* The last instance of a recurring time change, if not infinite */
VzicTime *until;
/* TRUE if the change has already been output. */
gboolean output;
/* These are the offsets of the previous VzicTime, and are used when
calculating the time of the change. We place them here in
output_zone_components() to simplify the output code. */
int prev_stdoff;
int prev_walloff;
/* The abbreviated form of the timezone name. Note that this may not be
unique. */
char *tzname;
};
static void expand_and_sort_rule_array (gpointer key,
gpointer value,
gpointer data);
static int rule_sort_func (const void *arg1,
const void *arg2);
static void output_zone (char *directory,
ZoneData *zone,
char *zone_name,
const char *alias_of,
GHashTable *rule_data);
static gboolean parse_zone_name (char *name,
char **directory,
char **subdirectory,
char **filename);
static void output_zone_to_files (ZoneData *zone,
char *zone_name,
const char *alias_of,
GHashTable *rule_data,
FILE *fp,
FILE *changes_fp);
static gboolean add_rule_changes (ZoneLineData *zone_line,
char *zone_name,
GArray *changes,
GHashTable *rule_data,
VzicTime *start,
VzicTime *end,
char **start_letter_s,
int *save_seconds);
static char* expand_tzname (char *zone_name,
char *format,
gboolean have_letter_s,
char *letter_s,
gboolean is_daylight);
static int compare_times (VzicTime *time1,
int stdoff1,
int walloff1,
VzicTime *time2,
int stdoff2,
int walloff2);
static gboolean times_match (VzicTime *time1,
int stdoff1,
int walloff1,
VzicTime *time2,
int stdoff2,
int walloff2);
static void output_zone_components (FILE *fp,
char *name,
const char *alias_of,
GArray *changes);
static void set_previous_offsets (GArray *changes);
static gboolean check_for_recurrence (FILE *fp,
GArray *changes,
int idx);
static void check_for_rdates (FILE *fp,
GArray *changes,
int idx);
static gboolean timezones_match (char *tzname1,
char *tzname2);
static int output_component_start (char *buffer,
VzicTime *vzictime,
gboolean output_rdate,
gboolean use_same_tz_offset);
static void output_component_end (FILE *fp,
VzicTime *vzictime);
static void vzictime_init (VzicTime *vzictime);
static int calculate_actual_time (VzicTime *vzictime,
TimeCode time_code,
int stdoff,
int walloff);
static int calculate_wall_time (int time,
TimeCode time_code,
int stdoff,
int walloff,
int *day_offset);
static int calculate_until_time (int time,
TimeCode time_code,
int stdoff,
int walloff,
int *year,
int *month,
int *day);
static void fix_time_overflow (int *year,
int *month,
int *day,
int day_offset);
static char* format_time (int year,
int month,
int day,
int time);
static char* format_tz_offset (int tz_offset,
gboolean round_seconds);
static gboolean output_rrule (char *rrule_buffer,
int month,
DayCode day_code,
int day_number,
int day_weekday,
int day_offset,
char *until);
static gboolean output_rrule_2 (char *buffer,
int month,
int day_number,
int day_weekday);
static char* format_vzictime (VzicTime *vzictime);
static void dump_changes (FILE *fp,
char *zone_name,
GArray *changes);
static void dump_change (FILE *fp,
char *zone_name,
VzicTime *vzictime,
int year);
static void expand_tzid_prefix (void);
void
output_vtimezone_files (char *directory,
GArray *zone_data,
GHashTable *rule_data,
GHashTable *link_data,
int max_until_year)
{
ZoneData *zone;
GList *links;
char *link_to;
int i;
/* Insert today's date into the TZIDs we output. */
expand_tzid_prefix ();
/* Expand the rule data so that each entry specifies only one year, and
sort it so we can easily find the rules applicable to each Zone span. */
g_hash_table_foreach (rule_data, expand_and_sort_rule_array,
GINT_TO_POINTER (max_until_year));
/* Output each timezone. */
for (i = 0; i < zone_data->len; i++) {
zone = &g_array_index (zone_data, ZoneData, i);
output_zone (directory, zone, zone->zone_name, NULL, rule_data);
/* Look for any links from this zone. */
links = g_hash_table_lookup (link_data, zone->zone_name);
while (links) {
link_to = links->data;
#if IGNORE_TOP_LEVEL_LINK
/* We ignore Links that don't have a '/' in them (things like 'EST5EDT').
*/
if (strchr (link_to, '/')) {
#endif
output_zone (directory, zone, link_to, zone->zone_name, rule_data);
#if IGNORE_TOP_LEVEL_LINK
}
#endif
links = links->next;
}
}
}
static void
expand_and_sort_rule_array (gpointer key,
gpointer value,
gpointer data)
{
char *name = key;
GArray *rule_array = value;
RuleData *rule, tmp_rule;
int len, max_year, i, from, to, year;
gboolean is_infinite;
/* We expand the rule data to a year greater than any year used in a Zone
UNTIL value. This is so that we can easily get parts of the array to
use for each Zone line. */
max_year = GPOINTER_TO_INT (data) + 2;
/* If any of the rules apply to several years, we turn it into a single rule
for each year. If the Rule is infinite we go up to max_year.
We change the FROM field in the copies of the Rule, setting it to each
of the years, and set TO to FROM, except if TO was YEAR_MAXIMUM we set
the last TO to YEAR_MAXIMUM, so we still know the Rule is infinite. */
len = rule_array->len;
for (i = 0; i < len; i++) {
rule = &g_array_index (rule_array, RuleData, i);
/* None of the Rules currently use the TYPE field, but we'd better check.
*/
if (rule->type) {
fprintf (stderr, "Rules %s has a TYPE: %s\n", name, rule->type);
exit (1);
}
if (rule->from_year != rule->to_year) {
from = rule->from_year;
to = rule->to_year;
tmp_rule = *rule;
/* Flag that this is a shallow copy so we don't free anything twice. */
tmp_rule.is_shallow_copy = TRUE;
/* See if it is an infinite Rule. */
if (to == YEAR_MAXIMUM) {
is_infinite = TRUE;
to = max_year;
if (from < to)
rule->to_year = rule->from_year;
} else {
is_infinite = FALSE;
}
/* Create a copy of the Rule for each year. */
for (year = from + 1; year <= to; year++) {
tmp_rule.from_year = year;
/* If the Rule is infinite, mark the last copy as infinite. */
if (year == to && is_infinite)
tmp_rule.to_year = YEAR_MAXIMUM;
else
tmp_rule.to_year = year;
g_array_append_val (rule_array, tmp_rule);
}
}
}
/* Now sort the rules. */
qsort (rule_array->data, rule_array->len, sizeof (RuleData), rule_sort_func);
#if 0
dump_rule_array (name, rule_array, stdout);
#endif
}
/* This is used to sort the rules, after the rules have all been expanded so
that each one is only for one year. */
static int
rule_sort_func (const void *arg1,
const void *arg2)
{
RuleData *rule1, *rule2;
int time1_year, time1_month, time1_day;
int time2_year, time2_month, time2_day;
int month_diff, result;
VzicTime t1, t2;
rule1 = (RuleData*) arg1;
rule2 = (RuleData*) arg2;
time1_year = rule1->from_year;
time1_month = rule1->in_month;
time2_year = rule2->from_year;
time2_month = rule2->in_month;
/* If there is more that one month difference we don't need to calculate
the day or time. */
month_diff = (time1_year - time2_year) * 12 + time1_month - time2_month;
if (month_diff > 1)
return 1;
if (month_diff < -1)
return -1;
/* Now we have to calculate the day and time of the Rule start and the
VzicTime, using the given offsets. */
t1.year = time1_year;
t1.month = time1_month;
t1.day_code = rule1->on_day_code;
t1.day_number = rule1->on_day_number;
t1.day_weekday = rule1->on_day_weekday;
t1.time_code = rule1->at_time_code;
t1.time_seconds = rule1->at_time_seconds;
t2.year = time2_year;
t2.month = time2_month;
t2.day_code = rule2->on_day_code;
t2.day_number = rule2->on_day_number;
t2.day_weekday = rule2->on_day_weekday;
t2.time_code = rule2->at_time_code;
t2.time_seconds = rule2->at_time_seconds;
/* FIXME: We don't know the offsets yet, but I don't think any Rules are
close enough together that the offsets can make a difference. Should
check this. */
calculate_actual_time (&t1, TIME_WALL, 0, 0);
calculate_actual_time (&t2, TIME_WALL, 0, 0);
/* Now we can compare the entire time. */
if (t1.year > t2.year)
result = 1;
else if (t1.year < t2.year)
result = -1;
else if (t1.month > t2.month)
result = 1;
else if (t1.month < t2.month)
result = -1;
else if (t1.day_number > t2.day_number)
result = 1;
else if (t1.day_number < t2.day_number)
result = -1;
else if (t1.time_seconds > t2.time_seconds)
result = 1;
else if (t1.time_seconds < t2.time_seconds)
result = -1;
else {
printf ("WARNING: Rule dates matched.\n");
result = 0;
}
return result;
}
static void
output_zone (char *directory,
ZoneData *zone,
char *zone_name,
const char *alias_of,
GHashTable *rule_data)
{
FILE *fp, *changes_fp = NULL;
char output_directory[PATHNAME_BUFFER_SIZE];
char filename[PATHNAME_BUFFER_SIZE];
char changes_filename[PATHNAME_BUFFER_SIZE];
char *zone_directory, *zone_subdirectory, *zone_filename;
/* Set a global for the zone_name, to be used only for debug messages. */
CurrentZoneName = zone_name;
/* Use this to only output a particular zone. */
#if 0
if (strcmp (zone_name, "Atlantic/Azores"))
return;
#endif
#if 0
printf ("Outputting Zone: %s\n", zone_name);
#endif
if (!parse_zone_name (zone_name, &zone_directory, &zone_subdirectory,
&zone_filename))
return;
if (VzicDumpZoneNamesAndCoords) {
VzicTimeZoneNames = g_list_prepend (VzicTimeZoneNames,
g_strdup (zone_name));
}
if (zone_subdirectory) {
sprintf (output_directory, "%s/%s/%s", directory, zone_directory,
zone_subdirectory);
ensure_directory_exists (output_directory);
sprintf (filename, "%s/%s.ics", output_directory, zone_filename);
if (VzicDumpChanges) {
sprintf (output_directory, "%s/ChangesVzic/%s/%s", directory,
zone_directory, zone_subdirectory);
ensure_directory_exists (output_directory);
sprintf (changes_filename, "%s/%s", output_directory, zone_filename);
}
}
else if (zone_directory) {
sprintf (output_directory, "%s/%s", directory, zone_directory);
ensure_directory_exists (output_directory);
sprintf (filename, "%s/%s.ics", output_directory, zone_filename);
if (VzicDumpChanges) {
sprintf (output_directory, "%s/ChangesVzic/%s", directory, zone_directory);
ensure_directory_exists (output_directory);
sprintf (changes_filename, "%s/%s", output_directory, zone_filename);
}
}
else {
sprintf (output_directory, "%s", directory);
ensure_directory_exists (output_directory);
sprintf (filename, "%s/%s.ics", output_directory, zone_filename);
if (VzicDumpChanges) {
sprintf (output_directory, "%s/ChangesVzic", directory);
ensure_directory_exists (output_directory);
sprintf (changes_filename, "%s/%s", output_directory, zone_filename);
}
}
/* Create the files. */
fp = fopen (filename, "w");
if (!fp) {
fprintf (stderr, "Couldn't create file: %s\n", filename);
exit (1);
}
if (VzicDumpChanges) {
changes_fp = fopen (changes_filename, "w");
if (!changes_fp) {
fprintf (stderr, "Couldn't create file: %s\n", changes_filename);
exit (1);
}
}
fprintf (fp, "BEGIN:VCALENDAR\r\nPRODID:%s\r\nVERSION:2.0\r\n", ProductID);
output_zone_to_files (zone, zone_name, alias_of, rule_data, fp, changes_fp);
if (ferror (fp)) {
fprintf (stderr, "Error writing file: %s\n", filename);
exit (1);
}
fprintf (fp, "END:VCALENDAR\r\n");
fclose (fp);
g_free (zone_directory);
g_free (zone_subdirectory);
g_free (zone_filename);
}
/* This checks that the Zone name only uses the characters in [-+_/a-zA-Z0-9],
and outputs a warning if it isn't. */
static gboolean
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;
gboolean 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 0
if (!first_slash_pos) {
#if 0
fprintf (stderr, "No '/' character in Zone name: %s. Skipping.\n", name);
#endif
return FALSE;
}
#endif
if (invalid) {
*directory = g_strdup ("Invalid");
*filename = g_strdup_printf ("Zone%i", invalid_zone_num++);
} else if (!first_slash_pos) {
*directory = NULL;
*subdirectory = NULL;
*filename = g_strdup (name);
} else {
*first_slash_pos = '\0';
*directory = g_strdup (name);
*first_slash_pos = '/';
if (second_slash_pos) {
*second_slash_pos = '\0';
*subdirectory = g_strdup (first_slash_pos + 1);
*second_slash_pos = '/';
*filename = g_strdup (second_slash_pos + 1);
} else {
*subdirectory = NULL;
*filename = g_strdup (first_slash_pos + 1);
}
}
return invalid ? FALSE : TRUE;
}
static void
output_zone_to_files (ZoneData *zone,
char *zone_name,
const char *alias_of,
GHashTable *rule_data,
FILE *fp,
FILE *changes_fp)
{
ZoneLineData *zone_line;
GArray *changes;
int i, stdoff, walloff, start_index, save_seconds;
VzicTime start, end, *vzictime_start, *vzictime, *vzictime_first_rule_change;
gboolean is_daylight, found_letter_s;
char *start_letter_s;
changes = g_array_new (FALSE, FALSE, sizeof (VzicTime));
vzictime_init (&start);
vzictime_init (&end);
/* The first period starts at -infinity. */
start.year = YEAR_MINIMUM;
for (i = 0; i < zone->zone_line_data->len; i++) {
zone_line = &g_array_index (zone->zone_line_data, ZoneLineData, i);
if (i == 0) start.is_infinite = (zone_line->rules == NULL);
/* This is the local standard time offset from GMT for this period. */
start.stdoff = stdoff = zone_line->stdoff_seconds;
start.walloff = walloff = stdoff + zone_line->save_seconds;
if (zone_line->until_set) {
end.year = zone_line->until_year;
end.month = zone_line->until_month;
end.day_code = zone_line->until_day_code;
end.day_number = zone_line->until_day_number;
end.day_weekday = zone_line->until_day_weekday;
end.time_seconds = zone_line->until_time_seconds;
end.time_code = zone_line->until_time_code;
} else {
/* The last period ends at +infinity. */
end.year = YEAR_MAXIMUM;
}
/* Add a time change for the start of the period. This may be removed
later if one of the rules expands to exactly the same time. */
start_index = changes->len;
g_array_append_val (changes, start);
/* If there are Rules associated with this period, add all the relevant
time changes. */
save_seconds = 0;
if (zone_line->rules)
found_letter_s = add_rule_changes (zone_line, zone_name, changes,
rule_data, &start, &end,
&start_letter_s, &save_seconds);
else
found_letter_s = FALSE;
/* FIXME: I'm not really sure what to do about finding a LETTER_S for the
first part of the period (i.e. before the first Rule comes into effect).
Currently we try to use the same LETTER_S as the first Rule of the
period which is in local standard time. */
if (zone_line->save_seconds)
save_seconds = zone_line->save_seconds;
is_daylight = save_seconds ? TRUE : FALSE;
vzictime_start = &g_array_index (changes, VzicTime, start_index);
walloff = vzictime_start->walloff = stdoff + save_seconds;
/* TEST: See if the first Rule time is exactly the same as the change from
the Zone line. In which case we can remove the Zone line change. */
if (changes->len > start_index + 1) {
int prev_stdoff, prev_walloff;
if (start_index > 0) {
VzicTime *v = &g_array_index (changes, VzicTime, start_index - 1);
prev_stdoff = v->stdoff;
prev_walloff = v->walloff;
} else {
prev_stdoff = 0;
prev_walloff = 0;
}
vzictime_first_rule_change = &g_array_index (changes, VzicTime,
start_index + 1);
if (times_match (vzictime_start, prev_stdoff, prev_walloff,
vzictime_first_rule_change, stdoff, walloff)) {
#if 0
printf ("Removing zone-line change (using new offsets)\n");
#endif
g_array_remove_index (changes, start_index);
vzictime_start = NULL;
} else if (times_match (vzictime_start, prev_stdoff, prev_walloff,
vzictime_first_rule_change, prev_stdoff, prev_walloff)) {
#if 0
printf ("Removing zone-line change (using previous offsets)\n");
#endif
g_array_remove_index (changes, start_index);
vzictime_start = NULL;
}
}
if (vzictime_start) {
vzictime_start->tzname = expand_tzname (zone_name, zone_line->format,
found_letter_s,
start_letter_s, is_daylight);
}
/* The start of the next Zone line is the end time of this one. */
start = end;
}
set_previous_offsets (changes);
output_zone_components (fp, zone_name, alias_of, changes);
if (VzicDumpChanges)
dump_changes (changes_fp, zone_name, changes);
/* Free all the TZNAME fields. */
for (i = 0; i < changes->len; i++) {
vzictime = &g_array_index (changes, VzicTime, i);
g_free (vzictime->tzname);
}
g_array_free (changes, TRUE);
}
/* This appends any timezone changes specified by the rules associated with
the timezone, that happen between the start and end times.
It returns the letter_s field of the first STANDARD rule found in the
search. We need this to fill in any %s in the FORMAT field of the first
component of the time period (the Zone line). */
static gboolean
add_rule_changes (ZoneLineData *zone_line,
char *zone_name,
GArray *changes,
GHashTable *rule_data,
VzicTime *start,
VzicTime *end,
char **start_letter_s,
int *save_seconds)
{
GArray *rule_array;
RuleData *rule, *prev_rule = NULL;
int stdoff, walloff, i, prev_stdoff, prev_walloff;
VzicTime vzictime;
gboolean is_daylight, found_start_letter_s = FALSE;
gboolean checked_for_previous = FALSE;
*save_seconds = 0;
rule_array = g_hash_table_lookup (rule_data, zone_line->rules);
if (!rule_array) {
fprintf (stderr, "Couldn't access rules: %s\n", zone_line->rules);
exit (1);
}
/* The stdoff is the same for all the rules. */
stdoff = start->stdoff;
/* The walloff changes as we go through the rules. */
walloff = start->walloff;
/* Get the stdoff & walloff from the last change before this period. */
if (changes->len >= 2) {
VzicTime *change = &g_array_index (changes, VzicTime, changes->len - 2);
prev_stdoff = change->stdoff;
prev_walloff = change->walloff;
} else {
prev_stdoff = prev_walloff = 0;
}
for (i = 0; i < rule_array->len; i++) {
int r;
rule = &g_array_index (rule_array, RuleData, i);
is_daylight = rule->save_seconds != 0 ? TRUE : FALSE;
vzictime_init (&vzictime);
vzictime.year = rule->from_year;
vzictime.month = rule->in_month;
vzictime.day_code = rule->on_day_code;
vzictime.day_number = rule->on_day_number;
vzictime.day_weekday = rule->on_day_weekday;
vzictime.time_seconds = rule->at_time_seconds;
vzictime.time_code = rule->at_time_code;
vzictime.stdoff = stdoff;
vzictime.walloff = stdoff + rule->save_seconds;
vzictime.is_infinite = (rule->to_year == YEAR_MAXIMUM) ? TRUE : FALSE;
/* If the rule time is before or on the given start time, skip it. */
r = compare_times (&vzictime, stdoff, walloff,
start, prev_stdoff, prev_walloff);
if (r <= 0) {
/* Our next rule may start while this one is in effect
so we keep track of its name.
This seems to eliminate the need to guess in expand_tzname()
but hasn't had enough testing to prove foolproof as of yet. */
found_start_letter_s = TRUE;
*start_letter_s = rule->letter_s;
if (r == 0 && vzictime.time_code != start->time_code) {
/* Rule time is on the given start time.
We want to keep the rule time (for the time_code) but we need to
adjust the time to be based on the last change before this period */
if (vzictime.time_code == TIME_WALL) {
/* Adjust for rule possibly starting during DST */
vzictime.time_seconds += prev_walloff - walloff;
}
else if (vzictime.time_code == TIME_STANDARD) {
/* Adjust for rule possibly starting at UTC change */
vzictime.time_seconds += prev_stdoff - stdoff;
}
}
else continue;
}
/* If the previous Rule was a daylight Rule, then we may want to use the
walloff from that. */
if (!checked_for_previous) {
checked_for_previous = TRUE;
if (i > 0) {
prev_rule = &g_array_index (rule_array, RuleData, i - 1);
if (prev_rule->save_seconds) {
walloff = start->walloff = stdoff + prev_rule->save_seconds;
*save_seconds = prev_rule->save_seconds;
found_start_letter_s = TRUE;
*start_letter_s = prev_rule->letter_s;
#if 0
printf ("Could use save_seconds from previous Rule: %s\n",
zone_name);
#endif
}
}
}
/* If an end time has been given, then if the rule time is on or after it
break out of the loop. */
if (end->year != YEAR_MAXIMUM
&& compare_times (&vzictime, stdoff, walloff,
end, stdoff, walloff) >= 0)
break;
vzictime.tzname = expand_tzname (zone_name, zone_line->format, TRUE,
rule->letter_s, is_daylight);
g_array_append_val (changes, vzictime);
/* When we find the first STANDARD time we set letter_s. */
if (!found_start_letter_s && !is_daylight) {
found_start_letter_s = TRUE;
*start_letter_s = rule->letter_s;
}
/* Now that we have added the Rule, the new walloff comes into effect
for any following Rules. */
walloff = vzictime.walloff;
}
/* If last Rule is terminating, flag it */
if (end->year == YEAR_MAXIMUM &&
rule->to_year >= 2037 && rule->to_year < YEAR_MAXIMUM) {
VzicTime *v = &g_array_index (changes, VzicTime, changes->len - 1);
v->until = v;
}
return found_start_letter_s;
}
/* This expands the Zone line FORMAT field, using the given LETTER_S from a
Rule line. There are 3 types of FORMAT field:
1. a string with an %s in, e.g. "WE%sT". The %s is replaced with LETTER_S.
2. a string with an '/' in, e.g. "CAT/CAWT". The first part is used for
standard time and the second part for when daylight-saving is in effect.
3. a plain string, e.g. "LMT", which we leave as-is.
Note that (1) is the only type in which letter_s is required.
*/
static char*
expand_tzname (char *zone_name,
char *format,
gboolean have_letter_s,
char *letter_s,
gboolean is_daylight)
{
char *p, buffer[256], *guess = NULL;
int len;
#if 0
printf ("Expanding %s with %s\n", format, letter_s);
#endif
if (!format || !format[0]) {
fprintf (stderr, "Missing FORMAT\n");
exit (1);
}
/* 1. Look for a "%s". */
p = strchr (format, '%');
if (p && *(p + 1) == 's') {
if (!have_letter_s) {
/* NOTE: These are a few hard-coded TZNAMEs that I've looked up myself.
These are needed in a few places where a Zone line comes into effect
but no Rule has been found, so we have no LETTER_S to use.
I've tried to use whatever is the normal LETTER_S in the Rules for
the particular zone, in local standard time. */
if (!strcmp (zone_name, "Asia/Macao")
&& !strcmp (format, "C%sT"))
guess = "CST";
else if (!strcmp (zone_name, "Asia/Macau")
&& !strcmp (format, "C%sT"))
guess = "CST";
else if (!strcmp (zone_name, "Asia/Ashgabat")
&& !strcmp (format, "ASH%sT"))
guess = "ASHT";
else if (!strcmp (zone_name, "Asia/Ashgabat")
&& !strcmp (format, "TM%sT"))
guess = "TMT";
else if (!strcmp (zone_name, "Asia/Samarkand")
&& !strcmp (format, "TAS%sT"))
guess = "TAST";
else if (!strcmp (zone_name, "Atlantic/Azores")
&& !strcmp (format, "WE%sT"))
guess = "WET";
else if (!strcmp (zone_name, "Europe/Paris")
&& !strcmp (format, "WE%sT"))
guess = "WET";
else if (!strcmp (zone_name, "Europe/Warsaw")
&& !strcmp (format, "CE%sT"))
guess = "CET";
else if (!strcmp (zone_name, "America/Phoenix")
&& !strcmp (format, "M%sT"))
guess = "MST";
else if (!strcmp (zone_name, "America/Nome")
&& !strcmp (format, "Y%sT"))
guess = "YST";
if (guess) {
#if 0
fprintf (stderr,
"WARNING: Couldn't find a LETTER_S to use in FORMAT: %s in Zone: %s Guessing: %s\n",