PostgreSQL Source Code git master
Loading...
Searching...
No Matches
dt_common.c File Reference
#include "postgres_fe.h"
#include <time.h>
#include <ctype.h>
#include <math.h>
#include "common/string.h"
#include "dt.h"
#include "pgtypes_timestamp.h"
#include "pgtypeslib_extern.h"
Include dependency graph for dt_common.c:

Go to the source code of this file.

Typedefs

typedef long AbsoluteTime
 

Functions

static const datetkndatebsearch (const char *key, const datetkn *base, unsigned int nel)
 
int DecodeUnits (int field, char *lowtoken, int *val)
 
int date2j (int y, int m, int d)
 
void j2date (int jd, int *year, int *month, int *day)
 
static int DecodeSpecial (int field, char *lowtoken, int *val)
 
void EncodeDateOnly (struct tm *tm, int style, char *str, bool EuroDates)
 
void TrimTrailingZeros (char *str)
 
void EncodeDateTime (struct tm *tm, fsec_t fsec, bool print_tz, int tz, const char *tzn, int style, char *str, bool EuroDates)
 
int GetEpochTime (struct tm *tm)
 
static void abstime2tm (AbsoluteTime _time, int *tzp, struct tm *tm, char **tzn)
 
void GetCurrentDateTime (struct tm *tm)
 
void dt2time (double jd, int *hour, int *min, int *sec, fsec_t *fsec)
 
static int DecodeNumberField (int len, char *str, int fmask, int *tmask, struct tm *tm, fsec_t *fsec, bool *is2digits)
 
static int DecodeNumber (int flen, char *str, int fmask, int *tmask, struct tm *tm, fsec_t *fsec, bool *is2digits, bool EuroDates)
 
static int DecodeDate (char *str, int fmask, int *tmask, struct tm *tm, bool EuroDates)
 
int DecodeTime (char *str, int *tmask, struct tm *tm, fsec_t *fsec)
 
static int DecodeTimezone (char *str, int *tzp)
 
static int DecodePosixTimezone (char *str, int *tzp)
 
int ParseDateTime (char *timestr, char *lowstr, char **field, int *ftype, int *numfields, char **endstr)
 
int DecodeDateTime (char **field, int *ftype, int nf, int *dtype, struct tm *tm, fsec_t *fsec, bool EuroDates)
 
static charfind_end_token (char *str, char *fmt)
 
static int pgtypes_defmt_scan (union un_fmt_comb *scan_val, int scan_type, char **pstr, char *pfmt)
 
int PGTYPEStimestamp_defmt_scan (char **str, char *fmt, timestamp *d, int *year, int *month, int *day, int *hour, int *minute, int *second, int *tz)
 

Variables

const int day_tab [2][13]
 
static const datetkn datetktbl []
 
static const datetkn deltatktbl []
 
static const unsigned int szdatetktbl = lengthof(datetktbl)
 
static const unsigned int szdeltatktbl = lengthof(deltatktbl)
 
static const datetkndatecache [MAXDATEFIELDS] = {NULL}
 
static const datetkndeltacache [MAXDATEFIELDS] = {NULL}
 
charmonths [] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", NULL}
 
chardays [] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", NULL}
 
charpgtypes_date_weekdays_short [] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL}
 
charpgtypes_date_months [] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December", NULL}
 

Typedef Documentation

◆ AbsoluteTime

Definition at line 18 of file dt_common.c.

Function Documentation

◆ abstime2tm()

static void abstime2tm ( AbsoluteTime  _time,
int tzp,
struct tm tm,
char **  tzn 
)
static

Definition at line 977 of file dt_common.c.

978{
979 time_t time = (time_t) _time;
980 struct tm *tx;
981 struct tm tmbuf;
982
983 errno = 0;
984 if (tzp != NULL)
985 tx = localtime_r(&time, &tmbuf);
986 else
987 tx = gmtime_r(&time, &tmbuf);
988
989 if (!tx)
990 {
992 return;
993 }
994
995 tm->tm_year = tx->tm_year + 1900;
996 tm->tm_mon = tx->tm_mon + 1;
997 tm->tm_mday = tx->tm_mday;
998 tm->tm_hour = tx->tm_hour;
999 tm->tm_min = tx->tm_min;
1000 tm->tm_sec = tx->tm_sec;
1001 tm->tm_isdst = tx->tm_isdst;
1002
1003#if defined(HAVE_STRUCT_TM_TM_ZONE)
1004 tm->tm_gmtoff = tx->tm_gmtoff;
1005 tm->tm_zone = tx->tm_zone;
1006
1007 if (tzp != NULL)
1008 {
1009 /*
1010 * We have a brute force time zone per SQL99? Then use it without
1011 * change since we have already rotated to the time zone.
1012 */
1013 *tzp = -tm->tm_gmtoff; /* tm_gmtoff is Sun/DEC-ism */
1014
1015 /*
1016 * FreeBSD man pages indicate that this should work - tgl 97/04/23
1017 */
1018 if (tzn != NULL)
1019 {
1020 /*
1021 * Copy no more than MAXTZLEN bytes of timezone to tzn, in case it
1022 * contains an error message, which doesn't fit in the buffer
1023 */
1024 strlcpy(*tzn, tm->tm_zone, MAXTZLEN + 1);
1025 if (strlen(tm->tm_zone) > MAXTZLEN)
1026 tm->tm_isdst = -1;
1027 }
1028 }
1029 else
1030 tm->tm_isdst = -1;
1031#elif defined(HAVE_INT_TIMEZONE)
1032 if (tzp != NULL)
1033 {
1035
1036 if (tzn != NULL)
1037 {
1038 /*
1039 * Copy no more than MAXTZLEN bytes of timezone to tzn, in case it
1040 * contains an error message, which doesn't fit in the buffer
1041 */
1044 tm->tm_isdst = -1;
1045 }
1046 }
1047 else
1048 tm->tm_isdst = -1;
1049#else /* not (HAVE_STRUCT_TM_TM_ZONE ||
1050 * HAVE_INT_TIMEZONE) */
1051 if (tzp != NULL)
1052 {
1053 /* default to UTC */
1054 *tzp = 0;
1055 if (tzn != NULL)
1056 *tzn = NULL;
1057 }
1058 else
1059 tm->tm_isdst = -1;
1060#endif
1061}
#define SECS_PER_HOUR
Definition timestamp.h:127
static struct pg_tm tm
Definition localtime.c:104
#define MAXTZLEN
Definition miscadmin.h:267
#define PGTYPES_TS_BAD_TIMESTAMP
#define TIMEZONE_GLOBAL
Definition port.h:291
#define TZNAME_GLOBAL
Definition port.h:292
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition strlcpy.c:45
static int fb(int x)
int tm_hour
Definition pgtime.h:38
int tm_mday
Definition pgtime.h:39
int tm_mon
Definition pgtime.h:40
int tm_min
Definition pgtime.h:37
const char * tm_zone
Definition pgtime.h:46
int tm_sec
Definition pgtime.h:36
int tm_isdst
Definition pgtime.h:44
long int tm_gmtoff
Definition pgtime.h:45
int tm_year
Definition pgtime.h:41

References fb(), MAXTZLEN, PGTYPES_TS_BAD_TIMESTAMP, SECS_PER_HOUR, strlcpy(), TIMEZONE_GLOBAL, tm, pg_tm::tm_gmtoff, pg_tm::tm_hour, pg_tm::tm_isdst, pg_tm::tm_mday, pg_tm::tm_min, pg_tm::tm_mon, pg_tm::tm_sec, pg_tm::tm_year, pg_tm::tm_zone, and TZNAME_GLOBAL.

Referenced by GetCurrentDateTime().

◆ date2j()

int date2j ( int  y,
int  m,
int  d 
)

Definition at line 582 of file dt_common.c.

583{
584 int julian;
585 int century;
586
587 if (m > 2)
588 {
589 m += 1;
590 y += 4800;
591 }
592 else
593 {
594 m += 13;
595 y += 4799;
596 }
597
598 century = y / 100;
599 julian = y * 365 - 32167;
600 julian += y / 4 - century + century / 4;
601 julian += 7834 * m / 256 + d;
602
603 return julian;
604} /* date2j() */
int y
Definition isn.c:76

References fb(), and y.

Referenced by DecodeDateTime(), DecodeNumber(), and EncodeDateTime().

◆ datebsearch()

static const datetkn * datebsearch ( const char key,
const datetkn base,
unsigned int  nel 
)
static

Definition at line 502 of file dt_common.c.

503{
504 if (nel > 0)
505 {
506 const datetkn *last = base + nel - 1,
507 *position;
508 int result;
509
510 while (last >= base)
511 {
512 position = base + ((last - base) >> 1);
513 /* precheck the first character for a bit of extra speed */
514 result = (int) key[0] - (int) position->token[0];
515 if (result == 0)
516 {
517 /* use strncmp so that we match truncated tokens */
518 result = strncmp(key, position->token, TOKMAXLEN);
519 if (result == 0)
520 return position;
521 }
522 if (result < 0)
523 last = position - 1;
524 else
525 base = position + 1;
526 }
527 }
528 return NULL;
529}
uint32 result
#define TOKMAXLEN
Definition datetime.h:204

References fb(), result, and TOKMAXLEN.

Referenced by DecodeSpecial(), and DecodeUnits().

◆ DecodeDate()

static int DecodeDate ( char str,
int  fmask,
int tmask,
struct tm tm,
bool  EuroDates 
)
static

Definition at line 1315 of file dt_common.c.

1316{
1317 fsec_t fsec;
1318
1319 int nf = 0;
1320 int i,
1321 len;
1322 bool bc = false;
1323 bool is2digits = false;
1324 int type,
1325 val,
1326 dmask = 0;
1327 char *field[MAXDATEFIELDS];
1328
1329 /* parse this string... */
1330 while (*str != '\0' && nf < MAXDATEFIELDS)
1331 {
1332 /* skip field separators */
1333 while (!isalnum((unsigned char) *str))
1334 str++;
1335
1336 field[nf] = str;
1337 if (isdigit((unsigned char) *str))
1338 {
1339 while (isdigit((unsigned char) *str))
1340 str++;
1341 }
1342 else if (isalpha((unsigned char) *str))
1343 {
1344 while (isalpha((unsigned char) *str))
1345 str++;
1346 }
1347
1348 /* Just get rid of any non-digit, non-alpha characters... */
1349 if (*str != '\0')
1350 *str++ = '\0';
1351 nf++;
1352 }
1353
1354#if 0
1355 /* don't allow too many fields */
1356 if (nf > 3)
1357 return -1;
1358#endif
1359
1360 *tmask = 0;
1361
1362 /* look first for text fields, since that will be unambiguous month */
1363 for (i = 0; i < nf; i++)
1364 {
1365 if (isalpha((unsigned char) *field[i]))
1366 {
1367 type = DecodeSpecial(i, field[i], &val);
1368 if (type == IGNORE_DTF)
1369 continue;
1370
1371 dmask = DTK_M(type);
1372 switch (type)
1373 {
1374 case MONTH:
1375 tm->tm_mon = val;
1376 break;
1377
1378 case ADBC:
1379 bc = (val == BC);
1380 break;
1381
1382 default:
1383 return -1;
1384 }
1385 if (fmask & dmask)
1386 return -1;
1387
1388 fmask |= dmask;
1389 *tmask |= dmask;
1390
1391 /* mark this field as being completed */
1392 field[i] = NULL;
1393 }
1394 }
1395
1396 /* now pick up remaining numeric fields */
1397 for (i = 0; i < nf; i++)
1398 {
1399 if (field[i] == NULL)
1400 continue;
1401
1402 if ((len = strlen(field[i])) <= 0)
1403 return -1;
1404
1405 if (DecodeNumber(len, field[i], fmask, &dmask, tm, &fsec, &is2digits, EuroDates) != 0)
1406 return -1;
1407
1408 if (fmask & dmask)
1409 return -1;
1410
1411 fmask |= dmask;
1412 *tmask |= dmask;
1413 }
1414
1415 if ((fmask & ~(DTK_M(DOY) | DTK_M(TZ))) != DTK_DATE_M)
1416 return -1;
1417
1418 /* there is no year zero in AD/BC notation; i.e. "1 BC" == year 0 */
1419 if (bc)
1420 {
1421 if (tm->tm_year > 0)
1422 tm->tm_year = -(tm->tm_year - 1);
1423 else
1424 return -1;
1425 }
1426 else if (is2digits)
1427 {
1428 if (tm->tm_year < 70)
1429 tm->tm_year += 2000;
1430 else if (tm->tm_year < 100)
1431 tm->tm_year += 1900;
1432 }
1433
1434 return 0;
1435} /* DecodeDate() */
int32 fsec_t
Definition timestamp.h:41
static int DecodeNumber(int flen, char *str, int fmask, int *tmask, struct tm *tm, fsec_t *fsec, bool *is2digits, bool EuroDates)
Definition dt_common.c:1205
static int DecodeSpecial(int field, char *lowtoken, int *val)
Definition dt_common.c:637
const char * str
#define MAXDATEFIELDS
Definition datetime.h:202
#define MONTH
Definition datetime.h:91
#define IGNORE_DTF
Definition datetime.h:98
#define DTK_M(t)
Definition datetime.h:187
#define ADBC
Definition datetime.h:108
#define TZ
Definition datetime.h:95
#define BC
Definition datetime.h:76
#define DTK_DATE_M
Definition datetime.h:191
#define DOY
Definition datetime.h:105
long val
Definition informix.c:689
int i
Definition isn.c:77
const void size_t len
const char * type

References ADBC, BC, DecodeNumber(), DecodeSpecial(), DOY, DTK_DATE_M, DTK_M, fb(), i, IGNORE_DTF, len, MAXDATEFIELDS, MONTH, str, tm, pg_tm::tm_mon, pg_tm::tm_year, type, TZ, and val.

Referenced by DecodeDateTime().

◆ DecodeDateTime()

int DecodeDateTime ( char **  field,
int ftype,
int  nf,
int dtype,
struct tm tm,
fsec_t fsec,
bool  EuroDates 
)

Definition at line 1794 of file dt_common.c.

