28#include <R_ext/Boolean.h>
72 static const int days_in_month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
75 #define isleap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0)
76 #define days_in_year(year) (isleap(year) ? 366 : 365)
88 }
else if (
year0 < 0) {
100 }
else if (
year0 < 1970) {
106 if ((
tm.tm_wday = (
day + 4) % 7) < 0)
tm.tm_wday += 7;
108 return tm.tm_sec + (
tm.tm_min * 60) + (
tm.tm_hour * 3600)
115#include "sys/types.h"
138#define TYPE_BIT(type) (sizeof (type) * CHAR_BIT)
139#define TYPE_SIGNED(type) (((type) -1) < 0)
140#define TYPE_INTEGRAL(type) (((type) 0.5) != 0.5)
141#define TWOS_COMPLEMENT(t) ((t) ~ (t) 0 < 0)
142#define GRANDPARENTED "Local time zone must be set--see zic manual page"
143#define YEARSPERREPEAT 400
144#define AVGSECSPERYEAR 31556952L
145#define SECSPERREPEAT ((int_fast64_t) YEARSPERREPEAT * (int_fast64_t) AVGSECSPERYEAR)
146#define SECSPERREPEAT_BITS 34
147#define is_digit(c) ((unsigned)(c) - '0' <= 9)
148#define INITIALIZE(x) (x = 0)
153#define MAXVAL(t, b) \
154 ((t) (((t) 1 << ((b) - 1 - TYPE_SIGNED(t))) \
155 - 1 + ((t) 1 << ((b) - 1 - TYPE_SIGNED(t)))))
156#define MINVAL(t, b) \
157 ((t) (TYPE_SIGNED(t) ? - TWOS_COMPLEMENT(t) - MAXVAL(t, b) : 0))
187#define TZDIR "/usr/local/etc/zoneinfo"
191#define TZDEFAULT "localtime"
195#define TZDEFRULES "America/New_York"
202#define TZ_MAGIC "TZif"
264#define TZ_MAX_TIMES 1200
269#define TZ_MAX_TYPES 256
276#define TZ_MAX_TYPES 20
282#define TZ_MAX_CHARS 100
287#define TZ_MAX_LEAPS 50
291#define MINSPERHOUR 60
292#define HOURSPERDAY 24
294#define DAYSPERNYEAR 365
295#define DAYSPERLYEAR 366
296#define SECSPERHOUR (SECSPERMIN * MINSPERHOUR)
297#define SECSPERDAY ((int_fast32_t) SECSPERHOUR * HOURSPERDAY)
298#define MONSPERYEAR 12
303#define TM_WEDNESDAY 3
316#define TM_SEPTEMBER 8
318#define TM_NOVEMBER 10
319#define TM_DECEMBER 11
321#define TM_YEAR_BASE 1900
323#define EPOCH_YEAR 1970
324#define EPOCH_WDAY TM_THURSDAY
326#define isleap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
340#define isleap_sum(a, b) isleap((a) % 400 + (b) % 400)
351#define OPEN_MODE (O_RDONLY | O_BINARY)
354#define OPEN_MODE O_RDONLY
357 static const char gmt[] =
"GMT";
366#ifndef TZDEFRULESTRING
367#define TZDEFRULESTRING ",M4.1.0,M10.5.0"
370#define BIGGEST(a, b) (((a) > (b)) ? (a) : (b))
373#define MY_TZNAME_MAX TZNAME_MAX
376#define MY_TZNAME_MAX 255
417#define MONTH_NTH_DAY_OF_WEEK 2
420 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
421 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
433#define gmtptr (&gmtmem)
483 for (
int i = 0; i < 4; ++i)
490 for (
int i = 0; i < 8; ++i)
526 }
else if (*
strp ==
'+')
594 }
else if (*
strp ==
'M') {
639 char buf[2 *
sizeof(
struct tzhead) +
671 snprintf(buf, 1000,
"%s/share/zoneinfo",
716 p =
u.tzhead.tzh_charcnt +
sizeof u.tzhead.tzh_charcnt;
733 for (i = 0; i <
sp->timecnt; ++i) {
737 for (i = 0; i <
sp->timecnt; ++i) {
738 sp->types[i] = (
unsigned char) *p++;
739 if (
sp->types[i] >=
sp->typecnt)
742 for (i = 0; i <
sp->typecnt; ++i) {
749 if (
ttisp->tt_isdst != 0 &&
ttisp->tt_isdst != 1)
751 ttisp->tt_abbrind = (
unsigned char) *p++;
752 if (
ttisp->tt_abbrind < 0 ||
753 ttisp->tt_abbrind >
sp->charcnt)
756 for (i = 0; i <
sp->charcnt; ++i)
759 for (i = 0; i <
sp->leapcnt; ++i) {
768 for (i = 0; i <
sp->typecnt; ++i) {
775 ttisp->tt_ttisstd = *p++;
780 for (i = 0; i <
sp->typecnt; ++i) {
787 ttisp->tt_ttisgmt = *p++;
797 for (i = 0; i <
sp->timecnt - 2; ++i)
798 if (
sp->ats[i] >
sp->ats[i + 1]) {
811 for (j = 0; j + i <
sp->timecnt; ++j) {
812 sp->ats[j] =
sp->ats[j + i];
813 sp->types[j] =
sp->types[j + i];
822 if (
u.tzhead.tzh_version[0] ==
'\0')
825 for (i = 0; i <
nread; ++i)
834 u.buf[0] ==
'\n' &&
u.buf[
nread - 1] ==
'\n' &&
841 if (
result == 0 &&
ts.typecnt == 2 &&
843 for (i = 0; i < 2; ++i)
844 ts.ttis[i].tt_abbrind +=
sp->charcnt;
845 for (i = 0; i <
ts.charcnt; ++i)
846 sp->chars[
sp->charcnt++] =
ts.chars[i];
848 while (i <
ts.timecnt &&
ts.ats[i] <=
sp->ats[
sp->timecnt - 1])
850 while (i <
ts.timecnt &&
852 sp->ats[
sp->timecnt] =
ts.ats[i];
853 sp->types[
sp->timecnt] = (
unsigned char)
sp->typecnt +
ts.types[i];
857 sp->ttis[
sp->typecnt++] =
ts.ttis[0];
858 sp->ttis[
sp->typecnt++] =
ts.ttis[1];
862 sp->goback =
sp->goahead =
sp->timecnt > i;
863 sp->goback =
sp->goback &&
866 sp->goahead =
sp->goahead &&
868 sp->types[
sp->timecnt - 1 - i]) &&
870 sp->ats[
sp->timecnt - 1 - i]);
885 switch (
rulep->r_type) {
918 m1 = (
rulep->r_mon + 9) % 12 + 1;
922 dow = ((26 *
m1 - 2) / 10 +
935 for (
int i = 1; i <
rulep->r_week; ++i) {
946 for (
int i = 0; i <
rulep->r_mon - 1; ++i)
957 return value +
rulep->r_time + offset;
1006 if (*name !=
'\0') {
1019 if (*name !=
'\0' && *name !=
',' && *name !=
';') {
1026 if (*name ==
',' || *name ==
';') {
1049 sp->ttis[0].tt_isdst = 1;
1052 sp->ttis[1].tt_isdst = 0;
1053 sp->ttis[1].tt_abbrind = 0;
1092 sp->timecnt = timecnt;
1105 for (
int i = 0; i <
sp->timecnt; ++i) {
1106 int j =
sp->types[i];
1107 if (!
sp->ttis[j].tt_isdst) {
1109 -
sp->ttis[j].tt_gmtoff;
1114 for (
int i = 0; i <
sp->timecnt; ++i) {
1115 int j =
sp->types[i];
1116 if (
sp->ttis[j].tt_isdst) {
1118 -
sp->ttis[j].tt_gmtoff;
1131 for (
int i = 0; i <
sp->timecnt; ++i) {
1132 int j =
sp->types[i];
1133 sp->types[i] = (
unsigned char)
sp->ttis[j].tt_isdst;
1134 if (
sp->ttis[j].tt_ttisgmt) {
1151 if (
isdst && !
sp->ttis[j].tt_ttisstd) {
1160 if (
sp->ttis[j].tt_isdst)
1170 sp->ttis[0].tt_abbrind = 0;
1172 sp->ttis[1].tt_isdst =
TRUE;
1182 sp->ttis[0].tt_isdst = 0;
1183 sp->ttis[0].tt_abbrind = 0;
1188 if ((
size_t)
sp->charcnt >
sizeof sp->chars)
1212 ap->tt_isdst ==
bp->tt_isdst &&
1213 ap->tt_ttisstd ==
bp->tt_ttisstd &&
1214 ap->tt_ttisgmt ==
bp->tt_ttisgmt &&
1216 &
sp->chars[
bp->tt_abbrind]) == 0;
1222 return (y >= 0) ? (y / 4 - y / 100 + y / 400) :
1245 hit = ((i == 0 &&
lp->ls_corr > 0) ||
1246 lp->ls_corr >
sp->lsis[i - 1].ls_corr);
1249 sp->lsis[i].ls_trans ==
1250 sp->lsis[i - 1].ls_trans + 1 &&
1251 sp->lsis[i].ls_corr ==
1252 sp->lsis[i - 1].ls_corr + 1) {
1333 if (
tmp->tm_wday < 0)
1348#if ! (defined(__MINGW32__) || defined(__MINGW64__) || defined(__sun) || defined(sun) || defined(_AIX))
1350 tmp->tm_gmtoff = offset;
#define TYPE_INTEGRAL(type)
#define days_in_year(year)
#define MONTH_NTH_DAY_OF_WEEK
#define SECSPERREPEAT_BITS
#define TYPE_SIGNED(type)
static const char * getnum(const char *strp, int *const nump, const int min, const int max)
static int_fast64_t detzcode64(const char *const codep)
static const char * getrule(const char *strp, struct rule *const rulep)
static const char * getsecs(const char *strp, int_fast32_t *secsp)
sugar::Min< RTYPE, NA, T > min(const VectorBase< RTYPE, NA, T > &x)
static int differ_by_repeat(const time_t t1, const time_t t0)
static int tzparse(const char *name, struct state *sp, int lastditch)
static int increment_overflow(int *const ip, int j)
static void gmtload(struct state *const sp)
static int typesequiv(const struct state *sp, int a, int b)
static time_t const time_t_max
static const char * getqzname(const char *strp, const int delim)
static const int mon_lengths[2][MONSPERYEAR]
static const char * getzname(const char *strp)
static struct tm * gmtsub(const time_t *const timep, const int_fast32_t offset, struct tm *const tmp)
attribute_hidden struct tm * gmtime_(const time_t *const x)
static const char * getoffset(const char *strp, int_fast32_t *const offsetp)
sugar::Max< RTYPE, NA, T > max(const VectorBase< RTYPE, NA, T > &x)
static int leaps_thru_end_of(const int y)
attribute_hidden double mktime00(struct tm &tm)
static time_t const time_t_min
static struct tm * timesub(const time_t *timep, int_fast32_t offset, const struct state *sp, struct tm *tmp)
static int increment_overflow_time(time_t *tp, int_fast32_t j)
static int_fast32_t detzcode(const char *const codep)
static struct state gmtmem
static int_fast32_t transtime(int year, const struct rule *rulep, int_fast32_t offset)
static const int year_lengths[2]
static int tzload(const char *name, struct state *const sp, const int doextend)
struct lsinfo lsis[TZ_MAX_LEAPS]
char chars[BIGGEST(BIGGEST(TZ_MAX_CHARS+1, sizeof gmt),(2 *(MY_TZNAME_MAX+1)))]
unsigned char types[TZ_MAX_TIMES]
struct ttinfo ttis[TZ_MAX_TYPES]