15 {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 0},
16 {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 0}};
23 {
"acsst",
DTZ, 37800},
24 {
"acst",
DTZ, -14400},
28 {
"aesst",
DTZ, 39600},
32 {
"akdt",
DTZ, -28800},
33 {
"akst",
DTZ, -32400},
40 {
"amst",
DTZ, -10800},
43 {
"anast",
DTZ, 46800},
62 {
"awsst",
DTZ, 32400},
101 {
"cetdst",
DTZ, 7200},
102 {
"chadt",
DTZ, 49500},
103 {
"chast",
TZ, 45900},
108 {
"clst",
DTZ, -10800},
124 {
"december",
MONTH, 12},
130 {
"dusst",
DTZ, 21600},
132 {
"easst",
DTZ, -18000},
133 {
"east",
TZ, -21600},
136 {
"east",
DTZ, 14400},
141 {
"edt",
DTZ, -14400},
142 {
"eest",
DTZ, 10800},
144 {
"eetdst",
DTZ, 10800},
153 {
"february",
MONTH, 2},
154 {
"fjst",
DTZ, -46800},
156 {
"fkst",
DTZ, -10800},
166 {
"galt",
TZ, -21600},
167 {
"gamt",
TZ, -32400},
168 {
"gest",
DTZ, 18000},
183 {
"hdt",
DTZ, -32400},
199 {
"idlw",
TZ, -43200},
205 {
"irkst",
DTZ, 32400},
216 {
"january",
MONTH, 1},
228 {
"kgst",
DTZ, 21600},
231 {
"krast",
DTZ, 25200},
234 {
"lhdt",
DTZ, 39600},
240 {
"magst",
DTZ, 43200},
244 {
"mart",
TZ, -34200},
247 {
"mdt",
DTZ, -21600},
250 {
"metdst",
DTZ, 7200},
277 {
"november",
MONTH, 11},
278 {
"novst",
DTZ, 25200},
285 {
"nzdt",
DTZ, 46800},
289 {
"october",
MONTH, 10},
290 {
"omsst",
DTZ, 25200},
293 {
"pdt",
DTZ, -25200},
298 {
"petst",
DTZ, 46800},
308 {
"pmdt",
DTZ, -7200},
315 {
"pyst",
DTZ, -10800},
319 {
"sadt",
DTZ, 37800},
326 {
"saturday",
DOW, 6},
333 {
"september",
MONTH, 9},
347 {
"that",
TZ, -36000},
351 {
"thursday",
DOW, 4},
372 {
"ulast",
DTZ, 32400},
376 {
"uyst",
DTZ, -7200},
378 {
"uzst",
DTZ, 21600},
381 {
"vlast",
DTZ, 39600},
387 {
"wadt",
DTZ, 28800},
396 {
"wednesday",
DOW, 3},
400 {
"wetdst",
DTZ, 3600},
402 {
"wgst",
DTZ, -7200},
406 {
"yakst",
DTZ, 36000},
409 {
"ydt",
DTZ, -28800},
410 {
"yekst",
DTZ, 21600},
493 char *
months[] = {
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec", NULL};
495 char *
days[] = {
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday", NULL};
499 char *
pgtypes_date_months[] = {
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December", NULL};
506 const datetkn *last = base + nel - 1,
512 position = base + ((last - base) >> 1);
514 result = (int)
key[0] - (
int) position->token[0];
598 julian =
y * 365 - 32167;
599 julian +=
y / 4 - century + century / 4;
600 julian += 7834 * m / 256 + d;
606 j2date(
int jd,
int *year,
int *month,
int *day)
615 quad = julian / 146097;
616 extra = (julian - quad * 146097) * 4 + 3;
617 julian += 60 + quad * 3 + extra / 146097;
618 quad = julian / 1461;
619 julian -= quad * 1461;
620 y = julian * 4 / 1461;
621 julian = ((
y != 0) ? (julian + 305) % 365 : (julian + 306) % 366) + 123;
624 quad = julian * 2141 / 65536;
625 *day = julian - 7834 * quad / 256;
626 *month = (quad + 10) % 12 + 1;
727 while (*(
str +
len - 1) ==
'0' && *(
str +
len - 3) !=
'.')
898 strcpy(
str + 3,
" ");
955 t0 = gmtime_r(&
epoch, &tmbuf);
975 time_t time = (time_t) _time;
981 tx = localtime_r(&time, &tmbuf);
983 tx = gmtime_r(&time, &tmbuf);
999 #if defined(HAVE_STRUCT_TM_TM_ZONE)
1027 #elif defined(HAVE_INT_TIMEZONE)
1090 int *tmask,
struct tm *
tm,
fsec_t *fsec,
bool *is2digits)
1098 if ((cp = strchr(
str,
'.')) != NULL)
1113 for (
i = 0;
i < 6;
i++)
1114 fstr[
i] = *cp !=
'\0' ? *cp++ :
'0';
1200 int *tmask,
struct tm *
tm,
fsec_t *fsec,
bool *is2digits,
bool EuroDates)
1219 tmask,
tm, fsec, is2digits);
1221 *fsec = strtod(cp, &cp);
1225 else if (*cp !=
'\0')
1267 else if ((EuroDates || (fmask &
DTK_M(
MONTH))) &&
1289 else if (!(fmask &
DTK_M(
YEAR)) && (flen >= 4 || flen == 2))
1295 *is2digits = (flen == 2);
1316 bool is2digits =
false;
1326 while (!isalnum((
unsigned char) *
str))
1330 if (isdigit((
unsigned char) *
str))
1332 while (isdigit((
unsigned char) *
str))
1335 else if (isalpha((
unsigned char) *
str))
1337 while (isalpha((
unsigned char) *
str))
1356 for (
i = 0;
i < nf;
i++)
1358 if (isalpha((
unsigned char) *field[
i]))
1390 for (
i = 0;
i < nf;
i++)
1392 if (field[
i] == NULL)
1395 if ((
len = strlen(field[
i])) <= 0)
1398 if (
DecodeNumber(
len, field[
i], fmask, &dmask,
tm, &fsec, &is2digits, EuroDates) != 0)
1453 else if (*cp !=
':')
1461 else if (*cp ==
'.')
1476 for (
i = 0;
i < 6;
i++)
1477 fstr[
i] = *cp !=
'\0' ? *cp++ :
'0';
1517 else if (*cp ==
'\0' && (
len = strlen(
str)) > 3)
1520 if (min < 0 || min >= 60)
1525 if (hr < 0 || hr > 13)
1556 while (*cp !=
'\0' && isalpha((
unsigned char) *cp))
1601 char **field,
int *ftype,
int *numfields,
char **endstr)
1608 while (*(*endstr) !=
'\0')
1616 if (isdigit((
unsigned char) *(*endstr)))
1618 *lp++ = *(*endstr)++;
1619 while (isdigit((
unsigned char) *(*endstr)))
1620 *lp++ = *(*endstr)++;
1623 if (*(*endstr) ==
':')
1626 *lp++ = *(*endstr)++;
1627 while (isdigit((
unsigned char) *(*endstr)) ||
1628 (*(*endstr) ==
':') || (*(*endstr) ==
'.'))
1629 *lp++ = *(*endstr)++;
1632 else if (*(*endstr) ==
'-' || *(*endstr) ==
'/' || *(*endstr) ==
'.')
1635 char *dp = (*endstr);
1637 *lp++ = *(*endstr)++;
1639 if (isdigit((
unsigned char) *(*endstr)))
1642 while (isdigit((
unsigned char) *(*endstr)))
1643 *lp++ = *(*endstr)++;
1649 if (*(*endstr) == *dp)
1652 *lp++ = *(*endstr)++;
1653 while (isdigit((
unsigned char) *(*endstr)) || (*(*endstr) == *dp))
1654 *lp++ = *(*endstr)++;
1660 while (isalnum((
unsigned char) *(*endstr)) || (*(*endstr) == *dp))
1661 *lp++ =
pg_tolower((
unsigned char) *(*endstr)++);
1673 else if (*(*endstr) ==
'.')
1675 *lp++ = *(*endstr)++;
1676 while (isdigit((
unsigned char) *(*endstr)))
1677 *lp++ = *(*endstr)++;
1685 else if (isalpha((
unsigned char) *(*endstr)))
1688 *lp++ =
pg_tolower((
unsigned char) *(*endstr)++);
1689 while (isalpha((
unsigned char) *(*endstr)))
1690 *lp++ =
pg_tolower((
unsigned char) *(*endstr)++);
1696 if (*(*endstr) ==
'-' || *(*endstr) ==
'/' || *(*endstr) ==
'.')
1698 char *dp = (*endstr);
1701 *lp++ = *(*endstr)++;
1702 while (isdigit((
unsigned char) *(*endstr)) || *(*endstr) == *dp)
1703 *lp++ = *(*endstr)++;
1707 else if (isspace((
unsigned char) *(*endstr)))
1713 else if (*(*endstr) ==
'+' || *(*endstr) ==
'-')
1715 *lp++ = *(*endstr)++;
1717 while (isspace((
unsigned char) *(*endstr)))
1720 if (isdigit((
unsigned char) *(*endstr)))
1723 *lp++ = *(*endstr)++;
1724 while (isdigit((
unsigned char) *(*endstr)) ||
1725 (*(*endstr) ==
':') || (*(*endstr) ==
'.'))
1726 *lp++ = *(*endstr)++;
1729 else if (isalpha((
unsigned char) *(*endstr)))
1732 *lp++ =
pg_tolower((
unsigned char) *(*endstr)++);
1733 while (isalpha((
unsigned char) *(*endstr)))
1734 *lp++ =
pg_tolower((
unsigned char) *(*endstr)++);
1741 else if (ispunct((
unsigned char) *(*endstr)))
1783 int *dtype,
struct tm *
tm,
fsec_t *fsec,
bool EuroDates)
1792 bool haveTextMonth =
false;
1793 bool is2digits =
false;
1812 for (
i = 0;
i < nf;
i++)
1856 if (isdigit((
unsigned char) *field[
i]) || ptype != 0)
1876 if ((cp = strchr(field[
i],
'-')) == NULL)
1889 &tmask,
tm, fsec, &is2digits)) < 0)
1907 else if (
DecodeDate(field[
i], fmask, &tmask,
tm, EuroDates) != 0)
1939 if (
i > 0 && (fmask &
DTK_M(
TZ)) != 0 &&
1941 isalpha((
unsigned char) *field[
i - 1]))
1982 else if (*cp !=
'\0')
2033 frac = strtod(cp, &cp);
2036 *fsec = frac * 1000000;
2057 time = strtod(cp, &cp);
2069 &tmask,
tm, fsec, &is2digits)) < 0)
2089 flen = strlen(field[
i]);
2090 cp = strchr(field[
i],
'.');
2095 if (
DecodeDate(field[
i], fmask, &tmask,
tm, EuroDates) != 0)
2099 else if (cp != NULL && flen - strlen(cp) > 2)
2107 &tmask,
tm, fsec, &is2digits)) < 0)
2113 &tmask,
tm, fsec, &is2digits)) < 0)
2118 &tmask,
tm, fsec, &is2digits, EuroDates) != 0)
2194 if ((fmask &
DTK_M(
MONTH)) && !haveTextMonth &&
2200 haveTextMonth =
true;
2371 char *end_position = NULL;
2373 *subst_location = NULL;
2374 int scan_offset = 0;
2381 return end_position;
2385 while (
fmt[scan_offset] ==
'%' &&
fmt[scan_offset + 1])
2395 next_percent = strchr(
fmt + scan_offset,
'%');
2405 subst_location = next_percent;
2406 while (*(subst_location - 1) ==
' ' && subst_location - 1 >
fmt + scan_offset)
2408 last_char = *subst_location;
2409 *subst_location =
'\0';
2425 end_position = strstr(
str,
fmt + scan_offset);
2426 *subst_location = last_char;
2434 end_position =
str + strlen(
str);
2452 if ((
fmt + scan_offset)[0] ==
' ' &&
fmt + scan_offset + 1 == subst_location)
2453 end_position =
str + strlen(
str);
2455 return end_position;
2469 char *strtol_end = NULL;
2471 while (**pstr ==
' ')
2479 last_char = *pstr_end;
2490 while (**pstr ==
' ')
2493 scan_val->
uint_val = (
unsigned int) strtol(*pstr, &strtol_end, 10);
2498 while (**pstr ==
' ')
2501 scan_val->
luint_val = (
unsigned long int) strtol(*pstr, &strtol_end, 10);
2507 if (scan_val->
str_val == NULL)
2511 if (strtol_end && *strtol_end)
2515 *pstr_end = last_char;
2522 int *year,
int *month,
int *day,
2523 int *hour,
int *minute,
int *second,
2542 while (*pfmt ==
' ')
2544 while (*pstr ==
' ')
2594 if (strncmp(
days[
j], pstr, strlen(
days[
j])) == 0)
2598 pstr += strlen(
days[
j]);
2666 strcpy(tmp,
"%m/%d/%y");
2735 if (strncmp(pstr,
"am", 2) == 0)
2741 if (strncmp(pstr,
"a.m.", 4) == 0)
2747 if (strncmp(pstr,
"pm", 2) == 0)
2753 if (strncmp(pstr,
"p.m.", 4) == 0)
2763 if (strncmp(pstr,
"AM", 2) == 0)
2769 if (strncmp(pstr,
"A.M.", 4) == 0)
2775 if (strncmp(pstr,
"PM", 2) == 0)
2781 if (strncmp(pstr,
"P.M.", 4) == 0)
2790 tmp =
pgtypes_alloc(strlen(
"%I:%M:%S %p") + strlen(pstr) + 1);
2793 strcpy(tmp,
"%I:%M:%S %p");
2803 strcpy(tmp,
"%H:%M");
2816 time_t et = (time_t) scan_val.
luint_val;
2818 tms = gmtime_r(&et, &tmbuf);
2822 *year = tms->tm_year + 1900;
2823 *month = tms->tm_mon + 1;
2824 *day = tms->tm_mday;
2825 *hour = tms->tm_hour;
2826 *minute = tms->tm_min;
2827 *second = tms->tm_sec;
2851 strcpy(tmp,
"%H:%M:%S");
2985 (*hour == 24 && (*minute > 0 || *second > 0)))
int tm2timestamp(struct pg_tm *tm, fsec_t fsec, int *tzp, Timestamp *result)
#define Assert(condition)
void EncodeDateOnly(struct tm *tm, int style, char *str, bool EuroDates)
int GetEpochTime(struct tm *tm)
int PGTYPEStimestamp_defmt_scan(char **str, char *fmt, timestamp *d, int *year, int *month, int *day, int *hour, int *minute, int *second, int *tz)
void EncodeDateTime(struct tm *tm, fsec_t fsec, bool print_tz, int tz, const char *tzn, int style, char *str, bool EuroDates)
void TrimTrailingZeros(char *str)
void GetCurrentDateTime(struct tm *tm)
static int DecodeTimezone(char *str, int *tzp)
static const datetkn datetktbl[]
static const unsigned int szdatetktbl
static int DecodePosixTimezone(char *str, int *tzp)
static char * find_end_token(char *str, char *fmt)
static const datetkn * deltacache[MAXDATEFIELDS]
static void abstime2tm(AbsoluteTime _time, int *tzp, struct tm *tm, char **tzn)
static const datetkn * datecache[MAXDATEFIELDS]
int DecodeTime(char *str, int *tmask, struct tm *tm, fsec_t *fsec)
int date2j(int y, int m, int d)
static int DecodeNumber(int flen, char *str, int fmask, int *tmask, struct tm *tm, fsec_t *fsec, bool *is2digits, bool EuroDates)
int ParseDateTime(char *timestr, char *lowstr, char **field, int *ftype, int *numfields, char **endstr)
static int DecodeSpecial(int field, char *lowtoken, int *val)
void j2date(int jd, int *year, int *month, int *day)
int DecodeDateTime(char **field, int *ftype, int nf, int *dtype, struct tm *tm, fsec_t *fsec, bool EuroDates)
static int DecodeNumberField(int len, char *str, int fmask, int *tmask, struct tm *tm, fsec_t *fsec, bool *is2digits)
char * pgtypes_date_months[]
char * pgtypes_date_weekdays_short[]
void dt2time(double jd, int *hour, int *min, int *sec, fsec_t *fsec)
int DecodeUnits(int field, char *lowtoken, int *val)
static const unsigned int szdeltatktbl
static const datetkn deltatktbl[]
static int DecodeDate(char *str, int fmask, int *tmask, struct tm *tm, bool EuroDates)
static const datetkn * datebsearch(const char *key, const datetkn *base, unsigned int nel)
static int pgtypes_defmt_scan(union un_fmt_comb *scan_val, int scan_type, char **pstr, char *pfmt)
void err(int eval, const char *fmt,...)
char * pgtypes_strdup(const char *str)
char * pgtypes_alloc(long size)
static void const char * fmt
#define USE_POSTGRES_DATES
#define PGTYPES_TS_BAD_TIMESTAMP
#define PGTYPES_TYPE_UINT
#define PGTYPES_TYPE_STRING_MALLOCED
#define PGTYPES_TYPE_UINT_LONG
int pg_strcasecmp(const char *s1, const char *s2)
unsigned char pg_tolower(unsigned char ch)
size_t strlcpy(char *dst, const char *src, size_t siz)
int strtoint(const char *pg_restrict str, char **pg_restrict endptr, int base)
unsigned long int luint_val
static const unsigned __int64 epoch