1796{
1797 int fmask = 0,
1798 tmask,
1799 type;
1800 int ptype = 0; /* "prefix type" for ISO y2001m02d04 format */
1801 int i;
1802 int val;
1803 int mer = HR24;
1804 bool haveTextMonth = false;
1805 bool is2digits = false;
1806 bool bc = false;
1807 int t = 0;
1808 int *tzp = &t;
1809
1810 /***
1811 * We'll insist on at least all of the date fields, but initialize the
1812 * remaining fields in case they are not set later...
1813 ***/
1814 *dtype = DTK_DATE;
1815 tm->tm_hour = 0;
1816 tm->tm_min = 0;
1817 tm->tm_sec = 0;
1818 *fsec = 0;
1819 /* don't know daylight savings time status apriori */
1820 tm->tm_isdst = -1;
1821 if (tzp != NULL)
1822 *tzp = 0;
1823
1824 for (i = 0; i < nf; i++)
1825 {
1826 switch (ftype[i])
1827 {
1828 case DTK_DATE:
1829 /***
1830 * Integral julian day with attached time zone?
1831 * All other forms with JD will be separated into
1832 * distinct fields, so we handle just this case here.
1833 ***/
1834 if (ptype == DTK_JULIAN)
1835 {
1836 char *cp;
1837 int jday;
1838
1839 if (tzp == NULL)
1840 return -1;
1841
1842 jday = strtoint(field[i], &cp, 10);
1843 if (*cp != '-')
1844 return -1;
1845
1846 j2date(jday, &tm->tm_year, &tm->tm_mon, &tm->tm_mday);
1847 /* Get the time zone from the end of the string */
1848 if (DecodeTimezone(cp, tzp) != 0)
1849 return -1;
1850
1852 ptype = 0;
1853 break;
1854 }
1855 /***
1856 * Already have a date? Then this might be a POSIX time
1857 * zone with an embedded dash (e.g. "PST-3" == "EST") or
1858 * a run-together time with trailing time zone (e.g. hhmmss-zz).
1859 * - thomas 2001-12-25
1860 ***/
1861 else if (((fmask & DTK_DATE_M) == DTK_DATE_M)
1862 || (ptype != 0))
1863 {
1864 /* No time zone accepted? Then quit... */
1865 if (tzp == NULL)
1866 return -1;
1867
1868 if (isdigit((unsigned char) *field[i]) || ptype != 0)
1869 {
1870 char *cp;
1871
1872 if (ptype != 0)
1873 {
1874 /* Sanity check; should not fail this test */
1875 if (ptype != DTK_TIME)
1876 return -1;
1877 ptype = 0;
1878 }
1879
1880 /*
1881 * Starts with a digit but we already have a time
1882 * field? Then we are in trouble with a date and time
1883 * already...
1884 */
1885 if ((fmask & DTK_TIME_M) == DTK_TIME_M)
1886 return -1;
1887
1888 if ((cp = strchr(field[i], '-')) == NULL)
1889 return -1;
1890
1891 /* Get the time zone from the end of the string */
1892 if (DecodeTimezone(cp, tzp) != 0)
1893 return -1;
1894 *cp = '\0';
1895
1896 /*
1897 * Then read the rest of the field as a concatenated
1898 * time
1899 */
1900 if ((ftype[i] = DecodeNumberField(strlen(field[i]), field[i], fmask,
1901 &tmask, tm, fsec, &is2digits)) < 0)
1902 return -1;
1903
1904 /*
1905 * modify tmask after returning from
1906 * DecodeNumberField()
1907 */
1908 tmask |= DTK_M(TZ);
1909 }
1910 else
1911 {
1912 if (DecodePosixTimezone(field[i], tzp) != 0)
1913 return -1;
1914
1915 ftype[i] = DTK_TZ;
1916 tmask = DTK_M(TZ);
1917 }
1918 }
1919 else if (DecodeDate(field[i], fmask, &tmask, tm, EuroDates) != 0)
1920 return -1;
1921 break;
1922
1923 case DTK_TIME:
1924 if (DecodeTime(field[i], &tmask, tm, fsec) != 0)
1925 return -1;
1926
1927 /*
1928 * Check upper limit on hours; other limits checked in
1929 * DecodeTime()
1930 */
1931 /* test for > 24:00:00 */
1932 if (tm->tm_hour > 24 ||
1933 (tm->tm_hour == 24 && (tm->tm_min > 0 || tm->tm_sec > 0)))
1934 return -1;
1935 break;
1936
1937 case DTK_TZ:
1938 {
1939 int tz;
1940
1941 if (tzp == NULL)
1942 return -1;
1943
1944 if (DecodeTimezone(field[i], &tz) != 0)
1945 return -1;
1946
1947 /*
1948 * Already have a time zone? Then maybe this is the second
1949 * field of a POSIX time: EST+3 (equivalent to PST)
1950 */
1951 if (i > 0 && (fmask & DTK_M(TZ)) != 0 &&
1952 ftype[i - 1] == DTK_TZ &&
1953 isalpha((unsigned char) *field[i - 1]))
1954 {
1955 *tzp -= tz;
1956 tmask = 0;
1957 }
1958 else
1959 {
1960 *tzp = tz;
1961 tmask = DTK_M(TZ);
1962 }
1963 }
1964 break;
1965
1966 case DTK_NUMBER:
1967
1968 /*
1969 * Was this an "ISO date" with embedded field labels? An
1970 * example is "y2001m02d04" - thomas 2001-02-04
1971 */
1972 if (ptype != 0)
1973 {
1974 char *cp;
1975 int value;
1976
1977 value = strtoint(field[i], &cp, 10);
1978
1979 /*
1980 * only a few kinds are allowed to have an embedded
1981 * decimal
1982 */
1983 if (*cp == '.')
1984 switch (ptype)
1985 {
1986 case DTK_JULIAN:
1987 case DTK_TIME:
1988 case DTK_SECOND:
1989 break;
1990 default:
1991 return 1;
1992 break;
1993 }
1994 else if (*cp != '\0')
1995 return -1;
1996
1997 switch (ptype)
1998 {
1999 case DTK_YEAR:
2000 tm->tm_year = value;
2001 tmask = DTK_M(YEAR);
2002 break;
2003
2004 case DTK_MONTH:
2005
2006 /*
2007 * already have a month and hour? then assume
2008 * minutes
2009 */
2010 if ((fmask & DTK_M(MONTH)) != 0 &&
2011 (fmask & DTK_M(HOUR)) != 0)
2012 {
2013 tm->tm_min = value;
2014 tmask = DTK_M(MINUTE);
2015 }
2016 else
2017 {
2018 tm->tm_mon = value;
2019 tmask = DTK_M(MONTH);
2020 }
2021 break;
2022
2023 case DTK_DAY:
2024 tm->tm_mday = value;
2025 tmask = DTK_M(DAY);
2026 break;
2027
2028 case DTK_HOUR:
2029 tm->tm_hour = value;
2030 tmask = DTK_M(HOUR);
2031 break;
2032
2033 case DTK_MINUTE:
2034 tm->tm_min = value;
2035 tmask = DTK_M(MINUTE);
2036 break;
2037
2038 case DTK_SECOND:
2039 tm->tm_sec = value;
2040 tmask = DTK_M(SECOND);
2041 if (*cp == '.')
2042 {
2043 double frac;
2044
2045 frac = strtod(cp, &cp);
2046 if (*cp != '\0')
2047 return -1;
2048 *fsec = frac * 1000000;
2049 }
2050 break;
2051
2052 case DTK_TZ:
2053 tmask = DTK_M(TZ);
2054 if (DecodeTimezone(field[i], tzp) != 0)
2055 return -1;
2056 break;
2057
2058 case DTK_JULIAN:
2059 /***
2060 * previous field was a label for "julian date"?
2061 ***/
2062 tmask = DTK_DATE_M;
2064 /* fractional Julian Day? */
2065 if (*cp == '.')
2066 {
2067 double time;
2068
2069 time = strtod(cp, &cp);
2070 if (*cp != '\0')
2071 return -1;
2072
2073 tmask |= DTK_TIME_M;
2074 dt2time((time * USECS_PER_DAY), &tm->tm_hour, &tm->tm_min, &tm->tm_sec, fsec);
2075 }
2076 break;
2077
2078 case DTK_TIME:
2079 /* previous field was "t" for ISO time */
2080 if ((ftype[i] = DecodeNumberField(strlen(field[i]), field[i], (fmask | DTK_DATE_M),
2081 &tmask, tm, fsec, &is2digits)) < 0)
2082 return -1;
2083
2084 if (tmask != DTK_TIME_M)
2085 return -1;
2086 break;
2087
2088 default:
2089 return -1;
2090 break;
2091 }
2092
2093 ptype = 0;
2094 *dtype = DTK_DATE;
2095 }
2096 else
2097 {
2098 char *cp;
2099 int flen;
2100
2101 flen = strlen(field[i]);
2102 cp = strchr(field[i], '.');
2103
2104 /* Embedded decimal and no date yet? */
2105 if (cp != NULL && !(fmask & DTK_DATE_M))
2106 {
2107 if (DecodeDate(field[i], fmask, &tmask, tm, EuroDates) != 0)
2108 return -1;
2109 }
2110 /* embedded decimal and several digits before? */
2111 else if (cp != NULL && flen - strlen(cp) > 2)
2112 {
2113 /*
2114 * Interpret as a concatenated date or time Set the
2115 * type field to allow decoding other fields later.
2116 * Example: 20011223 or 040506
2117 */
2118 if ((ftype[i] = DecodeNumberField(flen, field[i], fmask,
2119 &tmask, tm, fsec, &is2digits)) < 0)
2120 return -1;
2121 }
2122 else if (flen > 4)
2123 {
2124 if ((ftype[i] = DecodeNumberField(flen, field[i], fmask,
2125 &tmask, tm, fsec, &is2digits)) < 0)
2126 return -1;
2127 }
2128 /* otherwise it is a single date/time field... */
2129 else if (DecodeNumber(flen, field[i], fmask,
2130 &tmask, tm, fsec, &is2digits, EuroDates) != 0)
2131 return -1;
2132 }
2133 break;
2134
2135 case DTK_STRING:
2136 case DTK_SPECIAL:
2137 type = DecodeSpecial(i, field[i], &val);
2138 if (type == IGNORE_DTF)
2139 continue;
2140
2141 tmask = DTK_M(type);
2142 switch (type)
2143 {
2144 case RESERV:
2145 switch (val)
2146 {
2147 case DTK_NOW:
2149 *dtype = DTK_DATE;
2151 break;
2152
2153 case DTK_YESTERDAY:
2154 tmask = DTK_DATE_M;
2155 *dtype = DTK_DATE;
2158 &tm->tm_year, &tm->tm_mon, &tm->tm_mday);
2159 tm->tm_hour = 0;
2160 tm->tm_min = 0;
2161 tm->tm_sec = 0;
2162 break;
2163
2164 case DTK_TODAY:
2165 tmask = DTK_DATE_M;
2166 *dtype = DTK_DATE;
2168 tm->tm_hour = 0;
2169 tm->tm_min = 0;
2170 tm->tm_sec = 0;
2171 break;
2172
2173 case DTK_TOMORROW:
2174 tmask = DTK_DATE_M;
2175 *dtype = DTK_DATE;
2178 &tm->tm_year, &tm->tm_mon, &tm->tm_mday);
2179 tm->tm_hour = 0;
2180 tm->tm_min = 0;
2181 tm->tm_sec = 0;
2182 break;
2183
2184 case DTK_ZULU:
2185 tmask = (DTK_TIME_M | DTK_M(TZ));
2186 *dtype = DTK_DATE;
2187 tm->tm_hour = 0;
2188 tm->tm_min = 0;
2189 tm->tm_sec = 0;
2190 if (tzp != NULL)
2191 *tzp = 0;
2192 break;
2193
2194 default:
2195 *dtype = val;
2196 }
2197
2198 break;
2199
2200 case MONTH:
2201
2202 /*
2203 * already have a (numeric) month? then see if we can
2204 * substitute...
2205 */
2206 if ((fmask & DTK_M(MONTH)) && !haveTextMonth &&
2207 !(fmask & DTK_M(DAY)) && tm->tm_mon >= 1 && tm->tm_mon <= 31)
2208 {
2209 tm->tm_mday = tm->tm_mon;
2210 tmask = DTK_M(DAY);
2211 }
2212 haveTextMonth = true;
2213 tm->tm_mon = val;
2214 break;
2215
2216 case DTZMOD:
2217
2218 /*
2219 * daylight savings time modifier (solves "MET DST"
2220 * syntax)
2221 */
2222 tmask |= DTK_M(DTZ);
2223 tm->tm_isdst = 1;
2224 if (tzp == NULL)
2225 return -1;
2226 *tzp -= val;
2227 break;
2228
2229 case DTZ:
2230
2231 /*
2232 * set mask for TZ here _or_ check for DTZ later when
2233 * getting default timezone
2234 */
2235 tmask |= DTK_M(TZ);
2236 tm->tm_isdst = 1;
2237 if (tzp == NULL)
2238 return -1;
2239 *tzp = -val;
2240 ftype[i] = DTK_TZ;
2241 break;
2242
2243 case TZ:
2244 tm->tm_isdst = 0;
2245 if (tzp == NULL)
2246 return -1;
2247 *tzp = -val;
2248 ftype[i] = DTK_TZ;
2249 break;
2250
2251 case IGNORE_DTF:
2252 break;
2253
2254 case AMPM:
2255 mer = val;
2256 break;
2257
2258 case ADBC:
2259 bc = (val == BC);
2260 break;
2261
2262 case DOW:
2263 tm->tm_wday = val;
2264 break;
2265
2266 case UNITS:
2267 tmask = 0;
2268 ptype = val;
2269 break;
2270
2271 case ISOTIME:
2272
2273 /*
2274 * This is a filler field "t" indicating that the next
2275 * field is time. Try to verify that this is sensible.
2276 */
2277 tmask = 0;
2278
2279 /* No preceding date? Then quit... */
2280 if ((fmask & DTK_DATE_M) != DTK_DATE_M)
2281 return -1;
2282
2283 /***
2284 * We will need one of the following fields:
2285 * DTK_NUMBER should be hhmmss.fff
2286 * DTK_TIME should be hh:mm:ss.fff
2287 * DTK_DATE should be hhmmss-zz
2288 ***/
2289 if (i >= nf - 1 ||
2290 (ftype[i + 1] != DTK_NUMBER &&
2291 ftype[i + 1] != DTK_TIME &&
2292 ftype[i + 1] != DTK_DATE))
2293 return -1;
2294
2295 ptype = val;
2296 break;
2297
2298 default:
2299 return -1;
2300 }
2301 break;
2302
2303 default:
2304 return -1;
2305 }
2306
2307 if (tmask & fmask)
2308 return -1;
2309 fmask |= tmask;
2310 }
2311
2312 /* there is no year zero in AD/BC notation; i.e. "1 BC" == year 0 */
2313 if (bc)
2314 {
2315 if (tm->tm_year > 0)
2316 tm->tm_year = -(tm->tm_year - 1);
2317 else
2318 return -1;
2319 }
2320 else if (is2digits)
2321 {
2322 if (tm->tm_year < 70)
2323 tm->tm_year += 2000;
2324 else if (tm->tm_year < 100)
2325 tm->tm_year += 1900;
2326 }
2327
2328 if (mer != HR24 && tm->tm_hour > 12)
2329 return -1;
2330 if (mer == AM && tm->tm_hour == 12)
2331 tm->tm_hour = 0;
2332 else if (mer == PM && tm->tm_hour != 12)
2333 tm->tm_hour += 12;
2334
2335 /* do additional checking for full date specs... */
2336 if (*dtype == DTK_DATE)
2337 {
2338 if ((fmask & DTK_DATE_M) != DTK_DATE_M)
2339 return ((fmask & DTK_TIME_M) == DTK_TIME_M) ? 1 : -1;
2340
2341 /*
2342 * check for valid day of month and month, now that we know for sure
2343 * the month and year...
2344 */
2345 if (tm->tm_mon < 1 || tm->tm_mday < 1 || tm->tm_mday > day_tab[isleap(tm->tm_year)][tm->tm_mon - 1])
2346 return -1;
2347
2348 /*
2349 * backend tried to find local timezone here but we don't use the
2350 * result afterwards anyway so we only check for this error: daylight
2351 * savings time modifier but no standard timezone?
2352 */
2353 if ((fmask & DTK_DATE_M) == DTK_DATE_M && tzp != NULL && !(fmask & DTK_M(TZ)) && (fmask & DTK_M(DTZMOD)))
2354 return -1;
2355 }
2356
2357 return 0;
2358} /* DecodeDateTime() */
#define USECS_PER_DAY
Definition timestamp.h:131
const int day_tab[2][13]
Definition dt_common.c:14
void GetCurrentDateTime(struct tm *tm)
Definition dt_common.c:1064
static int DecodeTimezone(char *str, int *tzp)
Definition dt_common.c:1511
static int DecodePosixTimezone(char *str, int *tzp)
Definition dt_common.c:1557
int DecodeTime(char *str, int *tmask, struct tm *tm, fsec_t *fsec)
Definition dt_common.c:1445
int date2j(int y, int m, int d)
Definition dt_common.c:582
void j2date(int jd, int *year, int *month, int *day)
Definition dt_common.c:607
static int DecodeNumberField(int len, char *str, int fmask, int *tmask, struct tm *tm, fsec_t *fsec, bool *is2digits)
Definition dt_common.c:1094
void dt2time(double jd, int *hour, int *min, int *sec, fsec_t *fsec)
Definition dt_common.c:1072
static int DecodeDate(char *str, int fmask, int *tmask, struct tm *tm, bool EuroDates)
Definition dt_common.c:1315
#define DTK_TOMORROW
Definition datetime.h:156
#define DTK_SPECIAL
Definition datetime.h:149
#define AMPM
Definition datetime.h:99
#define DTK_TIME
Definition datetime.h:145
#define DTK_SECOND
Definition datetime.h:160
#define PM
Definition datetime.h:72
#define DTK_NUMBER
Definition datetime.h:141
#define DTK_STRING
Definition datetime.h:142
#define DTK_JULIAN
Definition datetime.h:173
#define DTK_TIME_M
Definition datetime.h:192
#define HOUR
Definition datetime.h:100
#define DAY
Definition datetime.h:93
#define YEAR
Definition datetime.h:92
#define DTK_DATE
Definition datetime.h:144
#define DTK_DAY
Definition datetime.h:163
#define RESERV
Definition datetime.h:90
#define HR24
Definition datetime.h:73
#define SECOND
Definition datetime.h:102
#define isleap(y)
Definition datetime.h:273
#define DTZMOD
Definition datetime.h:122
#define DTK_TZ
Definition datetime.h:146
#define DOW
Definition datetime.h:106
#define ISOTIME
Definition datetime.h:115
#define DTK_HOUR
Definition datetime.h:162
#define MINUTE
Definition datetime.h:101
#define DTZ
Definition datetime.h:96
#define DTK_YEAR
Definition datetime.h:167
#define AM
Definition datetime.h:71
#define DTK_MONTH
Definition datetime.h:165
#define DTK_YESTERDAY
Definition datetime.h:154
#define DTK_ZULU
Definition datetime.h:157
#define DTK_MINUTE
Definition datetime.h:161
#define UNITS
Definition datetime.h:107
#define DTK_TODAY
Definition datetime.h:155
#define DTK_NOW
Definition datetime.h:153
static struct @177 value
int strtoint(const char *pg_restrict str, char **pg_restrict endptr, int base)
Definition string.c:50
int tm_wday
Definition pgtime.h:42

