]>
git.saurik.com Git - apple/icu.git/blob - icuSources/tools/tzcode/zic.c
2 ** This file is in the public domain, so clarified as of
3 ** 2006-07-17 by Arthur David Olson.
6 /* Enable extensions and modifications for ICU. */
9 /* Continue executing after link failure. Even if ICU is undefined
10 * (for vanilla zic behavior), ICU_LINKS should be defined, since zic
11 * appears to fail on the 2003 data the first time through during the
12 * linking phase. Running zic twice, with ICU_LINKS defined, causes
13 * links to be handled correctly. */
16 #define LEAVE_SOME_PRE_2011_SYSTEMS_IN_THE_LURCH
19 /* These constants are embedded in dynamically generated header
20 * version.h in the standard tzcode distribution. */
21 static char const PKGVERSION
[]="N/A";
22 static char const TZVERSION
[]="N/A";
23 static char const REPORT_BUGS_TO
[]="N/A";
32 #if U_PLATFORM_IS_DARWIN_BASED || U_PLATFORM_IS_LINUX_BASED || U_PLATFORM == U_PF_BSD || U_PLATFORM == U_PF_SOLARIS
36 #define ZIC_VERSION_PRE_2013 '2'
37 #define ZIC_VERSION '3'
39 typedef int_fast64_t zic_t
;
40 #define ZIC_MIN INT_FAST64_MIN
41 #define ZIC_MAX INT_FAST64_MAX
42 #define SCNdZIC SCNdFAST64
44 #ifndef ZIC_MAX_ABBR_LEN_WO_WARN
45 #define ZIC_MAX_ABBR_LEN_WO_WARN 6
46 #endif /* !defined ZIC_MAX_ABBR_LEN_WO_WARN */
52 #define MKDIR_UMASK (S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
54 #define MKDIR_UMASK 0755
62 ** On some ancient hosts, predicates like `isspace(C)' are defined
63 ** only if isascii(C) || C == EOF. Modern hosts obey the C Standard,
64 ** which says they are defined only if C == ((unsigned char) C) || C == EOF.
65 ** Neither the C Standard nor Posix require that `isascii' exist.
66 ** For portability, we check both ancient and modern requirements.
67 ** If isascii is not defined, the isascii check succeeds trivially.
74 #define end(cp) (strchr((cp), '\0'))
77 const char * r_filename
;
81 zic_t r_loyear
; /* for example, 1986 */
82 zic_t r_hiyear
; /* for example, 1986 */
83 const char * r_yrtype
;
87 int r_month
; /* 0..11 */
89 int r_dycode
; /* see below */
93 zic_t r_tod
; /* time from midnight */
94 int r_todisstd
; /* above is standard time if TRUE */
95 /* or wall clock time if FALSE */
96 int r_todisgmt
; /* above is GMT if TRUE */
97 /* or local time if FALSE */
98 zic_t r_stdoff
; /* offset from standard time */
99 const char * r_abbrvar
; /* variable part of abbreviation */
101 int r_todo
; /* a rule to do (used in outzone) */
102 zic_t r_temp
; /* used in outzone */
106 ** r_dycode r_dayofmonth r_wday
109 #define DC_DOM 0 /* 1..31 */ /* unused */
110 #define DC_DOWGEQ 1 /* 1..31 */ /* 0..6 (Sun..Sat) */
111 #define DC_DOWLEQ 2 /* 1..31 */ /* 0..6 (Sun..Sat) */
114 const char * z_filename
;
120 const char * z_format
;
124 struct rule
* z_rules
;
127 struct rule z_untilrule
;
131 extern int getopt(int argc
, char * const argv
[],
132 const char * options
);
133 extern int link(const char * fromname
, const char * toname
);
134 extern char * optarg
;
138 # define link(from, to) (-1)
141 # define symlink(from, to) (-1)
144 static void addtt(zic_t starttime
, int type
);
146 static int addtype(const zic_t gmtoff
, const zic_t rawoff
, const zic_t dstoff
,
147 char *const abbr
, int isdst
,
148 int ttisstd
, int ttisgmt
);
150 static int addtype(zic_t gmtoff
, const char * abbr
, int isdst
,
151 int ttisstd
, int ttisgmt
);
153 static void leapadd(zic_t t
, int positive
, int rolling
, int count
);
154 static void adjleap(void);
155 static void associate(void);
156 static void dolink(const char * fromfield
, const char * tofield
);
157 static char ** getfields(char * buf
);
158 static zic_t
gethms(const char * string
, const char * errstrng
,
160 static void infile(const char * filename
);
161 static void inleap(char ** fields
, int nfields
);
162 static void inlink(char ** fields
, int nfields
);
163 static void inrule(char ** fields
, int nfields
);
164 static int inzcont(char ** fields
, int nfields
);
165 static int inzone(char ** fields
, int nfields
);
166 static int inzsub(char ** fields
, int nfields
, int iscont
);
167 static int itsdir(const char * name
);
168 static int lowerit(int c
);
169 static int mkdirs(char * filename
);
170 static void newabbr(const char * abbr
);
171 static zic_t
oadd(zic_t t1
, zic_t t2
);
172 static void outzone(const struct zone
* zp
, int ntzones
);
173 static zic_t
rpytime(const struct rule
* rp
, zic_t wantedy
);
174 static void rulesub(struct rule
* rp
,
175 const char * loyearp
, const char * hiyearp
,
176 const char * typep
, const char * monthp
,
177 const char * dayp
, const char * timep
);
178 static zic_t
tadd(zic_t t1
, zic_t t2
);
179 static int yearistype(int year
, const char * type
);
181 static void emit_icu_zone(FILE* f
, const char* zoneName
, int zoneOffset
,
182 const struct rule
* rule
,
183 int ruleIndex
, int startYear
);
184 static void emit_icu_link(FILE* f
, const char* from
, const char* to
);
185 static void emit_icu_rule(FILE* f
, const struct rule
* r
, int ruleIndex
);
186 static int add_icu_final_rules(const struct rule
* r1
, const struct rule
* r2
);
191 static const char * filename
;
194 static zic_t leapminyear
;
195 static zic_t leapmaxyear
;
197 static int max_abbrvar_len
;
198 static int max_format_len
;
199 static zic_t max_year
;
200 static zic_t min_year
;
202 static const char * rfilename
;
204 static const char * progname
;
206 static int timecnt_alloc
;
219 ** Which fields are which on a Zone line.
227 #define ZF_TILMONTH 6
230 #define ZONE_MINFIELDS 5
231 #define ZONE_MAXFIELDS 9
234 ** Which fields are which on a Zone continuation line.
240 #define ZFC_TILYEAR 3
241 #define ZFC_TILMONTH 4
243 #define ZFC_TILTIME 6
244 #define ZONEC_MINFIELDS 3
245 #define ZONEC_MAXFIELDS 7
248 ** Which files are which on a Rule line.
260 #define RULE_FIELDS 10
263 ** Which fields are which on a Link line.
268 #define LINK_FIELDS 3
271 ** Which fields are which on a Leap line.
280 #define LEAP_FIELDS 7
290 static struct rule
* rules
;
291 static int nrules
; /* number of rules */
292 static int nrules_alloc
;
294 static struct zone
* zones
;
295 static int nzones
; /* number of zones */
296 static int nzones_alloc
;
299 const char * l_filename
;
305 static struct link
* links
;
307 static int nlinks_alloc
;
315 /* Indices into rules[] for final rules. They will occur in pairs,
316 * with finalRules[i] occurring before finalRules[i+1] in the year.
317 * Each zone need only store a start year, a standard offset, and an
318 * index into finalRules[]. FinalRules[] are aliases into rules[]. */
319 static const struct rule
** finalRules
= NULL
;
320 static int finalRulesCount
= 0;
323 static struct lookup
const * byword(const char * string
,
324 const struct lookup
* lp
);
326 static struct lookup
const line_codes
[] = {
334 static struct lookup
const mon_names
[] = {
335 { "January", TM_JANUARY
},
336 { "February", TM_FEBRUARY
},
337 { "March", TM_MARCH
},
338 { "April", TM_APRIL
},
342 { "August", TM_AUGUST
},
343 { "September", TM_SEPTEMBER
},
344 { "October", TM_OCTOBER
},
345 { "November", TM_NOVEMBER
},
346 { "December", TM_DECEMBER
},
350 static struct lookup
const wday_names
[] = {
351 { "Sunday", TM_SUNDAY
},
352 { "Monday", TM_MONDAY
},
353 { "Tuesday", TM_TUESDAY
},
354 { "Wednesday", TM_WEDNESDAY
},
355 { "Thursday", TM_THURSDAY
},
356 { "Friday", TM_FRIDAY
},
357 { "Saturday", TM_SATURDAY
},
361 static struct lookup
const lasts
[] = {
362 { "last-Sunday", TM_SUNDAY
},
363 { "last-Monday", TM_MONDAY
},
364 { "last-Tuesday", TM_TUESDAY
},
365 { "last-Wednesday", TM_WEDNESDAY
},
366 { "last-Thursday", TM_THURSDAY
},
367 { "last-Friday", TM_FRIDAY
},
368 { "last-Saturday", TM_SATURDAY
},
372 static struct lookup
const begin_years
[] = {
373 { "minimum", YR_MINIMUM
},
374 { "maximum", YR_MAXIMUM
},
378 static struct lookup
const end_years
[] = {
379 { "minimum", YR_MINIMUM
},
380 { "maximum", YR_MAXIMUM
},
385 static struct lookup
const leap_types
[] = {
387 { "Stationary", FALSE
},
391 static const int len_months
[2][MONSPERYEAR
] = {
392 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
393 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
396 static const int len_years
[2] = {
397 DAYSPERNYEAR
, DAYSPERLYEAR
400 static struct attype
{
404 static zic_t gmtoffs
[TZ_MAX_TYPES
];
406 /* gmtoffs[i] = rawoffs[i] + dstoffs[i] */
407 static zic_t rawoffs
[TZ_MAX_TYPES
];
408 static zic_t dstoffs
[TZ_MAX_TYPES
];
410 static char isdsts
[TZ_MAX_TYPES
];
411 static unsigned char abbrinds
[TZ_MAX_TYPES
];
412 static char ttisstds
[TZ_MAX_TYPES
];
413 static char ttisgmts
[TZ_MAX_TYPES
];
414 static char chars
[TZ_MAX_CHARS
];
415 static zic_t trans
[TZ_MAX_LEAPS
];
416 static zic_t corr
[TZ_MAX_LEAPS
];
417 static char roll
[TZ_MAX_LEAPS
];
420 ** Memory allocation.
423 static _Noreturn
void
424 memory_exhausted(const char *msg
)
426 fprintf(stderr
, _("%s: Memory exhausted: %s\n"), progname
, msg
);
430 static ATTRIBUTE_PURE
size_t
431 size_product(size_t nitems
, size_t itemsize
)
433 if (SIZE_MAX
/ itemsize
< nitems
)
434 memory_exhausted("size overflow");
435 return nitems
* itemsize
;
438 static ATTRIBUTE_PURE
void *
439 memcheck(void *const ptr
)
442 memory_exhausted(strerror(errno
));
446 #define emalloc(size) memcheck(malloc(size))
447 #define erealloc(ptr, size) memcheck(realloc(ptr, size))
448 #define ecpyalloc(ptr) memcheck(icpyalloc(ptr))
449 #define ecatalloc(oldp, newp) memcheck(icatalloc((oldp), (newp)))
452 growalloc(void *ptr
, size_t itemsize
, int nitems
, int *nitems_alloc
)
454 if (nitems
< *nitems_alloc
)
457 int amax
= INT_MAX
< SIZE_MAX
? INT_MAX
: SIZE_MAX
;
458 if ((amax
- 1) / 3 * 2 < *nitems_alloc
)
459 memory_exhausted("int overflow");
460 *nitems_alloc
= *nitems_alloc
+ (*nitems_alloc
>> 1) + 1;
461 return erealloc(ptr
, size_product(*nitems_alloc
, itemsize
));
470 eats(const char *const name
, const int num
, const char *const rname
,
480 eat(const char *const name
, const int num
)
482 eats(name
, num
, NULL
, -1);
485 static void ATTRIBUTE_FORMAT((printf
, 1, 0))
486 verror(const char *const string
, va_list args
)
489 ** Match the format of "cc" to allow sh users to
490 ** zic ... 2>&1 | error -t "*" -v
493 fprintf(stderr
, _("\"%s\", line %d: "), filename
, linenum
);
494 vfprintf(stderr
, string
, args
);
495 if (rfilename
!= NULL
)
496 (void) fprintf(stderr
, _(" (rule from \"%s\", line %d)"),
497 rfilename
, rlinenum
);
498 (void) fprintf(stderr
, "\n");
502 static void ATTRIBUTE_FORMAT((printf
, 1, 2))
503 error(const char *const string
, ...)
506 va_start(args
, string
);
507 verror(string
, args
);
511 static void ATTRIBUTE_FORMAT((printf
, 1, 2))
512 warning(const char *const string
, ...)
515 fprintf(stderr
, _("warning: "));
516 va_start(args
, string
);
517 verror(string
, args
);
522 static _Noreturn
void
523 usage(FILE *stream
, int status
)
525 (void) fprintf(stream
, _("%s: usage is %s \
526 [ --version ] [ --help ] [ -v ] [ -l localtime ] [ -p posixrules ] \\\n\
527 \t[ -d directory ] [ -L leapseconds ] [ -y yearistype ] [ filename ... ]\n\
529 Report bugs to %s.\n"),
530 progname
, progname
, REPORT_BUGS_TO
);
535 /* File into which we will write supplemental ICU data. */
536 static FILE * icuFile
;
539 emit_icu_zone(FILE* f
, const char* zoneName
, int zoneOffset
,
540 const struct rule
* rule
,
541 int ruleIndex
, int startYear
) {
542 /* machine-readable section */
543 fprintf(f
, "zone %s %d %d %s", zoneName
, zoneOffset
, startYear
, rule
->r_name
);
545 /* human-readable section */
546 fprintf(f
, " # zone %s, offset %d, year >= %d, rule %s (%d)\n",
547 zoneName
, zoneOffset
, startYear
,
548 rule
->r_name
, ruleIndex
);
552 emit_icu_link(FILE* f
, const char* from
, const char* to
) {
553 /* machine-readable section */
554 fprintf(f
, "link %s %s\n", from
, to
);
557 static const char* DYCODE
[] = {"DOM", "DOWGEQ", "DOWLEQ"};
560 emit_icu_rule(FILE* f
, const struct rule
* r
, int ruleIndex
) {
561 if (r
->r_yrtype
!= NULL
) {
562 warning("year types not supported by ICU");
563 fprintf(stderr
, "rule %s, file %s, line %d\n",
564 r
->r_name
, r
->r_filename
, r
->r_linenum
);
567 /* machine-readable section */
568 fprintf(f
, "rule %s %s %d %d %d %lld %d %d %lld",
569 r
->r_name
, DYCODE
[r
->r_dycode
],
570 r
->r_month
, r
->r_dayofmonth
,
571 (r
->r_dycode
== DC_DOM
? -1 : r
->r_wday
),
572 r
->r_tod
, r
->r_todisstd
, r
->r_todisgmt
, r
->r_stdoff
575 /* human-readable section */
576 fprintf(f
, " # %d: %s, file %s, line %d",
577 ruleIndex
, r
->r_name
, r
->r_filename
, r
->r_linenum
);
578 fprintf(f
, ", mode %s", DYCODE
[r
->r_dycode
]);
579 fprintf(f
, ", %s, dom %d", mon_names
[r
->r_month
].l_word
, r
->r_dayofmonth
);
580 if (r
->r_dycode
!= DC_DOM
) {
581 fprintf(f
, ", %s", wday_names
[r
->r_wday
].l_word
);
583 fprintf(f
, ", time %lld", r
->r_tod
);
584 fprintf(f
, ", isstd %d", r
->r_todisstd
);
585 fprintf(f
, ", isgmt %d", r
->r_todisgmt
);
586 fprintf(f
, ", offset %lld", r
->r_stdoff
);
591 add_icu_final_rules(const struct rule
* r1
, const struct rule
* r2
) {
594 for (i
=0; i
<finalRulesCount
; ++i
) { /* i+=2 should work too */
595 if (r1
==finalRules
[i
]) return i
; /* [sic] pointer comparison */
598 finalRules
= (const struct rule
**) (void*) erealloc((char *) finalRules
,
599 (finalRulesCount
+ 2) * sizeof(*finalRules
));
600 finalRules
[finalRulesCount
++] = r1
;
601 finalRules
[finalRulesCount
++] = r2
;
602 return finalRulesCount
- 2;
606 static const char * psxrules
;
607 static const char * lcltime
;
608 static const char * directory
;
609 static const char * leapsec
;
610 static const char * yitcommand
;
613 main(int argc
, char **argv
)
620 (void) umask(umask(S_IWGRP
| S_IWOTH
) | (S_IWGRP
| S_IWOTH
));
623 (void) setlocale(LC_ALL
, "");
625 (void) bindtextdomain(TZ_DOMAIN
, TZ_DOMAINDIR
);
626 #endif /* defined TEXTDOMAINDIR */
627 (void) textdomain(TZ_DOMAIN
);
628 #endif /* HAVE_GETTEXT */
630 if (TYPE_BIT(zic_t
) < 64) {
631 (void) fprintf(stderr
, "%s: %s\n", progname
,
632 _("wild compilation-time specification of zic_t"));
635 for (i
= 1; i
< argc
; ++i
)
636 if (strcmp(argv
[i
], "--version") == 0) {
637 (void) printf("zic %s%s\n", PKGVERSION
, TZVERSION
);
639 } else if (strcmp(argv
[i
], "--help") == 0) {
640 usage(stdout
, EXIT_SUCCESS
);
642 while ((c
= getopt(argc
, argv
, "d:l:p:L:vsy:")) != EOF
&& c
!= -1)
645 usage(stderr
, EXIT_FAILURE
);
647 if (directory
== NULL
)
650 (void) fprintf(stderr
,
651 _("%s: More than one -d option specified\n"),
660 (void) fprintf(stderr
,
661 _("%s: More than one -l option specified\n"),
667 if (psxrules
== NULL
)
670 (void) fprintf(stderr
,
671 _("%s: More than one -p option specified\n"),
677 if (yitcommand
== NULL
)
680 (void) fprintf(stderr
,
681 _("%s: More than one -y option specified\n"),
690 (void) fprintf(stderr
,
691 _("%s: More than one -L option specified\n"),
700 (void) printf("%s: -s ignored\n", progname
);
703 if (optind
== argc
- 1 && strcmp(argv
[optind
], "=") == 0)
704 usage(stderr
, EXIT_FAILURE
); /* usage message by request */
705 if (directory
== NULL
)
707 if (yitcommand
== NULL
)
708 yitcommand
= "yearistype";
710 if (optind
< argc
&& leapsec
!= NULL
) {
716 if ((icuFile
= fopen(ICU_ZONE_FILE
, "w")) == NULL
) {
717 const char *e
= strerror(errno
);
718 (void) fprintf(stderr
, _("%s: Can't open %s: %s\n"),
719 progname
, ICU_ZONE_FILE
, e
);
720 (void) exit(EXIT_FAILURE
);
723 for (i
= optind
; i
< argc
; ++i
)
728 for (i
= 0; i
< nzones
; i
= j
) {
730 ** Find the next non-continuation zone entry.
732 for (j
= i
+ 1; j
< nzones
&& zones
[j
].z_name
== NULL
; ++j
)
734 outzone(&zones
[i
], j
- i
);
739 for (i
= 0; i
< nlinks
; ++i
) {
740 eat(links
[i
].l_filename
, links
[i
].l_linenum
);
741 dolink(links
[i
].l_from
, links
[i
].l_to
);
743 emit_icu_link(icuFile
, links
[i
].l_from
, links
[i
].l_to
);
746 for (j
= 0; j
< nlinks
; ++j
)
747 if (strcmp(links
[i
].l_to
,
748 links
[j
].l_from
) == 0)
749 warning(_("link to link"));
751 if (lcltime
!= NULL
) {
752 eat("command line", 1);
753 dolink(lcltime
, TZDEFAULT
);
755 if (psxrules
!= NULL
) {
756 eat("command line", 1);
757 dolink(psxrules
, TZDEFRULES
);
760 for (i
=0; i
<finalRulesCount
; ++i
) {
761 emit_icu_rule(icuFile
, finalRules
[i
], i
);
764 return (errors
== 0) ? EXIT_SUCCESS
: EXIT_FAILURE
;
768 dolink(const char *const fromfield
, const char *const tofield
)
770 register char * fromname
;
771 register char * toname
;
773 if (fromfield
[0] == '/')
774 fromname
= ecpyalloc(fromfield
);
776 fromname
= ecpyalloc(directory
);
777 fromname
= ecatalloc(fromname
, "/");
778 fromname
= ecatalloc(fromname
, fromfield
);
780 if (tofield
[0] == '/')
781 toname
= ecpyalloc(tofield
);
783 toname
= ecpyalloc(directory
);
784 toname
= ecatalloc(toname
, "/");
785 toname
= ecatalloc(toname
, tofield
);
788 ** We get to be careful here since
789 ** there's a fair chance of root running us.
792 (void) remove(toname
);
793 if (link(fromname
, toname
) != 0
794 && access(fromname
, F_OK
) == 0 && !itsdir(fromname
)) {
797 if (mkdirs(toname
) != 0)
800 result
= link(fromname
, toname
);
802 const char *s
= fromfield
;
804 register char * symlinkcontents
= NULL
;
808 while ((s
= strchr(s
, '/'))
809 && ! strncmp (fromfield
, tofield
,
812 for (s
= tofield
+ (t
- fromfield
);
813 (s
= strchr(s
, '/'));
816 ecatalloc(symlinkcontents
,
818 symlinkcontents
= ecatalloc(symlinkcontents
, t
);
819 result
= symlink(symlinkcontents
, toname
);
821 warning(_("hard link failed, symbolic link used"));
822 free(symlinkcontents
);
827 fp
= fopen(fromname
, "rb");
829 const char *e
= strerror(errno
);
830 (void) fprintf(stderr
,
831 _("%s: Can't read %s: %s\n"),
832 progname
, fromname
, e
);
835 tp
= fopen(toname
, "wb");
837 const char *e
= strerror(errno
);
838 (void) fprintf(stderr
,
839 _("%s: Can't create %s: %s\n"),
840 progname
, toname
, e
);
843 while ((c
= getc(fp
)) != EOF
)
845 if (ferror(fp
) || fclose(fp
)) {
846 (void) fprintf(stderr
,
847 _("%s: Error reading %s\n"),
851 if (ferror(tp
) || fclose(tp
)) {
852 (void) fprintf(stderr
,
853 _("%s: Error writing %s\n"),
857 warning(_("link failed, copy used"));
867 #define TIME_T_BITS_IN_FILE 64
869 static const zic_t min_time
= (zic_t
) -1 << (TIME_T_BITS_IN_FILE
- 1);
870 static const zic_t max_time
= -1 - ((zic_t
) -1 << (TIME_T_BITS_IN_FILE
- 1));
873 itsdir(const char *const name
)
875 register char * myname
;
878 myname
= ecpyalloc(name
);
879 myname
= ecatalloc(myname
, "/.");
880 accres
= access(myname
, F_OK
);
886 ** Associate sets of rules with zones.
890 ** Sort by rule name.
894 rcomp(const void *cp1
, const void *cp2
)
896 return strcmp(((const struct rule
*) cp1
)->r_name
,
897 ((const struct rule
*) cp2
)->r_name
);
903 register struct zone
* zp
;
904 register struct rule
* rp
;
905 register int base
, out
;
909 (void) qsort(rules
, nrules
, sizeof *rules
, rcomp
);
910 for (i
= 0; i
< nrules
- 1; ++i
) {
911 if (strcmp(rules
[i
].r_name
,
912 rules
[i
+ 1].r_name
) != 0)
914 if (strcmp(rules
[i
].r_filename
,
915 rules
[i
+ 1].r_filename
) == 0)
917 eat(rules
[i
].r_filename
, rules
[i
].r_linenum
);
918 warning(_("same rule name in multiple files"));
919 eat(rules
[i
+ 1].r_filename
, rules
[i
+ 1].r_linenum
);
920 warning(_("same rule name in multiple files"));
921 for (j
= i
+ 2; j
< nrules
; ++j
) {
922 if (strcmp(rules
[i
].r_name
,
923 rules
[j
].r_name
) != 0)
925 if (strcmp(rules
[i
].r_filename
,
926 rules
[j
].r_filename
) == 0)
928 if (strcmp(rules
[i
+ 1].r_filename
,
929 rules
[j
].r_filename
) == 0)
936 for (i
= 0; i
< nzones
; ++i
) {
941 for (base
= 0; base
< nrules
; base
= out
) {
943 for (out
= base
+ 1; out
< nrules
; ++out
)
944 if (strcmp(rp
->r_name
, rules
[out
].r_name
) != 0)
946 for (i
= 0; i
< nzones
; ++i
) {
948 if (strcmp(zp
->z_rule
, rp
->r_name
) != 0)
951 zp
->z_nrules
= out
- base
;
954 for (i
= 0; i
< nzones
; ++i
) {
956 if (zp
->z_nrules
== 0) {
958 ** Maybe we have a local standard time offset.
960 eat(zp
->z_filename
, zp
->z_linenum
);
961 zp
->z_stdoff
= gethms(zp
->z_rule
, _("unruly zone"),
964 ** Note, though, that if there's no rule,
965 ** a '%s' in the format is a bad thing.
967 if (strchr(zp
->z_format
, '%') != 0)
968 error("%s", _("%s in ruleless zone"));
976 infile(const char *name
)
979 register char ** fields
;
981 register const struct lookup
* lp
;
982 register int nfields
;
983 register int wantcont
;
987 if (strcmp(name
, "-") == 0) {
988 name
= _("standard input");
990 } else if ((fp
= fopen(name
, "r")) == NULL
) {
991 const char *e
= strerror(errno
);
993 (void) fprintf(stderr
, _("%s: Can't open %s: %s\n"),
998 for (num
= 1; ; ++num
) {
1000 if (fgets(buf
, sizeof buf
, fp
) != buf
)
1002 cp
= strchr(buf
, '\n');
1004 error(_("line too long"));
1008 fields
= getfields(buf
);
1010 while (fields
[nfields
] != NULL
) {
1013 if (strcmp(fields
[nfields
], "-") == 0)
1014 fields
[nfields
] = &nada
;
1019 } else if (wantcont
) {
1020 wantcont
= inzcont(fields
, nfields
);
1022 lp
= byword(fields
[0], line_codes
);
1024 error(_("input line of unknown type"));
1025 else switch ((int) (lp
->l_value
)) {
1027 inrule(fields
, nfields
);
1031 wantcont
= inzone(fields
, nfields
);
1034 inlink(fields
, nfields
);
1038 if (name
!= leapsec
)
1039 (void) fprintf(stderr
,
1040 _("%s: Leap line in non leap seconds file %s\n"),
1042 else inleap(fields
, nfields
);
1045 default: /* "cannot happen" */
1046 (void) fprintf(stderr
,
1047 _("%s: panic: Invalid l_value %d\n"),
1048 progname
, lp
->l_value
);
1055 (void) fprintf(stderr
, _("%s: Error reading %s\n"),
1056 progname
, filename
);
1059 if (fp
!= stdin
&& fclose(fp
)) {
1060 const char *e
= strerror(errno
);
1062 (void) fprintf(stderr
, _("%s: Error closing %s: %s\n"),
1063 progname
, filename
, e
);
1067 error(_("expected continuation line not found"));
1071 ** Convert a string of one of the forms
1072 ** h -h hh:mm -hh:mm hh:mm:ss -hh:mm:ss
1073 ** into a number of seconds.
1074 ** A null string maps to zero.
1075 ** Call error with errstring and return zero on errors.
1079 gethms(const char *string
, const char *const errstring
, const int signable
)
1084 if (string
== NULL
|| *string
== '\0')
1088 else if (*string
== '-') {
1092 if (sscanf(string
, scheck(string
, "%"SCNdZIC
), &hh
) == 1)
1094 else if (sscanf(string
, scheck(string
, "%"SCNdZIC
":%d"), &hh
, &mm
) == 2)
1096 else if (sscanf(string
, scheck(string
, "%"SCNdZIC
":%d:%d"),
1097 &hh
, &mm
, &ss
) != 3) {
1098 error("%s", errstring
);
1102 mm
< 0 || mm
>= MINSPERHOUR
||
1103 ss
< 0 || ss
> SECSPERMIN
) {
1104 error("%s", errstring
);
1107 if (ZIC_MAX
/ SECSPERHOUR
< hh
) {
1108 error(_("time overflow"));
1111 if (noise
&& hh
== HOURSPERDAY
&& mm
== 0 && ss
== 0)
1112 warning(_("24:00 not handled by pre-1998 versions of zic"));
1113 if (noise
&& (hh
> HOURSPERDAY
||
1114 (hh
== HOURSPERDAY
&& (mm
!= 0 || ss
!= 0))))
1115 warning(_("values over 24 hours not handled by pre-2007 versions of zic"));
1116 return oadd(sign
* hh
* SECSPERHOUR
,
1117 sign
* (mm
* SECSPERMIN
+ ss
));
1121 inrule(register char **const fields
, const int nfields
)
1123 static struct rule r
;
1125 if (nfields
!= RULE_FIELDS
) {
1126 error(_("wrong number of fields on Rule line"));
1129 if (*fields
[RF_NAME
] == '\0') {
1130 error(_("nameless rule"));
1133 r
.r_filename
= filename
;
1134 r
.r_linenum
= linenum
;
1135 r
.r_stdoff
= gethms(fields
[RF_STDOFF
], _("invalid saved time"), TRUE
);
1136 rulesub(&r
, fields
[RF_LOYEAR
], fields
[RF_HIYEAR
], fields
[RF_COMMAND
],
1137 fields
[RF_MONTH
], fields
[RF_DAY
], fields
[RF_TOD
]);
1138 r
.r_name
= ecpyalloc(fields
[RF_NAME
]);
1139 r
.r_abbrvar
= ecpyalloc(fields
[RF_ABBRVAR
]);
1140 if (max_abbrvar_len
< strlen(r
.r_abbrvar
))
1141 max_abbrvar_len
= strlen(r
.r_abbrvar
);
1142 rules
= growalloc(rules
, sizeof *rules
, nrules
, &nrules_alloc
);
1143 rules
[nrules
++] = r
;
1147 inzone(register char **const fields
, const int nfields
)
1151 if (nfields
< ZONE_MINFIELDS
|| nfields
> ZONE_MAXFIELDS
) {
1152 error(_("wrong number of fields on Zone line"));
1155 if (strcmp(fields
[ZF_NAME
], TZDEFAULT
) == 0 && lcltime
!= NULL
) {
1157 _("\"Zone %s\" line and -l option are mutually exclusive"),
1161 if (strcmp(fields
[ZF_NAME
], TZDEFRULES
) == 0 && psxrules
!= NULL
) {
1163 _("\"Zone %s\" line and -p option are mutually exclusive"),
1167 for (i
= 0; i
< nzones
; ++i
)
1168 if (zones
[i
].z_name
!= NULL
&&
1169 strcmp(zones
[i
].z_name
, fields
[ZF_NAME
]) == 0) {
1171 _("duplicate zone name %s (file \"%s\", line %d)"),
1173 zones
[i
].z_filename
,
1174 zones
[i
].z_linenum
);
1177 return inzsub(fields
, nfields
, FALSE
);
1181 inzcont(register char **const fields
, const int nfields
)
1183 if (nfields
< ZONEC_MINFIELDS
|| nfields
> ZONEC_MAXFIELDS
) {
1184 error(_("wrong number of fields on Zone continuation line"));
1187 return inzsub(fields
, nfields
, TRUE
);
1191 inzsub(register char **const fields
, const int nfields
, const int iscont
)
1194 static struct zone z
;
1195 register int i_gmtoff
, i_rule
, i_format
;
1196 register int i_untilyear
, i_untilmonth
;
1197 register int i_untilday
, i_untiltime
;
1198 register int hasuntil
;
1201 i_gmtoff
= ZFC_GMTOFF
;
1203 i_format
= ZFC_FORMAT
;
1204 i_untilyear
= ZFC_TILYEAR
;
1205 i_untilmonth
= ZFC_TILMONTH
;
1206 i_untilday
= ZFC_TILDAY
;
1207 i_untiltime
= ZFC_TILTIME
;
1210 i_gmtoff
= ZF_GMTOFF
;
1212 i_format
= ZF_FORMAT
;
1213 i_untilyear
= ZF_TILYEAR
;
1214 i_untilmonth
= ZF_TILMONTH
;
1215 i_untilday
= ZF_TILDAY
;
1216 i_untiltime
= ZF_TILTIME
;
1217 z
.z_name
= ecpyalloc(fields
[ZF_NAME
]);
1219 z
.z_filename
= filename
;
1220 z
.z_linenum
= linenum
;
1221 z
.z_gmtoff
= gethms(fields
[i_gmtoff
], _("invalid UT offset"), TRUE
);
1222 if ((cp
= strchr(fields
[i_format
], '%')) != 0) {
1223 if (*++cp
!= 's' || strchr(cp
, '%') != 0) {
1224 error(_("invalid abbreviation format"));
1228 z
.z_rule
= ecpyalloc(fields
[i_rule
]);
1229 z
.z_format
= ecpyalloc(fields
[i_format
]);
1230 if (max_format_len
< strlen(z
.z_format
))
1231 max_format_len
= strlen(z
.z_format
);
1232 hasuntil
= nfields
> i_untilyear
;
1234 z
.z_untilrule
.r_filename
= filename
;
1235 z
.z_untilrule
.r_linenum
= linenum
;
1236 rulesub(&z
.z_untilrule
,
1237 fields
[i_untilyear
],
1240 (nfields
> i_untilmonth
) ?
1241 fields
[i_untilmonth
] : "Jan",
1242 (nfields
> i_untilday
) ? fields
[i_untilday
] : "1",
1243 (nfields
> i_untiltime
) ? fields
[i_untiltime
] : "0");
1244 z
.z_untiltime
= rpytime(&z
.z_untilrule
,
1245 z
.z_untilrule
.r_loyear
);
1246 if (iscont
&& nzones
> 0 &&
1247 z
.z_untiltime
> min_time
&&
1248 z
.z_untiltime
< max_time
&&
1249 zones
[nzones
- 1].z_untiltime
> min_time
&&
1250 zones
[nzones
- 1].z_untiltime
< max_time
&&
1251 zones
[nzones
- 1].z_untiltime
>= z
.z_untiltime
) {
1253 "Zone continuation line end time is not after end time of previous line"
1258 zones
= growalloc(zones
, sizeof *zones
, nzones
, &nzones_alloc
);
1259 zones
[nzones
++] = z
;
1261 ** If there was an UNTIL field on this line,
1262 ** there's more information about the zone on the next line.
1268 inleap(register char ** const fields
, const int nfields
)
1270 register const char * cp
;
1271 register const struct lookup
* lp
;
1278 if (nfields
!= LEAP_FIELDS
) {
1279 error(_("wrong number of fields on Leap line"));
1283 cp
= fields
[LP_YEAR
];
1284 if (sscanf(cp
, scheck(cp
, "%"SCNdZIC
), &year
) != 1) {
1288 error(_("invalid leaping year"));
1291 if (!leapseen
|| leapmaxyear
< year
)
1293 if (!leapseen
|| leapminyear
> year
)
1299 i
= len_years
[isleap(j
)];
1303 i
= -len_years
[isleap(j
)];
1305 dayoff
= oadd(dayoff
, i
);
1307 if ((lp
= byword(fields
[LP_MONTH
], mon_names
)) == NULL
) {
1308 error(_("invalid month name"));
1311 month
= lp
->l_value
;
1313 while (j
!= month
) {
1314 i
= len_months
[isleap(year
)][j
];
1315 dayoff
= oadd(dayoff
, i
);
1318 cp
= fields
[LP_DAY
];
1319 if (sscanf(cp
, scheck(cp
, "%d"), &day
) != 1 ||
1320 day
<= 0 || day
> len_months
[isleap(year
)][month
]) {
1321 error(_("invalid day of month"));
1324 dayoff
= oadd(dayoff
, day
- 1);
1325 if (dayoff
< 0 && !TYPE_SIGNED(zic_t
)) {
1326 error(_("time before zero"));
1329 if (dayoff
< min_time
/ SECSPERDAY
) {
1330 error(_("time too small"));
1333 if (dayoff
> max_time
/ SECSPERDAY
) {
1334 error(_("time too large"));
1337 t
= (zic_t
) dayoff
* SECSPERDAY
;
1338 tod
= gethms(fields
[LP_TIME
], _("invalid time of day"), FALSE
);
1339 cp
= fields
[LP_CORR
];
1341 register int positive
;
1344 if (strcmp(cp
, "") == 0) { /* infile() turns "-" into "" */
1347 } else if (strcmp(cp
, "--") == 0) {
1350 } else if (strcmp(cp
, "+") == 0) {
1353 } else if (strcmp(cp
, "++") == 0) {
1357 error(_("illegal CORRECTION field on Leap line"));
1360 if ((lp
= byword(fields
[LP_ROLL
], leap_types
)) == NULL
) {
1362 "illegal Rolling/Stationary field on Leap line"
1366 leapadd(tadd(t
, tod
), positive
, lp
->l_value
, count
);
1371 inlink(register char **const fields
, const int nfields
)
1375 if (nfields
!= LINK_FIELDS
) {
1376 error(_("wrong number of fields on Link line"));
1379 if (*fields
[LF_FROM
] == '\0') {
1380 error(_("blank FROM field on Link line"));
1383 if (*fields
[LF_TO
] == '\0') {
1384 error(_("blank TO field on Link line"));
1387 l
.l_filename
= filename
;
1388 l
.l_linenum
= linenum
;
1389 l
.l_from
= ecpyalloc(fields
[LF_FROM
]);
1390 l
.l_to
= ecpyalloc(fields
[LF_TO
]);
1391 links
= growalloc(links
, sizeof *links
, nlinks
, &nlinks_alloc
);
1392 links
[nlinks
++] = l
;
1396 rulesub(register struct rule
*const rp
,
1397 const char *const loyearp
,
1398 const char *const hiyearp
,
1399 const char *const typep
,
1400 const char *const monthp
,
1401 const char *const dayp
,
1402 const char *const timep
)
1404 register const struct lookup
* lp
;
1405 register const char * cp
;
1409 if ((lp
= byword(monthp
, mon_names
)) == NULL
) {
1410 error(_("invalid month name"));
1413 rp
->r_month
= lp
->l_value
;
1414 rp
->r_todisstd
= FALSE
;
1415 rp
->r_todisgmt
= FALSE
;
1416 dp
= ecpyalloc(timep
);
1418 ep
= dp
+ strlen(dp
) - 1;
1419 switch (lowerit(*ep
)) {
1420 case 's': /* Standard */
1421 rp
->r_todisstd
= TRUE
;
1422 rp
->r_todisgmt
= FALSE
;
1425 case 'w': /* Wall */
1426 rp
->r_todisstd
= FALSE
;
1427 rp
->r_todisgmt
= FALSE
;
1430 case 'g': /* Greenwich */
1431 case 'u': /* Universal */
1432 case 'z': /* Zulu */
1433 rp
->r_todisstd
= TRUE
;
1434 rp
->r_todisgmt
= TRUE
;
1439 rp
->r_tod
= gethms(dp
, _("invalid time of day"), FALSE
);
1445 lp
= byword(cp
, begin_years
);
1446 rp
->r_lowasnum
= lp
== NULL
;
1447 if (!rp
->r_lowasnum
) switch ((int) lp
->l_value
) {
1449 rp
->r_loyear
= ZIC_MIN
;
1452 rp
->r_loyear
= ZIC_MAX
;
1454 default: /* "cannot happen" */
1455 (void) fprintf(stderr
,
1456 _("%s: panic: Invalid l_value %d\n"),
1457 progname
, lp
->l_value
);
1459 } else if (sscanf(cp
, scheck(cp
, "%"SCNdZIC
), &rp
->r_loyear
) != 1) {
1460 error(_("invalid starting year"));
1464 lp
= byword(cp
, end_years
);
1465 rp
->r_hiwasnum
= lp
== NULL
;
1466 if (!rp
->r_hiwasnum
) switch ((int) lp
->l_value
) {
1468 rp
->r_hiyear
= ZIC_MIN
;
1471 rp
->r_hiyear
= ZIC_MAX
;
1474 rp
->r_hiyear
= rp
->r_loyear
;
1476 default: /* "cannot happen" */
1477 (void) fprintf(stderr
,
1478 _("%s: panic: Invalid l_value %d\n"),
1479 progname
, lp
->l_value
);
1481 } else if (sscanf(cp
, scheck(cp
, "%"SCNdZIC
), &rp
->r_hiyear
) != 1) {
1482 error(_("invalid ending year"));
1485 if (rp
->r_loyear
> rp
->r_hiyear
) {
1486 error(_("starting year greater than ending year"));
1490 rp
->r_yrtype
= NULL
;
1492 if (rp
->r_loyear
== rp
->r_hiyear
) {
1493 error(_("typed single year"));
1496 rp
->r_yrtype
= ecpyalloc(typep
);
1500 ** Accept things such as:
1506 dp
= ecpyalloc(dayp
);
1507 if ((lp
= byword(dp
, lasts
)) != NULL
) {
1508 rp
->r_dycode
= DC_DOWLEQ
;
1509 rp
->r_wday
= lp
->l_value
;
1510 rp
->r_dayofmonth
= len_months
[1][rp
->r_month
];
1512 if ((ep
= strchr(dp
, '<')) != 0)
1513 rp
->r_dycode
= DC_DOWLEQ
;
1514 else if ((ep
= strchr(dp
, '>')) != 0)
1515 rp
->r_dycode
= DC_DOWGEQ
;
1518 rp
->r_dycode
= DC_DOM
;
1520 if (rp
->r_dycode
!= DC_DOM
) {
1523 error(_("invalid day of month"));
1527 if ((lp
= byword(dp
, wday_names
)) == NULL
) {
1528 error(_("invalid weekday name"));
1532 rp
->r_wday
= lp
->l_value
;
1534 if (sscanf(ep
, scheck(ep
, "%d"), &rp
->r_dayofmonth
) != 1 ||
1535 rp
->r_dayofmonth
<= 0 ||
1536 (rp
->r_dayofmonth
> len_months
[1][rp
->r_month
])) {
1537 error(_("invalid day of month"));
1546 convert(const int_fast32_t val
, char *const buf
)
1550 unsigned char *const b
= (unsigned char *) buf
;
1552 for (i
= 0, shift
= 24; i
< 4; ++i
, shift
-= 8)
1553 b
[i
] = val
>> shift
;
1557 convert64(const zic_t val
, char *const buf
)
1561 unsigned char *const b
= (unsigned char *) buf
;
1563 for (i
= 0, shift
= 56; i
< 8; ++i
, shift
-= 8)
1564 b
[i
] = val
>> shift
;
1568 puttzcode(const int_fast32_t val
, FILE *const fp
)
1573 (void) fwrite(buf
, sizeof buf
, 1, fp
);
1577 puttzcode64(const zic_t val
, FILE *const fp
)
1581 convert64(val
, buf
);
1582 (void) fwrite(buf
, sizeof buf
, 1, fp
);
1586 atcomp(const void *avp
, const void *bvp
)
1588 const zic_t a
= ((const struct attype
*) avp
)->at
;
1589 const zic_t b
= ((const struct attype
*) bvp
)->at
;
1591 return (a
< b
) ? -1 : (a
> b
);
1597 return INT32_MIN
<= x
&& x
<= INT32_MAX
;
1601 writezone(const char *const name
, const char *const string
, char version
)
1605 register int leapcnt32
, leapi32
;
1606 register int timecnt32
, timei32
;
1608 static char * fullname
;
1609 static const struct tzhead tzh0
;
1610 static struct tzhead tzh
;
1611 zic_t
*ats
= emalloc(size_product(timecnt
, sizeof *ats
+ 1));
1612 void *typesptr
= ats
+ timecnt
;
1613 unsigned char *types
= typesptr
;
1619 (void) qsort(attypes
, timecnt
, sizeof *attypes
, atcomp
);
1629 while (fromi
< timecnt
&& attypes
[fromi
].at
< min_time
)
1632 ** Remember that type 0 is reserved.
1635 while (fromi
< timecnt
&& attypes
[fromi
].type
== 1)
1636 ++fromi
; /* handled by default rule */
1637 for ( ; fromi
< timecnt
; ++fromi
) {
1638 if (toi
!= 0 && ((attypes
[fromi
].at
+
1639 gmtoffs
[attypes
[toi
- 1].type
]) <=
1640 (attypes
[toi
- 1].at
+ gmtoffs
[toi
== 1 ? 0
1641 : attypes
[toi
- 2].type
]))) {
1642 attypes
[toi
- 1].type
=
1643 attypes
[fromi
].type
;
1647 attypes
[toi
- 1].type
!= attypes
[fromi
].type
)
1648 attypes
[toi
++] = attypes
[fromi
];
1655 for (i
= 0; i
< timecnt
; ++i
) {
1656 ats
[i
] = attypes
[i
].at
;
1657 types
[i
] = attypes
[i
].type
;
1660 ** Correct for leap seconds.
1662 for (i
= 0; i
< timecnt
; ++i
) {
1665 if (ats
[i
] > trans
[j
] - corr
[j
]) {
1666 ats
[i
] = tadd(ats
[i
], corr
[j
]);
1671 ** Figure out 32-bit-limited starts and counts.
1673 timecnt32
= timecnt
;
1675 leapcnt32
= leapcnt
;
1677 while (timecnt32
> 0 && !is32(ats
[timecnt32
- 1]))
1679 while (timecnt32
> 0 && !is32(ats
[timei32
])) {
1683 while (leapcnt32
> 0 && !is32(trans
[leapcnt32
- 1]))
1685 while (leapcnt32
> 0 && !is32(trans
[leapi32
])) {
1689 fullname
= erealloc(fullname
,
1690 strlen(directory
) + 1 + strlen(name
) + 1);
1691 (void) sprintf(fullname
, "%s/%s", directory
, name
);
1693 ** Remove old file, if any, to snap links.
1695 if (!itsdir(fullname
) && remove(fullname
) != 0 && errno
!= ENOENT
) {
1696 const char *e
= strerror(errno
);
1698 (void) fprintf(stderr
, _("%s: Can't remove %s: %s\n"),
1699 progname
, fullname
, e
);
1702 if ((fp
= fopen(fullname
, "wb")) == NULL
) {
1703 if (mkdirs(fullname
) != 0)
1705 if ((fp
= fopen(fullname
, "wb")) == NULL
) {
1706 const char *e
= strerror(errno
);
1708 (void) fprintf(stderr
, _("%s: Can't create %s: %s\n"),
1709 progname
, fullname
, e
);
1713 for (pass
= 1; pass
<= 2; ++pass
) {
1714 register int thistimei
, thistimecnt
;
1715 register int thisleapi
, thisleapcnt
;
1716 register int thistimelim
, thisleaplim
;
1717 int writetype
[TZ_MAX_TYPES
];
1718 int typemap
[TZ_MAX_TYPES
];
1719 register int thistypecnt
;
1720 char thischars
[TZ_MAX_CHARS
];
1722 int indmap
[TZ_MAX_CHARS
];
1725 thistimei
= timei32
;
1726 thistimecnt
= timecnt32
;
1727 thisleapi
= leapi32
;
1728 thisleapcnt
= leapcnt32
;
1731 thistimecnt
= timecnt
;
1733 thisleapcnt
= leapcnt
;
1735 thistimelim
= thistimei
+ thistimecnt
;
1736 thisleaplim
= thisleapi
+ thisleapcnt
;
1738 ** Remember that type 0 is reserved.
1740 writetype
[0] = FALSE
;
1741 for (i
= 1; i
< typecnt
; ++i
)
1742 writetype
[i
] = thistimecnt
== timecnt
;
1743 if (thistimecnt
== 0) {
1745 ** No transition times fall in the current
1746 ** (32- or 64-bit) window.
1749 writetype
[typecnt
- 1] = TRUE
;
1751 for (i
= thistimei
- 1; i
< thistimelim
; ++i
)
1753 writetype
[types
[i
]] = TRUE
;
1755 ** For America/Godthab and Antarctica/Palmer
1758 ** Remember that type 0 is reserved.
1761 writetype
[1] = TRUE
;
1763 #ifndef LEAVE_SOME_PRE_2011_SYSTEMS_IN_THE_LURCH
1765 ** For some pre-2011 systems: if the last-to-be-written
1766 ** standard (or daylight) type has an offset different from the
1767 ** most recently used offset,
1768 ** append an (unused) copy of the most recently used type
1769 ** (to help get global "altzone" and "timezone" variables
1773 register int mrudst
, mrustd
, hidst
, histd
, type
;
1775 hidst
= histd
= mrudst
= mrustd
= -1;
1776 for (i
= thistimei
; i
< thistimelim
; ++i
)
1777 if (isdsts
[types
[i
]])
1779 else mrustd
= types
[i
];
1780 for (i
= 0; i
< typecnt
; ++i
)
1786 if (hidst
>= 0 && mrudst
>= 0 && hidst
!= mrudst
&&
1787 gmtoffs
[hidst
] != gmtoffs
[mrudst
]) {
1788 isdsts
[mrudst
] = -1;
1789 type
= addtype(gmtoffs
[mrudst
],
1791 rawoffs
[mrudst
], dstoffs
[mrudst
],
1793 &chars
[abbrinds
[mrudst
]],
1797 isdsts
[mrudst
] = TRUE
;
1798 writetype
[type
] = TRUE
;
1800 if (histd
>= 0 && mrustd
>= 0 && histd
!= mrustd
&&
1801 gmtoffs
[histd
] != gmtoffs
[mrustd
]) {
1802 isdsts
[mrustd
] = -1;
1803 type
= addtype(gmtoffs
[mrustd
],
1805 rawoffs
[mrudst
], dstoffs
[mrudst
],
1807 &chars
[abbrinds
[mrustd
]],
1811 isdsts
[mrustd
] = FALSE
;
1812 writetype
[type
] = TRUE
;
1815 #endif /* !defined LEAVE_SOME_PRE_2011_SYSTEMS_IN_THE_LURCH */
1818 ** Potentially, set type 0 to that of lowest-valued time.
1820 if (thistimei
> 0) {
1821 for (i
= 1; i
< typecnt
; ++i
)
1822 if (writetype
[i
] && !isdsts
[i
])
1824 if (i
!= types
[thistimei
- 1]) {
1825 i
= types
[thistimei
- 1];
1826 gmtoffs
[0] = gmtoffs
[i
];
1827 isdsts
[0] = isdsts
[i
];
1828 ttisstds
[0] = ttisstds
[i
];
1829 ttisgmts
[0] = ttisgmts
[i
];
1830 abbrinds
[0] = abbrinds
[i
];
1831 writetype
[0] = TRUE
;
1832 writetype
[i
] = FALSE
;
1835 for (i
= 0; i
< typecnt
; ++i
)
1836 typemap
[i
] = writetype
[i
] ? thistypecnt
++ : 0;
1837 for (i
= 0; i
< sizeof indmap
/ sizeof indmap
[0]; ++i
)
1840 for (i
= 0; i
< typecnt
; ++i
) {
1841 register char * thisabbr
;
1845 if (indmap
[abbrinds
[i
]] >= 0)
1847 thisabbr
= &chars
[abbrinds
[i
]];
1848 for (j
= 0; j
< thischarcnt
; ++j
)
1849 if (strcmp(&thischars
[j
], thisabbr
) == 0)
1851 if (j
== thischarcnt
) {
1852 (void) strcpy(&thischars
[(int) thischarcnt
],
1854 thischarcnt
+= strlen(thisabbr
) + 1;
1856 indmap
[abbrinds
[i
]] = j
;
1858 #define DO(field) ((void) fwrite(tzh.field, sizeof tzh.field, 1, fp))
1861 * (ICUZoneinfoVersion
*) &tzh
.tzh_reserved
= TZ_ICU_VERSION
;
1862 (void) strncpy(tzh
.tzh_magic
, TZ_ICU_MAGIC
, sizeof tzh
.tzh_magic
);
1864 (void) strncpy(tzh
.tzh_magic
, TZ_MAGIC
, sizeof tzh
.tzh_magic
);
1866 tzh
.tzh_version
[0] = version
;
1867 convert(thistypecnt
, tzh
.tzh_ttisgmtcnt
);
1868 convert(thistypecnt
, tzh
.tzh_ttisstdcnt
);
1869 convert(thisleapcnt
, tzh
.tzh_leapcnt
);
1870 convert(thistimecnt
, tzh
.tzh_timecnt
);
1871 convert(thistypecnt
, tzh
.tzh_typecnt
);
1872 convert(thischarcnt
, tzh
.tzh_charcnt
);
1883 for (i
= thistimei
; i
< thistimelim
; ++i
)
1885 puttzcode(ats
[i
], fp
);
1886 else puttzcode64(ats
[i
], fp
);
1887 for (i
= thistimei
; i
< thistimelim
; ++i
) {
1890 uc
= typemap
[types
[i
]];
1891 (void) fwrite(&uc
, sizeof uc
, 1, fp
);
1893 for (i
= 0; i
< typecnt
; ++i
)
1896 puttzcode(rawoffs
[i
], fp
);
1897 puttzcode(dstoffs
[i
], fp
);
1899 puttzcode(gmtoffs
[i
], fp
);
1901 (void) putc(isdsts
[i
], fp
);
1902 (void) putc((unsigned char) indmap
[abbrinds
[i
]], fp
);
1904 if (thischarcnt
!= 0)
1905 (void) fwrite(thischars
, sizeof thischars
[0],
1907 for (i
= thisleapi
; i
< thisleaplim
; ++i
) {
1908 register zic_t todo
;
1911 if (timecnt
== 0 || trans
[i
] < ats
[0]) {
1914 if (++j
>= typecnt
) {
1920 while (j
< timecnt
&&
1925 todo
= tadd(trans
[i
], -gmtoffs
[j
]);
1926 } else todo
= trans
[i
];
1928 puttzcode(todo
, fp
);
1929 else puttzcode64(todo
, fp
);
1930 puttzcode(corr
[i
], fp
);
1932 for (i
= 0; i
< typecnt
; ++i
)
1934 (void) putc(ttisstds
[i
], fp
);
1935 for (i
= 0; i
< typecnt
; ++i
)
1937 (void) putc(ttisgmts
[i
], fp
);
1939 (void) fprintf(fp
, "\n%s\n", string
);
1940 if (ferror(fp
) || fclose(fp
)) {
1941 (void) fprintf(stderr
, _("%s: Error writing %s\n"),
1942 progname
, fullname
);
1949 doabbr(char *const abbr
, const char *const format
, const char *const letters
,
1950 const int isdst
, const int doquotes
)
1953 register char * slashp
;
1956 slashp
= strchr(format
, '/');
1957 if (slashp
== NULL
) {
1958 if (letters
== NULL
)
1959 (void) strcpy(abbr
, format
);
1960 else (void) sprintf(abbr
, format
, letters
);
1962 (void) strcpy(abbr
, slashp
+ 1);
1964 if (slashp
> format
)
1965 (void) strncpy(abbr
, format
, slashp
- format
);
1966 abbr
[slashp
- format
] = '\0';
1970 for (cp
= abbr
; *cp
!= '\0'; ++cp
)
1971 if (strchr("ABCDEFGHIJKLMNOPQRSTUVWXYZ", *cp
) == NULL
&&
1972 strchr("abcdefghijklmnopqrstuvwxyz", *cp
) == NULL
)
1975 if (len
> 0 && *cp
== '\0')
1977 abbr
[len
+ 2] = '\0';
1978 abbr
[len
+ 1] = '>';
1979 for ( ; len
> 0; --len
)
1980 abbr
[len
] = abbr
[len
- 1];
1985 updateminmax(const zic_t x
)
1994 stringoffset(char *result
, zic_t offset
)
1997 register int minutes
;
1998 register int seconds
;
2002 (void) strcpy(result
, "-");
2005 seconds
= offset
% SECSPERMIN
;
2006 offset
/= SECSPERMIN
;
2007 minutes
= offset
% MINSPERHOUR
;
2008 offset
/= MINSPERHOUR
;
2010 if (hours
>= HOURSPERDAY
* DAYSPERWEEK
) {
2014 (void) sprintf(end(result
), "%d", hours
);
2015 if (minutes
!= 0 || seconds
!= 0) {
2016 (void) sprintf(end(result
), ":%02d", minutes
);
2018 (void) sprintf(end(result
), ":%02d", seconds
);
2024 stringrule(char *result
, const struct rule
*const rp
, const zic_t dstoff
,
2027 register zic_t tod
= rp
->r_tod
;
2028 register int compat
= 0;
2030 result
= end(result
);
2031 if (rp
->r_dycode
== DC_DOM
) {
2032 register int month
, total
;
2034 if (rp
->r_dayofmonth
== 29 && rp
->r_month
== TM_FEBRUARY
)
2037 for (month
= 0; month
< rp
->r_month
; ++month
)
2038 total
+= len_months
[0][month
];
2039 /* Omit the "J" in Jan and Feb, as that's shorter. */
2040 if (rp
->r_month
<= 1)
2041 (void) sprintf(result
, "%d", total
+ rp
->r_dayofmonth
- 1);
2043 (void) sprintf(result
, "J%d", total
+ rp
->r_dayofmonth
);
2046 register int wday
= rp
->r_wday
;
2047 register int wdayoff
;
2049 if (rp
->r_dycode
== DC_DOWGEQ
) {
2050 wdayoff
= (rp
->r_dayofmonth
- 1) % DAYSPERWEEK
;
2054 tod
+= wdayoff
* SECSPERDAY
;
2055 week
= 1 + (rp
->r_dayofmonth
- 1) / DAYSPERWEEK
;
2056 } else if (rp
->r_dycode
== DC_DOWLEQ
) {
2057 if (rp
->r_dayofmonth
== len_months
[1][rp
->r_month
])
2060 wdayoff
= rp
->r_dayofmonth
% DAYSPERWEEK
;
2064 tod
+= wdayoff
* SECSPERDAY
;
2065 week
= rp
->r_dayofmonth
/ DAYSPERWEEK
;
2067 } else return -1; /* "cannot happen" */
2069 wday
+= DAYSPERWEEK
;
2070 (void) sprintf(result
, "M%d.%d.%d",
2071 rp
->r_month
+ 1, week
, wday
);
2075 if (rp
->r_todisstd
&& rp
->r_stdoff
== 0)
2077 if (tod
!= 2 * SECSPERMIN
* MINSPERHOUR
) {
2078 (void) strcat(result
, "/");
2079 if (stringoffset(end(result
), tod
) != 0)
2084 } else if (SECSPERDAY
<= tod
) {
2093 rule_cmp(struct rule
const *a
, struct rule
const *b
)
2099 if (a
->r_hiyear
!= b
->r_hiyear
)
2100 return a
->r_hiyear
< b
->r_hiyear
? -1 : 1;
2101 if (a
->r_month
- b
->r_month
!= 0)
2102 return a
->r_month
- b
->r_month
;
2103 return a
->r_dayofmonth
- b
->r_dayofmonth
;
2106 enum { YEAR_BY_YEAR_ZONE
= 1 };
2109 stringzone(char *result
, const struct zone
*const zpfirst
, const int zonecount
)
2111 register const struct zone
* zp
;
2112 register struct rule
* rp
;
2113 register struct rule
* stdrp
;
2114 register struct rule
* dstrp
;
2116 register const char * abbrvar
;
2117 register int compat
= 0;
2119 struct rule stdr
, dstr
;
2122 zp
= zpfirst
+ zonecount
- 1;
2123 stdrp
= dstrp
= NULL
;
2124 for (i
= 0; i
< zp
->z_nrules
; ++i
) {
2125 rp
= &zp
->z_rules
[i
];
2126 if (rp
->r_hiwasnum
|| rp
->r_hiyear
!= ZIC_MAX
)
2128 if (rp
->r_yrtype
!= NULL
)
2130 if (rp
->r_stdoff
== 0) {
2140 if (stdrp
== NULL
&& dstrp
== NULL
) {
2142 ** There are no rules running through "max".
2143 ** Find the latest std rule in stdabbrrp
2144 ** and latest rule of any type in stdrp.
2146 register struct rule
*stdabbrrp
= NULL
;
2147 for (i
= 0; i
< zp
->z_nrules
; ++i
) {
2148 rp
= &zp
->z_rules
[i
];
2149 if (rp
->r_stdoff
== 0 && rule_cmp(stdabbrrp
, rp
) < 0)
2151 if (rule_cmp(stdrp
, rp
) < 0)
2155 ** Horrid special case: if year is 2037,
2156 ** presume this is a zone handled on a year-by-year basis;
2157 ** do not try to apply a rule to the zone.
2159 if (stdrp
!= NULL
&& stdrp
->r_hiyear
== 2037)
2160 return YEAR_BY_YEAR_ZONE
;
2162 if (stdrp
!= NULL
&& stdrp
->r_stdoff
!= 0) {
2163 /* Perpetual DST. */
2164 dstr
.r_month
= TM_JANUARY
;
2165 dstr
.r_dycode
= DC_DOM
;
2166 dstr
.r_dayofmonth
= 1;
2168 dstr
.r_todisstd
= dstr
.r_todisgmt
= FALSE
;
2169 dstr
.r_stdoff
= stdrp
->r_stdoff
;
2170 dstr
.r_abbrvar
= stdrp
->r_abbrvar
;
2171 stdr
.r_month
= TM_DECEMBER
;
2172 stdr
.r_dycode
= DC_DOM
;
2173 stdr
.r_dayofmonth
= 31;
2174 stdr
.r_tod
= SECSPERDAY
+ stdrp
->r_stdoff
;
2175 stdr
.r_todisstd
= stdr
.r_todisgmt
= FALSE
;
2178 = (stdabbrrp
? stdabbrrp
->r_abbrvar
: "");
2183 if (stdrp
== NULL
&& (zp
->z_nrules
!= 0 || zp
->z_stdoff
!= 0))
2185 abbrvar
= (stdrp
== NULL
) ? "" : stdrp
->r_abbrvar
;
2186 doabbr(result
, zp
->z_format
, abbrvar
, FALSE
, TRUE
);
2187 if (stringoffset(end(result
), -zp
->z_gmtoff
) != 0) {
2193 doabbr(end(result
), zp
->z_format
, dstrp
->r_abbrvar
, TRUE
, TRUE
);
2194 if (dstrp
->r_stdoff
!= SECSPERMIN
* MINSPERHOUR
)
2195 if (stringoffset(end(result
),
2196 -(zp
->z_gmtoff
+ dstrp
->r_stdoff
)) != 0) {
2200 (void) strcat(result
, ",");
2201 c
= stringrule(result
, dstrp
, dstrp
->r_stdoff
, zp
->z_gmtoff
);
2208 (void) strcat(result
, ",");
2209 c
= stringrule(result
, stdrp
, dstrp
->r_stdoff
, zp
->z_gmtoff
);
2220 outzone(const struct zone
* const zpfirst
, const int zonecount
)
2222 register const struct zone
* zp
;
2223 register struct rule
* rp
;
2225 register int usestart
, useuntil
;
2226 register zic_t starttime
, untiltime
;
2227 register zic_t gmtoff
;
2228 register zic_t stdoff
;
2229 register zic_t year
;
2230 register zic_t startoff
;
2231 register int startttisstd
;
2232 register int startttisgmt
;
2234 register char * startbuf
;
2236 register char * envvar
;
2237 register int max_abbr_len
;
2238 register int max_envvar_len
;
2239 register int prodstic
; /* all rules are min to max */
2240 register int compat
;
2241 register int do_extend
;
2242 register char version
;
2244 int finalRuleYear
, finalRuleIndex
;
2245 const struct rule
* finalRule1
;
2246 const struct rule
* finalRule2
;
2249 max_abbr_len
= 2 + max_format_len
+ max_abbrvar_len
;
2250 max_envvar_len
= 2 * max_abbr_len
+ 5 * 9;
2251 startbuf
= emalloc(max_abbr_len
+ 1);
2252 ab
= emalloc(max_abbr_len
+ 1);
2253 envvar
= emalloc(max_envvar_len
+ 1);
2254 INITIALIZE(untiltime
);
2255 INITIALIZE(starttime
);
2257 ** Now. . .finally. . .generate some useful data!
2262 prodstic
= zonecount
== 1;
2264 ** Thanks to Earl Chew
2265 ** for noting the need to unconditionally initialize startttisstd.
2267 startttisstd
= FALSE
;
2268 startttisgmt
= FALSE
;
2269 min_year
= max_year
= EPOCH_YEAR
;
2271 updateminmax(leapminyear
);
2272 updateminmax(leapmaxyear
+ (leapmaxyear
< ZIC_MAX
));
2277 gmtoffs
[0] = isdsts
[0] = ttisstds
[0] = ttisgmts
[0] = abbrinds
[0] = -1;
2279 for (i
= 0; i
< zonecount
; ++i
) {
2281 if (i
< zonecount
- 1)
2282 updateminmax(zp
->z_untilrule
.r_loyear
);
2283 for (j
= 0; j
< zp
->z_nrules
; ++j
) {
2284 rp
= &zp
->z_rules
[j
];
2286 updateminmax(rp
->r_loyear
);
2288 updateminmax(rp
->r_hiyear
);
2289 if (rp
->r_lowasnum
|| rp
->r_hiwasnum
)
2294 ** Generate lots of data if a rule can't cover all future times.
2296 compat
= stringzone(envvar
, zpfirst
, zonecount
);
2297 version
= compat
< 2013 ? ZIC_VERSION_PRE_2013
: ZIC_VERSION
;
2298 do_extend
= compat
< 0 || compat
== YEAR_BY_YEAR_ZONE
;
2305 _("no POSIX environment variable for zone"),
2307 else if (compat
!= 0 && compat
!= YEAR_BY_YEAR_ZONE
) {
2308 /* Circa-COMPAT clients, and earlier clients, might
2309 not work for this zone when given dates before
2310 1970 or after 2038. */
2311 warning(_("%s: pre-%d clients may mishandle"
2312 " distant timestamps"),
2313 zpfirst
->z_name
, compat
);
2318 ** Search through a couple of extra years past the obvious
2319 ** 400, to avoid edge cases. For example, suppose a non-POSIX
2320 ** rule applies from 2012 onwards and has transitions in March
2321 ** and September, plus some one-off transitions in November
2322 ** 2013. If zic looked only at the last 400 years, it would
2323 ** set max_year=2413, with the intent that the 400 years 2014
2324 ** through 2413 will be repeated. The last transition listed
2325 ** in the tzfile would be in 2413-09, less than 400 years
2326 ** after the last one-off transition in 2013-11. Two years
2327 ** might be overkill, but with the kind of edge cases
2328 ** available we're not sure that one year would suffice.
2330 enum { years_of_observations
= YEARSPERREPEAT
+ 2 };
2332 if (min_year
>= ZIC_MIN
+ years_of_observations
)
2333 min_year
-= years_of_observations
;
2334 else min_year
= ZIC_MIN
;
2335 if (max_year
<= ZIC_MAX
- years_of_observations
)
2336 max_year
+= years_of_observations
;
2337 else max_year
= ZIC_MAX
;
2339 ** Regardless of any of the above,
2340 ** for a "proDSTic" zone which specifies that its rules
2341 ** always have and always will be in effect,
2342 ** we only need one cycle to define the zone.
2346 max_year
= min_year
+ years_of_observations
;
2350 ** For the benefit of older systems,
2351 ** generate data from 1900 through 2037.
2353 if (min_year
> 1900)
2355 if (max_year
< 2037)
2357 for (i
= 0; i
< zonecount
; ++i
) {
2359 ** A guess that may well be corrected later.
2363 usestart
= i
> 0 && (zp
- 1)->z_untiltime
> min_time
;
2364 useuntil
= i
< (zonecount
- 1);
2365 if (useuntil
&& zp
->z_untiltime
<= min_time
)
2367 gmtoff
= zp
->z_gmtoff
;
2368 eat(zp
->z_filename
, zp
->z_linenum
);
2370 startoff
= zp
->z_gmtoff
;
2372 finalRuleYear
= finalRuleIndex
= -1;
2373 finalRule1
= finalRule2
= NULL
;
2374 if (i
== (zonecount
- 1)) { /* !useuntil */
2375 /* Look for exactly 2 rules that end at 'max' and
2376 * note them. Determine max(r_loyear) for the 2 of
2378 for (j
=0; j
<zp
->z_nrules
; ++j
) {
2379 rp
= &zp
->z_rules
[j
];
2380 if (rp
->r_hiyear
== ZIC_MAX
) {
2381 if (rp
->r_loyear
> finalRuleYear
) {
2382 finalRuleYear
= rp
->r_loyear
;
2384 if (finalRule1
== NULL
) {
2386 } else if (finalRule2
== NULL
) {
2389 error("more than two max rules found (ICU)");
2392 } else if (rp
->r_hiyear
>= finalRuleYear
) {
2393 /* There might be an overriding non-max rule
2394 * to be applied to a specific year after one of
2395 * max rule's start year. For example,
2397 * Rule Foo 2010 max ...
2398 * Rule Foo 2015 only ...
2400 * In this case, we need to change the start year of
2401 * the final (max) rules to the next year. */
2402 finalRuleYear
= rp
->r_hiyear
+ 1;
2404 /* When above adjustment is done, max_year might need
2405 * to be adjusted, so the final rule will be properly
2406 * evaluated and emitted by the later code block.
2408 * Note: This may push the start year of the final
2409 * rules ahead by 1 year unnecessarily. For example,
2410 * If there are two rules, non-max rule and max rule
2411 * starting in the same year, such as
2413 * Rule Foo 2010 only ....
2414 * Rule Foo 2010 max ....
2416 * In this case, the final (max) rule actually starts
2417 * in 2010, instead of 2010. We could make this tool
2418 * more intelligent to detect such situation. But pushing
2419 * final rule start year to 1 year ahead (in the worst case)
2420 * will just populate a few extra transitions, and it still
2421 * works fine. So for now, we're not trying to put additional
2422 * logic to optimize the case.
2424 if (max_year
< finalRuleYear
) {
2425 max_year
= finalRuleYear
;
2429 if (finalRule1
!= NULL
) {
2430 if (finalRule2
== NULL
) {
2431 warning("only one max rule found (ICU)");
2432 finalRuleYear
= finalRuleIndex
= -1;
2435 if (finalRule1
->r_stdoff
== finalRule2
->r_stdoff
) {
2436 /* America/Resolute in 2009a uses a pair of rules
2437 * which does not change the offset. ICU ignores
2438 * such rules without actual time transitions. */
2439 finalRuleYear
= finalRuleIndex
= -1;
2440 finalRule1
= finalRule2
= NULL
;
2442 /* Swap if necessary so finalRule1 occurs before
2444 if (finalRule1
->r_month
> finalRule2
->r_month
) {
2445 const struct rule
* t
= finalRule1
;
2446 finalRule1
= finalRule2
;
2449 /* Add final rule to our list */
2450 finalRuleIndex
= add_icu_final_rules(finalRule1
, finalRule2
);
2457 if (zp
->z_nrules
== 0) {
2458 stdoff
= zp
->z_stdoff
;
2459 doabbr(startbuf
, zp
->z_format
,
2460 NULL
, stdoff
!= 0, FALSE
);
2461 type
= addtype(oadd(zp
->z_gmtoff
, stdoff
),
2463 zp
->z_gmtoff
, stdoff
,
2465 startbuf
, stdoff
!= 0, startttisstd
,
2468 addtt(starttime
, type
);
2470 } else if (stdoff
!= 0)
2471 addtt(min_time
, type
);
2472 } else for (year
= min_year
; year
<= max_year
; ++year
) {
2473 if (useuntil
&& year
> zp
->z_untilrule
.r_hiyear
)
2476 ** Mark which rules to do in the current year.
2477 ** For those to do, calculate rpytime(rp, year);
2479 for (j
= 0; j
< zp
->z_nrules
; ++j
) {
2480 rp
= &zp
->z_rules
[j
];
2481 eats(zp
->z_filename
, zp
->z_linenum
,
2482 rp
->r_filename
, rp
->r_linenum
);
2483 rp
->r_todo
= year
>= rp
->r_loyear
&&
2484 year
<= rp
->r_hiyear
&&
2485 yearistype(year
, rp
->r_yrtype
);
2487 rp
->r_temp
= rpytime(rp
, year
);
2491 register zic_t jtime
, ktime
;
2492 register zic_t offset
;
2497 ** Turn untiltime into UT
2498 ** assuming the current gmtoff and
2501 untiltime
= zp
->z_untiltime
;
2502 if (!zp
->z_untilrule
.r_todisgmt
)
2503 untiltime
= tadd(untiltime
,
2505 if (!zp
->z_untilrule
.r_todisstd
)
2506 untiltime
= tadd(untiltime
,
2510 ** Find the rule (of those to do, if any)
2511 ** that takes effect earliest in the year.
2514 for (j
= 0; j
< zp
->z_nrules
; ++j
) {
2515 rp
= &zp
->z_rules
[j
];
2518 eats(zp
->z_filename
, zp
->z_linenum
,
2519 rp
->r_filename
, rp
->r_linenum
);
2520 offset
= rp
->r_todisgmt
? 0 : gmtoff
;
2521 if (!rp
->r_todisstd
)
2522 offset
= oadd(offset
, stdoff
);
2524 if (jtime
== min_time
||
2527 jtime
= tadd(jtime
, -offset
);
2528 if (k
< 0 || jtime
< ktime
) {
2534 break; /* go on to next year */
2535 rp
= &zp
->z_rules
[k
];
2537 if (useuntil
&& ktime
>= untiltime
)
2539 stdoff
= rp
->r_stdoff
;
2540 if (usestart
&& ktime
== starttime
)
2543 if (ktime
< starttime
) {
2544 startoff
= oadd(zp
->z_gmtoff
,
2546 doabbr(startbuf
, zp
->z_format
,
2552 if (*startbuf
== '\0' &&
2553 startoff
== oadd(zp
->z_gmtoff
,
2564 if (year
>= finalRuleYear
&& rp
== finalRule1
) {
2565 /* We want to shift final year 1 year after
2566 * the actual final rule takes effect (year + 1),
2567 * because the previous type is valid until the first
2568 * transition defined by the final rule. Otherwise
2569 * we may see unexpected offset shift at the
2570 * begining of the year when the final rule takes
2573 * Note: This may results some 64bit second transitions
2574 * at the very end (year 2038). ICU 4.2 or older releases
2575 * cannot handle 64bit second transitions and they are
2576 * dropped from zoneinfo.txt. */
2577 emit_icu_zone(icuFile
,
2578 zpfirst
->z_name
, zp
->z_gmtoff
,
2579 rp
, finalRuleIndex
, year
+ 1);
2580 /* only emit this for the first year */
2584 eats(zp
->z_filename
, zp
->z_linenum
,
2585 rp
->r_filename
, rp
->r_linenum
);
2586 doabbr(ab
, zp
->z_format
, rp
->r_abbrvar
,
2587 rp
->r_stdoff
!= 0, FALSE
);
2588 offset
= oadd(zp
->z_gmtoff
, rp
->r_stdoff
);
2590 type
= addtype(offset
, zp
->z_gmtoff
, rp
->r_stdoff
,
2591 ab
, rp
->r_stdoff
!= 0,
2592 rp
->r_todisstd
, rp
->r_todisgmt
);
2594 type
= addtype(offset
, ab
, rp
->r_stdoff
!= 0,
2595 rp
->r_todisstd
, rp
->r_todisgmt
);
2601 if (*startbuf
== '\0' &&
2602 zp
->z_format
!= NULL
&&
2603 strchr(zp
->z_format
, '%') == NULL
&&
2604 strchr(zp
->z_format
, '/') == NULL
)
2605 (void) strcpy(startbuf
, zp
->z_format
);
2606 eat(zp
->z_filename
, zp
->z_linenum
);
2607 if (*startbuf
== '\0')
2608 error(_("can't determine time zone abbreviation to use just after until time"));
2609 else addtt(starttime
,
2612 zp
->z_gmtoff
, startoff
- zp
->z_gmtoff
,
2614 startoff
!= zp
->z_gmtoff
,
2618 addtype(startoff
, startbuf
,
2619 startoff
!= zp
->z_gmtoff
,
2625 ** Now we may get to set starttime for the next zone line.
2628 startttisstd
= zp
->z_untilrule
.r_todisstd
;
2629 startttisgmt
= zp
->z_untilrule
.r_todisgmt
;
2630 starttime
= zp
->z_untiltime
;
2632 starttime
= tadd(starttime
, -stdoff
);
2634 starttime
= tadd(starttime
, -gmtoff
);
2639 ** If we're extending the explicitly listed observations
2640 ** for 400 years because we can't fill the POSIX-TZ field,
2641 ** check whether we actually ended up explicitly listing
2642 ** observations through that period. If there aren't any
2643 ** near the end of the 400-year period, add a redundant
2644 ** one at the end of the final year, to make it clear
2645 ** that we are claiming to have definite knowledge of
2646 ** the lack of transitions up to that point.
2649 struct attype
*lastat
;
2650 xr
.r_month
= TM_JANUARY
;
2651 xr
.r_dycode
= DC_DOM
;
2652 xr
.r_dayofmonth
= 1;
2654 for (lastat
= &attypes
[0], i
= 1; i
< timecnt
; i
++)
2655 if (attypes
[i
].at
> lastat
->at
)
2656 lastat
= &attypes
[i
];
2657 if (lastat
->at
< rpytime(&xr
, max_year
- 1)) {
2659 ** Create new type code for the redundant entry,
2660 ** to prevent it being optimised away.
2662 if (typecnt
>= TZ_MAX_TYPES
) {
2663 error(_("too many local time types"));
2666 gmtoffs
[typecnt
] = gmtoffs
[lastat
->type
];
2667 isdsts
[typecnt
] = isdsts
[lastat
->type
];
2668 ttisstds
[typecnt
] = ttisstds
[lastat
->type
];
2669 ttisgmts
[typecnt
] = ttisgmts
[lastat
->type
];
2670 abbrinds
[typecnt
] = abbrinds
[lastat
->type
];
2672 addtt(rpytime(&xr
, max_year
+ 1), typecnt
-1);
2675 writezone(zpfirst
->z_name
, envvar
, version
);
2682 addtt(const zic_t starttime
, int type
)
2684 if (starttime
<= min_time
||
2685 (timecnt
== 1 && attypes
[0].at
< min_time
)) {
2686 gmtoffs
[0] = gmtoffs
[type
];
2688 rawoffs
[0] = rawoffs
[type
];
2689 dstoffs
[0] = dstoffs
[type
];
2691 isdsts
[0] = isdsts
[type
];
2692 ttisstds
[0] = ttisstds
[type
];
2693 ttisgmts
[0] = ttisgmts
[type
];
2694 if (abbrinds
[type
] != 0)
2695 (void) strcpy(chars
, &chars
[abbrinds
[type
]]);
2697 charcnt
= strlen(chars
) + 1;
2702 attypes
= growalloc(attypes
, sizeof *attypes
, timecnt
, &timecnt_alloc
);
2703 attypes
[timecnt
].at
= starttime
;
2704 attypes
[timecnt
].type
= type
;
2710 addtype(const zic_t gmtoff
, const zic_t rawoff
, const zic_t dstoff
, char *const abbr
, const int isdst
,
2711 const int ttisstd
, const int ttisgmt
)
2713 addtype(const zic_t gmtoff
, const char *const abbr
, const int isdst
,
2714 const int ttisstd
, const int ttisgmt
)
2719 if (isdst
!= TRUE
&& isdst
!= FALSE
) {
2720 error(_("internal error - addtype called with bad isdst"));
2723 if (ttisstd
!= TRUE
&& ttisstd
!= FALSE
) {
2724 error(_("internal error - addtype called with bad ttisstd"));
2727 if (ttisgmt
!= TRUE
&& ttisgmt
!= FALSE
) {
2728 error(_("internal error - addtype called with bad ttisgmt"));
2732 if (isdst
!= (dstoff
!= 0)) {
2733 error(_("internal error - addtype called with bad isdst/dstoff"));
2736 if (gmtoff
!= (rawoff
+ dstoff
)) {
2737 error(_("internal error - addtype called with bad gmt/raw/dstoff"));
2742 ** See if there's already an entry for this zone type.
2743 ** If so, just return its index.
2745 for (i
= 0; i
< typecnt
; ++i
) {
2746 if (gmtoff
== gmtoffs
[i
] && isdst
== isdsts
[i
] &&
2748 rawoff
== rawoffs
[i
] && dstoff
== dstoffs
[i
] &&
2750 strcmp(abbr
, &chars
[abbrinds
[i
]]) == 0 &&
2751 ttisstd
== ttisstds
[i
] &&
2752 ttisgmt
== ttisgmts
[i
])
2756 ** There isn't one; add a new one, unless there are already too
2759 if (typecnt
>= TZ_MAX_TYPES
) {
2760 error(_("too many local time types"));
2763 if (! (-1L - 2147483647L <= gmtoff
&& gmtoff
<= 2147483647L)) {
2764 error(_("UT offset out of range"));
2767 gmtoffs
[i
] = gmtoff
;
2769 rawoffs
[i
] = rawoff
;
2770 dstoffs
[i
] = dstoff
;
2773 ttisstds
[i
] = ttisstd
;
2774 ttisgmts
[i
] = ttisgmt
;
2776 for (j
= 0; j
< charcnt
; ++j
)
2777 if (strcmp(&chars
[j
], abbr
) == 0)
2787 leapadd(const zic_t t
, const int positive
, const int rolling
, int count
)
2791 if (leapcnt
+ (positive
? count
: 1) > TZ_MAX_LEAPS
) {
2792 error(_("too many leap seconds"));
2795 for (i
= 0; i
< leapcnt
; ++i
)
2796 if (t
<= trans
[i
]) {
2797 if (t
== trans
[i
]) {
2798 error(_("repeated leap second moment"));
2804 for (j
= leapcnt
; j
> i
; --j
) {
2805 trans
[j
] = trans
[j
- 1];
2806 corr
[j
] = corr
[j
- 1];
2807 roll
[j
] = roll
[j
- 1];
2810 corr
[i
] = positive
? 1 : -count
;
2813 } while (positive
&& --count
!= 0);
2820 register zic_t last
= 0;
2823 ** propagate leap seconds forward
2825 for (i
= 0; i
< leapcnt
; ++i
) {
2826 trans
[i
] = tadd(trans
[i
], last
);
2827 last
= corr
[i
] += last
;
2832 yearistype(const int year
, const char *const type
)
2837 if (type
== NULL
|| *type
== '\0')
2839 buf
= erealloc(buf
, 132 + strlen(yitcommand
) + strlen(type
));
2840 (void) sprintf(buf
, "%s %d %s", yitcommand
, year
, type
);
2841 result
= system(buf
);
2842 if (WIFEXITED(result
)) switch (WEXITSTATUS(result
)) {
2848 error(_("Wild result from command execution"));
2849 (void) fprintf(stderr
, _("%s: command was '%s', result was %d\n"),
2850 progname
, buf
, result
);
2858 a
= (unsigned char) a
;
2859 return (isascii(a
) && isupper(a
)) ? tolower(a
) : a
;
2862 /* case-insensitive equality */
2863 static ATTRIBUTE_PURE
int
2864 ciequal(register const char *ap
, register const char *bp
)
2866 while (lowerit(*ap
) == lowerit(*bp
++))
2872 static ATTRIBUTE_PURE
int
2873 itsabbr(register const char *abbr
, register const char *word
)
2875 if (lowerit(*abbr
) != lowerit(*word
))
2878 while (*++abbr
!= '\0')
2882 } while (lowerit(*word
++) != lowerit(*abbr
));
2886 static ATTRIBUTE_PURE
const struct lookup
*
2887 byword(register const char *const word
,
2888 register const struct lookup
*const table
)
2890 register const struct lookup
* foundlp
;
2891 register const struct lookup
* lp
;
2893 if (word
== NULL
|| table
== NULL
)
2896 ** Look for exact match.
2898 for (lp
= table
; lp
->l_word
!= NULL
; ++lp
)
2899 if (ciequal(word
, lp
->l_word
))
2902 ** Look for inexact match.
2905 for (lp
= table
; lp
->l_word
!= NULL
; ++lp
)
2906 if (itsabbr(word
, lp
->l_word
)) {
2907 if (foundlp
== NULL
)
2909 else return NULL
; /* multiple inexact matches */
2915 getfields(register char *cp
)
2918 register char ** array
;
2923 array
= emalloc(size_product(strlen(cp
) + 1, sizeof *array
));
2926 while (isascii((unsigned char) *cp
) &&
2927 isspace((unsigned char) *cp
))
2929 if (*cp
== '\0' || *cp
== '#')
2931 array
[nsubs
++] = dp
= cp
;
2933 if ((*dp
= *cp
++) != '"')
2935 else while ((*dp
= *cp
++) != '"')
2940 "Odd number of quotation marks"
2944 } while (*cp
!= '\0' && *cp
!= '#' &&
2945 (!isascii(*cp
) || !isspace((unsigned char) *cp
)));
2946 if (isascii(*cp
) && isspace((unsigned char) *cp
))
2950 array
[nsubs
] = NULL
;
2954 static ATTRIBUTE_PURE zic_t
2955 oadd(const zic_t t1
, const zic_t t2
)
2957 if (t1
< 0 ? t2
< ZIC_MIN
- t1
: ZIC_MAX
- t1
< t2
) {
2958 error(_("time overflow"));
2964 static ATTRIBUTE_PURE zic_t
2965 tadd(const zic_t t1
, const zic_t t2
)
2967 if (t1
== max_time
&& t2
> 0)
2969 if (t1
== min_time
&& t2
< 0)
2971 if (t1
< 0 ? t2
< min_time
- t1
: max_time
- t1
< t2
) {
2972 error(_("time overflow"));
2979 ** Given a rule, and a year, compute the date - in seconds since January 1,
2980 ** 1970, 00:00 LOCAL time - in that year that the rule refers to.
2984 rpytime(register const struct rule
*const rp
, register const zic_t wantedy
)
2987 register zic_t dayoff
; /* with a nod to Margaret O. */
2988 register zic_t t
, y
;
2990 if (wantedy
== ZIC_MIN
)
2992 if (wantedy
== ZIC_MAX
)
2997 while (wantedy
!= y
) {
2999 i
= len_years
[isleap(y
)];
3003 i
= -len_years
[isleap(y
)];
3005 dayoff
= oadd(dayoff
, i
);
3007 while (m
!= rp
->r_month
) {
3008 i
= len_months
[isleap(y
)][m
];
3009 dayoff
= oadd(dayoff
, i
);
3012 i
= rp
->r_dayofmonth
;
3013 if (m
== TM_FEBRUARY
&& i
== 29 && !isleap(y
)) {
3014 if (rp
->r_dycode
== DC_DOWLEQ
)
3017 error(_("use of 2/29 in non leap-year"));
3022 dayoff
= oadd(dayoff
, i
);
3023 if (rp
->r_dycode
== DC_DOWGEQ
|| rp
->r_dycode
== DC_DOWLEQ
) {
3024 register zic_t wday
;
3026 #define LDAYSPERWEEK ((zic_t) DAYSPERWEEK)
3029 ** Don't trust mod of negative numbers.
3032 wday
= (wday
+ dayoff
) % LDAYSPERWEEK
;
3034 wday
-= ((-dayoff
) % LDAYSPERWEEK
);
3036 wday
+= LDAYSPERWEEK
;
3038 while (wday
!= rp
->r_wday
)
3039 if (rp
->r_dycode
== DC_DOWGEQ
) {
3040 dayoff
= oadd(dayoff
, 1);
3041 if (++wday
>= LDAYSPERWEEK
)
3045 dayoff
= oadd(dayoff
, -1);
3047 wday
= LDAYSPERWEEK
- 1;
3050 if (i
< 0 || i
>= len_months
[isleap(y
)][m
]) {
3052 warning(_("rule goes past start/end of month--\
3053 will not work with pre-2004 versions of zic"));
3056 if (dayoff
< min_time
/ SECSPERDAY
)
3058 if (dayoff
> max_time
/ SECSPERDAY
)
3060 t
= (zic_t
) dayoff
* SECSPERDAY
;
3061 return tadd(t
, rp
->r_tod
);
3065 newabbr(const char *const string
)
3069 if (strcmp(string
, GRANDPARENTED
) != 0) {
3070 register const char * cp
;
3074 ** Want one to ZIC_MAX_ABBR_LEN_WO_WARN alphabetics
3075 ** optionally followed by a + or - and a number from 1 to 14.
3079 while (isascii((unsigned char) *cp
) &&
3080 isalpha((unsigned char) *cp
))
3082 if (cp
- string
== 0)
3083 mp
= _("time zone abbreviation lacks alphabetic at start");
3084 if (noise
&& cp
- string
< 3)
3085 mp
= _("time zone abbreviation has fewer than 3 alphabetics");
3086 if (cp
- string
> ZIC_MAX_ABBR_LEN_WO_WARN
)
3087 mp
= _("time zone abbreviation has too many alphabetics");
3088 if (mp
== NULL
&& (*cp
== '+' || *cp
== '-')) {
3090 if (isascii((unsigned char) *cp
) &&
3091 isdigit((unsigned char) *cp
))
3093 *cp
>= '0' && *cp
<= '4')
3097 mp
= _("time zone abbreviation differs from POSIX standard");
3099 warning("%s (%s)", mp
, string
);
3101 i
= strlen(string
) + 1;
3102 if (charcnt
+ i
> TZ_MAX_CHARS
) {
3103 error(_("too many, or too long, time zone abbreviations"));
3106 (void) strcpy(&chars
[charcnt
], string
);
3111 mkdirs(char *argname
)
3113 register char * name
;
3116 if (argname
== NULL
|| *argname
== '\0')
3118 cp
= name
= ecpyalloc(argname
);
3119 while ((cp
= strchr(cp
+ 1, '/')) != 0) {
3121 #ifdef HAVE_DOS_FILE_NAMES
3123 ** DOS drive specifier?
3125 if (isalpha((unsigned char) name
[0]) &&
3126 name
[1] == ':' && name
[2] == '\0') {
3131 if (!itsdir(name
)) {
3133 ** It doesn't seem to exist, so we try to create it.
3134 ** Creation may fail because of the directory being
3135 ** created by some other multiprocessor, so we get
3136 ** to do extra checking.
3138 if (mkdir(name
, MKDIR_UMASK
) != 0) {
3139 const char *e
= strerror(errno
);
3141 if (errno
!= EEXIST
|| !itsdir(name
)) {
3142 (void) fprintf(stderr
,
3143 _("%s: Can't create directory %s: %s\n"),
3157 ** UNIX was a registered trademark of The Open Group in 2003.