summaryrefslogtreecommitdiff
path: root/src/libical/icaltimezone.c
blob: 1826d2d0097a7c015475f9eb6677bc711ee2e584 (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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
/*======================================================================
 FILE: icaltimezone.c
 CREATOR: Damon Chaplin 15 March 2001

 (C) COPYRIGHT 2001, Damon Chaplin <damon@ximian.com>

 This library is free software; you can redistribute it and/or modify
 it under the terms of either:

    The LGPL as published by the Free Software Foundation, version
    2.1, available at: https://www.gnu.org/licenses/lgpl-2.1.html

 Or:

    The Mozilla Public License Version 2.0. You may obtain a copy of
    the License at https://www.mozilla.org/MPL/
======================================================================*/
//krazy:excludeall=cpp

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "icaltimezone.h"
#include "icaltimezoneimpl.h"
#include "icalarray.h"
#include "icalerror.h"
#include "icalparser.h"
#include "icalmemory.h"
#include "icaltz-util.h"

#include <ctype.h>
#include <stddef.h>     /* for ptrdiff_t */
#include <stdlib.h>
#include <limits.h>

#if defined(HAVE_PTHREAD)
#include <pthread.h>
#if defined(PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP)
// It seems the same thread can attempt to lock builtin_mutex multiple times
// (at least when using builtin tzdata), so make it builtin_mutex recursive:
static pthread_mutex_t builtin_mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
#else
static pthread_mutex_t builtin_mutex = PTHREAD_MUTEX_INITIALIZER;
#endif
// To avoid use-after-free in multithreaded applications when accessing icaltimezone::changes
static pthread_mutex_t changes_mutex = PTHREAD_MUTEX_INITIALIZER;
#endif

#if defined(_WIN32)
#if !defined(_WIN32_WCE)
#include <mbstring.h>
#endif
#include <windows.h>
#endif

/** This is the toplevel directory where the timezone data is installed in. */
#define ZONEINFO_DIRECTORY      PACKAGE_DATA_DIR "/zoneinfo"

/** The prefix we use to uniquely identify TZIDs.
    It must begin and end with forward slashes.
 */
#define BUILTIN_TZID_PREFIX_LEN 256
#define BUILTIN_TZID_PREFIX     "/freeassociation.sourceforge.net/"

/* Known prefixes from the old versions of libical */
static struct _compat_tzids {
    const char *tzid;
    int slashes;
} glob_compat_tzids[] = {
    { "/freeassociation.sourceforge.net/Tzfile/", 3 },
    { "/freeassociation.sourceforge.net/", 2 },
    { "/citadel.org/", 3 }, /* Full TZID for this can be: "/citadel.org/20190914_1/" */
    { NULL, -1 }
};

/* The prefix to be used for tzid's generated from system tzdata */
static char s_ical_tzid_prefix[BUILTIN_TZID_PREFIX_LEN] = {0};

/** This is the filename of the file containing the city names and
    coordinates of all the builtin timezones. */
#define ZONES_TAB_FILENAME      "zones.tab"

/** This is the number of years of extra coverage we do when expanding
    the timezone changes. */
#define ICALTIMEZONE_EXTRA_COVERAGE     5

#if (SIZEOF_TIME_T > 4)
/** Arbitrarily go up to 1000th anniversary of Gregorian calendar, since
    64-bit time_t values get us up to the tm_year limit of 2+ billion years. */
#define ICALTIMEZONE_MAX_YEAR           2582
#else
/** This is the maximum year we will expand to, since 32-bit time_t values
    only go up to the start of 2038. */
#define ICALTIMEZONE_MAX_YEAR           2037
#endif

typedef struct _icaltimezonechange icaltimezonechange;

struct _icaltimezonechange
{
    int utc_offset;
    /**< The offset to add to UTC to get local time, in seconds. */

    int prev_utc_offset;
    /**< The offset to add to UTC, before this change, in seconds. */

    int year;                   /**< Actual year, e.g. 2001. */
    int month;                  /**< 1 (Jan) to 12 (Dec). */
    int day;
    int hour;
    int minute;
    int second;
    /**< The time that the change came into effect, in UTC.
       Note that the prev_utc_offset applies to this local time,
       since we haven't changed to the new offset yet. */

    int is_daylight;
    /**< Whether this is STANDARD or DAYLIGHT time. */
};

/** An array of icaltimezones for the builtin timezones. */
static icalarray *builtin_timezones = NULL;

/** This is the special UTC timezone, which isn't in builtin_timezones. */
static icaltimezone utc_timezone = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };

static char *zone_files_directory = NULL;

#if defined(USE_BUILTIN_TZDATA)
static int use_builtin_tzdata = 1;
#else
static int use_builtin_tzdata = 0;
#endif

static void icaltimezone_reset(icaltimezone *zone);
static void icaltimezone_expand_changes(icaltimezone *zone, int end_year);
static int icaltimezone_compare_change_fn(const void *elem1, const void *elem2);

static size_t icaltimezone_find_nearby_change(icaltimezone *zone, icaltimezonechange *change);

static void icaltimezone_adjust_change(icaltimezonechange *tt,
                                       int days, int hours, int minutes, int seconds);

static void icaltimezone_init(icaltimezone *zone);

/** @brief Gets the TZID, LOCATION/X-LIC-LOCATION, and TZNAME properties from
 * the VTIMEZONE component and places them in the icaltimezone.
 *
 * @returns 1 on success, or 0 if the TZID can't be found.
 */
static int icaltimezone_get_vtimezone_properties(icaltimezone *zone, icalcomponent *component)
#if defined(THREAD_SANITIZER)
__attribute__((no_sanitize("thread")))
#endif
;

static void icaltimezone_load_builtin_timezone(icaltimezone *zone)
#if defined(THREAD_SANITIZER)
__attribute__((no_sanitize("thread")))
#endif
;

static void icaltimezone_ensure_coverage(icaltimezone *zone, int end_year);

static void icaltimezone_init_builtin_timezones(void);

static void icaltimezone_parse_zone_tab(void);

static char *icaltimezone_load_get_line_fn(char *s, size_t size, void *data);

static void format_utc_offset(int utc_offset, char *buffer, size_t buffer_size);
static const char *get_zone_directory(void);

static void icaltimezone_builtin_lock(void)
{
#if defined(HAVE_PTHREAD)
    pthread_mutex_lock(&builtin_mutex);
#endif
}

static void icaltimezone_builtin_unlock(void)
{
#if defined(HAVE_PTHREAD)
    pthread_mutex_unlock(&builtin_mutex);
#endif
}

static void icaltimezone_changes_lock(void)
{
#if defined(HAVE_PTHREAD)
    pthread_mutex_lock(&changes_mutex);
#endif
}

static void icaltimezone_changes_unlock(void)
{
#if defined(HAVE_PTHREAD)
    pthread_mutex_unlock(&changes_mutex);
#endif
}

const char *icaltimezone_tzid_prefix(void)
{
    if (s_ical_tzid_prefix[0] == '\0') {
        strncpy(s_ical_tzid_prefix, BUILTIN_TZID_PREFIX, BUILTIN_TZID_PREFIX_LEN-1);
    }
    return s_ical_tzid_prefix;
}

icaltimezone *icaltimezone_new(void)
{
    icaltimezone *zone;

    zone = (icaltimezone *) malloc(sizeof(icaltimezone));
    if (!zone) {
        icalerror_set_errno(ICAL_NEWFAILED_ERROR);
        return NULL;
    }

    icaltimezone_init(zone);

    return zone;
}

icaltimezone *icaltimezone_copy(icaltimezone *originalzone)
{
    icaltimezone *zone;

    zone = (icaltimezone *) malloc(sizeof(icaltimezone));
    if (!zone) {
        icalerror_set_errno(ICAL_NEWFAILED_ERROR);
        return NULL;
    }

    memcpy(zone, originalzone, sizeof(icaltimezone));
    if (zone->tzid != NULL) {
        zone->tzid = strdup(zone->tzid);
    }
    if (zone->location != NULL) {
        zone->location = strdup(zone->location);
    }
    if (zone->tznames != NULL) {
        zone->tznames = strdup(zone->tznames);
    }

    icaltimezone_changes_lock();
    if (zone->changes != NULL) {
        zone->changes = icalarray_copy(zone->changes);
    }
    icaltimezone_changes_unlock();

    /* Let the caller set the component because then they will
       know to be careful not to free this reference twice. */
    zone->component = NULL;

    return zone;
}

void icaltimezone_free(icaltimezone *zone, int free_struct)
{
    icaltimezone_reset(zone);
    if (free_struct)
        free(zone);
}

/** @brief Resets the icaltimezone to the initial state, freeing most of the
 * fields.
 */
static void icaltimezone_reset(icaltimezone *zone)
{
    if (zone->tzid)
        free(zone->tzid);

    if (zone->location)
        free(zone->location);

    if (zone->tznames)
        free(zone->tznames);

    if (zone->component)
        icalcomponent_free(zone->component);

    //    icaltimezone_changes_lock();
    if (zone->changes) {
        icalarray_free(zone->changes);
        zone->changes = NULL;
    }
    //    icaltimezone_changes_unlock();

    icaltimezone_init(zone);
}

/** @brief Initializes an icaltimezone. */
static void icaltimezone_init(icaltimezone *zone)
{
    zone->tzid = NULL;
    zone->location = NULL;
    zone->tznames = NULL;
    zone->latitude = 0.0;
    zone->longitude = 0.0;
    zone->component = NULL;
    zone->builtin_timezone = NULL;
    zone->end_year = 0;
    zone->changes = NULL;
}

/** @brief Gets the TZID, LOCATION/X-LIC-LOCATION and TZNAME properties of
 * the VTIMEZONE component and stores them in the icaltimezone.
 *
 * @returns 1 on success, or 0 if the TZID can't be found.
 *
 * Note that it expects the zone to be initialized or reset - it doesn't free
 * any old values.
 */
static int icaltimezone_get_vtimezone_properties(icaltimezone *zone, icalcomponent *component)
{
    icalproperty *prop;
    const char *tzid;

    prop = icalcomponent_get_first_property(component, ICAL_TZID_PROPERTY);
    if (!prop)
        return 0;

    /* A VTIMEZONE MUST have a TZID, or a lot of our code won't work. */
    tzid = icalproperty_get_tzid(prop);
    if (!tzid) {
        return 0;
    }

    if (zone->tzid) {
        free(zone->tzid);
    }
    zone->tzid = strdup(tzid);

    if (zone->component) {
        icalcomponent_free(zone->component);
    }
    zone->component = component;

    if (zone->location) {
        free(zone->location);
    }
    zone->location = icaltimezone_get_location_from_vtimezone(component);

    if (zone->tznames) {
        free(zone->tznames);
    }
    zone->tznames = icaltimezone_get_tznames_from_vtimezone(component);

    return 1;
}