References ADBC, AM, AMPM, BC, date2j(), DAY, day_tab, DecodeDate(), DecodeNumber(), DecodeNumberField(), DecodePosixTimezone(), DecodeSpecial(), DecodeTime(), DecodeTimezone(), DOW, dt2time(), DTK_DATE, DTK_DATE_M, DTK_DAY, DTK_HOUR, DTK_JULIAN, DTK_M, DTK_MINUTE, DTK_MONTH, DTK_NOW, DTK_NUMBER, DTK_SECOND, DTK_SPECIAL, DTK_STRING, DTK_TIME, DTK_TIME_M, DTK_TODAY, DTK_TOMORROW, DTK_TZ, DTK_YEAR, DTK_YESTERDAY, DTK_ZULU, DTZ, DTZMOD, fb(), GetCurrentDateTime(), HOUR, HR24, i, IGNORE_DTF, isleap, ISOTIME, j2date(), MINUTE, MONTH, PM, RESERV, SECOND, strtoint(), tm, pg_tm::tm_hour, pg_tm::tm_isdst, pg_tm::tm_mday, pg_tm::tm_min, pg_tm::tm_mon, pg_tm::tm_sec, pg_tm::tm_wday, pg_tm::tm_year, type, TZ, UNITS, USECS_PER_DAY, val, value, and YEAR.

◆ DecodeNumber()

static int DecodeNumber ( int  flen,
char str,
int  fmask,
int tmask,
struct tm tm,
fsec_t fsec,
bool is2digits,
bool  EuroDates 
)
static

Definition at line 1205 of file dt_common.c.

1207{
1208 int val;
1209 char *cp;
1210
1211 *tmask = 0;
1212
1213 val = strtoint(str, &cp, 10);
1214 if (cp == str)
1215 return -1;
1216
1217 if (*cp == '.')
1218 {
1219 /*
1220 * More than two digits? Then could be a date or a run-together time:
1221 * 2001.360 20011225 040506.789
1222 */
1223 if (cp - str > 2)
1225 tmask, tm, fsec, is2digits);
1226
1227 *fsec = strtod(cp, &cp);
1228 if (*cp != '\0')
1229 return -1;
1230 }
1231 else if (*cp != '\0')
1232 return -1;
1233
1234 /* Special case day of year? */
1235 if (flen == 3 && (fmask & DTK_M(YEAR)) && val >= 1 && val <= 366)
1236 {
1237 *tmask = (DTK_M(DOY) | DTK_M(MONTH) | DTK_M(DAY));
1238 tm->tm_yday = val;
1239 j2date(date2j(tm->tm_year, 1, 1) + tm->tm_yday - 1,
1240 &tm->tm_year, &tm->tm_mon, &tm->tm_mday);
1241 }
1242
1243 /***
1244 * Enough digits to be unequivocal year? Used to test for 4 digits or
1245 * more, but we now test first for a three-digit doy so anything
1246 * bigger than two digits had better be an explicit year.
1247 * - thomas 1999-01-09
1248 * Back to requiring a 4 digit year. We accept a two digit
1249 * year farther down. - thomas 2000-03-28
1250 ***/
1251 else if (flen >= 4)
1252 {
1253 *tmask = DTK_M(YEAR);
1254
1255 /* already have a year? then see if we can substitute... */
1256 if ((fmask & DTK_M(YEAR)) && !(fmask & DTK_M(DAY)) &&
1257 tm->tm_year >= 1 && tm->tm_year <= 31)
1258 {
1259 tm->tm_mday = tm->tm_year;
1260 *tmask = DTK_M(DAY);
1261 }
1262
1263 tm->tm_year = val;
1264 }
1265
1266 /* already have year? then could be month */
1267 else if ((fmask & DTK_M(YEAR)) && !(fmask & DTK_M(MONTH)) && val >= 1 && val <= MONTHS_PER_YEAR)
1268 {
1269 *tmask = DTK_M(MONTH);
1270 tm->tm_mon = val;
1271 }
1272 /* no year and EuroDates enabled? then could be day */
1273 else if ((EuroDates || (fmask & DTK_M(MONTH))) &&
1274 !(fmask & DTK_M(YEAR)) && !(fmask & DTK_M(DAY)) &&
1275 val >= 1 && val <= 31)
1276 {
1277 *tmask = DTK_M(DAY);
1278 tm->tm_mday = val;
1279 }
1280 else if (!(fmask & DTK_M(MONTH)) && val >= 1 && val <= MONTHS_PER_YEAR)
1281 {
1282 *tmask = DTK_M(MONTH);
1283 tm->tm_mon = val;
1284 }
1285 else if (!(fmask & DTK_M(DAY)) && val >= 1 && val <= 31)
1286 {
1287 *tmask = DTK_M(DAY);
1288 tm->tm_mday = val;
1289 }
1290
1291 /*
1292 * Check for 2 or 4 or more digits, but currently we reach here only if
1293 * two digits. - thomas 2000-03-28
1294 */
1295 else if (!(fmask & DTK_M(YEAR)) && (flen >= 4 || flen == 2))
1296 {
1297 *tmask = DTK_M(YEAR);
1298 tm->tm_year = val;
1299
1300 /* adjust ONLY if exactly two digits... */
1301 *is2digits = (flen == 2);
1302 }
1303 else
1304 return -1;
1305
1306 return 0;
1307} /* DecodeNumber() */
#define MONTHS_PER_YEAR
Definition timestamp.h:108
int tm_yday
Definition pgtime.h:43

References date2j(), DAY, DecodeNumberField(), DOY, DTK_DATE_M, DTK_M, fb(), j2date(), MONTH, MONTHS_PER_YEAR, str, strtoint(), tm, pg_tm::tm_mday, pg_tm::tm_mon, pg_tm::tm_yday, pg_tm::tm_year, val, and YEAR.

Referenced by DecodeDate(), and DecodeDateTime().

◆ DecodeNumberField()

static int DecodeNumberField ( int  len,
char str,
int  fmask,
int tmask,
struct tm tm,
fsec_t fsec,
bool is2digits 
)
static

Definition at line 1094 of file dt_common.c.

1096{
1097 char *cp;
1098
1099 /*
1100 * Have a decimal point? Then this is a date or something with a seconds
1101 * field...
1102 */
1103 if ((cp = strchr(str, '.')) != NULL)
1104 {
1105 char fstr[7];
1106 int i;
1107
1108 cp++;
1109
1110 /*
1111 * OK, we have at most six digits to care about. Let's construct a
1112 * string with those digits, zero-padded on the right, and then do the
1113 * conversion to an integer.
1114 *
1115 * XXX This truncates the seventh digit, unlike rounding it as the
1116 * backend does.
1117 */
1118 for (i = 0; i < 6; i++)
1119 fstr[i] = *cp != '\0' ? *cp++ : '0';
1120 fstr[i] = '\0';
1121 *fsec = strtoint(fstr, NULL, 10);
1122 *cp = '\0';
1123 len = strlen(str);
1124 }
1125 /* No decimal point and no complete date yet? */
1126 else if ((fmask & DTK_DATE_M) != DTK_DATE_M)
1127 {
1128 /* yyyymmdd? */
1129 if (len == 8)
1130 {
1131 *tmask = DTK_DATE_M;
1132
1133 tm->tm_mday = atoi(str + 6);
1134 *(str + 6) = '\0';
1135 tm->tm_mon = atoi(str + 4);
1136 *(str + 4) = '\0';
1137 tm->tm_year = atoi(str + 0);
1138
1139 return DTK_DATE;
1140 }
1141 /* yymmdd? */
1142 else if (len == 6)
1143 {
1144 *tmask = DTK_DATE_M;
1145 tm->tm_mday = atoi(str + 4);
1146 *(str + 4) = '\0';
1147 tm->tm_mon = atoi(str + 2);
1148 *(str + 2) = '\0';
1149 tm->tm_year = atoi(str + 0);
1150 *is2digits = true;
1151
1152 return DTK_DATE;
1153 }
1154 /* yyddd? */
1155 else if (len == 5)
1156 {
1157 *tmask = DTK_DATE_M;
1158 tm->tm_mday = atoi(str + 2);
1159 *(str + 2) = '\0';
1160 tm->tm_mon = 1;
1161 tm->tm_year = atoi(str + 0);
1162 *is2digits = true;
1163
1164 return DTK_DATE;
1165 }
1166 }
1167
1168 /* not all time fields are specified? */
1169 if ((fmask & DTK_TIME_M) != DTK_TIME_M)
1170 {
1171 /* hhmmss */
1172 if (len == 6)
1173 {
1174 *tmask = DTK_TIME_M;
1175 tm->tm_sec = atoi(str + 4);
1176 *(str + 4) = '\0';
1177 tm->tm_min = atoi(str + 2);
1178 *(str + 2) = '\0';
1179 tm->tm_hour = atoi(str + 0);
1180
1181 return DTK_TIME;
1182 }
1183 /* hhmm? */
1184 else if (len == 4)
1185 {
1186 *tmask = DTK_TIME_M;
1187 tm->tm_sec = 0;
1188 tm->tm_min = atoi(str + 2);
1189 *(str + 2) = '\0';
1190 tm->tm_hour = atoi(str + 0);
1191
1192 return DTK_TIME;
1193 }
1194 }
1195
1196 return -1;
1197} /* DecodeNumberField() */

References DTK_DATE, DTK_DATE_M, DTK_TIME, DTK_TIME_M, fb(), i, len, str, strtoint(), tm, pg_tm::tm_hour, pg_tm::tm_mday, pg_tm::tm_min, pg_tm::tm_mon, pg_tm::tm_sec, and pg_tm::tm_year.

Referenced by DecodeDateTime(), and DecodeNumber().

◆ DecodePosixTimezone()

static int DecodePosixTimezone ( char str,
int tzp 
)
static

Definition at line 1557 of file dt_common.c.

1558{
1559 int val,
1560 tz;
1561 int type;
1562 char *cp;
1563 char delim;
1564
1565 cp = str;
1566 while (*cp != '\0' && isalpha((unsigned char) *cp))
1567 cp++;
1568
1569 if (DecodeTimezone(cp, &tz) != 0)
1570 return -1;
1571
1572 delim = *cp;
1573 *cp = '\0';
1575 *cp = delim;
1576
1577 switch (type)
1578 {
1579 case DTZ:
1580 case TZ:
1581 *tzp = -(val + tz);
1582 break;
1583
1584 default:
1585 return -1;
1586 }
1587
1588 return 0;
1589} /* DecodePosixTimezone() */

References DecodeSpecial(), DecodeTimezone(), DTZ, fb(), MAXDATEFIELDS, str, type, TZ, and val.

Referenced by DecodeDateTime().

◆ DecodeSpecial()

static int DecodeSpecial ( int  field,
char lowtoken,
int val 
)
static

Definition at line 637 of file dt_common.c.

638{
639 int type;
640 const datetkn *tp;
641
642 /* use strncmp so that we match truncated tokens */
643 if (datecache[field] != NULL &&
644 strncmp(lowtoken, datecache[field]->token, TOKMAXLEN) == 0)
645 tp = datecache[field];
646 else
647 {
648 tp = NULL;
649 if (!tp)
651 }
652 datecache[field] = tp;
653 if (tp == NULL)
654 {
656 *val = 0;
657 }
658 else
659 {
660 type = tp->type;
661 *val = tp->value;
662 }
663
664 return type;
665} /* DecodeSpecial() */
static const datetkn datetktbl[]
Definition dt_common.c:20
static const unsigned int szdatetktbl
Definition dt_common.c:486
static const datetkn * datecache[MAXDATEFIELDS]
Definition dt_common.c:489
static const datetkn * datebsearch(const char *key, const datetkn *base, unsigned int nel)
Definition dt_common.c:502
#define UNKNOWN_FIELD
Definition datetime.h:124
int32 value
Definition datetime.h:211
char type
Definition datetime.h:210

