#ifndef WILDABBR
/*
* Someone might make incorrect use of a time zone abbreviation:
- * 1. They might reference tzname[0] before calling tzset (explicitly
+ * 1. They might reference tzname[0] before calling tzset (explicitly
* or implicitly).
- * 2. They might reference tzname[1] before calling tzset (explicitly
+ * 2. They might reference tzname[1] before calling tzset (explicitly
* or implicitly).
- * 3. They might reference tzname[1] after setting to a time zone
+ * 3. They might reference tzname[1] after setting to a time zone
* in which Daylight Saving Time is never observed.
- * 4. They might reference tzname[0] after setting to a time zone
+ * 4. They might reference tzname[0] after setting to a time zone
* in which Standard Time is never observed.
- * 5. They might reference tm.TM_ZONE after calling offtime.
+ * 5. They might reference tm.TM_ZONE after calling offtime.
* What's best to do in the above cases is open to debate;
* for now, we just set things up so that in any of the five cases
* WILDABBR is used. Another possibility: initialize tzname[0] to the
static const char gmt[] = "GMT";
-/* The minimum and maximum finite time values. This assumes no padding. */
-static const pg_time_t time_t_min = MINVAL(pg_time_t, TYPE_BIT(pg_time_t));
-static const pg_time_t time_t_max = MAXVAL(pg_time_t, TYPE_BIT(pg_time_t));
-
/*
- * We cache the result of trying to load the TZDEFRULES zone here.
+ * PG: We cache the result of trying to load the TZDEFRULES zone here.
* tzdefrules_loaded is 0 if not tried yet, +1 if good, -1 if failed.
*/
static struct state tzdefrules_s;
/*
* The DST rules to use if TZ has no rules and we can't load TZDEFRULES.
- * We default to US rules as of 1999-08-17.
+ * Default to US rules as of 2017-05-07.
* POSIX 1003.1 section 8.1.1 says that the default DST rules are
* implementation dependent; for historical reasons, US rules are a
* common default.
*/
-#define TZDEFRULESTRING ",M4.1.0,M10.5.0"
+#define TZDEFRULESTRING ",M3.2.0,M11.1.0"
/* structs ttinfo, lsinfo, state have been moved to pgtz.h */
/* Local storage needed for 'tzloadbody'. */
union local_storage
{
- /* We don't need the "fullname" member */
-
/* The results of analyzing the file's contents after it is opened. */
- struct
+ struct file_analysis
{
/* The input buffer. */
union input_buffer u;
/* A temporary state used for parsing a TZ string in the file. */
struct state st;
} u;
+
+ /* We don't need the "fullname" member */
};
/* Load tz data from the file named NAME into *SP. Read extended
{
int32 ttisstdcnt = detzcode(up->tzhead.tzh_ttisstdcnt);
int32 ttisgmtcnt = detzcode(up->tzhead.tzh_ttisgmtcnt);
+ int64 prevtr = 0;
+ int32 prevcorr = 0;
int32 leapcnt = detzcode(up->tzhead.tzh_leapcnt);
int32 timecnt = detzcode(up->tzhead.tzh_timecnt);
int32 typecnt = detzcode(up->tzhead.tzh_typecnt);
/*
* Read transitions, discarding those out of pg_time_t range. But
- * pretend the last transition before time_t_min occurred at
- * time_t_min.
+ * pretend the last transition before TIME_T_MIN occurred at
+ * TIME_T_MIN.
*/
timecnt = 0;
for (i = 0; i < sp->timecnt; ++i)
int64 at
= stored == 4 ? detzcode(p) : detzcode64(p);
- sp->types[i] = at <= time_t_max;
+ sp->types[i] = at <= TIME_T_MAX;
if (sp->types[i])
{
pg_time_t attime
- = ((TYPE_SIGNED(pg_time_t) ? at < time_t_min : at < 0)
- ? time_t_min : at);
+ = ((TYPE_SIGNED(pg_time_t) ? at < TIME_T_MIN : at < 0)
+ ? TIME_T_MIN : at);
if (timecnt && attime <= sp->ats[timecnt - 1])
{
int32 corr = detzcode(p + stored);
p += stored + 4;
- if (tr <= time_t_max)
+ /* Leap seconds cannot occur before the Epoch. */
+ if (tr < 0)
+ return EINVAL;
+ if (tr <= TIME_T_MAX)
{
- pg_time_t trans
- = ((TYPE_SIGNED(pg_time_t) ? tr < time_t_min : tr < 0)
- ? time_t_min : tr);
-
- if (leapcnt && trans <= sp->lsis[leapcnt - 1].ls_trans)
- {
- if (trans < sp->lsis[leapcnt - 1].ls_trans)
- return EINVAL;
- leapcnt--;
- }
- sp->lsis[leapcnt].ls_trans = trans;
- sp->lsis[leapcnt].ls_corr = corr;
+ /*
+ * Leap seconds cannot occur more than once per UTC month, and
+ * UTC months are at least 28 days long (minus 1 second for a
+ * negative leap second). Each leap second's correction must
+ * differ from the previous one's by 1 second.
+ */
+ if (tr - prevtr < 28 * SECSPERDAY - 1
+ || (corr != prevcorr - 1 && corr != prevcorr + 1))
+ return EINVAL;
+ sp->lsis[leapcnt].ls_trans = prevtr = tr;
+ sp->lsis[leapcnt].ls_corr = prevcorr = corr;
leapcnt++;
}
}
* Return the number of leap years through the end of the given year
* where, to make the math easy, the answer for year zero is defined as zero.
*/
+static int
+leaps_thru_end_of_nonneg(int y)
+{
+ return y / 4 - y / 100 + y / 400;
+}
+
static int
leaps_thru_end_of(const int y)
{
- return (y >= 0) ? (y / 4 - y / 100 + y / 400) :
- -(leaps_thru_end_of(-(y + 1)) + 1);
+ return (y < 0
+ ? -1 - leaps_thru_end_of_nonneg(-1 - y)
+ : leaps_thru_end_of_nonneg(y));
}
static struct pg_tm *
lp = &sp->lsis[i];
if (*timep >= lp->ls_trans)
{
- if (*timep == lp->ls_trans)
- {
- hit = ((i == 0 && lp->ls_corr > 0) ||
- lp->ls_corr > sp->lsis[i - 1].ls_corr);
- if (hit)
- while (i > 0 &&
- sp->lsis[i].ls_trans ==
- sp->lsis[i - 1].ls_trans + 1 &&
- sp->lsis[i].ls_corr ==
- sp->lsis[i - 1].ls_corr + 1)
- {
- ++hit;
- --i;
- }
- }
corr = lp->ls_corr;
+ hit = (*timep == lp->ls_trans
+ && (i == 0 ? 0 : lp[-1].ls_corr) < corr);
break;
}
}
{
/*----------
* This is like
- * 'if (! (time_t_min <= *tp + j && *tp + j <= time_t_max)) ...',
+ * 'if (! (TIME_T_MIN <= *tp + j && *tp + j <= TIME_T_MAX)) ...',
* except that it does the right thing even if *tp + j would overflow.
*----------
*/
if (!(j < 0
- ? (TYPE_SIGNED(pg_time_t) ? time_t_min - j <= *tp : -1 - j < *tp)
- : *tp <= time_t_max - j))
+ ? (TYPE_SIGNED(pg_time_t) ? TIME_T_MIN - j <= *tp : -1 - j < *tp)
+ : *tp <= TIME_T_MAX - j))
return true;
*tp += j;
return false;
#define EOVERFLOW EINVAL
#endif
-#ifndef WIFEXITED
-#define WIFEXITED(status) (((status) & 0xff) == 0)
-#endif /* !defined WIFEXITED */
-#ifndef WEXITSTATUS
-#define WEXITSTATUS(status) (((status) >> 8) & 0xff)
-#endif /* !defined WEXITSTATUS */
-
/* Unlike <ctype.h>'s isdigit, this also works if c < 0 | c > UCHAR_MAX. */
#define is_digit(c) ((unsigned)(c) - '0' <= 9)
-/*
- * SunOS 4.1.1 libraries lack remove.
- */
-
-#ifndef remove
-extern int unlink(const char *filename);
-
-#define remove unlink
-#endif /* !defined remove */
-
/*
* Finally, some convenience items.
#define MINVAL(t, b) \
((t) (TYPE_SIGNED(t) ? - TWOS_COMPLEMENT(t) - MAXVAL(t, b) : 0))
+/* The extreme time values, assuming no padding. */
+#define TIME_T_MIN MINVAL(pg_time_t, TYPE_BIT(pg_time_t))
+#define TIME_T_MAX MAXVAL(pg_time_t, TYPE_BIT(pg_time_t))
+
/*
* 302 / 1000 is log10(2.0) rounded up.
* Subtract one for the sign bit if the type is signed;
/*
* INITIALIZE(x)
*/
-#define INITIALIZE(x) ((x) = 0)
+#define INITIALIZE(x) ((x) = 0)
#undef _
#define _(msgid) (msgid)
* or
* isleap(a + b) == isleap(a % 400 + b % 400)
* This is true even if % means modulo rather than Fortran remainder
- * (which is allowed by C89 but not C99).
+ * (which is allowed by C89 but not by C99 or later).
* We use this to avoid addition overflow problems.
*/
/*
* x_fmt
*
- * C99 requires this format. Using just numbers (as here) makes Quakers
- * happier; it's also compatible with SVR4.
+ * C99 and later require this format. Using just numbers (as here) makes
+ * Quakers happier; it's also compatible with SVR4.
*/
"%m/%d/%y",
/*
* c_fmt
*
- * C99 requires this format. Previously this code used "%D %X", but we now
- * conform to C99. Note that "%a %b %d %H:%M:%S %Y" is used by Solaris
- * 2.3.
+ * C99 and later require this format. Previously this code used "%D %X",
+ * but we now conform to C99. Note that "%a %b %d %H:%M:%S %Y" is used by
+ * Solaris 2.3.
*/
"%a %b %e %T %Y",
"%a %b %e %H:%M:%S %Z %Y"
};
+enum warn
+{
+ IN_NONE, IN_SOME, IN_THIS, IN_ALL
+};
+
static char *_add(const char *, char *, const char *);
static char *_conv(int, const char *, char *, const char *);
-static char *_fmt(const char *, const struct pg_tm *, char *,
- const char *, int *);
+static char *_fmt(const char *, const struct pg_tm *, char *, const char *,
+ enum warn *);
static char *_yconv(int, int, bool, bool, char *, const char *);
-#define IN_NONE 0
-#define IN_SOME 1
-#define IN_THIS 2
-#define IN_ALL 3
-
size_t
pg_strftime(char *s, size_t maxsize, const char *format,
const struct pg_tm *t)
{
char *p;
- int warn;
+ enum warn warn = IN_NONE;
- warn = IN_NONE;
p = _fmt(format, t, s, s + maxsize, &warn);
if (p == s + maxsize)
return 0;
}
static char *
-_fmt(const char *format, const struct pg_tm *t, char *pt, const char *ptlim,
- int *warnp)
+_fmt(const char *format, const struct pg_tm *t, char *pt,
+ const char *ptlim, enum warn *warnp)
{
for (; *format; ++format)
{
continue;
case 'c':
{
- int warn2 = IN_SOME;
+ enum warn warn2 = IN_SOME;
pt = _fmt(Locale->c_fmt, t, pt, ptlim, &warn2);
if (warn2 == IN_ALL)
case 'O':
/*
- * C99 locale modifiers. The sequences %Ec %EC %Ex %EX
- * %Ey %EY %Od %oe %OH %OI %Om %OM %OS %Ou %OU %OV %Ow
- * %OW %Oy are supposed to provide alternate
+ * Locale modifiers of C99 and later. The sequences %Ec
+ * %EC %Ex %EX %Ey %EY %Od %oe %OH %OI %Om %OM %OS %Ou %OU
+ * %OV %Ow %OW %Oy are supposed to provide alternate
* representations.
*/
goto label;
continue;
case 'x':
{
- int warn2 = IN_SOME;
+ enum warn warn2 = IN_SOME;
pt = _fmt(Locale->x_fmt, t, pt, ptlim, &warn2);
if (warn2 == IN_ALL)
pt = _add(t->tm_zone, pt, ptlim);
/*
- * C99 says that %Z must be replaced by the empty string
- * if the time zone is not determinable.
+ * C99 and later say that %Z must be replaced by the empty
+ * string if the time zone is not determinable.
*/
continue;
case 'z':
#include <fcntl.h>
#include <sys/stat.h>
#include <time.h>
-#include <unistd.h>
#include "pg_getopt.h"
static ptrdiff_t const PTRDIFF_MAX = MAXVAL(ptrdiff_t, TYPE_BIT(ptrdiff_t));
#endif
-/* The type and printf format for line numbers. */
+/*
+ * The type for line numbers. In Postgres, use %d to format them; upstream
+ * uses PRIdMAX but we prefer not to rely on that, not least because it
+ * results in platform-dependent strings to be translated.
+ */
typedef int lineno_t;
-#define PRIdLINENO "d"
struct rule
{
static struct lookup const *byword(const char *string,
const struct lookup *lp);
-static struct lookup const line_codes[] = {
+static struct lookup const zi_line_codes[] = {
{"Rule", LC_RULE},
{"Zone", LC_ZONE},
{"Link", LC_LINK},
+ {NULL, 0}
+};
+static struct lookup const leap_line_codes[] = {
{"Leap", LC_LEAP},
{NULL, 0}
};
return ptr;
else
{
- ptrdiff_t amax = PTRDIFF_MAX - WORK_AROUND_QTBUG_53071;
+ ptrdiff_t nitems_max = PTRDIFF_MAX - WORK_AROUND_QTBUG_53071;
+ ptrdiff_t amax = nitems_max < SIZE_MAX ? nitems_max : SIZE_MAX;
if ((amax - 1) / 3 * 2 < *nitems_alloc)
memory_exhausted(_("integer overflow"));
* "*" -v on BSD systems.
*/
if (filename)
- fprintf(stderr, _("\"%s\", line %" PRIdLINENO ": "), filename, linenum);
+ fprintf(stderr, _("\"%s\", line %d: "), filename, linenum);
vfprintf(stderr, string, args);
if (rfilename != NULL)
- fprintf(stderr, _(" (rule from \"%s\", line %" PRIdLINENO ")"),
+ fprintf(stderr, _(" (rule from \"%s\", line %d)"),
rfilename, rlinenum);
fprintf(stderr, "\n");
}
static const char *yitcommand;
int
-main(int argc, char *argv[])
+main(int argc, char **argv)
{
int c,
k;
#ifndef WIN32
umask(umask(S_IWGRP | S_IWOTH) | (S_IWGRP | S_IWOTH));
-#endif /* !WIN32 */
+#endif
progname = argv[0];
if (TYPE_BIT(zic_t) <64)
{
break;
case 'y':
if (yitcommand == NULL)
+ {
+ warning(_("-y is obsolescent"));
yitcommand = strdup(optarg);
+ }
else
{
fprintf(stderr,
wantcont = inzcont(fields, nfields);
else
{
+ struct lookup const *line_codes
+ = name == leapsec ? leap_line_codes : zi_line_codes;
+
lp = byword(fields[0], line_codes);
if (lp == NULL)
error(_("input line of unknown type"));
wantcont = false;
break;
case LC_LEAP:
- if (name != leapsec)
- warning(_("%s: Leap line in non leap"
- " seconds file %s"),
- progname, name);
- else
- inleap(fields, nfields);
+ inleap(fields, nfields);
wantcont = false;
break;
default: /* "cannot happen" */
strcmp(zones[i].z_name, fields[ZF_NAME]) == 0)
{
error(_("duplicate zone name %s"
- " (file \"%s\", line %" PRIdLINENO ")"),
+ " (file \"%s\", line %d)"),
fields[ZF_NAME],
zones[i].z_filename,
zones[i].z_linenum);
positive = false;
count = 1;
}
- else if (strcmp(cp, "--") == 0)
- {
- positive = false;
- count = 2;
- }
else if (strcmp(cp, "+") == 0)
{
positive = true;
count = 1;
}
- else if (strcmp(cp, "++") == 0)
- {
- positive = true;
- count = 2;
- }
else
{
error(_("illegal CORRECTION field on Leap line"));
return;
}
t = tadd(t, tod);
- if (t < early_time)
+ if (t < 0)
{
- error(_("leap second precedes Big Bang"));
+ error(_("leap second precedes Epoch"));
return;
}
leapadd(t, positive, lp->l_value, count);
error(_("typed single year"));
return;
}
+ warning(_("year type \"%s\" is obsolete; use \"-\" instead"),
+ typep);
rp->r_yrtype = ecpyalloc(typep);
}
/*
- * Day work. Accept things such as: 1 last-Sunday Sun<=20 Sun>=7
+ * Day work. Accept things such as: 1 lastSunday last-Sunday
+ * (undocumented; warn about this) Sun<=20 Sun>=7
*/
dp = ecpyalloc(dayp);
if ((lp = byword(dp, lasts)) != NULL)
{
ptrdiff_t k;
zic_t jtime,
- ktime = 0;
+ ktime;
zic_t offset;
+ INITIALIZE(ktime);
if (useuntil)
{
/*
continue;
}
if (*startbuf == '\0' &&
- startoff == oadd(zp->z_gmtoff, stdoff))
+ startoff == oadd(zp->z_gmtoff,
+ stdoff))
{
doabbr(startbuf,
zp,
}
for (i = 0; i < leapcnt; ++i)
if (t <= trans[i])
- {
- if (t == trans[i])
- {
- error(_("repeated leap second moment"));
- exit(EXIT_FAILURE);
- }
break;
- }
do
{
for (j = leapcnt; j > i; --j)
{
int i;
zic_t last = 0;
+ zic_t prevtrans = 0;
/*
* propagate leap seconds forward
*/
for (i = 0; i < leapcnt; ++i)
{
+ if (trans[i] - prevtrans < 28 * SECSPERDAY)
+ {
+ error(_("Leap seconds too close together"));
+ exit(EXIT_FAILURE);
+ }
+ prevtrans = trans[i];
trans[i] = tadd(trans[i], last);
last = corr[i] += last;
}
exit(EXIT_FAILURE);
}
-/* Is A a space character in the C locale? */
+/* Is A a space character in the C locale? */
static bool
is_space(char a)
{
return true;
}
+/* Return true if ABBR is an initial prefix of WORD, ignoring ASCII case. */
+
+static bool
+ciprefix(char const *abbr, char const *word)
+{
+ do
+ if (!*abbr)
+ return true;
+ while (lowerit(*abbr++) == lowerit(*word++));
+
+ return false;
+}
+
static const struct lookup *
byword(const char *word, const struct lookup *table)
{
if (word == NULL || table == NULL)
return NULL;
+ /*
+ * If TABLE is LASTS and the word starts with "last" followed by a
+ * non-'-', skip the "last" and look in WDAY_NAMES instead. Warn about any
+ * usage of the undocumented prefix "last-".
+ */
+ if (table == lasts && ciprefix("last", word) && word[4])
+ {
+ if (word[4] == '-')
+ warning(_("\"%s\" is undocumented; use \"last%s\" instead"),
+ word, word + 5);
+ else
+ {
+ word += 4;
+ table = wday_names;
+ }
+ }
+
/*
* Look for exact match.
*/
*/
foundlp = NULL;
for (lp = table; lp->l_word != NULL; ++lp)
- if (itsabbr(word, lp->l_word))
+ if (ciprefix(word, lp->l_word))
{
if (foundlp == NULL)
foundlp = lp;
else
return NULL; /* multiple inexact matches */
}
+
+ /* Warn about any backward-compatibility issue with pre-2017c zic. */
+ if (foundlp)
+ {
+ bool pre_2017c_match = false;
+
+ for (lp = table; lp->l_word; lp++)
+ if (itsabbr(word, lp->l_word))
+ {
+ if (pre_2017c_match)
+ {
+ warning(_("\"%s\" is ambiguous in pre-2017c zic"), word);
+ break;
+ }
+ pre_2017c_match = true;
+ }
+ }
+
return foundlp;
}
cp = name = ecpyalloc(argname);
+ /*
+ * On MS-Windows systems, do not worry about drive letters or backslashes,
+ * as this should suffice in practice. Time zone names do not use drive
+ * letters and backslashes. If the -d option of zic does not name an
+ * already-existing directory, it can use slashes to separate the
+ * already-existing ancestor prefix from the to-be-created subdirectories.
+ */
+
/* Do not mkdir a root directory, as it must exist. */
-#ifdef WIN32
- if (is_alpha(name[0]) && name[1] == ':')
- cp += 2;
-#endif
while (*cp == '/')
cp++;