char *icaltimezone_get_location_from_vtimezone(icalcomponent *component)
{
    icalproperty *prop;
    const char *location;
    const char *name;

    prop = icalcomponent_get_first_property(component, ICAL_LOCATION_PROPERTY);
    if (prop) {
        location = icalproperty_get_location(prop);
        if (location)
            return strdup(location);
    }

    prop = icalcomponent_get_first_property(component, ICAL_X_PROPERTY);
    while (prop) {
        name = icalproperty_get_x_name(prop);
        if (name && !strcasecmp(name, "X-LIC-LOCATION")) {
            location = icalproperty_get_x(prop);
            if (location)
                return strdup(location);
        }
        prop = icalcomponent_get_next_property(component, ICAL_X_PROPERTY);
    }

    return NULL;
}

char *icaltimezone_get_tznames_from_vtimezone(icalcomponent *component)
{
    icalcomponent *comp;
    icalcomponent_kind type;
    icalproperty *prop;
    struct icaltimetype dtstart;
    struct icaldatetimeperiodtype rdate;
    const char *current_tzname;
    const char *standard_tzname = NULL, *daylight_tzname = NULL;
    struct icaltimetype standard_max_date, daylight_max_date;
    struct icaltimetype current_max_date;

    standard_max_date = icaltime_null_time();
    daylight_max_date = icaltime_null_time();

    /* Step through the STANDARD & DAYLIGHT subcomponents. */
    comp = icalcomponent_get_first_component(component, ICAL_ANY_COMPONENT);
    while (comp) {
        type = icalcomponent_isa(comp);
        if (type == ICAL_XSTANDARD_COMPONENT || type == ICAL_XDAYLIGHT_COMPONENT) {
            current_max_date = icaltime_null_time();
            current_tzname = NULL;

            /* Step through the properties. We want to find the TZNAME, and
               the largest DTSTART or RDATE. */
            prop = icalcomponent_get_first_property(comp, ICAL_ANY_PROPERTY);
            while (prop) {
                switch (icalproperty_isa(prop)) {
                case ICAL_TZNAME_PROPERTY:
                    current_tzname = icalproperty_get_tzname(prop);
                    break;

                case ICAL_DTSTART_PROPERTY:
                    dtstart = icalproperty_get_dtstart(prop);
                    if (icaltime_compare(dtstart, current_max_date) > 0)
                        current_max_date = dtstart;

                    break;

                case ICAL_RDATE_PROPERTY:
                    rdate = icalproperty_get_rdate(prop);
                    if (icaltime_compare(rdate.time, current_max_date) > 0)
                        current_max_date = rdate.time;

                    break;

                default:
                    break;
                }

                prop = icalcomponent_get_next_property(comp, ICAL_ANY_PROPERTY);
            }

            if (current_tzname) {
                if (type == ICAL_XSTANDARD_COMPONENT) {
                    if (!standard_tzname ||
                        icaltime_compare(current_max_date, standard_max_date) > 0) {
                        standard_max_date = current_max_date;
                        standard_tzname = current_tzname;
                    }
                } else {
                    if (!daylight_tzname ||
                        icaltime_compare(current_max_date, daylight_max_date) > 0) {
                        daylight_max_date = current_max_date;
                        daylight_tzname = current_tzname;
                    }
                }
            }
        }

        comp = icalcomponent_get_next_component(component, ICAL_ANY_COMPONENT);
    }

    /* Outlook (2000) places "Standard Time" and "Daylight Time" in the TZNAME
       strings, which is totally useless. So we return NULL in that case. */
    if (standard_tzname && !strcmp(standard_tzname, "Standard Time"))
        return NULL;

    /* If both standard and daylight TZNAMEs were found, if they are the same
       we return just one, else we format them like "EST/EDT". */
    if (standard_tzname && daylight_tzname) {
        size_t standard_len, daylight_len;
        char *tznames;

        if (!strcmp(standard_tzname, daylight_tzname))
            return strdup(standard_tzname);

        standard_len = strlen(standard_tzname);
        daylight_len = strlen(daylight_tzname);
        tznames = malloc(standard_len + daylight_len + 2);
        strcpy(tznames, standard_tzname);
        tznames[standard_len] = '/';
        strcpy(tznames + standard_len + 1, daylight_tzname);
        return tznames;
    } else {
        const char *tznames;

        /* If either of the TZNAMEs was found just return that, else NULL. */
        tznames = standard_tzname ? standard_tzname : daylight_tzname;
        return tznames ? strdup(tznames) : NULL;
    }
}

static void icaltimezone_ensure_coverage(icaltimezone *zone, int end_year)
{
    /* When we expand timezone changes we always expand at least up to this
       year, plus ICALTIMEZONE_EXTRA_COVERAGE. */
    static int icaltimezone_minimum_expansion_year = -1;

    int changes_end_year;

    icaltimezone_load_builtin_timezone(zone);

    if (icaltimezone_minimum_expansion_year == -1) {
        struct icaltimetype today = icaltime_today();

        icaltimezone_minimum_expansion_year = today.year;
    }

    changes_end_year = end_year;
    if (changes_end_year < icaltimezone_minimum_expansion_year)
        changes_end_year = icaltimezone_minimum_expansion_year;

    changes_end_year += ICALTIMEZONE_EXTRA_COVERAGE;

    if (changes_end_year > ICALTIMEZONE_MAX_YEAR)
        changes_end_year = ICALTIMEZONE_MAX_YEAR;

    if (!zone->changes || zone->end_year < end_year)
        icaltimezone_expand_changes(zone, changes_end_year);
}

/* Hold the icaltimezone_changes_lock(); before calling this function */
static void icaltimezone_expand_changes(icaltimezone *zone, int end_year)
{
    icalarray *changes;
    icalcomponent *comp;

#if 0
    printf("\nExpanding changes for: %s to year: %i\n", zone->tzid, end_year);
#endif

    changes = icalarray_new(sizeof(icaltimezonechange), 32);
    if (!changes)
        return;

    /* Scan the STANDARD and DAYLIGHT subcomponents. */
    comp = icalcomponent_get_first_component(zone->component, ICAL_ANY_COMPONENT);
    while (comp) {
        icaltimezone_expand_vtimezone(comp, end_year, changes);
        comp = icalcomponent_get_next_component(zone->component, ICAL_ANY_COMPONENT);
    }

    /* Sort the changes. We may have duplicates but I don't think it will
       matter. */
    icalarray_sort(changes, icaltimezone_compare_change_fn);

    if (zone->changes) {
        icalarray_free(zone->changes);
        zone->changes = 0;
    }

    zone->changes = changes;
    zone->end_year = end_year;
}

void icaltimezone_expand_vtimezone(icalcomponent *comp, int end_year, icalarray *changes)
{
    icaltimezonechange change;
    icalproperty *prop;
    struct icaltimetype dtstart, occ;
    struct icalrecurrencetype rrule;
    icalrecur_iterator *rrule_iterator;
    struct icaldatetimeperiodtype rdate;
    int found_dtstart = 0, found_tzoffsetto = 0, found_tzoffsetfrom = 0;
    int has_rdate = 0, has_rrule = 0;

    /* First we check if it is a STANDARD or DAYLIGHT component, and
       just return if it isn't. */
    if (icalcomponent_isa(comp) == ICAL_XSTANDARD_COMPONENT) {
        change.is_daylight = 0;
    } else if (icalcomponent_isa(comp) == ICAL_XDAYLIGHT_COMPONENT) {
        change.is_daylight = 1;
    } else {
        return;
    }

    /* Step through each of the properties to find the DTSTART,
       TZOFFSETFROM and TZOFFSETTO. We can't expand recurrences here
       since we need these properties before we can do that. */
    prop = icalcomponent_get_first_property(comp, ICAL_ANY_PROPERTY);
    while (prop) {
        switch (icalproperty_isa(prop)) {
        case ICAL_DTSTART_PROPERTY:
            dtstart = icalproperty_get_dtstart(prop);
            found_dtstart = 1;
            break;
        case ICAL_TZOFFSETTO_PROPERTY:
            change.utc_offset = icalproperty_get_tzoffsetto(prop);
            /*printf ("Found TZOFFSETTO: %i\n", change.utc_offset); */
            found_tzoffsetto = 1;
            break;
        case ICAL_TZOFFSETFROM_PROPERTY:
            change.prev_utc_offset = icalproperty_get_tzoffsetfrom(prop);
            /*printf ("Found TZOFFSETFROM: %i\n", change.prev_utc_offset); */
            found_tzoffsetfrom = 1;
            break;
        case ICAL_RDATE_PROPERTY:
            has_rdate = 1;
            break;
        case ICAL_RRULE_PROPERTY:
            has_rrule = 1;
            break;
        default:
            /* Just ignore any other properties. */
            break;
        }

        prop = icalcomponent_get_next_property(comp, ICAL_ANY_PROPERTY);
    }

    /* Microsoft Outlook for Mac (and possibly other versions) will create
       timezones without a tzoffsetfrom property if it's a timezone that
       doesn't change for DST. */
    if (found_tzoffsetto && !found_tzoffsetfrom) {
        change.prev_utc_offset = change.utc_offset;
        found_tzoffsetfrom = 1;
    }

    /* If we didn't find a DTSTART, TZOFFSETTO and TZOFFSETFROM we have to
       ignore the component. FIXME: Add an error property? */
    if (!found_dtstart || !found_tzoffsetto || !found_tzoffsetfrom)
        return;

#if 0
    printf("\n Expanding component DTSTART (Y/M/D): %i/%i/%i %i:%02i:%02i\n",
           dtstart.year, dtstart.month, dtstart.day, dtstart.hour, dtstart.minute, dtstart.second);
#endif

    /* If the STANDARD/DAYLIGHT component has no recurrence rule, we add
       a single change for the DTSTART. */
    if (!has_rrule) {
        change.year = dtstart.year;
        change.month = dtstart.month;
        change.day = dtstart.day;
        change.hour = dtstart.hour;
        change.minute = dtstart.minute;
        change.second = dtstart.second;

        /* Convert to UTC. */
        icaltimezone_adjust_change(&change, 0, 0, 0, -change.prev_utc_offset);

#if 0
        printf("  Appending single DTSTART (Y/M/D): %i/%02i/%02i %i:%02i:%02i\n",
               change.year, change.month, change.day, change.hour, change.minute, change.second);
#endif

        /* Add the change to the array. */
        icalarray_append(changes, &change);
    }

    /* The component has recurrence data, so we expand that now. */
    prop = icalcomponent_get_first_property(comp, ICAL_ANY_PROPERTY);
    while (prop && (has_rdate || has_rrule)) {
#if 0
        printf("Expanding property...\n");
#endif
        switch (icalproperty_isa(prop)) {
        case ICAL_RDATE_PROPERTY:
            rdate = icalproperty_get_rdate(prop);
            change.year = rdate.time.year;
            change.month = rdate.time.month;
            change.day = rdate.time.day;
            /* RDATEs with a DATE value inherit the time from
               the DTSTART. */
            if (icaltime_is_date(rdate.time)) {
                change.hour = dtstart.hour;
                change.minute = dtstart.minute;
                change.second = dtstart.second;
            } else {
                change.hour = rdate.time.hour;
                change.minute = rdate.time.minute;
                change.second = rdate.time.second;

                /* The spec was a bit vague about whether RDATEs were in local
                   time or UTC so we support both to be safe. So if it is in
                   UTC we have to add the UTC offset to get a local time. */
                if (!icaltime_is_utc(rdate.time))
                    icaltimezone_adjust_change(&change, 0, 0, 0, -change.prev_utc_offset);
            }

#if 0
            printf("  Appending RDATE element (Y/M/D): %i/%02i/%02i %i:%02i:%02i\n",
                   change.year, change.month, change.day,
                   change.hour, change.minute, change.second);
#endif

            icalarray_append(changes, &change);
            break;
        case ICAL_RRULE_PROPERTY:
            rrule = icalproperty_get_rrule(prop);

            /* If the rrule UNTIL value is set and is in UTC, we convert it to
               a local time, since the recurrence code has no way to convert
               it itself. */
            if (!icaltime_is_null_time(rrule.until) && icaltime_is_utc(rrule.until)) {
#if 0
                printf("  Found RRULE UNTIL in UTC.\n");
#endif

                /* To convert from UTC to a local time, we use the TZOFFSETFROM
                   since that is the offset from UTC that will be in effect
                   when each of the RRULE occurrences happens. */
                icaltime_adjust(&rrule.until, 0, 0, 0, change.prev_utc_offset);
                rrule.until.zone = NULL;
            }

            /* Add the dtstart to changes, otherwise some oddly-defined VTIMEZONE
               components can cause the first year to get skipped. */
            change.year = dtstart.year;
            change.month = dtstart.month;
            change.day = dtstart.day;
            change.hour = dtstart.hour;
            change.minute = dtstart.minute;
            change.second = dtstart.second;

#if 0
            printf("  Appending RRULE element (Y/M/D): %i/%02i/%02i %i:%02i:%02i\n",
                   change.year, change.month, change.day,
                   change.hour, change.minute, change.second);
#endif

            icaltimezone_adjust_change(&change, 0, 0, 0, -change.prev_utc_offset);

            icalarray_append(changes, &change);

            rrule_iterator = icalrecur_iterator_new(rrule, dtstart);
            for (; rrule_iterator;) {
                occ = icalrecur_iterator_next(rrule_iterator);
                /* Skip dtstart since we just added it */
                if (icaltime_compare(dtstart, occ) == 0) {
                    continue;
                }
                if (occ.year > end_year || icaltime_is_null_time(occ)) {
                    break;
                }
                change.year = occ.year;
                change.month = occ.month;
                change.day = occ.day;
                change.hour = occ.hour;
                change.minute = occ.minute;
                change.second = occ.second;

#if 0
                printf("  Appending RRULE element (Y/M/D): %i/%02i/%02i %i:%02i:%02i\n",
                       change.year, change.month, change.day,
                       change.hour, change.minute, change.second);
#endif

                icaltimezone_adjust_change(&change, 0, 0, 0, -change.prev_utc_offset);

                icalarray_append(changes, &change);
            }

            icalrecur_iterator_free(rrule_iterator);
            break;
        default:
            break;
        }

        prop = icalcomponent_get_next_property(comp, ICAL_ANY_PROPERTY);
    }
}