References datebsearch(), datecache, datetktbl, fb(), szdatetktbl, TOKMAXLEN, type, datetkn::type, UNKNOWN_FIELD, val, and datetkn::value.

Referenced by DecodeDate(), DecodeDateTime(), and DecodePosixTimezone().

◆ DecodeTime()

int DecodeTime ( char str,
int tmask,
struct tm tm,
fsec_t fsec 
)

Definition at line 1445 of file dt_common.c.

1446{
1447 char *cp;
1448
1449 *tmask = DTK_TIME_M;
1450
1451 tm->tm_hour = strtoint(str, &cp, 10);
1452 if (*cp != ':')
1453 return -1;
1454 str = cp + 1;
1455 tm->tm_min = strtoint(str, &cp, 10);
1456 if (*cp == '\0')
1457 {
1458 tm->tm_sec = 0;
1459 *fsec = 0;
1460 }
1461 else if (*cp != ':')
1462 return -1;
1463 else
1464 {
1465 str = cp + 1;
1466 tm->tm_sec = strtoint(str, &cp, 10);
1467 if (*cp == '\0')
1468 *fsec = 0;
1469 else if (*cp == '.')
1470 {
1471 char fstr[7];
1472 int i;
1473
1474 cp++;
1475
1476 /*
1477 * OK, we have at most six digits to care about. Let's construct a
1478 * string with those digits, zero-padded on the right, and then do
1479 * the conversion to an integer.
1480 *
1481 * XXX This truncates the seventh digit, unlike rounding it as the
1482 * backend does.
1483 */
1484 for (i = 0; i < 6; i++)
1485 fstr[i] = *cp != '\0' ? *cp++ : '0';
1486 fstr[i] = '\0';
1487 *fsec = strtoint(fstr, &cp, 10);
1488 if (*cp != '\0')
1489 return -1;
1490 }
1491 else
1492 return -1;
1493 }
1494
1495 /* do a sanity check */
1496 if (tm->tm_hour < 0 || tm->tm_min < 0 || tm->tm_min > 59 ||
1497 tm->tm_sec < 0 || tm->tm_sec > 59 || *fsec >= USECS_PER_SEC)
1498 return -1;
1499
1500 return 0;
1501} /* DecodeTime() */
#define USECS_PER_SEC
Definition timestamp.h:134

References DTK_TIME_M, fb(), i, str, strtoint(), tm, pg_tm::tm_hour, pg_tm::tm_min, pg_tm::tm_sec, and USECS_PER_SEC.

Referenced by DecodeDateTime().

◆ DecodeTimezone()

static int DecodeTimezone ( char str,
int tzp 
)
static

Definition at line 1511 of file dt_common.c.

1512{
1513 int tz;
1514 int hr,
1515 min;
1516 char *cp;
1517 int len;
1518
1519 /* assume leading character is "+" or "-" */
1520 hr = strtoint(str + 1, &cp, 10);
1521
1522 /* explicit delimiter? */
1523 if (*cp == ':')
1524 min = strtoint(cp + 1, &cp, 10);
1525 /* otherwise, might have run things together... */
1526 else if (*cp == '\0' && (len = strlen(str)) > 3)
1527 {
1528 min = strtoint(str + len - 2, &cp, 10);
1529 if (min < 0 || min >= 60)
1530 return -1;
1531
1532 *(str + len - 2) = '\0';
1533 hr = strtoint(str + 1, &cp, 10);
1534 if (hr < 0 || hr > 13)
1535 return -1;
1536 }
1537 else
1538 min = 0;
1539
1540 tz = (hr * MINS_PER_HOUR + min) * SECS_PER_MINUTE;
1541 if (*str == '-')
1542 tz = -tz;
1543
1544 *tzp = -tz;
1545 return *cp != '\0';
1546} /* DecodeTimezone() */
#define MINS_PER_HOUR
Definition timestamp.h:129
#define SECS_PER_MINUTE
Definition timestamp.h:128

References fb(), len, MINS_PER_HOUR, SECS_PER_MINUTE, str, and strtoint().

Referenced by DecodeDateTime(), DecodePosixTimezone(), and PGTYPEStimestamp_defmt_scan().

◆ DecodeUnits()

int DecodeUnits ( int  field,
char lowtoken,
int val 
)

Definition at line 537 of file dt_common.c.

538{
539 int type;
540 const datetkn *tp;
541
542 /* use strncmp so that we match truncated tokens */
543 if (deltacache[field] != NULL &&
544 strncmp(lowtoken, deltacache[field]->token, TOKMAXLEN) == 0)
545 tp = deltacache[field];
546 else
548 deltacache[field] = tp;
549 if (tp == NULL)
550 {
552 *val = 0;
553 }
554 else
555 {
556 type = tp->type;
557 *val = tp->value;
558 }
559
560 return type;
561} /* DecodeUnits() */
static const datetkn * deltacache[MAXDATEFIELDS]
Definition dt_common.c:491
static const unsigned int szdeltatktbl
Definition dt_common.c:487
static const datetkn deltatktbl[]
Definition dt_common.c:421

References datebsearch(), deltacache, deltatktbl, fb(), szdeltatktbl, TOKMAXLEN, type, datetkn::type, UNKNOWN_FIELD, val, and datetkn::value.

◆ dt2time()

void dt2time ( double  jd,
int hour,
int min,
int sec,
fsec_t fsec 
)

Definition at line 1072 of file dt_common.c.

1073{
1074 int64 time;
1075
1076 time = jd;
1077 *hour = time / USECS_PER_HOUR;
1078 time -= (*hour) * USECS_PER_HOUR;
1079 *min = time / USECS_PER_MINUTE;
1080 time -= (*min) * USECS_PER_MINUTE;
1081 *sec = time / USECS_PER_SEC;
1082 *fsec = time - (*sec * USECS_PER_SEC);
1083} /* dt2time() */
int64_t int64
Definition c.h:621
#define USECS_PER_HOUR
Definition timestamp.h:132
#define USECS_PER_MINUTE
Definition timestamp.h:133

References fb(), USECS_PER_HOUR, USECS_PER_MINUTE, and USECS_PER_SEC.

Referenced by DecodeDateTime().

◆ EncodeDateOnly()

void EncodeDateOnly ( struct tm tm,
int  style,
char str,
bool  EuroDates 
)

Definition at line 672 of file dt_common.c.

