Time Zone code
==============
-The code in this directory is currently synced with tzcode release 2016j.
+The code in this directory is currently synced with tzcode release 2017b.
There are many cosmetic (and not so cosmetic) differences from the
original tzcode library, but diffs in the upstream version should usually
be propagated to our version. Here are some notes about that.
#include <fcntl.h>
#include "datatype/timestamp.h"
-#include "private.h"
#include "pgtz.h"
+
+#include "private.h"
#include "tzfile.h"
{
/*
* Attempt to reuse existing abbreviations. Without this,
- * America/Anchorage would stop working after 2037 when
- * TZ_MAX_CHARS is 50, as sp->charcnt equals 42 (for LMT CAT CAWT
- * CAPT AHST AHDT YST AKDT AKST) and ts->charcnt equals 10 (for
- * AKST AKDT). Reusing means sp->charcnt can stay 42 in this
+ * America/Anchorage would be right on the edge after 2037 when
+ * TZ_MAX_CHARS is 50, as sp->charcnt equals 40 (for LMT AST AWT
+ * APT AHST AHDT YST AKDT AKST) and ts->charcnt equals 10 (for
+ * AKST AKDT). Reusing means sp->charcnt can stay 40 in this
* example.
*/
int gotabbr = 0;
if (gotabbr == 2)
{
sp->charcnt = charcnt;
+
+ /*
+ * Ignore any trailing, no-op transitions generated by zic as
+ * they don't help here and can run afoul of bugs in zic 2016j
+ * or earlier.
+ */
+ while (1 < sp->timecnt
+ && (sp->types[sp->timecnt - 1]
+ == sp->types[sp->timecnt - 2]))
+ sp->timecnt--;
+
for (i = 0; i < ts->timecnt; i++)
if (sp->ats[sp->timecnt - 1] < ts->ats[i])
break;
int yearlim;
int timecnt;
pg_time_t janfirst;
+ int32 janoffset = 0;
+ int yearbeg;
++name;
if ((name = getrule(name, &start)) == NULL)
sp->defaulttype = 0;
timecnt = 0;
janfirst = 0;
- yearlim = EPOCH_YEAR + YEARSPERREPEAT;
- for (year = EPOCH_YEAR; year < yearlim; year++)
+ yearbeg = EPOCH_YEAR;
+
+ do
+ {
+ int32 yearsecs
+ = year_lengths[isleap(yearbeg - 1)] * SECSPERDAY;
+
+ yearbeg--;
+ if (increment_overflow_time(&janfirst, -yearsecs))
+ {
+ janoffset = -yearsecs;
+ break;
+ }
+ } while (EPOCH_YEAR - YEARSPERREPEAT / 2 < yearbeg);
+
+ yearlim = yearbeg + YEARSPERREPEAT + 1;
+ for (year = yearbeg; year < yearlim; year++)
{
int32
starttime = transtime(year, &start, stdoffset),
{
if (TZ_MAX_TIMES - 2 < timecnt)
break;
- yearlim = year + YEARSPERREPEAT + 1;
sp->ats[timecnt] = janfirst;
- if (increment_overflow_time
- (&sp->ats[timecnt], starttime))
- break;
- sp->types[timecnt++] = reversed;
+ if (!increment_overflow_time
+ (&sp->ats[timecnt],
+ janoffset + starttime))
+ sp->types[timecnt++] = reversed;
+ else if (janoffset)
+ sp->defaulttype = reversed;
sp->ats[timecnt] = janfirst;
- if (increment_overflow_time
- (&sp->ats[timecnt], endtime))
- break;
- sp->types[timecnt++] = !reversed;
+ if (!increment_overflow_time
+ (&sp->ats[timecnt],
+ janoffset + endtime))
+ {
+ sp->types[timecnt++] = !reversed;
+ yearlim = year + YEARSPERREPEAT + 1;
+ }
+ else if (janoffset)
+ sp->defaulttype = !reversed;
}
- if (increment_overflow_time(&janfirst, yearsecs))
+ if (increment_overflow_time
+ (&janfirst, janoffset + yearsecs))
break;
+ janoffset = 0;
}
sp->timecnt = timecnt;
if (!timecnt)
sp->typecnt = 1; /* Perpetual DST. */
+ else if (YEARSPERREPEAT < year - yearbeg)
+ sp->goback = sp->goahead = true;
}
else
{
#include <sys/stat.h>
#include <time.h>
+#include "datatype/timestamp.h"
#include "miscadmin.h"
#include "pgtz.h"
#include "storage/fd.h"
char tzname[128];
snprintf(offsetstr, sizeof(offsetstr),
- "%02ld", absoffset / SECSPERHOUR);
- absoffset %= SECSPERHOUR;
+ "%02ld", absoffset / SECS_PER_HOUR);
+ absoffset %= SECS_PER_HOUR;
if (absoffset != 0)
{
snprintf(offsetstr + strlen(offsetstr),
sizeof(offsetstr) - strlen(offsetstr),
- ":%02ld", absoffset / SECSPERMIN);
- absoffset %= SECSPERMIN;
+ ":%02ld", absoffset / SECS_PER_MINUTE);
+ absoffset %= SECS_PER_MINUTE;
if (absoffset != 0)
snprintf(offsetstr + strlen(offsetstr),
sizeof(offsetstr) - strlen(offsetstr),
* Finally, some convenience items.
*/
-#ifndef TYPE_BIT
#define TYPE_BIT(type) (sizeof (type) * CHAR_BIT)
-#endif /* !defined TYPE_BIT */
-
-#ifndef TYPE_SIGNED
#define TYPE_SIGNED(type) (((type) -1) < 0)
-#endif /* !defined TYPE_SIGNED */
-
#define TWOS_COMPLEMENT(t) ((t) ~ (t) 0 < 0)
/*
#define MINVAL(t, b) \
((t) (TYPE_SIGNED(t) ? - TWOS_COMPLEMENT(t) - MAXVAL(t, b) : 0))
-#ifndef INT_STRLEN_MAXIMUM
/*
* 302 / 1000 is log10(2.0) rounded up.
* Subtract one for the sign bit if the type is signed;
#define INT_STRLEN_MAXIMUM(type) \
((TYPE_BIT(type) - TYPE_SIGNED(type)) * 302 / 1000 + \
1 + TYPE_SIGNED(type))
-#endif /* !defined INT_STRLEN_MAXIMUM */
/*
* INITIALIZE(x)
#undef _
#define _(msgid) (msgid)
-#ifndef YEARSPERREPEAT
+/* Handy macros that are independent of tzfile implementation. */
+
#define YEARSPERREPEAT 400 /* years before a Gregorian repeat */
-#endif /* !defined YEARSPERREPEAT */
+
+#define SECSPERMIN 60
+#define MINSPERHOUR 60
+#define HOURSPERDAY 24
+#define DAYSPERWEEK 7
+#define DAYSPERNYEAR 365
+#define DAYSPERLYEAR 366
+#define SECSPERHOUR (SECSPERMIN * MINSPERHOUR)
+#define SECSPERDAY ((int32) SECSPERHOUR * HOURSPERDAY)
+#define MONSPERYEAR 12
+
+#define TM_SUNDAY 0
+#define TM_MONDAY 1
+#define TM_TUESDAY 2
+#define TM_WEDNESDAY 3
+#define TM_THURSDAY 4
+#define TM_FRIDAY 5
+#define TM_SATURDAY 6
+
+#define TM_JANUARY 0
+#define TM_FEBRUARY 1
+#define TM_MARCH 2
+#define TM_APRIL 3
+#define TM_MAY 4
+#define TM_JUNE 5
+#define TM_JULY 6
+#define TM_AUGUST 7
+#define TM_SEPTEMBER 8
+#define TM_OCTOBER 9
+#define TM_NOVEMBER 10
+#define TM_DECEMBER 11
+
+#define TM_YEAR_BASE 1900
+
+#define EPOCH_YEAR 1970
+#define EPOCH_WDAY TM_THURSDAY
+
+#define isleap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
/*
- * The Gregorian year averages 365.2425 days, which is 31556952 seconds.
+ * Since everything in isleap is modulo 400 (or a factor of 400), we know that
+ * isleap(y) == isleap(y % 400)
+ * and so
+ * isleap(a + b) == isleap((a + b) % 400)
+ * 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).
+ * We use this to avoid addition overflow problems.
*/
-#ifndef AVGSECSPERYEAR
-#define AVGSECSPERYEAR 31556952L
-#endif /* !defined AVGSECSPERYEAR */
+#define isleap_sum(a, b) isleap((a) % 400 + (b) % 400)
-#ifndef SECSPERREPEAT
-#define SECSPERREPEAT ((int64) YEARSPERREPEAT * (int64) AVGSECSPERYEAR)
-#endif /* !defined SECSPERREPEAT */
-#ifndef SECSPERREPEAT_BITS
+/*
+ * The Gregorian year averages 365.2425 days, which is 31556952 seconds.
+ */
+
+#define AVGSECSPERYEAR 31556952L
+#define SECSPERREPEAT \
+ ((int64) YEARSPERREPEAT * (int64) AVGSECSPERYEAR)
#define SECSPERREPEAT_BITS 34 /* ceil(log2(SECSPERREPEAT)) */
-#endif /* !defined SECSPERREPEAT_BITS */
#endif /* !defined PRIVATE_H */
-/* Convert a broken-down time stamp to a string. */
+/* Convert a broken-down timestamp to a string. */
/*
* Copyright 1989 The Regents of the University of California.
#include <locale.h>
#include "private.h"
-#include "tzfile.h"
struct lc_time_T
{
long diff;
char const *sign;
+ bool negative;
if (t->tm_isdst < 0)
continue;
diff = t->tm_gmtoff;
- if (diff < 0)
+ negative = diff < 0;
+ if (diff == 0)
+ {
+ negative = t->tm_zone[0] == '-';
+ }
+ if (negative)
{
sign = "-";
diff = -diff;
#define TZ_MAX_LEAPS 50 /* Maximum number of leap second corrections */
-#define SECSPERMIN 60
-#define MINSPERHOUR 60
-#define HOURSPERDAY 24
-#define DAYSPERWEEK 7
-#define DAYSPERNYEAR 365
-#define DAYSPERLYEAR 366
-#define SECSPERHOUR (SECSPERMIN * MINSPERHOUR)
-#define SECSPERDAY ((int32) SECSPERHOUR * HOURSPERDAY)
-#define MONSPERYEAR 12
-
-#define TM_SUNDAY 0
-#define TM_MONDAY 1
-#define TM_TUESDAY 2
-#define TM_WEDNESDAY 3
-#define TM_THURSDAY 4
-#define TM_FRIDAY 5
-#define TM_SATURDAY 6
-
-#define TM_JANUARY 0
-#define TM_FEBRUARY 1
-#define TM_MARCH 2
-#define TM_APRIL 3
-#define TM_MAY 4
-#define TM_JUNE 5
-#define TM_JULY 6
-#define TM_AUGUST 7
-#define TM_SEPTEMBER 8
-#define TM_OCTOBER 9
-#define TM_NOVEMBER 10
-#define TM_DECEMBER 11
-
-#define TM_YEAR_BASE 1900
-
-#define EPOCH_YEAR 1970
-#define EPOCH_WDAY TM_THURSDAY
-
-#define isleap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
-
-/*
- * Since everything in isleap is modulo 400 (or a factor of 400), we know that
- * isleap(y) == isleap(y % 400)
- * and so
- * isleap(a + b) == isleap((a + b) % 400)
- * 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).
- * We use this to avoid addition overflow problems.
- */
-
-#define isleap_sum(a, b) isleap((a) % 400 + (b) % 400)
-
#endif /* !defined TZFILE_H */
bool do_extend;
char version;
ptrdiff_t lastatmax = -1;
+ zic_t one = 1;
+ zic_t y2038_boundary = one << 31;
+ zic_t max_year0;
max_abbr_len = 2 + max_format_len + max_abbrvar_len;
max_envvar_len = 2 * max_abbr_len + 5 * 9;
}
/*
- * For the benefit of older systems, generate data from 1900 through 2037.
+ * For the benefit of older systems, generate data from 1900 through 2038.
*/
if (min_year > 1900)
min_year = 1900;
- if (max_year < 2037)
- max_year = 2037;
+ max_year0 = max_year;
+ if (max_year < 2038)
+ max_year = 2038;
for (i = 0; i < zonecount; ++i)
{
/*
year <= rp->r_hiyear &&
yearistype(year, rp->r_yrtype);
if (rp->r_todo)
+ {
rp->r_temp = rpytime(rp, year);
+ rp->r_todo
+ = (rp->r_temp < y2038_boundary
+ || year <= max_year0);
+ }
}
for (;;)
{