/** @brief A function to compare 2 icaltimezonechange elements, used for
 * qsort().
 */
static int icaltimezone_compare_change_fn(const void *elem1, const void *elem2)
{
    const icaltimezonechange *change1, *change2;
    int retval;

    change1 = (const icaltimezonechange *)elem1;
    change2 = (const icaltimezonechange *)elem2;

    if (change1->year < change2->year) {
        retval = -1;
    } else if (change1->year > change2->year) {
        retval = 1;
    } else if (change1->month < change2->month) {
        retval = -1;
    } else if (change1->month > change2->month) {
        retval = 1;
    } else if (change1->day < change2->day) {
        retval = -1;
    } else if (change1->day > change2->day) {
        retval = 1;
    } else if (change1->hour < change2->hour) {
        retval = -1;
    } else if (change1->hour > change2->hour) {
        retval = 1;
    } else if (change1->minute < change2->minute) {
        retval = -1;
    } else if (change1->minute > change2->minute) {
        retval = 1;
    } else if (change1->second < change2->second) {
        retval = -1;
    } else if (change1->second > change2->second) {
        retval = 1;
    } else {
        retval = 0;
    }

    return retval;
}

void icaltimezone_convert_time(struct icaltimetype *tt,
                               icaltimezone *from_zone, icaltimezone *to_zone)
{
    int utc_offset, is_daylight;

    /* If the time is a DATE value or both timezones are the same, or we are
       converting a floating time, we don't need to do anything. */
    if (icaltime_is_date(*tt) || from_zone == to_zone || from_zone == NULL)
        return;

    /* Convert the time to UTC by getting the UTC offset and subtracting it. */
    utc_offset = icaltimezone_get_utc_offset(from_zone, tt, NULL);
    icaltime_adjust(tt, 0, 0, 0, -utc_offset);

    /* Now we convert the time to the new timezone by getting the UTC offset
       of our UTC time and adding it. */
    utc_offset = icaltimezone_get_utc_offset_of_utc_time(to_zone, tt, &is_daylight);
    tt->is_daylight = is_daylight;
    icaltime_adjust(tt, 0, 0, 0, utc_offset);
}

int icaltimezone_get_utc_offset(icaltimezone *zone, struct icaltimetype *tt, int *is_daylight)
{
    icaltimezonechange *zone_change, *prev_zone_change, tt_change, tmp_change;
    size_t change_num, change_num_to_use;
    int found_change;
    int step, utc_offset_change, cmp;
    int want_daylight;

    if (tt == NULL)
        return 0;

    if (is_daylight)
        *is_daylight = 0;

    /* For local times and UTC return 0. */
    if (zone == NULL || zone == &utc_timezone)
        return 0;

    /* Use the builtin icaltimezone if possible. */
    if (zone->builtin_timezone)
        zone = zone->builtin_timezone;

    icaltimezone_changes_lock();

    /* Make sure the changes array is expanded up to the given time. */
    icaltimezone_ensure_coverage(zone, tt->year);

    if (!zone->changes || zone->changes->num_elements == 0) {
        icaltimezone_changes_unlock();
        return 0;
    }

    /* Copy the time parts of the icaltimetype to an icaltimezonechange so we
       can use our comparison function on it. */
    tt_change.year = tt->year;
    tt_change.month = tt->month;
    tt_change.day = tt->day;
    tt_change.hour = tt->hour;
    tt_change.minute = tt->minute;
    tt_change.second = tt->second;

    /* This should find a change close to the time, either the change before
       it or the change after it. */
    change_num = icaltimezone_find_nearby_change(zone, &tt_change);

    /* Now move backwards or forwards to find the timezone change that applies
       to tt. It should only have to do 1 or 2 steps. */
    zone_change = icalarray_element_at(zone->changes, change_num);
    step = 1;
    found_change = 0;
    change_num_to_use = -1;
    for (;;) {
        /* Copy the change, so we can adjust it. */
        tmp_change = *zone_change;

        /* If the clock is going backward, check if it is in the region of time
           that is used twice. If it is, use the change with the daylight
           setting which matches tt, or use standard if we don't know. */
        if (tmp_change.utc_offset < tmp_change.prev_utc_offset) {
            /* If the time change is at 2:00AM local time and the clock is
               going back to 1:00AM we adjust the change to 1:00AM. We may
               have the wrong change but we'll figure that out later. */
            icaltimezone_adjust_change(&tmp_change, 0, 0, 0, tmp_change.utc_offset);
        } else {
            icaltimezone_adjust_change(&tmp_change, 0, 0, 0, tmp_change.prev_utc_offset);
        }

        cmp = icaltimezone_compare_change_fn(&tt_change, &tmp_change);

        /* If the given time is on or after this change, then this change may
           apply, but we continue as a later change may be the right one.
           If the given time is before this change, then if we have already
           found a change which applies we can use that, else we need to step
           backwards. */
        if (cmp >= 0) {
            found_change = 1;
            change_num_to_use = change_num;
        } else {
            step = -1;
        }

        /* If we are stepping backwards through the changes and we have found
           a change that applies, then we know this is the change to use so
           we exit the loop. */
        if (step == -1 && found_change == 1)
            break;

        /* If we go past the start of the changes array, then we have no data
           for this time so we return the prev UTC offset. */
        if (change_num == 0 && step < 0) {
            if (is_daylight) {
                *is_daylight = ! tmp_change.is_daylight;
            }

            icaltimezone_changes_unlock();

            return tmp_change.prev_utc_offset;
        }

        change_num += step;

        if (change_num >= zone->changes->num_elements)
            break;

        zone_change = icalarray_element_at(zone->changes, change_num);
    }

    /* If we didn't find a change to use, then we have a bug! */
    icalerror_assert(found_change == 1, "No applicable timezone change found");

    /* Now we just need to check if the time is in the overlapped region of
       time when clocks go back. */
    zone_change = icalarray_element_at(zone->changes, change_num_to_use);

    utc_offset_change = zone_change->utc_offset - zone_change->prev_utc_offset;
    if (utc_offset_change < 0 && change_num_to_use > 0) {
        tmp_change = *zone_change;
        icaltimezone_adjust_change(&tmp_change, 0, 0, 0, tmp_change.prev_utc_offset);

        if (icaltimezone_compare_change_fn(&tt_change, &tmp_change) < 0) {
            /* The time is in the overlapped region, so we may need to use
               either the current zone_change or the previous one. If the
               time has the is_daylight field set we use the matching change,
               else we use the change with standard time. */
            prev_zone_change = icalarray_element_at(zone->changes, change_num_to_use - 1);

            /* I was going to add an is_daylight flag to struct icaltimetype,
               but iCalendar doesn't let us distinguish between standard and
               daylight time anyway, so there's no point. So we just use the
               standard time instead. */
            want_daylight = (tt->is_daylight == 1) ? 1 : 0;

#if 0
            if (zone_change->is_daylight == prev_zone_change->is_daylight) {
                printf(" **** Same is_daylight setting\n");
            }
#endif

            if (zone_change->is_daylight != want_daylight &&
                prev_zone_change->is_daylight == want_daylight) {
                zone_change = prev_zone_change;
            }
        }
    }

    /* Now we know exactly which timezone change applies to the time, so
       we can return the UTC offset and whether it is a daylight time. */
    if (is_daylight) {
        *is_daylight = zone_change->is_daylight;
    }
    utc_offset_change = zone_change->utc_offset;

    icaltimezone_changes_unlock();

    return utc_offset_change;
}