673{
674 Assert(tm->tm_mon >= 1 && tm->tm_mon <= MONTHS_PER_YEAR);
675
676 switch (style)
677 {
678 case USE_ISO_DATES:
679 /* compatible with ISO date formats */
680 if (tm->tm_year > 0)
681 sprintf(str, "%04d-%02d-%02d",
683 else
684 sprintf(str, "%04d-%02d-%02d %s",
685 -(tm->tm_year - 1), tm->tm_mon, tm->tm_mday, "BC");
686 break;
687
688 case USE_SQL_DATES:
689 /* compatible with Oracle/Ingres date formats */
690 if (EuroDates)
691 sprintf(str, "%02d/%02d", tm->tm_mday, tm->tm_mon);
692 else
693 sprintf(str, "%02d/%02d", tm->tm_mon, tm->tm_mday);
694 if (tm->tm_year > 0)
695 sprintf(str + 5, "/%04d", tm->tm_year);
696 else
697 sprintf(str + 5, "/%04d %s", -(tm->tm_year - 1), "BC");
698 break;
699
700 case USE_GERMAN_DATES:
701 /* German-style date format */
702 sprintf(str, "%02d.%02d", tm->tm_mday, tm->tm_mon);
703 if (tm->tm_year > 0)
704 sprintf(str + 5, ".%04d", tm->tm_year);
705 else
706 sprintf(str + 5, ".%04d %s", -(tm->tm_year - 1), "BC");
707 break;
708
710 default:
711 /* traditional date-only style for Postgres */
712 if (EuroDates)
713 sprintf(str, "%02d-%02d", tm->tm_mday, tm->tm_mon);
714 else
715 sprintf(str, "%02d-%02d", tm->tm_mon, tm->tm_mday);
716 if (tm->tm_year > 0)
717 sprintf(str + 5, "-%04d", tm->tm_year);
718 else
719 sprintf(str + 5, "-%04d %s", -(tm->tm_year - 1), "BC");
720 break;
721 }
722}
#define Assert(condition)
Definition c.h:943
#define USE_SQL_DATES
Definition miscadmin.h:241
#define USE_POSTGRES_DATES
Definition miscadmin.h:239
#define USE_ISO_DATES
Definition miscadmin.h:240
#define USE_GERMAN_DATES
Definition miscadmin.h:242
#define sprintf
Definition port.h:263

References Assert, fb(), MONTHS_PER_YEAR, sprintf, str, tm, pg_tm::tm_mday, pg_tm::tm_mon, pg_tm::tm_year, USE_GERMAN_DATES, USE_ISO_DATES, USE_POSTGRES_DATES, and USE_SQL_DATES.

◆ EncodeDateTime()

void EncodeDateTime ( struct tm tm,
fsec_t  fsec,
bool  print_tz,
int  tz,
const char tzn,
int  style,
char str,
bool  EuroDates 
)

Definition at line 757 of file dt_common.c.

758{
759 int day,
760 hour,
761 min;
762
763 /*
764 * Negative tm_isdst means we have no valid time zone translation.
765 */
766 if (tm->tm_isdst < 0)
767 print_tz = false;
768
769 switch (style)
770 {
771 case USE_ISO_DATES:
772 /* Compatible with ISO-8601 date formats */
773
774 sprintf(str, "%04d-%02d-%02d %02d:%02d",
775 (tm->tm_year > 0) ? tm->tm_year : -(tm->tm_year - 1),
776 tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min);
777
778 /*
779 * Print fractional seconds if any. The field widths here should
780 * be at least equal to MAX_TIMESTAMP_PRECISION.
781 */
782 if (fsec != 0)
783 {
784 sprintf(str + strlen(str), ":%02d.%06d", tm->tm_sec, fsec);
786 }
787 else
788 sprintf(str + strlen(str), ":%02d", tm->tm_sec);
789
790 if (tm->tm_year <= 0)
791 sprintf(str + strlen(str), " BC");
792
793 if (print_tz)
794 {
795 hour = -(tz / SECS_PER_HOUR);
796 min = (abs(tz) / MINS_PER_HOUR) % MINS_PER_HOUR;
797 if (min != 0)
798 sprintf(str + strlen(str), "%+03d:%02d", hour, min);
799 else
800 sprintf(str + strlen(str), "%+03d", hour);
801 }
802 break;
803
804 case USE_SQL_DATES:
805 /* Compatible with Oracle/Ingres date formats */
806
807 if (EuroDates)
808 sprintf(str, "%02d/%02d", tm->tm_mday, tm->tm_mon);
809 else
810 sprintf(str, "%02d/%02d", tm->tm_mon, tm->tm_mday);
811
812 sprintf(str + 5, "/%04d %02d:%02d",
813 (tm->tm_year > 0) ? tm->tm_year : -(tm->tm_year - 1),
814 tm->tm_hour, tm->tm_min);
815
816 /*
817 * Print fractional seconds if any. The field widths here should
818 * be at least equal to MAX_TIMESTAMP_PRECISION.
819 */
820 if (fsec != 0)
821 {
822 sprintf(str + strlen(str), ":%02d.%06d", tm->tm_sec, fsec);
824 }
825 else
826 sprintf(str + strlen(str), ":%02d", tm->tm_sec);
827
828 if (tm->tm_year <= 0)
829 sprintf(str + strlen(str), " BC");
830
831 /*
832 * Note: the uses of %.*s in this function would be risky if the
833 * timezone names ever contain non-ASCII characters, since we are
834 * not being careful to do encoding-aware clipping. However, all
835 * TZ abbreviations in the IANA database are plain ASCII.
836 */
837
838 if (print_tz)
839 {
840 if (tzn)
841 sprintf(str + strlen(str), " %.*s", MAXTZLEN, tzn);
842 else
843 {
844 hour = -(tz / SECS_PER_HOUR);
845 min = (abs(tz) / MINS_PER_HOUR) % MINS_PER_HOUR;
846 if (min != 0)
847 sprintf(str + strlen(str), "%+03d:%02d", hour, min);
848 else
849 sprintf(str + strlen(str), "%+03d", hour);
850 }
851 }
852 break;
853
854 case USE_GERMAN_DATES:
855 /* German variant on European style */
856
857 sprintf(str, "%02d.%02d", tm->tm_mday, tm->tm_mon);
858
859 sprintf(str + 5, ".%04d %02d:%02d",
860 (tm->tm_year > 0) ? tm->tm_year : -(tm->tm_year - 1),
861 tm->tm_hour, tm->tm_min);
862
863 /*
864 * Print fractional seconds if any. The field widths here should
865 * be at least equal to MAX_TIMESTAMP_PRECISION.
866 */
867 if (fsec != 0)
868 {
869 sprintf(str + strlen(str), ":%02d.%06d", tm->tm_sec, fsec);
871 }
872 else
873 sprintf(str + strlen(str), ":%02d", tm->tm_sec);
874
875 if (tm->tm_year <= 0)
876 sprintf(str + strlen(str), " BC");
877
878 if (print_tz)
879 {
880 if (tzn)
881 sprintf(str + strlen(str), " %.*s", MAXTZLEN, tzn);
882 else
883 {
884 hour = -(tz / SECS_PER_HOUR);
885 min = (abs(tz) / MINS_PER_HOUR) % MINS_PER_HOUR;
886 if (min != 0)
887 sprintf(str + strlen(str), "%+03d:%02d", hour, min);
888 else
889 sprintf(str + strlen(str), "%+03d", hour);
890 }
891 }
892 break;
893
895 default:
896 /* Backward-compatible with traditional Postgres abstime dates */
897
898 day = date2j(tm->tm_year, tm->tm_mon, tm->tm_mday);
899 tm->tm_wday = (int) ((day + date2j(2000, 1, 1) + 1) % 7);
900
901 memcpy(str, days[tm->tm_wday], 3);
902 strcpy(str + 3, " ");
903
904 if (EuroDates)
905 sprintf(str + 4, "%02d %3s", tm->tm_mday, months[tm->tm_mon - 1]);
906 else
907 sprintf(str + 4, "%3s %02d", months[tm->tm_mon - 1], tm->tm_mday);
908
909 sprintf(str + 10, " %02d:%02d", tm->tm_hour, tm->tm_min);
910
911 /*
912 * Print fractional seconds if any. The field widths here should
913 * be at least equal to MAX_TIMESTAMP_PRECISION.
914 */
915 if (fsec != 0)
916 {
917 sprintf(str + strlen(str), ":%02d.%06d", tm->tm_sec, fsec);
919 }
920 else
921 sprintf(str + strlen(str), ":%02d", tm->tm_sec);
922
923 sprintf(str + strlen(str), " %04d",
924 (tm->tm_year > 0) ? tm->tm_year : -(tm->tm_year - 1));
925 if (tm->tm_year <= 0)
926 sprintf(str + strlen(str), " BC");
927
928 if (print_tz)
929 {
930 if (tzn)
931 sprintf(str + strlen(str), " %.*s", MAXTZLEN, tzn);
932 else
933 {
934 /*
935 * We have a time zone, but no string version. Use the
936 * numeric form, but be sure to include a leading space to
937 * avoid formatting something which would be rejected by
938 * the date/time parser later. - thomas 2001-10-19
939 */
940 hour = -(tz / SECS_PER_HOUR);
941 min = (abs(tz) / MINS_PER_HOUR) % MINS_PER_HOUR;
942 if (min != 0)
943 sprintf(str + strlen(str), " %+03d:%02d", hour, min);
944 else
945 sprintf(str + strlen(str), " %+03d", hour);
946 }
947 }
948 break;
949 }
950}
memcpy(sums, checksumBaseOffsets, sizeof(checksumBaseOffsets))
char * days[]
Definition dt_common.c:495
void TrimTrailingZeros(char *str)
Definition dt_common.c:725
char * months[]
Definition dt_common.c:493

References date2j(), days, fb(), MAXTZLEN, memcpy(), MINS_PER_HOUR, months, SECS_PER_HOUR, sprintf, str, tm, pg_tm::tm_hour, pg_tm::tm_isdst, pg_tm::tm_mday, pg_tm::tm_min, pg_tm::tm_mon, pg_tm::tm_sec, pg_tm::tm_wday, pg_tm::tm_year, TrimTrailingZeros(), USE_GERMAN_DATES, USE_ISO_DATES, USE_POSTGRES_DATES, and USE_SQL_DATES.

◆ find_end_token()

static char * find_end_token ( char str,
char fmt 
)
static

Definition at line 2368 of file dt_common.c.

2369{
2370 /*
2371 * str: here is28the day12the hour fmt: here is%dthe day%hthe hour
2372 *
2373 * we extract the 28, we read the percent sign and the type "d" then this
2374 * functions gets called as find_end_token("28the day12the hour", "the
2375 * day%hthehour")
2376 *
2377 * fmt points to "the day%hthehour", next_percent points to %hthehour and
2378 * we have to find a match for everything between these positions ("the
2379 * day"). We look for "the day" in str and know that the pattern we are
2380 * about to scan ends where this string starts (right after the "28")
2381 *
2382 * At the end, *fmt is '\0' and *str isn't. end_position then is
2383 * unchanged.
2384 */
2385 char *end_position = NULL;
2386 char *next_percent,
2388 int scan_offset = 0;
2389 char last_char;
2390
2391 /* are we at the end? */
2392 if (!*fmt)
2393 {
2394 end_position = fmt;
2395 return end_position;
2396 }
2397
2398 /* not at the end */
2399 while (fmt[scan_offset] == '%' && fmt[scan_offset + 1])
2400 {
2401 /*
2402 * there is no delimiter, skip to the next delimiter if we're reading
2403 * a number and then something that is not a number "9:15pm", we might
2404 * be able to recover with the strtol end pointer. Go for the next
2405 * percent sign
2406 */
2407 scan_offset += 2;
2408 }
2410 if (next_percent)
2411 {
2412 /*
2413 * we don't want to allocate extra memory, so we temporarily set the
2414 * '%' sign to '\0' and call strstr However since we allow whitespace
2415 * to float around everything, we have to shorten the pattern until we
2416 * reach a non-whitespace character
2417 */
2418
2420 while (*(subst_location - 1) == ' ' && subst_location - 1 > fmt + scan_offset)
2423 *subst_location = '\0';
2424
2425 /*
2426 * the haystack is the str and the needle is the original fmt but it
2427 * ends at the position where the next percent sign would be
2428 */
2429
2430 /*
2431 * There is one special case. Imagine: str = " 2", fmt = "%d %...",
2432 * since we want to allow blanks as "dynamic" padding we have to
2433 * accept this. Now, we are called with a fmt of " %..." and look for
2434 * " " in str. We find it at the first position and never read the
2435 * 2...
2436 */
2437 while (*str == ' ')
2438 str++;
2441 }
2442 else
2443 {
2444 /*
2445 * there is no other percent sign. So everything up to the end has to
2446 * match.
2447 */
2449 }
2450 if (!end_position)
2451 {
2452 /*
2453 * maybe we have the following case:
2454 *
2455 * str = "4:15am" fmt = "%M:%S %p"
2456 *
2457 * at this place we could have
2458 *
2459 * str = "15am" fmt = " %p"
2460 *
2461 * and have set fmt to " " because overwrote the % sign with a NULL
2462 *
2463 * In this case where we would have to match a space but can't find
2464 * it, set end_position to the end of the string
2465 */
2466 if ((fmt + scan_offset)[0] == ' ' && fmt + scan_offset + 1 == subst_location)
2468 }
2469 return end_position;
2470}

References fb(), and str.

Referenced by pgtypes_defmt_scan().

◆ GetCurrentDateTime()

void GetCurrentDateTime ( struct tm tm)

Definition at line 1064 of file dt_common.c.

1065{
1066 int tz;
1067
1068 abstime2tm(time(NULL), &tz, tm, NULL);
1069}
static void abstime2tm(AbsoluteTime _time, int *tzp, struct tm *tm, char **tzn)
Definition dt_common.c:977

References abstime2tm(), fb(), and tm.

Referenced by DecodeDateTime().

◆ GetEpochTime()

int GetEpochTime ( struct tm tm)

Definition at line 953 of file dt_common.c.

954{
955 struct tm *t0;
956 struct tm tmbuf;
957 time_t epoch = 0;
958
959 t0 = gmtime_r(&epoch, &tmbuf);
960
961 if (t0)
962 {
963 tm->tm_year = t0->tm_year + 1900;
964 tm->tm_mon = t0->tm_mon + 1;
965 tm->tm_mday = t0->tm_mday;
966 tm->tm_hour = t0->tm_hour;
967 tm->tm_min = t0->tm_min;
968 tm->tm_sec = t0->tm_sec;
969
970 return 0;
971 }
972
973 return -1;
974} /* GetEpochTime() */
static const unsigned __int64 epoch

References epoch, fb(), tm, pg_tm::tm_hour, pg_tm::tm_mday, pg_tm::tm_min, pg_tm::tm_mon, pg_tm::tm_sec, and pg_tm::tm_year.

◆ j2date()

void j2date ( int  jd,
int year,
int month,
int day 
)

Definition at line 607 of file dt_common.c.

608{
609 unsigned int julian;
610 unsigned int quad;
611 unsigned int extra;
612 int y;
613
614 julian = jd;
615 julian += 32044;
616 quad = julian / 146097;
617 extra = (julian - quad * 146097) * 4 + 3;
618 julian += 60 + quad * 3 + extra / 146097;
619 quad = julian / 1461;
620 julian -= quad * 1461;
621 y = julian * 4 / 1461;
622 julian = ((y != 0) ? (julian + 305) % 365 : (julian + 306) % 366) + 123;
623 y += quad * 4;
624 *year = y - 4800;
625 quad = julian * 2141 / 65536;
626 *day = julian - 7834 * quad / 256;
627 *month = (quad + 10) % 12 + 1;
628} /* j2date() */

References fb(), and y.

Referenced by DecodeDateTime(), and DecodeNumber().

◆ ParseDateTime()

int ParseDateTime ( char timestr,
char lowstr,
char **  field,
int ftype,
int numfields,
char **  endstr 
)

Definition at line 1611 of file dt_common.c.

1613{
1614 int nf = 0;
1615 char *lp = lowstr;
1616
1617 *endstr = timestr;
1618 /* outer loop through fields */
1619 while (*(*endstr) != '\0')
1620 {
1621 /* Record start of current field */
1622 if (nf >= MAXDATEFIELDS)
1623 return -1;
1624 field[nf] = lp;
1625
1626 /* leading digit? then date or time */
1627 if (isdigit((unsigned char) *(*endstr)))
1628 {
1629 *lp++ = *(*endstr)++;
1630 while (isdigit((unsigned char) *(*endstr)))
1631 *lp++ = *(*endstr)++;
1632
1633 /* time field? */
1634 if (*(*endstr) == ':')
1635 {
1636 ftype[nf] = DTK_TIME;
1637 *lp++ = *(*endstr)++;
1638 while (isdigit((unsigned char) *(*endstr)) ||
1639 (*(*endstr) == ':') || (*(*endstr) == '.'))
1640 *lp++ = *(*endstr)++;
1641 }
1642 /* date field? allow embedded text month */
1643 else if (*(*endstr) == '-' || *(*endstr) == '/' || *(*endstr) == '.')
1644 {
1645 /* save delimiting character to use later */
1646 char *dp = (*endstr);
1647
1648 *lp++ = *(*endstr)++;
1649 /* second field is all digits? then no embedded text month */
1650 if (isdigit((unsigned char) *(*endstr)))
1651 {
1652 ftype[nf] = (*dp == '.') ? DTK_NUMBER : DTK_DATE;
1653 while (isdigit((unsigned char) *(*endstr)))
1654 *lp++ = *(*endstr)++;
1655
1656 /*
1657 * insist that the delimiters match to get a three-field
1658 * date.
1659 */
1660 if (*(*endstr) == *dp)
1661 {
1662 ftype[nf] = DTK_DATE;
1663 *lp++ = *(*endstr)++;
1664 while (isdigit((unsigned char) *(*endstr)) || (*(*endstr) == *dp))
1665 *lp++ = *(*endstr)++;
1666 }
1667 }
1668 else
1669 {
1670 ftype[nf] = DTK_DATE;
1671 while (isalnum((unsigned char) *(*endstr)) || (*(*endstr) == *dp))
1672 *lp++ = pg_tolower((unsigned char) *(*endstr)++);
1673 }
1674 }
1675
1676 /*
1677 * otherwise, number only and will determine year, month, day, or
1678 * concatenated fields later...
1679 */
1680 else
1681 ftype[nf] = DTK_NUMBER;
1682 }
1683 /* Leading decimal point? Then fractional seconds... */
1684 else if (*(*endstr) == '.')
1685 {
1686 *lp++ = *(*endstr)++;
1687 while (isdigit((unsigned char) *(*endstr)))
1688 *lp++ = *(*endstr)++;
1689
1690 ftype[nf] = DTK_NUMBER;
1691 }
1692
1693 /*
1694 * text? then date string, month, day of week, special, or timezone
1695 */
1696 else if (isalpha((unsigned char) *(*endstr)))
1697 {
1698 ftype[nf] = DTK_STRING;
1699 *lp++ = pg_tolower((unsigned char) *(*endstr)++);
1700 while (isalpha((unsigned char) *(*endstr)))
1701 *lp++ = pg_tolower((unsigned char) *(*endstr)++);
1702
1703 /*
1704 * Full date string with leading text month? Could also be a POSIX
1705 * time zone...
1706 */
1707 if (*(*endstr) == '-' || *(*endstr) == '/' || *(*endstr) == '.')
1708 {
1709 char *dp = (*endstr);
1710
1711 ftype[nf] = DTK_DATE;
1712 *lp++ = *(*endstr)++;
1713 while (isdigit((unsigned char) *(*endstr)) || *(*endstr) == *dp)
1714 *lp++ = *(*endstr)++;
1715 }
1716 }
1717 /* skip leading spaces */
1718 else if (isspace((unsigned char) *(*endstr)))
1719 {
1720 (*endstr)++;
1721 continue;
1722 }
1723 /* sign? then special or numeric timezone */
1724 else if (*(*endstr) == '+' || *(*endstr) == '-')
1725 {
1726 *lp++ = *(*endstr)++;
1727 /* soak up leading whitespace */
1728 while (isspace((unsigned char) *(*endstr)))
1729 (*endstr)++;
1730 /* numeric timezone? */
1731 if (isdigit((unsigned char) *(*endstr)))
1732 {
1733 ftype[nf] = DTK_TZ;
1734 *lp++ = *(*endstr)++;
1735 while (isdigit((unsigned char) *(*endstr)) ||
1736 (*(*endstr) == ':') || (*(*endstr) == '.'))
1737 *lp++ = *(*endstr)++;
1738 }
1739 /* special? */
1740 else if (isalpha((unsigned char) *(*endstr)))
1741 {
1742 ftype[nf] = DTK_SPECIAL;
1743 *lp++ = pg_tolower((unsigned char) *(*endstr)++);
1744 while (isalpha((unsigned char) *(*endstr)))
1745 *lp++ = pg_tolower((unsigned char) *(*endstr)++);
1746 }
1747 /* otherwise something wrong... */
1748 else
1749 return -1;
1750 }
1751 /* ignore punctuation but use as delimiter */
1752 else if (ispunct((unsigned char) *(*endstr)))
1753 {
1754 (*endstr)++;
1755 continue;
1756 }
1757 /* otherwise, something is not right... */
1758 else
1759 return -1;
1760
1761 /* force in a delimiter after each field */
1762 *lp++ = '\0';
1763 nf++;
1764 }
1765
1766 *numfields = nf;
1767
1768 return 0;
1769} /* ParseDateTime() */
unsigned char pg_tolower(unsigned char ch)

References DTK_DATE, DTK_NUMBER, DTK_SPECIAL, DTK_STRING, DTK_TIME, DTK_TZ, fb(), MAXDATEFIELDS, and pg_tolower().

◆ pgtypes_defmt_scan()

static int pgtypes_defmt_scan ( union un_fmt_comb scan_val,
int  scan_type,
char **  pstr,
char pfmt 
)
static

Definition at line 2473 of file dt_common.c.

2474{
2475 /*
2476 * scan everything between pstr and pstr_end. This is not including the
2477 * last character so we might set it to '\0' for the parsing
2478 */
2479
2480 char last_char;
2481 int err = 0;
2482 char *pstr_end;
2483 char *strtol_end = NULL;
2484
2485 while (**pstr == ' ')
2486 pstr++;
2488 if (!pstr_end)
2489 {
2490 /* there was an error, no match */
2491 return 1;
2492 }
2494 *pstr_end = '\0';
2495
2496 switch (scan_type)
2497 {
2498 case PGTYPES_TYPE_UINT:
2499
2500 /*
2501 * numbers may be blank-padded, this is the only deviation from
2502 * the fmt-string we accept
2503 */
2504 while (**pstr == ' ')
2505 (*pstr)++;
2506 errno = 0;
2507 scan_val->uint_val = (unsigned int) strtol(*pstr, &strtol_end, 10);
2508 if (errno)
2509 err = 1;
2510 break;
2512 while (**pstr == ' ')
2513 (*pstr)++;
2514 errno = 0;
2515 scan_val->luint_val = (unsigned long int) strtol(*pstr, &strtol_end, 10);
2516 if (errno)
2517 err = 1;
2518 break;
2520 scan_val->str_val = pgtypes_strdup(*pstr);
2521 if (scan_val->str_val == NULL)
2522 err = 1;
2523 break;
2524 }
2525 if (strtol_end && *strtol_end)
2526 *pstr = strtol_end;
2527 else
2528 *pstr = pstr_end;
2530 return err;
2531}
static char * find_end_token(char *str, char *fmt)
Definition dt_common.c:2368
void err(int eval, const char *fmt,...)
Definition err.c:43
char * pgtypes_strdup(const char *str)
Definition common.c:20
#define PGTYPES_TYPE_UINT
#define PGTYPES_TYPE_STRING_MALLOCED
#define PGTYPES_TYPE_UINT_LONG

References err(), fb(), find_end_token(), pgtypes_strdup(), PGTYPES_TYPE_STRING_MALLOCED, PGTYPES_TYPE_UINT, and PGTYPES_TYPE_UINT_LONG.

Referenced by PGTYPEStimestamp_defmt_scan().

◆ PGTYPEStimestamp_defmt_scan()

int PGTYPEStimestamp_defmt_scan ( char **  str,
char fmt,
timestamp d,
int year,
int month,
int day,
int hour,
int minute,
int second,
int tz 
)

Definition at line 2535 of file dt_common.c.

2539{
2540 union un_fmt_comb scan_val;
2541 int scan_type;
2542
2543 char *pstr,
2544 *pfmt,
2545 *tmp;
2546 int err = 1;
2547 unsigned int j;
2548 struct tm tm;
2549
2550 pfmt = fmt;
2551 pstr = *str;
2552
2553 while (*pfmt)
2554 {
2555 err = 0;
2556 while (*pfmt == ' ')
2557 pfmt++;
2558 while (*pstr == ' ')
2559 pstr++;
2560 if (*pfmt != '%')
2561 {
2562 if (*pfmt == *pstr)
2563 {
2564 pfmt++;
2565 pstr++;
2566 }
2567 else
2568 {
2569 /* Error: no match */
2570 err = 1;
2571 return err;
2572 }
2573 continue;
2574 }
2575 /* here *pfmt equals '%' */
2576 pfmt++;
2577 switch (*pfmt)
2578 {
2579 case 'a':
2580 pfmt++;
2581
2582 /*
2583 * we parse the day and see if it is a week day but we do not
2584 * check if the week day really matches the date
2585 */
2586 err = 1;
2587 j = 0;
2589 {
2592 {
2593 /* found it */
2594 err = 0;
2596 break;
2597 }
2598 j++;
2599 }
2600 break;
2601 case 'A':
2602 /* see note above */
2603 pfmt++;
2604 err = 1;
2605 j = 0;
2606 while (days[j])
2607 {
2608 if (strncmp(days[j], pstr, strlen(days[j])) == 0)
2609 {
2610 /* found it */
2611 err = 0;
2612 pstr += strlen(days[j]);
2613 break;
2614 }
2615 j++;
2616 }
2617 break;
2618 case 'b':
2619 case 'h':
2620 pfmt++;
2621 err = 1;
2622 j = 0;
2623 while (months[j])
2624 {
2625 if (strncmp(months[j], pstr, strlen(months[j])) == 0)
2626 {
2627 /* found it */
2628 err = 0;
2629 pstr += strlen(months[j]);
2630 *month = j + 1;
2631 break;
2632 }
2633 j++;
2634 }
2635 break;
2636 case 'B':
2637 /* see note above */
2638 pfmt++;
2639 err = 1;
2640 j = 0;
2641 while (pgtypes_date_months[j])
2642 {
2644 {
2645 /* found it */
2646 err = 0;
2648 *month = j + 1;
2649 break;
2650 }
2651 j++;
2652 }
2653 break;
2654 case 'c':
2655 /* XXX */
2656 break;
2657 case 'C':
2658 pfmt++;
2661 *year = scan_val.uint_val * 100;
2662 break;
2663 case 'd':
2664 case 'e':
2665 pfmt++;
2668 *day = scan_val.uint_val;
2669 break;
2670 case 'D':
2671
2672 /*
2673 * we have to concatenate the strings in order to be able to
2674 * find the end of the substitution
2675 */
2676 pfmt++;
2677 tmp = pgtypes_alloc(strlen("%m/%d/%y") + strlen(pstr) + 1);
2678 if (!tmp)
2679 return 1;
2680 strcpy(tmp, "%m/%d/%y");
2681 strcat(tmp, pfmt);
2682 err = PGTYPEStimestamp_defmt_scan(&pstr, tmp, d, year, month, day, hour, minute, second, tz);
2683 free(tmp);
2684 return err;
2685 case 'm':
2686 pfmt++;
2689 *month = scan_val.uint_val;
2690 break;
2691 case 'y':
2692 case 'g': /* XXX difference to y (ISO) */
2693 pfmt++;
2696 if (*year < 0)
2697 {
2698 /* not yet set */
2699 *year = scan_val.uint_val;
2700 }
2701 else
2702 *year += scan_val.uint_val;
2703 if (*year < 100)
2704 *year += 1900;
2705 break;
2706 case 'G':
2707 /* XXX difference to %V (ISO) */
2708 pfmt++;
2711 *year = scan_val.uint_val;
2712 break;
2713 case 'H':
2714 case 'I':
2715 case 'k':
2716 case 'l':
2717 pfmt++;
2720 *hour += scan_val.uint_val;
2721 break;
2722 case 'j':
2723 pfmt++;
2726
2727 /*
2728 * XXX what should we do with that? We could say that it's
2729 * sufficient if we have the year and the day within the year
2730 * to get at least a specific day.
2731 */
2732 break;
2733 case 'M':
2734 pfmt++;
2737 *minute = scan_val.uint_val;
2738 break;
2739 case 'n':
2740 pfmt++;
2741 if (*pstr == '\n')
2742 pstr++;
2743 else
2744 err = 1;
2745 break;
2746 case 'p':
2747 err = 1;
2748 pfmt++;
2749 if (strncmp(pstr, "am", 2) == 0)
2750 {
2751 *hour += 0;
2752 err = 0;
2753 pstr += 2;
2754 }
2755 if (strncmp(pstr, "a.m.", 4) == 0)
2756 {
2757 *hour += 0;
2758 err = 0;
2759 pstr += 4;
2760 }
2761 if (strncmp(pstr, "pm", 2) == 0)
2762 {
2763 *hour += 12;
2764 err = 0;
2765 pstr += 2;
2766 }
2767 if (strncmp(pstr, "p.m.", 4) == 0)
2768 {
2769 *hour += 12;
2770 err = 0;
2771 pstr += 4;
2772 }
2773 break;
2774 case 'P':
2775 err = 1;
2776 pfmt++;
2777 if (strncmp(pstr, "AM", 2) == 0)
2778 {
2779 *hour += 0;
2780 err = 0;
2781 pstr += 2;
2782 }
2783 if (strncmp(pstr, "A.M.", 4) == 0)
2784 {
2785 *hour += 0;
2786 err = 0;
2787 pstr += 4;
2788 }
2789 if (strncmp(pstr, "PM", 2) == 0)
2790 {
2791 *hour += 12;
2792 err = 0;
2793 pstr += 2;
2794 }
2795 if (strncmp(pstr, "P.M.", 4) == 0)
2796 {
2797 *hour += 12;
2798 err = 0;
2799 pstr += 4;
2800 }
2801 break;
2802 case 'r':
2803 pfmt++;
2804 tmp = pgtypes_alloc(strlen("%I:%M:%S %p") + strlen(pstr) + 1);
2805 if (!tmp)
2806 return 1;
2807 strcpy(tmp, "%I:%M:%S %p");
2808 strcat(tmp, pfmt);
2809 err = PGTYPEStimestamp_defmt_scan(&pstr, tmp, d, year, month, day, hour, minute, second, tz);
2810 free(tmp);
2811 return err;
2812 case 'R':
2813 pfmt++;
2814 tmp = pgtypes_alloc(strlen("%H:%M") + strlen(pstr) + 1);
2815 if (!tmp)
2816 return 1;
2817 strcpy(tmp, "%H:%M");
2818 strcat(tmp, pfmt);
2819 err = PGTYPEStimestamp_defmt_scan(&pstr, tmp, d, year, month, day, hour, minute, second, tz);
2820 free(tmp);
2821 return err;
2822 case 's':
2823 pfmt++;
2826 /* number of seconds in scan_val.luint_val */
2827 {
2828 struct tm *tms;
2829 struct tm tmbuf;
2830 time_t et = (time_t) scan_val.luint_val;
2831
2832 tms = gmtime_r(&et, &tmbuf);
2833
2834 if (tms)
2835 {
2836 *year = tms->tm_year + 1900;
2837 *month = tms->tm_mon + 1;
2838 *day = tms->tm_mday;
2839 *hour = tms->tm_hour;
2840 *minute = tms->tm_min;
2841 *second = tms->tm_sec;
2842 }
2843 else
2844 err = 1;
2845 }
2846 break;
2847 case 'S':
2848 pfmt++;
2851 *second = scan_val.uint_val;
2852 break;
2853 case 't':
2854 pfmt++;
2855 if (*pstr == '\t')
2856 pstr++;
2857 else
2858 err = 1;
2859 break;
2860 case 'T':
2861 pfmt++;
2862 tmp = pgtypes_alloc(strlen("%H:%M:%S") + strlen(pstr) + 1);
2863 if (!tmp)
2864 return 1;
2865 strcpy(tmp, "%H:%M:%S");
2866 strcat(tmp, pfmt);
2867 err = PGTYPEStimestamp_defmt_scan(&pstr, tmp, d, year, month, day, hour, minute, second, tz);
2868 free(tmp);
2869 return err;
2870 case 'u':
2871 pfmt++;
2874 if (scan_val.uint_val < 1 || scan_val.uint_val > 7)
2875 err = 1;
2876 break;
2877 case 'U':
2878 pfmt++;
2881 if (scan_val.uint_val > 53)
2882 err = 1;
2883 break;
2884 case 'V':
2885 pfmt++;
2888 if (scan_val.uint_val < 1 || scan_val.uint_val > 53)
2889 err = 1;
2890 break;
2891 case 'w':
2892 pfmt++;
2895 if (scan_val.uint_val > 6)
2896 err = 1;
2897 break;
2898 case 'W':
2899 pfmt++;
2902 if (scan_val.uint_val > 53)
2903 err = 1;
2904 break;
2905 case 'x':
2906 case 'X':
2907 /* XXX */
2908 break;
2909 case 'Y':
2910 pfmt++;
2913 *year = scan_val.uint_val;
2914 break;
2915 case 'z':
2916 pfmt++;
2919 if (!err)
2920 {
2921 err = DecodeTimezone(scan_val.str_val, tz);
2922 free(scan_val.str_val);
2923 }
2924 break;
2925 case 'Z':
2926 pfmt++;
2929 if (!err)
2930 {
2931 /*
2932 * XXX use DecodeSpecial instead? Do we need strcasecmp
2933 * here?
2934 */
2935 err = 1;
2936 for (j = 0; j < szdatetktbl; j++)
2937 {
2938 if ((datetktbl[j].type == TZ || datetktbl[j].type == DTZ) &&
2940 scan_val.str_val) == 0)
2941 {
2942 *tz = -datetktbl[j].value;
2943 err = 0;
2944 break;
2945 }
2946 }
2947 free(scan_val.str_val);
2948 }
2949 break;
2950 case '+':
2951 /* XXX */
2952 break;
2953 case '%':
2954 pfmt++;
2955 if (*pstr == '%')
2956 pstr++;
2957 else
2958 err = 1;
2959 break;
2960 default:
2961 err = 1;
2962 }
2963 }
2964 if (!err)
2965 {
2966 if (*second < 0)
2967 *second = 0;
2968 if (*minute < 0)
2969 *minute = 0;
2970 if (*hour < 0)
2971 *hour = 0;
2972 if (*day < 0)
2973 {
2974 err = 1;
2975 *day = 1;
2976 }
2977 if (*month < 0)
2978 {
2979 err = 1;
2980 *month = 1;
2981 }
2982 if (*year < 0)
2983 {
2984 err = 1;
2985 *year = 1970;
2986 }
2987
2988 if (*second > 59)
2989 {
2990 err = 1;
2991 *second = 0;
2992 }
2993 if (*minute > 59)
2994 {
2995 err = 1;
2996 *minute = 0;
2997 }
2998 if (*hour > 24 || /* test for > 24:00:00 */
2999 (*hour == 24 && (*minute > 0 || *second > 0)))
3000 {
3001 err = 1;
3002 *hour = 0;
3003 }
3004 if (*month > MONTHS_PER_YEAR)
3005 {
3006 err = 1;
3007 *month = 1;
3008 }
3009 if (*day > day_tab[isleap(*year)][*month - 1])
3010 {
3011 *day = day_tab[isleap(*year)][*month - 1];
3012 err = 1;
3013 }
3014
3015 tm.tm_sec = *second;
3016 tm.tm_min = *minute;
3017 tm.tm_hour = *hour;
3018 tm.tm_mday = *day;
3019 tm.tm_mon = *month;
3020 tm.tm_year = *year;
3021
3022 tm2timestamp(&tm, 0, tz, d);
3023 }
3024 return err;
3025}
int tm2timestamp(struct pg_tm *tm, fsec_t fsec, int *tzp, Timestamp *result)
Definition timestamp.c:2015
int PGTYPEStimestamp_defmt_scan(char **str, char *fmt, timestamp *d, int *year, int *month, int *day, int *hour, int *minute, int *second, int *tz)
Definition dt_common.c:2535
char * pgtypes_date_months[]
Definition dt_common.c:499
char * pgtypes_date_weekdays_short[]
Definition dt_common.c:497
static int pgtypes_defmt_scan(union un_fmt_comb *scan_val, int scan_type, char **pstr, char *pfmt)
Definition dt_common.c:2473
char * pgtypes_alloc(long size)
Definition common.c:10
int j
Definition isn.c:78
int pg_strcasecmp(const char *s1, const char *s2)
#define free(a)

References datetktbl, day_tab, days, DecodeTimezone(), DTZ, err(), fb(), free, isleap, j, months, MONTHS_PER_YEAR, pg_strcasecmp(), pgtypes_alloc(), pgtypes_date_months, pgtypes_date_weekdays_short, pgtypes_defmt_scan(), PGTYPES_TYPE_STRING_MALLOCED, PGTYPES_TYPE_UINT, PGTYPES_TYPE_UINT_LONG, PGTYPEStimestamp_defmt_scan(), str, szdatetktbl, tm, tm2timestamp(), pg_tm::tm_hour, pg_tm::tm_mday, pg_tm::tm_min, pg_tm::tm_mon, pg_tm::tm_sec, pg_tm::tm_year, type, TZ, and datetkn::value.

Referenced by PGTYPEStimestamp_defmt_asc(), and PGTYPEStimestamp_defmt_scan().

◆ TrimTrailingZeros()

void TrimTrailingZeros ( char str)

Definition at line 725 of file dt_common.c.

726{
727 int len = strlen(str);
728
729 /* chop off trailing zeros... but leave at least 2 fractional digits */
730 while (*(str + len - 1) == '0' && *(str + len - 3) != '.')
731 {
732 len--;
733 *(str + len) = '\0';
734 }
735}

References fb(), len, and str.

Referenced by AppendSeconds(), and EncodeDateTime().

Variable Documentation

◆ datecache

const datetkn* datecache[MAXDATEFIELDS] = {NULL}
static

Definition at line 489 of file dt_common.c.

489{NULL};

Referenced by DecodeSpecial().

◆ datetktbl