int icaltimezone_get_utc_offset_of_utc_time(icaltimezone *zone,
                                            struct icaltimetype *tt, int *is_daylight)
{
    icaltimezonechange *zone_change, tt_change, tmp_change;
    size_t change_num, change_num_to_use;
    int found_change = 1;
    int step, utc_offset;

    if (is_daylight)
        *is_daylight = 0;

    /* For local times and UTC return 0. */
    if (zone == NULL || zone == &utc_timezone)
        return 0;

    /* Use the builtin icaltimezone if possible. */
    if (zone->builtin_timezone)
        zone = zone->builtin_timezone;

    icaltimezone_changes_lock();

    /* Make sure the changes array is expanded up to the given time. */
    icaltimezone_ensure_coverage(zone, tt->year);

    if (!zone->changes || zone->changes->num_elements == 0) {
        icaltimezone_changes_unlock();
        return 0;
    }

    /* Copy the time parts of the icaltimetype to an icaltimezonechange so we
       can use our comparison function on it. */
    tt_change.year = tt->year;
    tt_change.month = tt->month;
    tt_change.day = tt->day;
    tt_change.hour = tt->hour;
    tt_change.minute = tt->minute;
    tt_change.second = tt->second;

    /* This should find a change close to the time, either the change before
       it or the change after it. */
    change_num = icaltimezone_find_nearby_change(zone, &tt_change);

    /* Now move backwards or forwards to find the timezone change that applies
       to tt. It should only have to do 1 or 2 steps. */
    zone_change = icalarray_element_at(zone->changes, change_num);
    step = 1;
    found_change = 0;
    change_num_to_use = -1;
    for (;;) {
        /* Copy the change and adjust it to UTC. */
        tmp_change = *zone_change;

        /* If the given time is on or after this change, then this change may
           apply, but we continue as a later change may be the right one.
           If the given time is before this change, then if we have already
           found a change which applies we can use that, else we need to step
           backwards. */
        if (icaltimezone_compare_change_fn(&tt_change, &tmp_change) >= 0) {
            found_change = 1;
            change_num_to_use = change_num;
        } else {
            step = -1;
        }

        /* If we are stepping backwards through the changes and we have found
           a change that applies, then we know this is the change to use so
           we exit the loop. */
        if (step == -1 && found_change == 1)
            break;

        /* If we go past the start of the changes array, then we have no data
           for this time so we return the prev UTC offset. */
        if (change_num == 0 && step < 0) {
            if (is_daylight) {
                *is_daylight = ! tmp_change.is_daylight;
            }

            icaltimezone_changes_unlock();

            return tmp_change.prev_utc_offset;
        }

        change_num += step;

        if (change_num >= zone->changes->num_elements)
            break;

        zone_change = icalarray_element_at(zone->changes, change_num);
    }

    /* If we didn't find a change to use, then we have a bug! */
    icalerror_assert(found_change == 1, "No applicable timezone change found");

    /* Now we know exactly which timezone change applies to the time, so
       we can return the UTC offset and whether it is a daylight time. */
    zone_change = icalarray_element_at(zone->changes, change_num_to_use);
    if (is_daylight) {
        *is_daylight = zone_change->is_daylight;
    }
    utc_offset = zone_change->utc_offset;

    icaltimezone_changes_unlock();

    return utc_offset;
}

/** @brief Returns the index of a timezone change which is close to the time
 * given in change.
 *
 * Hold icaltimezone_changes_lock(); before calling this function.
*/
static size_t icaltimezone_find_nearby_change(icaltimezone *zone, icaltimezonechange * change)
{
    icaltimezonechange *zone_change;
    size_t lower, middle, upper;
    int cmp;

    /* Do a simple binary search. */
    lower = middle = 0;
    upper = zone->changes->num_elements;

    while (lower < upper) {
        middle = (lower + upper) / 2;
        zone_change = icalarray_element_at(zone->changes, middle);
        cmp = icaltimezone_compare_change_fn(change, zone_change);
        if (cmp == 0) {
            break;
        } else if (cmp < 0) {
            upper = middle;
        } else {
            lower = middle + 1;
        }
    }

    return middle;
}

/** @brief Adds (or subtracts) a time from a icaltimezonechange.
 *
 * NOTE: This function is exactly the same as icaltime_adjust() except
 * for the type of the first parameter.
 */
static void icaltimezone_adjust_change(icaltimezonechange *tt,
                                       int days, int hours, int minutes, int seconds)
{
    int second, minute, hour, day;
    int minutes_overflow, hours_overflow, days_overflow;
    int days_in_month;

    /* Add on the seconds. */
    second = tt->second + seconds;
    tt->second = second % 60;
    minutes_overflow = second / 60;
    if (tt->second < 0) {
        tt->second += 60;
        minutes_overflow--;
    }

    /* Add on the minutes. */
    minute = tt->minute + minutes + minutes_overflow;
    tt->minute = minute % 60;
    hours_overflow = minute / 60;
    if (tt->minute < 0) {
        tt->minute += 60;
        hours_overflow--;
    }

    /* Add on the hours. */
    hour = tt->hour + hours + hours_overflow;
    tt->hour = hour % 24;
    days_overflow = hour / 24;
    if (tt->hour < 0) {
        tt->hour += 24;
        days_overflow--;
    }

    /* Add on the days. */
    day = tt->day + days + days_overflow;
    if (day > 0) {
        for (;;) {
            days_in_month = icaltime_days_in_month(tt->month, tt->year);
            if (day <= days_in_month)
                break;

            tt->month++;
            if (tt->month >= 13) {
                tt->year++;
                tt->month = 1;
            }

            day -= days_in_month;
        }
    } else {
        while (day <= 0) {
            if (tt->month == 1) {
                tt->year--;
                tt->month = 12;
            } else {
                tt->month--;
            }

            day += icaltime_days_in_month(tt->month, tt->year);
        }
    }
    tt->day = day;
}

const char *icaltimezone_get_tzid(icaltimezone *zone)
{
    /* If this is a floating time, without a timezone, return NULL. */
    if (!zone)
        return NULL;

    icaltimezone_load_builtin_timezone(zone);

    return zone->tzid;
}

const char *icaltimezone_get_location(icaltimezone *zone)
{
    /* If this is a floating time, without a timezone, return NULL. */
    if (!zone)
        return NULL;

    /* Note that for builtin timezones this comes from zones.tab so we don't
       need to check the timezone is loaded here. */
    return zone->location;
}

const char *icaltimezone_get_tznames(icaltimezone *zone)
{
    /* If this is a floating time, without a timezone, return NULL. */
    if (!zone)
        return NULL;

    icaltimezone_load_builtin_timezone(zone);

    return zone->tznames;
}

double icaltimezone_get_latitude(icaltimezone *zone)
{
    /* If this is a floating time, without a timezone, return 0. */
    if (!zone)
        return 0.0;

    /* Note that for builtin timezones this comes from zones.tab so we don't
       need to check the timezone is loaded here. */
    return zone->latitude;
}

double icaltimezone_get_longitude(icaltimezone *zone)
{
    /* If this is a floating time, without a timezone, return 0. */
    if (!zone)
        return 0.0;

    /* Note that for builtin timezones this comes from zones.tab so we don't
       need to check the timezone is loaded here. */
    return zone->longitude;
}

icalcomponent *icaltimezone_get_component(icaltimezone *zone)
{
    /* If this is a floating time, without a timezone, return NULL. */
    if (!zone)
        return NULL;

    icaltimezone_load_builtin_timezone(zone);

    return zone->component;
}

int icaltimezone_set_component(icaltimezone *zone, icalcomponent *comp)
{
    icaltimezone_reset(zone);
    return icaltimezone_get_vtimezone_properties(zone, comp);
}

static const char *skip_slashes(const char *text, int n_slashes)
{
    const char *pp;
    int num_slashes = 0;

    if(!text)
        return NULL;

    for (pp = text; *pp; pp++) {
        if(*pp == '/') {
            num_slashes++;
            if(num_slashes == n_slashes)
                return pp + 1;
        }
    }

    return NULL;
}

const char *icaltimezone_get_display_name(icaltimezone *zone)
{
    const char *display_name;
    const char *tzid_prefix;

    display_name = icaltimezone_get_location(zone);
    if (!display_name) {
        display_name = icaltimezone_get_tznames(zone);
    }
    if (!display_name) {
        display_name = icaltimezone_get_tzid(zone);
        tzid_prefix = icaltimezone_tzid_prefix();
        /* Outlook will strip out X-LIC-LOCATION property and so all
           we get back in the iTIP replies is the TZID. So we see if
           this is one of our TZIDs and if so we jump to the city name
           at the end of it. */
        if (display_name &&
            !strncmp(display_name, tzid_prefix, strlen(tzid_prefix))) {
            /* Skip past our prefix */
            display_name += strlen(tzid_prefix);
        }
    }

    return display_name;
}

icalarray *icaltimezone_array_new(void)
{
    return icalarray_new(sizeof(icaltimezone), 16);
}

void icaltimezone_array_append_from_vtimezone(icalarray *timezones, icalcomponent *child)
{
    icaltimezone zone;

    icaltimezone_init(&zone);
    if (icaltimezone_get_vtimezone_properties(&zone, child))
        icalarray_append(timezones, &zone);
}

void icaltimezone_array_free(icalarray *timezones)
{
    icaltimezone *zone;
    size_t i;

    if (timezones) {
        for (i = 0; i < timezones->num_elements; i++) {
            zone = icalarray_element_at(timezones, i);
            icaltimezone_free(zone, 0);
        }

        icalarray_free(timezones);
    }
}

/*
 * BUILTIN TIMEZONE HANDLING
 */

icalarray *icaltimezone_get_builtin_timezones(void)
{
    if (!builtin_timezones)
        icaltimezone_init_builtin_timezones();

    return builtin_timezones;
}

void icaltimezone_free_builtin_timezones(void)
{
    icaltimezone_array_free(builtin_timezones);
    builtin_timezones = 0;
}