const datetkn datetktbl[]
static

Definition at line 20 of file dt_common.c.

20 {
21/* text, token, lexval */
22 {EARLY, RESERV, DTK_EARLY}, /* "-infinity" reserved for "early time" */
23 {"acsst", DTZ, 37800}, /* Cent. Australia */
24 {"acst", DTZ, -14400}, /* Atlantic/Porto Acre */
25 {"act", TZ, -18000}, /* Atlantic/Porto Acre */
26 {DA_D, ADBC, AD}, /* "ad" for years >= 0 */
27 {"adt", DTZ, -10800}, /* Atlantic Daylight Time */
28 {"aesst", DTZ, 39600}, /* E. Australia */
29 {"aest", TZ, 36000}, /* Australia Eastern Std Time */
30 {"aft", TZ, 16200}, /* Kabul */
31 {"ahst", TZ, -36000}, /* Alaska-Hawaii Std Time */
32 {"akdt", DTZ, -28800}, /* Alaska Daylight Time */
33 {"akst", DTZ, -32400}, /* Alaska Standard Time */
34 {"allballs", RESERV, DTK_ZULU}, /* 00:00:00 */
35 {"almst", TZ, 25200}, /* Almaty Savings Time */
36 {"almt", TZ, 21600}, /* Almaty Time */
37 {"am", AMPM, AM},
38 {"amst", DTZ, 18000}, /* Armenia Summer Time (Yerevan) */
39#if 0
40 {"amst", DTZ, -10800}, /* Porto Velho */
41#endif
42 {"amt", TZ, 14400}, /* Armenia Time (Yerevan) */
43 {"anast", DTZ, 46800}, /* Anadyr Summer Time (Russia) */
44 {"anat", TZ, 43200}, /* Anadyr Time (Russia) */
45 {"apr", MONTH, 4},
46 {"april", MONTH, 4},
47#if 0
48 aqtst
49 aqtt
50 arst
51#endif
52 {"art", TZ, -10800}, /* Argentina Time */
53#if 0
54 ashst
55 ast /* Atlantic Standard Time, Arabia Standard
56 * Time, Acre Standard Time */
57#endif
58 {"ast", TZ, -14400}, /* Atlantic Std Time (Canada) */
59 {"at", IGNORE_DTF, 0}, /* "at" (throwaway) */
60 {"aug", MONTH, 8},
61 {"august", MONTH, 8},
62 {"awsst", DTZ, 32400}, /* W. Australia */
63 {"awst", TZ, 28800}, /* W. Australia */
64 {"awt", DTZ, -10800},
65 {"azost", DTZ, 0}, /* Azores Summer Time */
66 {"azot", TZ, -3600}, /* Azores Time */
67 {"azst", DTZ, 18000}, /* Azerbaijan Summer Time */
68 {"azt", TZ, 14400}, /* Azerbaijan Time */
69 {DB_C, ADBC, BC}, /* "bc" for years < 0 */
70 {"bdst", TZ, 7200}, /* British Double Summer Time */
71 {"bdt", TZ, 21600}, /* Dacca */
72 {"bnt", TZ, 28800}, /* Brunei Darussalam Time */
73 {"bort", TZ, 28800}, /* Borneo Time (Indonesia) */
74#if 0
75 bortst
76 bost
77#endif
78 {"bot", TZ, -14400}, /* Bolivia Time */
79 {"bra", TZ, -10800}, /* Brazil Time */
80#if 0
81 brst
82 brt
83#endif
84 {"bst", DTZ, 3600}, /* British Summer Time */
85#if 0
86 {"bst", TZ, -10800}, /* Brazil Standard Time */
87 {"bst", DTZ, -39600}, /* Bering Summer Time */
88#endif
89 {"bt", TZ, 10800}, /* Baghdad Time */
90 {"btt", TZ, 21600}, /* Bhutan Time */
91 {"cadt", DTZ, 37800}, /* Central Australian DST */
92 {"cast", TZ, 34200}, /* Central Australian ST */
93 {"cat", TZ, -36000}, /* Central Alaska Time */
94 {"cct", TZ, 28800}, /* China Coast Time */
95#if 0
96 {"cct", TZ, 23400}, /* Indian Cocos (Island) Time */
97#endif
98 {"cdt", DTZ, -18000}, /* Central Daylight Time */
99 {"cest", DTZ, 7200}, /* Central European Dayl.Time */
100 {"cet", TZ, 3600}, /* Central European Time */
101 {"cetdst", DTZ, 7200}, /* Central European Dayl.Time */
102 {"chadt", DTZ, 49500}, /* Chatham Island Daylight Time (13:45) */
103 {"chast", TZ, 45900}, /* Chatham Island Time (12:45) */
104#if 0
105 ckhst
106#endif
107 {"ckt", TZ, 43200}, /* Cook Islands Time */
108 {"clst", DTZ, -10800}, /* Chile Summer Time */
109 {"clt", TZ, -14400}, /* Chile Time */
110#if 0
111 cost
112#endif
113 {"cot", TZ, -18000}, /* Columbia Time */
114 {"cst", TZ, -21600}, /* Central Standard Time */
115#if 0
116 cvst
117#endif
118 {"cvt", TZ, 25200}, /* Christmas Island Time (Indian Ocean) */
119 {"cxt", TZ, 25200}, /* Christmas Island Time (Indian Ocean) */
120 {"d", UNITS, DTK_DAY}, /* "day of month" for ISO input */
121 {"davt", TZ, 25200}, /* Davis Time (Antarctica) */
122 {"ddut", TZ, 36000}, /* Dumont-d'Urville Time (Antarctica) */
123 {"dec", MONTH, 12},
124 {"december", MONTH, 12},
125 {"dnt", TZ, 3600}, /* Dansk Normal Tid */
126 {"dow", UNITS, DTK_DOW}, /* day of week */
127 {"doy", UNITS, DTK_DOY}, /* day of year */
128 {"dst", DTZMOD, SECS_PER_HOUR},
129#if 0
130 {"dusst", DTZ, 21600}, /* Dushanbe Summer Time */
131#endif
132 {"easst", DTZ, -18000}, /* Easter Island Summer Time */
133 {"east", TZ, -21600}, /* Easter Island Time */
134 {"eat", TZ, 10800}, /* East Africa Time */
135#if 0
136 {"east", DTZ, 14400}, /* Indian Antananarivo Savings Time */
137 {"eat", TZ, 10800}, /* Indian Antananarivo Time */
138 {"ect", TZ, -14400}, /* Eastern Caribbean Time */
139 {"ect", TZ, -18000}, /* Ecuador Time */
140#endif
141 {"edt", DTZ, -14400}, /* Eastern Daylight Time */
142 {"eest", DTZ, 10800}, /* Eastern Europe Summer Time */
143 {"eet", TZ, 7200}, /* East. Europe, USSR Zone 1 */
144 {"eetdst", DTZ, 10800}, /* Eastern Europe Daylight Time */
145 {"egst", DTZ, 0}, /* East Greenland Summer Time */
146 {"egt", TZ, -3600}, /* East Greenland Time */
147#if 0
148 ehdt
149#endif
150 {EPOCH, RESERV, DTK_EPOCH}, /* "epoch" reserved for system epoch time */
151 {"est", TZ, -18000}, /* Eastern Standard Time */
152 {"feb", MONTH, 2},
153 {"february", MONTH, 2},
154 {"fjst", DTZ, -46800}, /* Fiji Summer Time (13 hour offset!) */
155 {"fjt", TZ, -43200}, /* Fiji Time */
156 {"fkst", DTZ, -10800}, /* Falkland Islands Summer Time */
157 {"fkt", TZ, -7200}, /* Falkland Islands Time */
158#if 0
159 fnst
160 fnt
161#endif
162 {"fri", DOW, 5},
163 {"friday", DOW, 5},
164 {"fst", TZ, 3600}, /* French Summer Time */
165 {"fwt", DTZ, 7200}, /* French Winter Time */
166 {"galt", TZ, -21600}, /* Galapagos Time */
167 {"gamt", TZ, -32400}, /* Gambier Time */
168 {"gest", DTZ, 18000}, /* Georgia Summer Time */
169 {"get", TZ, 14400}, /* Georgia Time */
170 {"gft", TZ, -10800}, /* French Guiana Time */
171#if 0
172 ghst
173#endif
174 {"gilt", TZ, 43200}, /* Gilbert Islands Time */
175 {"gmt", TZ, 0}, /* Greenwich Mean Time */
176 {"gst", TZ, 36000}, /* Guam Std Time, USSR Zone 9 */
177 {"gyt", TZ, -14400}, /* Guyana Time */
178 {"h", UNITS, DTK_HOUR}, /* "hour" */
179#if 0
180 hadt
181 hast
182#endif
183 {"hdt", DTZ, -32400}, /* Hawaii/Alaska Daylight Time */
184#if 0
185 hkst
186#endif
187 {"hkt", TZ, 28800}, /* Hong Kong Time */
188#if 0
189 {"hmt", TZ, 10800}, /* Hellas ? ? */
190 hovst
191 hovt
192#endif
193 {"hst", TZ, -36000}, /* Hawaii Std Time */
194#if 0
195 hwt
196#endif
197 {"ict", TZ, 25200}, /* Indochina Time */
198 {"idle", TZ, 43200}, /* Intl. Date Line, East */
199 {"idlw", TZ, -43200}, /* Intl. Date Line, West */
200#if 0
201 idt /* Israeli, Iran, Indian Daylight Time */
202#endif
203 {LATE, RESERV, DTK_LATE}, /* "infinity" reserved for "late time" */
204 {"iot", TZ, 18000}, /* Indian Chagos Time */
205 {"irkst", DTZ, 32400}, /* Irkutsk Summer Time */
206 {"irkt", TZ, 28800}, /* Irkutsk Time */
207 {"irt", TZ, 12600}, /* Iran Time */
208 {"isodow", UNITS, DTK_ISODOW}, /* ISO day of week, Sunday == 7 */
209#if 0
210 isst
211#endif
212 {"ist", TZ, 7200}, /* Israel */
213 {"it", TZ, 12600}, /* Iran Time */
214 {"j", UNITS, DTK_JULIAN},
215 {"jan", MONTH, 1},
216 {"january", MONTH, 1},
217 {"javt", TZ, 25200}, /* Java Time (07:00? see JT) */
218 {"jayt", TZ, 32400}, /* Jayapura Time (Indonesia) */
219 {"jd", UNITS, DTK_JULIAN},
220 {"jst", TZ, 32400}, /* Japan Std Time,USSR Zone 8 */
221 {"jt", TZ, 27000}, /* Java Time (07:30? see JAVT) */
222 {"jul", MONTH, 7},
223 {"julian", UNITS, DTK_JULIAN},
224 {"july", MONTH, 7},
225 {"jun", MONTH, 6},
226 {"june", MONTH, 6},
227 {"kdt", DTZ, 36000}, /* Korea Daylight Time */
228 {"kgst", DTZ, 21600}, /* Kyrgyzstan Summer Time */
229 {"kgt", TZ, 18000}, /* Kyrgyzstan Time */
230 {"kost", TZ, 43200}, /* Kosrae Time */
231 {"krast", DTZ, 25200}, /* Krasnoyarsk Summer Time */
232 {"krat", TZ, 28800}, /* Krasnoyarsk Standard Time */
233 {"kst", TZ, 32400}, /* Korea Standard Time */
234 {"lhdt", DTZ, 39600}, /* Lord Howe Daylight Time, Australia */
235 {"lhst", TZ, 37800}, /* Lord Howe Standard Time, Australia */
236 {"ligt", TZ, 36000}, /* From Melbourne, Australia */
237 {"lint", TZ, 50400}, /* Line Islands Time (Kiribati; +14 hours!) */
238 {"lkt", TZ, 21600}, /* Lanka Time */
239 {"m", UNITS, DTK_MONTH}, /* "month" for ISO input */
240 {"magst", DTZ, 43200}, /* Magadan Summer Time */
241 {"magt", TZ, 39600}, /* Magadan Time */
242 {"mar", MONTH, 3},
243 {"march", MONTH, 3},
244 {"mart", TZ, -34200}, /* Marquesas Time */
245 {"mawt", TZ, 21600}, /* Mawson, Antarctica */
246 {"may", MONTH, 5},
247 {"mdt", DTZ, -21600}, /* Mountain Daylight Time */
248 {"mest", DTZ, 7200}, /* Middle Europe Summer Time */
249 {"met", TZ, 3600}, /* Middle Europe Time */
250 {"metdst", DTZ, 7200}, /* Middle Europe Daylight Time */
251 {"mewt", TZ, 3600}, /* Middle Europe Winter Time */
252 {"mez", TZ, 3600}, /* Middle Europe Zone */
253 {"mht", TZ, 43200}, /* Kwajalein */
254 {"mm", UNITS, DTK_MINUTE}, /* "minute" for ISO input */
255 {"mmt", TZ, 23400}, /* Myannar Time */
256 {"mon", DOW, 1},
257 {"monday", DOW, 1},
258#if 0
259 most
260#endif
261 {"mpt", TZ, 36000}, /* North Mariana Islands Time */
262 {"msd", DTZ, 14400}, /* Moscow Summer Time */
263 {"msk", TZ, 10800}, /* Moscow Time */
264 {"mst", TZ, -25200}, /* Mountain Standard Time */
265 {"mt", TZ, 30600}, /* Moluccas Time */
266 {"mut", TZ, 14400}, /* Mauritius Island Time */
267 {"mvt", TZ, 18000}, /* Maldives Island Time */
268 {"myt", TZ, 28800}, /* Malaysia Time */
269#if 0
270 ncst
271#endif
272 {"nct", TZ, 39600}, /* New Caledonia Time */
273 {"ndt", DTZ, -9000}, /* Nfld. Daylight Time */
274 {"nft", TZ, -12600}, /* Newfoundland Standard Time */
275 {"nor", TZ, 3600}, /* Norway Standard Time */
276 {"nov", MONTH, 11},
277 {"november", MONTH, 11},
278 {"novst", DTZ, 25200}, /* Novosibirsk Summer Time */
279 {"novt", TZ, 21600}, /* Novosibirsk Standard Time */
280 {NOW, RESERV, DTK_NOW}, /* current transaction time */
281 {"npt", TZ, 20700}, /* Nepal Standard Time (GMT-5:45) */
282 {"nst", TZ, -12600}, /* Nfld. Standard Time */
283 {"nt", TZ, -39600}, /* Nome Time */
284 {"nut", TZ, -39600}, /* Niue Time */
285 {"nzdt", DTZ, 46800}, /* New Zealand Daylight Time */
286 {"nzst", TZ, 43200}, /* New Zealand Standard Time */
287 {"nzt", TZ, 43200}, /* New Zealand Time */
288 {"oct", MONTH, 10},
289 {"october", MONTH, 10},
290 {"omsst", DTZ, 25200}, /* Omsk Summer Time */
291 {"omst", TZ, 21600}, /* Omsk Time */
292 {"on", IGNORE_DTF, 0}, /* "on" (throwaway) */
293 {"pdt", DTZ, -25200}, /* Pacific Daylight Time */
294#if 0
295 pest
296#endif
297 {"pet", TZ, -18000}, /* Peru Time */
298 {"petst", DTZ, 46800}, /* Petropavlovsk-Kamchatski Summer Time */
299 {"pett", TZ, 43200}, /* Petropavlovsk-Kamchatski Time */
300 {"pgt", TZ, 36000}, /* Papua New Guinea Time */
301 {"phot", TZ, 46800}, /* Phoenix Islands (Kiribati) Time */
302#if 0
303 phst
304#endif
305 {"pht", TZ, 28800}, /* Philippine Time */
306 {"pkt", TZ, 18000}, /* Pakistan Time */
307 {"pm", AMPM, PM},
308 {"pmdt", DTZ, -7200}, /* Pierre & Miquelon Daylight Time */
309#if 0
310 pmst
311#endif
312 {"pont", TZ, 39600}, /* Ponape Time (Micronesia) */
313 {"pst", TZ, -28800}, /* Pacific Standard Time */
314 {"pwt", TZ, 32400}, /* Palau Time */
315 {"pyst", DTZ, -10800}, /* Paraguay Summer Time */
316 {"pyt", TZ, -14400}, /* Paraguay Time */
317 {"ret", DTZ, 14400}, /* Reunion Island Time */
318 {"s", UNITS, DTK_SECOND}, /* "seconds" for ISO input */
319 {"sadt", DTZ, 37800}, /* S. Australian Dayl. Time */
320#if 0
321 samst
322 samt
323#endif
324 {"sast", TZ, 34200}, /* South Australian Std Time */
325 {"sat", DOW, 6},
326 {"saturday", DOW, 6},
327#if 0
328 sbt
329#endif
330 {"sct", DTZ, 14400}, /* Mahe Island Time */
331 {"sep", MONTH, 9},
332 {"sept", MONTH, 9},
333 {"september", MONTH, 9},
334 {"set", TZ, -3600}, /* Seychelles Time ?? */
335#if 0
336 sgt
337#endif
338 {"sst", DTZ, 7200}, /* Swedish Summer Time */
339 {"sun", DOW, 0},
340 {"sunday", DOW, 0},
341 {"swt", TZ, 3600}, /* Swedish Winter Time */
342#if 0
343 syot
344#endif
345 {"t", ISOTIME, DTK_TIME}, /* Filler for ISO time fields */
346 {"tft", TZ, 18000}, /* Kerguelen Time */
347 {"that", TZ, -36000}, /* Tahiti Time */
348 {"thu", DOW, 4},
349 {"thur", DOW, 4},
350 {"thurs", DOW, 4},
351 {"thursday", DOW, 4},
352 {"tjt", TZ, 18000}, /* Tajikistan Time */
353 {"tkt", TZ, -36000}, /* Tokelau Time */
354 {"tmt", TZ, 18000}, /* Turkmenistan Time */
355 {TODAY, RESERV, DTK_TODAY}, /* midnight */
356 {TOMORROW, RESERV, DTK_TOMORROW}, /* tomorrow midnight */
357#if 0
358 tost
359#endif
360 {"tot", TZ, 46800}, /* Tonga Time */
361#if 0
362 tpt
363#endif
364 {"truk", TZ, 36000}, /* Truk Time */
365 {"tue", DOW, 2},
366 {"tues", DOW, 2},
367 {"tuesday", DOW, 2},
368 {"tvt", TZ, 43200}, /* Tuvalu Time */
369#if 0
370 uct
371#endif
372 {"ulast", DTZ, 32400}, /* Ulan Bator Summer Time */
373 {"ulat", TZ, 28800}, /* Ulan Bator Time */
374 {"ut", TZ, 0},
375 {"utc", TZ, 0},
376 {"uyst", DTZ, -7200}, /* Uruguay Summer Time */
377 {"uyt", TZ, -10800}, /* Uruguay Time */
378 {"uzst", DTZ, 21600}, /* Uzbekistan Summer Time */
379 {"uzt", TZ, 18000}, /* Uzbekistan Time */
380 {"vet", TZ, -14400}, /* Venezuela Time */
381 {"vlast", DTZ, 39600}, /* Vladivostok Summer Time */
382 {"vlat", TZ, 36000}, /* Vladivostok Time */
383#if 0
384 vust
385#endif
386 {"vut", TZ, 39600}, /* Vanuata Time */
387 {"wadt", DTZ, 28800}, /* West Australian DST */
388 {"wakt", TZ, 43200}, /* Wake Time */
389#if 0
390 warst
391#endif
392 {"wast", TZ, 25200}, /* West Australian Std Time */
393 {"wat", TZ, -3600}, /* West Africa Time */
394 {"wdt", DTZ, 32400}, /* West Australian DST */
395 {"wed", DOW, 3},
396 {"wednesday", DOW, 3},
397 {"weds", DOW, 3},
398 {"west", DTZ, 3600}, /* Western Europe Summer Time */
399 {"wet", TZ, 0}, /* Western Europe */
400 {"wetdst", DTZ, 3600}, /* Western Europe Daylight Savings Time */
401 {"wft", TZ, 43200}, /* Wallis and Futuna Time */
402 {"wgst", DTZ, -7200}, /* West Greenland Summer Time */
403 {"wgt", TZ, -10800}, /* West Greenland Time */
404 {"wst", TZ, 28800}, /* West Australian Standard Time */
405 {"y", UNITS, DTK_YEAR}, /* "year" for ISO input */
406 {"yakst", DTZ, 36000}, /* Yakutsk Summer Time */
407 {"yakt", TZ, 32400}, /* Yakutsk Time */
408 {"yapt", TZ, 36000}, /* Yap Time (Micronesia) */
409 {"ydt", DTZ, -28800}, /* Yukon Daylight Time */
410 {"yekst", DTZ, 21600}, /* Yekaterinburg Summer Time */
411 {"yekt", TZ, 18000}, /* Yekaterinburg Time */
412 {YESTERDAY, RESERV, DTK_YESTERDAY}, /* yesterday midnight */
413 {"yst", TZ, -32400}, /* Yukon Standard Time */
414 {"z", TZ, 0}, /* time zone tag per ISO-8601 */
415 {"zp4", TZ, -14400}, /* UTC +4 hours. */
416 {"zp5", TZ, -18000}, /* UTC +5 hours. */
417 {"zp6", TZ, -21600}, /* UTC +6 hours. */
418 {ZULU, TZ, 0}, /* UTC */
419};
#define EPOCH
Definition datetime.h:37
#define DTK_EPOCH
Definition datetime.h:152
#define DTK_LATE
Definition datetime.h:151
#define DTK_ISODOW
Definition datetime.h:180
#define ZULU
Definition datetime.h:45
#define DB_C
Definition datetime.h:61
#define DTK_EARLY
Definition datetime.h:150
#define DTK_DOY
Definition datetime.h:176
#define AD
Definition datetime.h:75
#define TOMORROW
Definition datetime.h:43
#define EARLY
Definition datetime.h:39
#define DA_D
Definition datetime.h:60
#define LATE
Definition datetime.h:40
#define NOW
Definition datetime.h:41
#define DTK_DOW
Definition datetime.h:175
#define TODAY
Definition datetime.h:42
#define YESTERDAY
Definition datetime.h:44