icaltimezone *icaltimezone_get_builtin_timezone(const char *location)
{
    icalcomponent *comp;
    icaltimezone *zone;
    size_t lower;
    const char *zone_location;

    if (!location || !location[0])
        return NULL;

    if (!builtin_timezones)
        icaltimezone_init_builtin_timezones();

    if (strcmp(location, "UTC") == 0 || strcmp(location, "GMT") == 0)
        return &utc_timezone;

#if 0
    /* Do a simple binary search. */
    lower = middle = 0;
    upper = builtin_timezones->num_elements;

    while (lower < upper) {
        middle = (lower + upper) / 2;
        zone = icalarray_element_at(builtin_timezones, middle);
        zone_location = icaltimezone_get_location(zone);
        cmp = strcmp(location, zone_location);
        if (cmp == 0) {
            return zone;
        } else if (cmp < 0) {
            upper = middle;
        } else {
            lower = middle + 1;
        }
    }
#endif

    /* The zones from the system are not stored in alphabetical order,
       so we just do a sequential search */
    for (lower = 0; lower < builtin_timezones->num_elements; lower++) {
        zone = icalarray_element_at(builtin_timezones, lower);
        zone_location = icaltimezone_get_location(zone);
        if (strcmp(location, zone_location) == 0)
            return zone;
    }

    /* Check whether file exists, but is not mentioned in zone.tab.
       It means it's a deprecated timezone, but still available. */
    comp = icaltzutil_fetch_timezone(location);
    if (comp) {
        icaltimezone tz;

        icaltimezone_init(&tz);
        if (icaltimezone_set_component(&tz, comp)) {
            icalarray_append(builtin_timezones, &tz);
            return icalarray_element_at(builtin_timezones, builtin_timezones->num_elements - 1);
        } else {
            icalcomponent_free(comp);
        }
    }

    return NULL;
}

static struct icaltimetype tm_to_icaltimetype(struct tm *tm)
{
    struct icaltimetype itt;

    memset(&itt, 0, sizeof(struct icaltimetype));

    /* cppcheck-suppress ctuuninitvar */
    itt.second = tm->tm_sec;
    itt.minute = tm->tm_min;
    itt.hour = tm->tm_hour;

    itt.day = tm->tm_mday;
    itt.month = tm->tm_mon + 1;
    itt.year = tm->tm_year + 1900;

    itt.is_date = 0;

    return itt;
}

static int get_offset(icaltimezone *zone)
{
    struct tm local;
    struct icaltimetype tt;
    int offset;
    const time_t now = time(NULL);

    if (!gmtime_r(&now, &local))
        return 0;

    tt = tm_to_icaltimetype(&local);
    offset = icaltimezone_get_utc_offset(zone, &tt, NULL);

    return offset;
}

icaltimezone *icaltimezone_get_builtin_timezone_from_offset(int offset, const char *tzname)
{
    icaltimezone *zone = NULL;
    size_t i, count;

    if (!builtin_timezones)
        icaltimezone_init_builtin_timezones();

    if (offset == 0)
        return &utc_timezone;

    if (!tzname)
        return NULL;

    count = builtin_timezones->num_elements;

    for (i = 0; i < count; i++) {
        int z_offset;

        zone = icalarray_element_at(builtin_timezones, i);
        icaltimezone_load_builtin_timezone(zone);

        z_offset = get_offset(zone);

        if (z_offset == offset && zone->tznames && !strcmp(tzname, zone->tznames))
            return zone;
    }

    return NULL;
}

icaltimezone *icaltimezone_get_builtin_timezone_from_tzid(const char *tzid)
{
    const char *p, *zone_tzid, *tzid_prefix;
    icaltimezone *zone;
    int compat = 0;

    if (!tzid || !tzid[0])
        return NULL;

    if (strcmp(tzid, "UTC") == 0 || strcmp(tzid, "GMT") == 0) {
        return icaltimezone_get_builtin_timezone(tzid);
    }

    tzid_prefix = icaltimezone_tzid_prefix();
    /* Check that the TZID starts with our unique prefix. */
    if (strncmp(tzid, tzid_prefix, strlen(tzid_prefix))) {
        int ii;

        for (ii = 0; glob_compat_tzids[ii].tzid; ii++) {
            if(strncmp(tzid, glob_compat_tzids[ii].tzid, strlen(glob_compat_tzids[ii].tzid)) == 0) {
                p = skip_slashes(tzid, glob_compat_tzids[ii].slashes);
                if(p) {
                    zone = icaltimezone_get_builtin_timezone(p);
                    /* Do not recheck the TZID matches exactly, it does not, because
                       fallbacking with the compatibility timezone prefix here. */
                    return zone;
                }
                break;
            }
        }

        return NULL;
    }

    /* Skip past our prefix */
    p = tzid + strlen(tzid_prefix);

    /* Special-case "/freeassociation.sourceforge.net/Tzfile/"
       because it shares prefix with BUILTIN_TZID_PREFIX */
    if (strcmp(tzid_prefix, BUILTIN_TZID_PREFIX) == 0 &&
        strncmp(p, "Tzfile/", 7) == 0) {
        p += 7;
        compat = 1;
    }

    /* Now we can use the function to get the builtin timezone from the
       location string. */
    zone = icaltimezone_get_builtin_timezone(p);
    if (!zone || compat)
        return zone;

#if defined(USE_BUILTIN_TZDATA)
    if (use_builtin_tzdata)
        return zone;
#endif

    /* Check that the builtin TZID matches exactly. We don't want to return
       a different version of the VTIMEZONE. */
    zone_tzid = icaltimezone_get_tzid(zone);
    if (!strcmp(zone_tzid, tzid)) {
        return zone;
    } else {
        return NULL;
    }
}

icaltimezone *icaltimezone_get_utc_timezone(void)
{
    if (!builtin_timezones)
        icaltimezone_init_builtin_timezones();

    return &utc_timezone;
}

/** @brief Initializes the builtin timezone data, i.e. the
 * builtin_timezones array and the special UTC timezone.
 *
 * It should be called before any code that uses the timezone functions.
 */
static void icaltimezone_init_builtin_timezones(void)
{
    /* Initialize the special UTC timezone. */
    utc_timezone.tzid = (char *)"UTC";

    icaltimezone_builtin_lock();
    if (!builtin_timezones) {
        icaltimezone_parse_zone_tab();
    }
    icaltimezone_builtin_unlock();
}

static int parse_coord(char *coord, int len, int *degrees, int *minutes, int *seconds)
{
    if (len == 5) {
        sscanf(coord + 1, "%2d%2d", degrees, minutes);
    } else if (len == 6) {
        sscanf(coord + 1, "%3d%2d", degrees, minutes);
    } else if (len == 7) {
        sscanf(coord + 1, "%2d%2d%2d", degrees, minutes, seconds);
    } else if (len == 8) {
        sscanf(coord + 1, "%3d%2d%2d", degrees, minutes, seconds);
    } else {
        fprintf(stderr, "Invalid coordinate: %s\n", coord);
        return 1;
    }

    if (coord[0] == '-')
        *degrees = -*degrees;

    return 0;
}

static int fetch_lat_long_from_string(const char *str,
                                      int *latitude_degrees, int *latitude_minutes,
                                      int *latitude_seconds,
                                      int *longitude_degrees, int *longitude_minutes,
                                      int *longitude_seconds,
                                      char *location)
{
    size_t len;
    char *sptr, *lat, *lon, *loc, *temp;

    /* We need to parse the latitude/longitude co-ordinates and location fields  */
    sptr = (char *)str;
    while ((*sptr != '\t') && (*sptr != '\0')) {
        sptr++;
    }
    temp = ++sptr;
    while (*sptr != '\t' && *sptr != '\0') {
        sptr++;
    }
    len = (ptrdiff_t) (sptr - temp);
    lat = (char *)malloc(len + 1);
    memset(lat, '\0', len + 1);
    strncpy(lat, temp, len);
    lat[len] = '\0';
    while ((*sptr != '\t') && (*sptr != '\0')) {
        sptr++;
    }
    loc = ++sptr;
    while (!isspace((int)(*sptr)) && (*sptr != '\0')) {
        sptr++;
    }
    len = (ptrdiff_t)(sptr - loc);
    strncpy(location, loc, len);
    location[len] = '\0';

#if defined(sun) && defined(__SVR4)
    /* Handle EET, MET and WET in zone_sun.tab. */
    if (!strcmp(location, "Europe/")) {
        while ((*sptr != '\t') && (*sptr != '\0')) {
            sptr++;
        }
        loc = ++sptr;
        while (!isspace(*sptr) && (*sptr != '\0')) {
            sptr++;
        }
        len = (ptrdiff_t)(sptr - loc);
        strncpy(location, loc, len);
        location[len] = '\0';
    }
#endif

    lon = lat + 1;
    while (*lon != '\0' && *lon != '+' && *lon != '-') {
        lon++;
    }

    if (parse_coord(lat, (int)(lon - lat),
                    latitude_degrees,
                    latitude_minutes,
                    latitude_seconds) == 1 ||
        parse_coord(lon, (int)strlen(lon),
                    longitude_degrees, longitude_minutes, longitude_seconds) == 1) {
        free(lat);
        return 1;
    }

    free(lat);

    return 0;
}

/** @brief Parses the zones.tab file containing the names and locations
 * of the builtin timezones.
 *
 * It creates the builtin_timezones array
 * which is an icalarray of icaltimezone structs. It only fills in the
 * location, latitude and longtude fields; the rest are left
 * blank. The VTIMEZONE component is loaded later if it is needed. The
 * timezones in the zones.tab file are sorted by their name, which is
 * useful for binary searches.
 */
static void icaltimezone_parse_zone_tab(void)
{
    const char *zonedir, *zonetab;
    char *filename;
    FILE *fp;
    char buf[1024];     /* Used to store each line of zones.tab as it is read. */
    char location[1024];        /* Stores the city name when parsing buf. */
    size_t filename_len;
    int latitude_degrees = 0, latitude_minutes = 0, latitude_seconds = 0;
    int longitude_degrees = 0, longitude_minutes = 0, longitude_seconds = 0;
    icaltimezone zone;

    icalerror_assert(builtin_timezones == NULL, "Parsing zones.tab file multiple times");

    builtin_timezones = icalarray_new(sizeof(icaltimezone), 1024);

    if (!use_builtin_tzdata) {
        zonedir = icaltzutil_get_zone_directory();
        zonetab = ZONES_TAB_SYSTEM_FILENAME;
    } else {
        zonedir = get_zone_directory();
        zonetab = ZONES_TAB_FILENAME;
    }

    filename_len = 0;
    if (zonedir) {
        filename_len = strlen(zonedir);
    }

    icalerror_assert(filename_len > 0, "Unable to locate a zoneinfo dir");
    if (filename_len == 0) {
        icalerror_set_errno(ICAL_INTERNAL_ERROR);\
        return;
    }

    filename_len += strlen(zonetab);
    filename_len += 2; /* for dir separator and final '\0' */

    filename = (char *)malloc(filename_len);
    if (!filename) {
        icalerror_set_errno(ICAL_NEWFAILED_ERROR);
        return;
    }
    snprintf(filename, filename_len, "%s/%s", zonedir, zonetab);

    fp = fopen(filename, "r");
    free(filename);
    icalerror_assert(fp, "Cannot open the zonetab file for reading");
    if (!fp) {
        icalerror_set_errno(ICAL_INTERNAL_ERROR);
        return;
    }

    while (fgets(buf, (int)sizeof(buf), fp)) {
        if (*buf == '#')
            continue;

        if (use_builtin_tzdata) {
            /* The format of each line is: "[ latitude longitude ] location". */
            if (buf[0] != '+' && buf[0] != '-') {
                latitude_degrees = longitude_degrees = 360;
                latitude_minutes = longitude_minutes = 0;
                latitude_seconds = longitude_seconds = 0;
                if (sscanf(buf, "%1000s", location) != 1) {     /*limit location to 1000chars */
                    /*increase as needed */
                    /*see location and buf declarations */
                    fprintf(stderr, "Invalid timezone description line: %s\n", buf);
                    continue;
                }
            } else if (sscanf(buf, "%4d%2d%2d %4d%2d%2d %1000s", /*limit location to 1000chars */
                              /*increase as needed */
                              /*see location and buf declarations */
                              &latitude_degrees, &latitude_minutes,
                              &latitude_seconds,
                              &longitude_degrees, &longitude_minutes,
                              &longitude_seconds, location) != 7) {
                fprintf(stderr, "Invalid timezone description line: %s\n", buf);
                continue;
            }
        } else {
            /* coverity[tainted_data] */
            if (fetch_lat_long_from_string(buf, &latitude_degrees, &latitude_minutes,
                                           &latitude_seconds,
                                           &longitude_degrees, &longitude_minutes,
                                           &longitude_seconds, location)) {
                fprintf(stderr, "Invalid timezone description line: %s\n", buf);
                continue;
            }
        }

        icaltimezone_init(&zone);
        zone.location = strdup(location);

        if (latitude_degrees >= 0) {
            zone.latitude =
                (double)latitude_degrees +
                (double)latitude_minutes / 60 +
                (double)latitude_seconds / 3600;
        } else {
            zone.latitude =
                (double)latitude_degrees -
                (double)latitude_minutes / 60 -
                (double)latitude_seconds / 3600;
        }

        if (longitude_degrees >= 0) {
            zone.longitude =
                (double)longitude_degrees +
                (double)longitude_minutes / 60 +
                (double)longitude_seconds / 3600;
        } else {
            zone.longitude =
                (double)longitude_degrees -
                (double)longitude_minutes / 60 -
                (double)longitude_seconds / 3600;
        }

        icalarray_append(builtin_timezones, &zone);

#if 0
        printf("Found zone: %s %f %f\n", location, zone.latitude, zone.longitude);
#endif
    }

    fclose(fp);
}

void icaltimezone_release_zone_tab(void)
{
    size_t i;
    icalarray *mybuiltin_timezones = builtin_timezones;

    if (builtin_timezones == NULL)
        return;

    builtin_timezones = NULL;
    for (i = 0; i < mybuiltin_timezones->num_elements; i++) {
        free(((icaltimezone *) icalarray_element_at(mybuiltin_timezones, i))->location);
    }
    icalarray_free(mybuiltin_timezones);
}

/** @brief Loads the builtin VTIMEZONE data for the given timezone. */
static void icaltimezone_load_builtin_timezone(icaltimezone *zone)
{
    icalcomponent *comp = 0, *subcomp;

    /* Prevent blocking on mutex lock caused by recursive calls */
    if (zone->component)
        return;

    icaltimezone_builtin_lock();

    /* Try again, maybe it had been set by other thread while waiting for the lock */
    if (zone->component) {
        icaltimezone_builtin_unlock();
        return;
    }

    /* If the location isn't set, it isn't a builtin timezone. */
    if (!zone->location || !zone->location[0]) {
        icaltimezone_builtin_unlock();
        return;
    }

    if (use_builtin_tzdata) {
        char *filename;
        size_t filename_len;
        FILE *fp;
        icalparser *parser;

        filename_len = strlen(get_zone_directory()) + strlen(zone->location) + 6;

        filename = (char *)malloc(filename_len);
        if (!filename) {
            icalerror_set_errno(ICAL_NEWFAILED_ERROR);
            goto out;
        }

        snprintf(filename, filename_len, "%s/%s.ics", get_zone_directory(), zone->location);

        fp = fopen(filename, "r");
        free(filename);
        if (!fp) {
            icalerror_set_errno(ICAL_FILE_ERROR);
            goto out;
        }

        /* ##### B.# Sun, 11 Nov 2001 04:04:29 +1100
           this is where the MALFORMEDDATA error is being set, after the call to 'icalparser_parse'
           fprintf(stderr, "** WARNING ** %s: %d %s\n",
                   __FILE__, __LINE__, icalerror_strerror(icalerrno));
         */

        parser = icalparser_new();
        icalparser_set_gen_data(parser, fp);
        comp = icalparser_parse(parser, icaltimezone_load_get_line_fn);
        icalparser_free(parser);
        fclose(fp);

        /* Find the VTIMEZONE component inside the VCALENDAR. There should be 1. */
        subcomp = icalcomponent_get_first_component(comp, ICAL_VTIMEZONE_COMPONENT);

        if (subcomp) {
            icalproperty *prop;

            /* Ensure expected TZID */
            prop = icalcomponent_get_first_property(subcomp, ICAL_TZID_PROPERTY);
            if(prop) {
                char *new_tzid;
                size_t new_tzid_len;
                const char *tzid_prefix = icaltimezone_tzid_prefix();

                new_tzid_len = strlen(tzid_prefix) + strlen(zone->location) + 1;
                new_tzid = (char *)malloc(sizeof(char) * (new_tzid_len + 1));
                if(new_tzid) {
                    snprintf(new_tzid, new_tzid_len, "%s%s", tzid_prefix, zone->location);
                    icalproperty_set_tzid(prop, new_tzid);
                    free(new_tzid);
                } else {
                    icalerror_set_errno(ICAL_NEWFAILED_ERROR);
                }
            }

            /* Ensure expected Location - it's for cases where one VTIMEZONE is shared
               between different locations (like Pacific/Midway is Pacific/Pago_Pago).
               This updates the properties, thus when the component is converted to
               the string and back to the component the Location will still match. */
            prop = icalcomponent_get_first_property(subcomp, ICAL_LOCATION_PROPERTY);
            if (prop)
                icalproperty_set_location(prop, zone->location);

            for (prop = icalcomponent_get_first_property(subcomp, ICAL_X_PROPERTY);
                 prop;
                 prop = icalcomponent_get_next_property(subcomp, ICAL_X_PROPERTY)) {
                const char *name;

                name = icalproperty_get_x_name(prop);
                if (name && !strcasecmp(name, "X-LIC-LOCATION")) {
                    icalproperty_set_x(prop, zone->location);
                    break;
                }
            }
        }
    } else {
        subcomp = icaltzutil_fetch_timezone(zone->location);
    }

    if (!subcomp) {
        icalerror_set_errno(ICAL_PARSE_ERROR);
        goto out;
    }

    icaltimezone_get_vtimezone_properties(zone, subcomp);

    if (use_builtin_tzdata) {
        icalcomponent_remove_component(comp, subcomp);
        icalcomponent_free(comp);
    }

  out:
    icaltimezone_builtin_unlock();
    return;
}

/** @brief Callback used from icalparser_parse() */
static char *icaltimezone_load_get_line_fn(char *s, size_t size, void *data)
{
    return fgets(s, (int)size, (FILE *) data);
}

/*
 * DEBUGGING
 */

int icaltimezone_dump_changes(icaltimezone *zone, int max_year, FILE *fp)
{
    static const char months[][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
        "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
    };
    icaltimezonechange *zone_change;
    size_t change_num;
    char buffer[8];

    /* Make sure the changes array is expanded up to the given time. */
    icaltimezone_ensure_coverage(zone, max_year);

#if 0
    printf("Num changes: %i\n", zone->changes->num_elements);
#endif

    icaltimezone_changes_lock();

    for (change_num = 0; change_num < zone->changes->num_elements; change_num++) {
        zone_change = icalarray_element_at(zone->changes, change_num);

        if (zone_change->year > max_year)
            break;

        fprintf(fp, "%s\t%2i %s %04i\t%2i:%02i:%02i",
                zone->location,
                zone_change->day, months[zone_change->month - 1],
                zone_change->year, zone_change->hour, zone_change->minute, zone_change->second);

        /* Wall Clock Time offset from UTC. */
        format_utc_offset(zone_change->utc_offset, buffer, sizeof(buffer));
        fprintf(fp, "\t%s", buffer);

        fprintf(fp, "\n");
    }

    icaltimezone_changes_unlock();

    return 1;
}

/** @brief Formats a UTC offset as "+HHMM" or "+HHMMSS".
 *
 * @p buffer should have space for 8 characters. */
static void format_utc_offset(int utc_offset, char *buffer, size_t buffer_size)
{
    const char *sign = "+";
    int hours, minutes, seconds;

    if (utc_offset < 0) {
        utc_offset = -utc_offset;
        sign = "-";
    }

    hours = utc_offset / 3600;
    minutes = (utc_offset % 3600) / 60;
    seconds = utc_offset % 60;

    /* Sanity check. Standard timezone offsets shouldn't be much more than 12
       hours, and daylight saving shouldn't change it by more than a few hours.
       (The maximum offset is 15 hours 56 minutes at present.) */
    if (hours < 0 || hours >= 24 || minutes < 0 || minutes >= 60 || seconds < 0 || seconds >= 60) {
        fprintf(stderr, "Warning: Strange timezone offset: H:%i M:%i S:%i\n",
                hours, minutes, seconds);
    }

    if (seconds == 0) {
        snprintf(buffer, buffer_size, "%s%02i%02i", sign, hours, minutes);
    } else {
        snprintf(buffer, buffer_size, "%s%02i%02i%02i", sign, hours, minutes, seconds);
    }
}