Referenced by DecodeSpecial(), and PGTYPEStimestamp_defmt_scan().

◆ day_tab

const int day_tab[2][13]
Initial value:
= {
{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 0},
{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 0}}

Definition at line 14 of file dt_common.c.

14 {
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}};

Referenced by DecodeDateTime(), and PGTYPEStimestamp_defmt_scan().

◆ days

char* days[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", NULL}

Definition at line 495 of file dt_common.c.

495{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", NULL};

Referenced by EncodeDateTime(), and PGTYPEStimestamp_defmt_scan().

◆ deltacache

const datetkn* deltacache[MAXDATEFIELDS] = {NULL}
static

Definition at line 491 of file dt_common.c.

491{NULL};

Referenced by DecodeUnits().

◆ deltatktbl

const datetkn deltatktbl[]
static

Definition at line 421 of file dt_common.c.

421 {
422 /* text, token, lexval */
423 {"@", IGNORE_DTF, 0}, /* postgres relative prefix */
424 {DAGO, AGO, 0}, /* "ago" indicates negative time offset */
425 {"c", UNITS, DTK_CENTURY}, /* "century" relative */
426 {"cent", UNITS, DTK_CENTURY}, /* "century" relative */
427 {"centuries", UNITS, DTK_CENTURY}, /* "centuries" relative */
428 {DCENTURY, UNITS, DTK_CENTURY}, /* "century" relative */
429 {"d", UNITS, DTK_DAY}, /* "day" relative */
430 {DDAY, UNITS, DTK_DAY}, /* "day" relative */
431 {"days", UNITS, DTK_DAY}, /* "days" relative */
432 {"dec", UNITS, DTK_DECADE}, /* "decade" relative */
433 {DDECADE, UNITS, DTK_DECADE}, /* "decade" relative */
434 {"decades", UNITS, DTK_DECADE}, /* "decades" relative */
435 {"decs", UNITS, DTK_DECADE}, /* "decades" relative */
436 {"h", UNITS, DTK_HOUR}, /* "hour" relative */
437 {DHOUR, UNITS, DTK_HOUR}, /* "hour" relative */
438 {"hours", UNITS, DTK_HOUR}, /* "hours" relative */
439 {"hr", UNITS, DTK_HOUR}, /* "hour" relative */
440 {"hrs", UNITS, DTK_HOUR}, /* "hours" relative */
441 {"m", UNITS, DTK_MINUTE}, /* "minute" relative */
442 {"microsecon", UNITS, DTK_MICROSEC}, /* "microsecond" relative */
443 {"mil", UNITS, DTK_MILLENNIUM}, /* "millennium" relative */
444 {"millennia", UNITS, DTK_MILLENNIUM}, /* "millennia" relative */
445 {DMILLENNIUM, UNITS, DTK_MILLENNIUM}, /* "millennium" relative */
446 {"millisecon", UNITS, DTK_MILLISEC}, /* relative */
447 {"mils", UNITS, DTK_MILLENNIUM}, /* "millennia" relative */
448 {"min", UNITS, DTK_MINUTE}, /* "minute" relative */
449 {"mins", UNITS, DTK_MINUTE}, /* "minutes" relative */
450 {DMINUTE, UNITS, DTK_MINUTE}, /* "minute" relative */
451 {"minutes", UNITS, DTK_MINUTE}, /* "minutes" relative */
452 {"mon", UNITS, DTK_MONTH}, /* "months" relative */
453 {"mons", UNITS, DTK_MONTH}, /* "months" relative */
454 {DMONTH, UNITS, DTK_MONTH}, /* "month" relative */
455 {"months", UNITS, DTK_MONTH},
456 {"ms", UNITS, DTK_MILLISEC},
457 {"msec", UNITS, DTK_MILLISEC},
459 {"mseconds", UNITS, DTK_MILLISEC},
460 {"msecs", UNITS, DTK_MILLISEC},
461 {"qtr", UNITS, DTK_QUARTER}, /* "quarter" relative */
462 {DQUARTER, UNITS, DTK_QUARTER}, /* "quarter" relative */
463 {"s", UNITS, DTK_SECOND},
464 {"sec", UNITS, DTK_SECOND},
466 {"seconds", UNITS, DTK_SECOND},
467 {"secs", UNITS, DTK_SECOND},
468 {DTIMEZONE, UNITS, DTK_TZ}, /* "timezone" time offset */
469 {"timezone_h", UNITS, DTK_TZ_HOUR}, /* timezone hour units */
470 {"timezone_m", UNITS, DTK_TZ_MINUTE}, /* timezone minutes units */
471 {"us", UNITS, DTK_MICROSEC}, /* "microsecond" relative */
472 {"usec", UNITS, DTK_MICROSEC}, /* "microsecond" relative */
473 {DMICROSEC, UNITS, DTK_MICROSEC}, /* "microsecond" relative */
474 {"useconds", UNITS, DTK_MICROSEC}, /* "microseconds" relative */
475 {"usecs", UNITS, DTK_MICROSEC}, /* "microseconds" relative */
476 {"w", UNITS, DTK_WEEK}, /* "week" relative */
477 {DWEEK, UNITS, DTK_WEEK}, /* "week" relative */
478 {"weeks", UNITS, DTK_WEEK}, /* "weeks" relative */
479 {"y", UNITS, DTK_YEAR}, /* "year" relative */
480 {DYEAR, UNITS, DTK_YEAR}, /* "year" relative */
481 {"years", UNITS, DTK_YEAR}, /* "years" relative */
482 {"yr", UNITS, DTK_YEAR}, /* "year" relative */
483 {"yrs", UNITS, DTK_YEAR}, /* "years" relative */
484};
#define DAGO
Definition datetime.h:35
#define DYEAR
Definition datetime.h:56
#define DTK_DECADE
Definition datetime.h:168
#define DTK_QUARTER
Definition datetime.h:166
#define DHOUR
Definition datetime.h:51
#define DWEEK
Definition datetime.h:53
#define DTK_TZ_HOUR
Definition datetime.h:177
#define DTIMEZONE
Definition datetime.h:62
#define DTK_TZ_MINUTE
Definition datetime.h:178
#define DTK_CENTURY
Definition datetime.h:169
#define DMONTH
Definition datetime.h:54
#define DTK_MILLENNIUM
Definition datetime.h:170
#define DDECADE
Definition datetime.h:57
#define DDAY
Definition datetime.h:52
#define DMICROSEC
Definition datetime.h:47
#define DMILLENNIUM
Definition datetime.h:59
#define DCENTURY
Definition datetime.h:58
#define DQUARTER
Definition datetime.h:55
#define DTK_WEEK
Definition datetime.h:164
#define DSECOND
Definition datetime.h:49
#define DTK_MICROSEC
Definition datetime.h:172
#define DMILLISEC
Definition datetime.h:48
#define AGO
Definition datetime.h:110
#define DTK_MILLISEC
Definition datetime.h:171
#define DMINUTE
Definition datetime.h:50

Referenced by DecodeUnits().

◆ months

char* months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", NULL}

Definition at line 493 of file dt_common.c.

493{"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", NULL};

Referenced by EncodeDateTime(), and PGTYPEStimestamp_defmt_scan().

◆ pgtypes_date_months

char* pgtypes_date_months[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December", NULL}

Definition at line 499 of file dt_common.c.

499{"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December", NULL};

Referenced by dttofmtasc_replace(), PGTYPESdate_defmt_asc(), and PGTYPEStimestamp_defmt_scan().

◆ pgtypes_date_weekdays_short

char* pgtypes_date_weekdays_short[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL}

Definition at line 497 of file dt_common.c.

497{"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL};

Referenced by dttofmtasc_replace(), PGTYPESdate_fmt_asc(), and PGTYPEStimestamp_defmt_scan().

◆ szdatetktbl

const unsigned int szdatetktbl = lengthof(datetktbl)
static

Definition at line 486 of file dt_common.c.

Referenced by DecodeSpecial(), and PGTYPEStimestamp_defmt_scan().

◆ szdeltatktbl

const unsigned int szdeltatktbl = lengthof(deltatktbl)
static

Definition at line 487 of file dt_common.c.

Referenced by DecodeUnits().