static const char *get_zone_directory(void)
{
#if !defined(_WIN32)
    return zone_files_directory == NULL ? ZONEINFO_DIRECTORY : zone_files_directory;
#else
    wchar_t wbuffer[1000];

#if !defined(_WIN32_WCE)
    char buffer[1000], zoneinfodir[1000], dirname[1000];
#else
    wchar_t zoneinfodir[1000], dirname[1000];
#endif
    int used_default;
    static char *cache = NULL;

#if !defined(_WIN32_WCE)
    unsigned char *dirslash, *zislash, *zislashp1;
#else
    wchar_t *dirslash, *zislash;
#endif
    struct stat st;

    if (zone_files_directory)
        return zone_files_directory;

    if (cache)
        return cache;

    /* Get the filename of the application */
    if (!GetModuleFileNameW(NULL, wbuffer, sizeof(wbuffer) / sizeof(wbuffer[0])))
        return ZONEINFO_DIRECTORY;

/*wince supports only unicode*/
#if !defined(_WIN32_WCE)
    /* Convert to system codepage */
    if (!WideCharToMultiByte(CP_ACP, 0, wbuffer, -1, buffer, sizeof(buffer),
                             NULL, &used_default) || used_default) {
        /* Failed, try 8.3 format */
        if (!GetShortPathNameW(wbuffer, wbuffer,
                               sizeof(wbuffer) / sizeof(wbuffer[0])) ||
            !WideCharToMultiByte(CP_ACP, 0, wbuffer, -1, buffer, sizeof(buffer),
                                 NULL, &used_default) || used_default) {
            return ZONEINFO_DIRECTORY;
        }
    }
#endif
    /* Look for the zoneinfo directory somewhere in the path where
     * the app is installed. If the path to the app is
     *
     *      C:\opt\evo-2.6\bin\evolution-2.6.exe
     *
     * and the compile-time ZONEINFO_DIRECTORY is
     *
     *      C:/devel/target/evo/share/evolution-data-server-1.6/zoneinfo,
     *
     * we check the pathnames:
     *
     *      C:\opt\evo-2.6/devel/target/evo/share/evolution-data-server-1.6/zoneinfo
     *      C:\opt\evo-2.6/target/evo/share/evolution-data-server-1.6/zoneinfo
     *      C:\opt\evo-2.6/evo/share/evolution-data-server-1.6/zoneinfo
     *      C:\opt\evo-2.6/share/evolution-data-server-1.6/zoneinfo         <===
     *      C:\opt\evo-2.6/evolution-data-server-1.6/zoneinfo
     *      C:\opt\evo-2.6/zoneinfo
     *      C:\opt/devel/target/evo/share/evolution-data-server-1.6/zoneinfo
     *      C:\opt/target/evo/share/evolution-data-server-1.6/zoneinfo
     *      C:\opt/evo/share/evolution-data-server-1.6/zoneinfo
     *      C:\opt/share/evolution-data-server-1.6/zoneinfo
     *      C:\opt/evolution-data-server-1.6/zoneinfo
     *      C:\opt/zoneinfo
     *      C:/devel/target/evo/share/evolution-data-server-1.6/zoneinfo
     *      C:/target/evo/share/evolution-data-server-1.6/zoneinfo
     *      C:/evo/share/evolution-data-server-1.6/zoneinfo
     *      C:/share/evolution-data-server-1.6/zoneinfo
     *      C:/evolution-data-server-1.6/zoneinfo
     *      C:/zoneinfo
     *
     * In Evolution's case, we would get a match already at the
     * fourth pathname check.
     */

    /* Strip away basename of app .exe first */
#if !defined(_WIN32_WCE)
    dirslash = _mbsrchr((unsigned char *)buffer, '\\');
#else
    dirslash = wcsrchr(wbuffer, L'\\');
#endif
    if (dirslash) {
#if !defined(_WIN32_WCE)
        *dirslash = '\0';
#else
        *dirslash = L'\0';
#endif
    }

#if defined(_WIN32_WCE)
    while ((dirslash = wcsrchr(wbuffer, '\\'))) {
        /* Strip one more directory from app .exe location */
        *dirslash = L'\0';

        MultiByteToWideChar(CP_ACP, 0, ZONEINFO_DIRECTORY, -1, zoneinfodir, 1000);

        while ((zislash = wcschr(zoneinfodir, L'/'))) {
            *zislash = L'.';
            wcscpy(dirname, wbuffer);
            wcscat(dirname, "/");
            wcscat(dirname, zislash + 1);
            if (stat(dirname, &st) == 0 && S_ISDIR(st.st_mode)) {
                cache = wce_wctomb(dirname);
                return cache;
            }
        }
    }
#else
    while ((dirslash = _mbsrchr((unsigned char *)buffer, '\\'))) {
        /* Strip one more directory from app .exe location */
        *dirslash = '\0';

        strcpy(zoneinfodir, ZONEINFO_DIRECTORY);
        while ((zislash = _mbschr((unsigned char *)zoneinfodir, '/'))) {
            *zislash = '.';
            strcpy(dirname, buffer);
            strcat(dirname, "/");
            zislashp1 = zislash + 1;
            strcat(dirname, (char *)zislashp1);
            if (stat(dirname, &st) == 0 && S_ISDIR(st.st_mode)) {
                cache = strdup(dirname);
                return cache;
            }
        }
    }
#endif
    return ZONEINFO_DIRECTORY;
#endif
}

void set_zone_directory(const char *path)
{
    if (zone_files_directory)
        free_zone_directory();

    zone_files_directory = malloc(strlen(path) + 1);

    if (zone_files_directory != NULL)
        strcpy(zone_files_directory, path);
}

void free_zone_directory(void)
{
    if (zone_files_directory != NULL) {
        free(zone_files_directory);
        zone_files_directory = NULL;
    }
}

void icaltimezone_set_tzid_prefix(const char *new_prefix)
{
    if (new_prefix) {
        strncpy(s_ical_tzid_prefix, new_prefix, BUILTIN_TZID_PREFIX_LEN-1);
    }
}

void icaltimezone_set_builtin_tzdata(int set)
{
    use_builtin_tzdata = set;
}

int icaltimezone_get_builtin_tzdata(void)
{
    return use_builtin_tzdata;
}

struct observance {
    const char *name;
    icaltimetype onset;
    int offset_from;
    int offset_to;
};

static void check_tombstone(struct observance *tombstone,
                            struct observance *obs)
{
    if (icaltime_compare(obs->onset, tombstone->onset) > 0) {
        /* onset is closer to cutoff than existing tombstone */
        tombstone->name = icalmemory_tmp_copy(obs->name);
        tombstone->offset_from = tombstone->offset_to = obs->offset_to;
        tombstone->onset = obs->onset;
    }
}

struct rdate {
    icalproperty *prop;
    struct icaldatetimeperiodtype date;
};

static int rdate_compare(const void *rdate1, const void *rdate2)
{
    return icaltime_compare(((struct rdate *) rdate1)->date.time,
                            ((struct rdate *) rdate2)->date.time);
}

void icaltimezone_truncate_vtimezone(icalcomponent *vtz,
                                     icaltimetype start, icaltimetype end,
                                     int ms_compatible)
{
    icalcomponent *comp, *nextc, *tomb_std = NULL, *tomb_day = NULL;
    icalproperty *prop, *proleptic_prop = NULL;
    struct observance tombstone;
    unsigned need_tomb = (unsigned)!icaltime_is_null_time(start);
    unsigned need_tzuntil = (unsigned)!icaltime_is_null_time(end);

    if (!need_tomb && !need_tzuntil) {
        /* Nothing to do */
        return;
    }

    /* See if we have a proleptic tzname in VTIMEZONE */
    for (prop = icalcomponent_get_first_property(vtz, ICAL_X_PROPERTY);
         prop;
         prop = icalcomponent_get_next_property(vtz, ICAL_X_PROPERTY)) {
        if (!strcmp("X-PROLEPTIC-TZNAME", icalproperty_get_x_name(prop))) {
            proleptic_prop = prop;
            break;
        }
    }

    memset(&tombstone, 0, sizeof(struct observance));
    tombstone.name = icalmemory_tmp_copy(proleptic_prop ?
                                         icalproperty_get_x(proleptic_prop) :
                                         "LMT");
    if (!proleptic_prop ||
        !icalproperty_get_parameter_as_string(proleptic_prop, "X-NO-BIG-BANG")) {
      tombstone.onset.year = -1;
    }

    /* Process each VTMEZONE STANDARD/DAYLIGHT subcomponent */
    for (comp = icalcomponent_get_first_component(vtz, ICAL_ANY_COMPONENT);
         comp; comp = nextc) {
        icalproperty *dtstart_prop = NULL, *rrule_prop = NULL;
        icalarray *rdates = icalarray_new(sizeof(struct rdate), 10);
        icaltimetype dtstart;
        struct observance obs;
        size_t n;
        unsigned trunc_dtstart = 0;
        int r;

        nextc = icalcomponent_get_next_component(vtz, ICAL_ANY_COMPONENT);

        memset(&obs, 0, sizeof(struct observance));
        obs.offset_from = obs.offset_to = INT_MAX;
        obs.onset.is_daylight =
            (icalcomponent_isa(comp) == ICAL_XDAYLIGHT_COMPONENT);

        /* Grab the properties that we require to expand recurrences */
        for (prop = icalcomponent_get_first_property(comp, ICAL_ANY_PROPERTY);
             prop;
             prop = icalcomponent_get_next_property(comp, ICAL_ANY_PROPERTY)) {

            switch (icalproperty_isa(prop)) {
            case ICAL_TZNAME_PROPERTY:
                obs.name = icalproperty_get_tzname(prop);
                break;

            case ICAL_DTSTART_PROPERTY:
                dtstart_prop = prop;
                obs.onset = dtstart = icalproperty_get_dtstart(prop);
                break;

            case ICAL_TZOFFSETFROM_PROPERTY:
                obs.offset_from = icalproperty_get_tzoffsetfrom(prop);
                break;

            case ICAL_TZOFFSETTO_PROPERTY:
                obs.offset_to = icalproperty_get_tzoffsetto(prop);
                break;

            case ICAL_RRULE_PROPERTY:
                rrule_prop = prop;
                break;

            case ICAL_RDATE_PROPERTY: {
                struct icaldatetimeperiodtype dtp = icalproperty_get_rdate(prop);
                struct rdate rdate;

                rdate.prop = prop;
                rdate.date.time = dtp.time;
                rdate.date.period = dtp.period;

                icalarray_append(rdates, &rdate);
                break;
            }

            default:
                /* ignore all other properties */
                break;
            }
        }

        /* We MUST have DTSTART, TZNAME, TZOFFSETFROM, and TZOFFSETTO */
        if (!dtstart_prop || !obs.name ||
            obs.offset_from == INT_MAX || obs.offset_to == INT_MAX) {
            icalarray_free(rdates);
            continue;
        }

        /* Adjust DTSTART observance to UTC */
        icaltime_adjust(&obs.onset, 0, 0, 0, -obs.offset_from);
        (void)icaltime_set_timezone(&obs.onset, icaltimezone_get_utc_timezone());

        /* Check DTSTART vs window close */
        if (need_tzuntil && icaltime_compare(obs.onset, end) >= 0) {
            /* All observances occur on/after window close - remove component */
            icalcomponent_remove_component(vtz, comp);
            icalcomponent_free(comp);

            /* Nothing else to do */
            icalarray_free(rdates);
            continue;
        }

        /* Check DTSTART vs window open */
        r = icaltime_compare(obs.onset, start);
        if (r < 0) {
            /* DTSTART is prior to our window open - check it vs tombstone */
            if (need_tomb) {
                check_tombstone(&tombstone, &obs);
            }

            /* Adjust it */
            trunc_dtstart = 1;
        } else if (r == 0) {
            /* DTSTART is on/after our window open */
            need_tomb = 0;
        }

        if (rrule_prop) {
            struct icalrecurrencetype rrule = icalproperty_get_rrule(rrule_prop);
            unsigned eternal = (unsigned)icaltime_is_null_time(rrule.until);
            icalrecur_iterator *ritr = NULL;
            unsigned trunc_until = 0;

            /* Check RRULE duration */
            if (!eternal && icaltime_compare(rrule.until, start) < 0) {
                /* RRULE ends prior to our window open -
                   check UNTIL vs tombstone */
                obs.onset = rrule.until;
                if (need_tomb) {
                    check_tombstone(&tombstone, &obs);
                }

                /* Remove RRULE */
                icalcomponent_remove_property(comp, rrule_prop);
                icalproperty_free(rrule_prop);
            } else {
                /* RRULE ends on/after our window open */
                if (need_tzuntil &&
                    (eternal || icaltime_compare(rrule.until, end) >= 0)) {
                    /* RRULE ends after our window close - need to adjust it */
                    trunc_until = 1;
                }

                if (!eternal) {
                    /* Adjust UNTIL to local time (for iterator) */
                    icaltime_adjust(&rrule.until, 0, 0, 0, obs.offset_from);
                    (void)icaltime_set_timezone(&rrule.until, NULL);
                }

                ritr = icalrecur_iterator_new(rrule, dtstart);

                if (trunc_dtstart) {
                    /* Bump RRULE start to 1 year prior to our window open */
                    icaltimetype newstart = dtstart;
                    newstart.year  = start.year - 1;
                    newstart.month = start.month;
                    newstart.day   = start.day;
                    (void)icaltime_normalize(newstart);
                    icalrecur_iterator_set_start(ritr, newstart);
                }
            }

            /* Process any RRULE observances within our window */
            if (ritr) {
                icaltimetype recur, prev_onset;

                while (!icaltime_is_null_time(recur = icalrecur_iterator_next(ritr))) {
                    unsigned ydiff;

                    obs.onset = recur;

                    /* Adjust observance to UTC */
                    icaltime_adjust(&obs.onset, 0, 0, 0, -obs.offset_from);
                    (void)icaltime_set_timezone(&obs.onset,
                                                icaltimezone_get_utc_timezone());

                    if (trunc_until && icaltime_compare(obs.onset, end) >= 0) {
                        /* Observance is on/after window close */

                        /* Check if DSTART is within 1yr of prev onset */
                        ydiff = (unsigned)(prev_onset.year - dtstart.year);
                        if (ydiff <= 1) {
                            /* Remove RRULE */
                            icalcomponent_remove_property(comp, rrule_prop);
                            icalproperty_free(rrule_prop);

                            if (ydiff) {
                                /* Add previous onset as RDATE */
                                struct icaldatetimeperiodtype rdate;
                                rdate.time = prev_onset;
                                rdate.period = icalperiodtype_null_period();

                                prop = icalproperty_new_rdate(rdate);
                                icalcomponent_add_property(comp, prop);
                            }
                        } else {
                            /* Set UNTIL to previous onset */
                            rrule.until = prev_onset;
                            icalproperty_set_rrule(rrule_prop, rrule);
                        }

                        /* We're done */
                        break;
                    }

                    /* Check observance vs our window open */
                    r = icaltime_compare(obs.onset, start);
                    if (r < 0) {
                        /* Observance is prior to our window open -
                           check it vs tombstone */
                        if (ms_compatible) {
                            /* XXX  We don't want to move DTSTART of the RRULE
                               as Outlook/Exchange doesn't appear to like
                               truncating the frontend of RRULEs */
                            need_tomb = 0;
                            trunc_dtstart = 0;
                            if (proleptic_prop) {
                                icalcomponent_remove_property(vtz,
                                                              proleptic_prop);
                                icalproperty_free(proleptic_prop);
                                proleptic_prop = NULL;
                            }
                        }
                        if (need_tomb) {
                            check_tombstone(&tombstone, &obs);
                        }
                    } else {
                        /* Observance is on/after our window open */
                        if (r == 0) need_tomb = 0;

                        if (trunc_dtstart) {
                            /* Make this observance the new DTSTART */
                            icalproperty_set_dtstart(dtstart_prop, recur);
                            dtstart = obs.onset;
                            trunc_dtstart = 0;

                            /* Check if new DSTART is within 1yr of UNTIL */
                            ydiff = (unsigned)(rrule.until.year - recur.year);
                            if (!trunc_until && ydiff <= 1) {
                                /* Remove RRULE */
                                icalcomponent_remove_property(comp, rrule_prop);
                                icalproperty_free(rrule_prop);

                                if (ydiff) {
                                    /* Add UNTIL as RDATE */
                                    struct icaldatetimeperiodtype rdate;
                                    rdate.time = rrule.until;
                                    rdate.period = icalperiodtype_null_period();

                                    prop = icalproperty_new_rdate(rdate);
                                    icalcomponent_add_property(comp, prop);
                                }
                            }
                        }

                        if (!trunc_until) {
                            /* We're done */
                            break;
                        }

                        /* Check if observance is outside 1yr of window close */
                        ydiff = (unsigned)(end.year - recur.year);
                        if (ydiff > 1) {
                            /* Bump RRULE to restart at 1 year prior to our window close */
                            icaltimetype newstart = recur;
                            newstart.year  = end.year - 1;
                            newstart.month = end.month;
                            newstart.day   = end.day;
                            (void)icaltime_normalize(newstart);
                            icalrecur_iterator_set_start(ritr, newstart);
                        }
                    }
                    prev_onset = obs.onset;
                }
                icalrecur_iterator_free(ritr);
            }
        }

        /* Sort the RDATEs by onset */
        icalarray_sort(rdates, &rdate_compare);

        /* Check RDATEs */
        for (n = 0; n < rdates->num_elements; n++) {
            struct rdate *rdate = icalarray_element_at(rdates, n);

            /* RDATEs with a DATE value inherit the time from the DTSTART. */
            if (icaltime_is_date(rdate->date.time)) {
                rdate->date.time.hour   = dtstart.hour;
                rdate->date.time.minute = dtstart.minute;
                rdate->date.time.second = dtstart.second;
            }

            if (n == 0 && icaltime_compare(rdate->date.time, dtstart) == 0) {
                /* RDATE is same as DTSTART - remove it */
                icalcomponent_remove_property(comp, rdate->prop);
                icalproperty_free(rdate->prop);
                continue;
            }

            obs.onset = rdate->date.time;

            /* Adjust observance to UTC */
            icaltime_adjust(&obs.onset, 0, 0, 0, -obs.offset_from);
            (void)icaltime_set_timezone(&obs.onset, icaltimezone_get_utc_timezone());

            if (need_tzuntil && icaltime_compare(obs.onset, end) >= 0) {
                /* RDATE is after our window close - remove it */
                icalcomponent_remove_property(comp, rdate->prop);
                icalproperty_free(rdate->prop);

                continue;
            }

            r = icaltime_compare(obs.onset, start);
            if (r < 0) {
                /* RDATE is prior to window open - check it vs tombstone */
                if (need_tomb) {
                    check_tombstone(&tombstone, &obs);
                }

                /* Remove it */
                icalcomponent_remove_property(comp, rdate->prop);
                icalproperty_free(rdate->prop);
            } else {
                /* RDATE is on/after our window open */
                if (r == 0) need_tomb = 0;

                if (trunc_dtstart) {
                    /* Make this RDATE the new DTSTART */
                    icalproperty_set_dtstart(dtstart_prop,
                                             rdate->date.time);
                    trunc_dtstart = 0;

                    icalcomponent_remove_property(comp, rdate->prop);
                    icalproperty_free(rdate->prop);
                }
            }
        }
        icalarray_free(rdates);

        /* Final check */
        if (trunc_dtstart) {
            /* All observances in comp occur prior to window open, remove it
               unless we haven't saved a tombstone comp of this type yet */
            if (icalcomponent_isa(comp) == ICAL_XDAYLIGHT_COMPONENT) {
                if (!tomb_day) {
                    tomb_day = comp;
                    comp = NULL;
                }
            }
            else if (!tomb_std) {
                tomb_std = comp;
                comp = NULL;
            }

            if (comp) {
                icalcomponent_remove_component(vtz, comp);
                icalcomponent_free(comp);
            }
        }
    }

    if (need_tomb && !icaltime_is_null_time(tombstone.onset)) {
        /* Need to add tombstone component/observance starting at window open
           as long as its not prior to start of TZ data */
        icalcomponent *tomb;
        icalproperty *prop, *nextp;

        /* Determine which tombstone component we need */
        if (tombstone.onset.is_daylight) {
            tomb = tomb_day;
            tomb_day = NULL;
        } else {
            tomb = tomb_std;
            tomb_std = NULL;
        }

        /* Set property values on our tombstone */
        for (prop = icalcomponent_get_first_property(tomb, ICAL_ANY_PROPERTY);
             prop; prop = nextp) {

            nextp = icalcomponent_get_next_property(tomb, ICAL_ANY_PROPERTY);

            switch (icalproperty_isa(prop)) {
            case ICAL_TZNAME_PROPERTY:
                icalproperty_set_tzname(prop, tombstone.name);
                break;
            case ICAL_TZOFFSETFROM_PROPERTY:
                icalproperty_set_tzoffsetfrom(prop, tombstone.offset_from);
                break;
            case ICAL_TZOFFSETTO_PROPERTY:
                icalproperty_set_tzoffsetto(prop, tombstone.offset_to);
                break;
            case ICAL_DTSTART_PROPERTY:
                /* Adjust window open to local time */
                icaltime_adjust(&start, 0, 0, 0, tombstone.offset_from);
                (void)icaltime_set_timezone(&start, NULL);

                icalproperty_set_dtstart(prop, start);
                break;
            default:
                icalcomponent_remove_property(tomb, prop);
                icalproperty_free(prop);
                break;
            }
        }

        /* Remove X-PROLEPTIC-TZNAME as it no longer applies */
        if (proleptic_prop) {
            icalcomponent_remove_property(vtz, proleptic_prop);
            icalproperty_free(proleptic_prop);
        }
    }

    /* Remove any unused tombstone components */
    if (tomb_std) {
        icalcomponent_remove_component(vtz, tomb_std);
        icalcomponent_free(tomb_std);
    }
    if (tomb_day) {
        icalcomponent_remove_component(vtz, tomb_day);
        icalcomponent_free(tomb_day);
    }

    if (need_tzuntil) {
        /* Add TZUNTIL to VTIMEZONE */
        prop = icalcomponent_get_first_property(vtz, ICAL_TZUNTIL_PROPERTY);

        if (prop) {
            icalproperty_set_tzuntil(prop, end);
        } else {
            icalcomponent_add_property(vtz, icalproperty_new_tzuntil(end));
        }
    }
}