PostgreSQL Source Code  git master
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 char * find_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}
 
char * months [] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", NULL}
 
char * days [] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", NULL}
 
char * pgtypes_date_weekdays_short [] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL}
 
char * pgtypes_date_months [] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December", NULL}
 

Typedef Documentation

◆ AbsoluteTime

typedef long 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 972 of file dt_common.c.

973 {
974  time_t time = (time_t) _time;
975  struct tm *tx;
976 
977  errno = 0;
978  if (tzp != NULL)
979  tx = localtime((time_t *) &time);
980  else
981  tx = gmtime((time_t *) &time);
982 
983  if (!tx)
984  {
985  errno = PGTYPES_TS_BAD_TIMESTAMP;
986  return;
987  }
988 
989  tm->tm_year = tx->tm_year + 1900;
990  tm->tm_mon = tx->tm_mon + 1;
991  tm->tm_mday = tx->tm_mday;
992  tm->tm_hour = tx->tm_hour;
993  tm->tm_min = tx->tm_min;
994  tm->tm_sec = tx->tm_sec;
995  tm->tm_isdst = tx->tm_isdst;
996 
997 #if defined(HAVE_STRUCT_TM_TM_ZONE)
998  tm->tm_gmtoff = tx->tm_gmtoff;
999  tm->tm_zone = tx->tm_zone;
1000 
1001  if (tzp != NULL)
1002  {
1003  /*
1004  * We have a brute force time zone per SQL99? Then use it without
1005  * change since we have already rotated to the time zone.
1006  */
1007  *tzp = -tm->tm_gmtoff; /* tm_gmtoff is Sun/DEC-ism */
1008 
1009  /*
1010  * FreeBSD man pages indicate that this should work - tgl 97/04/23
1011  */
1012  if (tzn != NULL)
1013  {
1014  /*
1015  * Copy no more than MAXTZLEN bytes of timezone to tzn, in case it
1016  * contains an error message, which doesn't fit in the buffer
1017  */
1018  strlcpy(*tzn, tm->tm_zone, MAXTZLEN + 1);
1019  if (strlen(tm->tm_zone) > MAXTZLEN)
1020  tm->tm_isdst = -1;
1021  }
1022  }
1023  else
1024  tm->tm_isdst = -1;
1025 #elif defined(HAVE_INT_TIMEZONE)
1026  if (tzp != NULL)
1027  {
1029 
1030  if (tzn != NULL)
1031  {
1032  /*
1033  * Copy no more than MAXTZLEN bytes of timezone to tzn, in case it
1034  * contains an error message, which doesn't fit in the buffer
1035  */
1036  strlcpy(*tzn, TZNAME_GLOBAL[tm->tm_isdst], MAXTZLEN + 1);
1037  if (strlen(TZNAME_GLOBAL[tm->tm_isdst]) > MAXTZLEN)
1038  tm->tm_isdst = -1;
1039  }
1040  }
1041  else
1042  tm->tm_isdst = -1;
1043 #else /* not (HAVE_STRUCT_TM_TM_ZONE ||
1044  * HAVE_INT_TIMEZONE) */
1045  if (tzp != NULL)
1046  {
1047  /* default to UTC */
1048  *tzp = 0;
1049  if (tzn != NULL)
1050  *tzn = NULL;
1051  }
1052  else
1053  tm->tm_isdst = -1;
1054 #endif
1055 }
#define SECS_PER_HOUR
Definition: timestamp.h:126
static struct pg_tm tm
Definition: localtime.c:102
#define MAXTZLEN
Definition: miscadmin.h:257
#define PGTYPES_TS_BAD_TIMESTAMP
Definition: pgtypes_error.h:15
#define TIMEZONE_GLOBAL
Definition: port.h:255
#define TZNAME_GLOBAL
Definition: port.h:256
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
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 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 581 of file dt_common.c.

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

References 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 }
#define TOKMAXLEN
Definition: datetime.h:205

References sort-test::key, 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 1306 of file dt_common.c.

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

References ADBC, BC, DecodeNumber(), DecodeSpecial(), DOY, DTK_DATE_M, DTK_M, i, IGNORE_DTF, len, MAXDATEFIELDS, MONTH, generate_unaccent_rules::str, tm, pg_tm::tm_mon, pg_tm::tm_year, generate_unaccent_rules::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 1780 of file dt_common.c.

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

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

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

References DTK_DATE, DTK_DATE_M, DTK_TIME, DTK_TIME_M, i, len, generate_unaccent_rules::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 1545 of file dt_common.c.

1546 {
1547  int val,
1548  tz;
1549  int type;
1550  char *cp;
1551  char delim;
1552 
1553  cp = str;
1554  while (*cp != '\0' && isalpha((unsigned char) *cp))
1555  cp++;
1556 
1557  if (DecodeTimezone(cp, &tz) != 0)
1558  return -1;
1559 
1560  delim = *cp;
1561  *cp = '\0';
1563  *cp = delim;
1564 
1565  switch (type)
1566  {
1567  case DTZ:
1568  case TZ:
1569  *tzp = -(val + tz);
1570  break;
1571 
1572  default:
1573  return -1;
1574  }
1575 
1576  return 0;
1577 } /* DecodePosixTimezone() */

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

Referenced by DecodeDateTime().

◆ DecodeSpecial()

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

Definition at line 635 of file dt_common.c.

636 {
637  int type;
638  const datetkn *tp;
639 
640  /* use strncmp so that we match truncated tokens */
641  if (datecache[field] != NULL &&
642  strncmp(lowtoken, datecache[field]->token, TOKMAXLEN) == 0)
643  tp = datecache[field];
644  else
645  {
646  tp = NULL;
647  if (!tp)
648  tp = datebsearch(lowtoken, datetktbl, szdatetktbl);
649  }
650  datecache[field] = tp;
651  if (tp == NULL)
652  {
654  *val = 0;
655  }
656  else
657  {
658  type = tp->type;
659  *val = tp->value;
660  }
661 
662  return type;
663 } /* 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:125
int32 value
Definition: datetime.h:212
char type
Definition: datetime.h:211

References datebsearch(), datecache, datetktbl, szdatetktbl, TOKMAXLEN, generate_unaccent_rules::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 1435 of file dt_common.c.

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

References DTK_TIME_M, i, generate_unaccent_rules::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 1500 of file dt_common.c.

1501 {
1502  int tz;
1503  int hr,
1504  min;
1505  char *cp;
1506  int len;
1507 
1508  /* assume leading character is "+" or "-" */
1509  hr = strtoint(str + 1, &cp, 10);
1510 
1511  /* explicit delimiter? */
1512  if (*cp == ':')
1513  min = strtoint(cp + 1, &cp, 10);
1514  /* otherwise, might have run things together... */
1515  else if (*cp == '\0' && (len = strlen(str)) > 3)
1516  {
1517  min = strtoint(str + len - 2, &cp, 10);
1518  if (min < 0 || min >= 60)
1519  return -1;
1520 
1521  *(str + len - 2) = '\0';
1522  hr = strtoint(str + 1, &cp, 10);
1523  if (hr < 0 || hr > 13)
1524  return -1;
1525  }
1526  else
1527  min = 0;
1528 
1529  tz = (hr * MINS_PER_HOUR + min) * SECS_PER_MINUTE;
1530  if (*str == '-')
1531  tz = -tz;
1532 
1533  *tzp = -tz;
1534  return *cp != '\0';
1535 } /* DecodeTimezone() */
#define MINS_PER_HOUR
Definition: timestamp.h:128
#define SECS_PER_MINUTE
Definition: timestamp.h:127

References len, MINS_PER_HOUR, SECS_PER_MINUTE, generate_unaccent_rules::str, and strtoint().

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

◆ DecodeUnits()

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

Definition at line 536 of file dt_common.c.

537 {
538  int type;
539  const datetkn *tp;
540 
541  /* use strncmp so that we match truncated tokens */
542  if (deltacache[field] != NULL &&
543  strncmp(lowtoken, deltacache[field]->token, TOKMAXLEN) == 0)
544  tp = deltacache[field];
545  else
546  tp = datebsearch(lowtoken, deltatktbl, szdeltatktbl);
547  deltacache[field] = tp;
548  if (tp == NULL)
549  {
551  *val = 0;
552  }
553  else
554  {
555  type = tp->type;
556  *val = tp->value;
557  }
558 
559  return type;
560 } /* 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, szdeltatktbl, TOKMAXLEN, generate_unaccent_rules::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 1066 of file dt_common.c.

1067 {
1068  int64 time;
1069 
1070  time = jd;
1071  *hour = time / USECS_PER_HOUR;
1072  time -= (*hour) * USECS_PER_HOUR;
1073  *min = time / USECS_PER_MINUTE;
1074  time -= (*min) * USECS_PER_MINUTE;
1075  *sec = time / USECS_PER_SEC;
1076  *fsec = time - (*sec * USECS_PER_SEC);
1077 } /* dt2time() */
#define USECS_PER_HOUR
Definition: timestamp.h:131
#define USECS_PER_MINUTE
Definition: timestamp.h:132

References 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 669 of file dt_common.c.

670 {
671  Assert(tm->tm_mon >= 1 && tm->tm_mon <= MONTHS_PER_YEAR);
672 
673  switch (style)
674  {
675  case USE_ISO_DATES:
676  /* compatible with ISO date formats */
677  if (tm->tm_year > 0)
678  sprintf(str, "%04d-%02d-%02d",
679  tm->tm_year, tm->tm_mon, tm->tm_mday);
680  else
681  sprintf(str, "%04d-%02d-%02d %s",
682  -(tm->tm_year - 1), tm->tm_mon, tm->tm_mday, "BC");
683  break;
684 
685  case USE_SQL_DATES:
686  /* compatible with Oracle/Ingres date formats */
687  if (EuroDates)
688  sprintf(str, "%02d/%02d", tm->tm_mday, tm->tm_mon);
689  else
690  sprintf(str, "%02d/%02d", tm->tm_mon, tm->tm_mday);
691  if (tm->tm_year > 0)
692  sprintf(str + 5, "/%04d", tm->tm_year);
693  else
694  sprintf(str + 5, "/%04d %s", -(tm->tm_year - 1), "BC");
695  break;
696 
697  case USE_GERMAN_DATES:
698  /* German-style date format */
699  sprintf(str, "%02d.%02d", tm->tm_mday, tm->tm_mon);
700  if (tm->tm_year > 0)
701  sprintf(str + 5, ".%04d", tm->tm_year);
702  else
703  sprintf(str + 5, ".%04d %s", -(tm->tm_year - 1), "BC");
704  break;
705 
706  case USE_POSTGRES_DATES:
707  default:
708  /* traditional date-only style for Postgres */
709  if (EuroDates)
710  sprintf(str, "%02d-%02d", tm->tm_mday, tm->tm_mon);
711  else
712  sprintf(str, "%02d-%02d", tm->tm_mon, tm->tm_mday);
713  if (tm->tm_year > 0)
714  sprintf(str + 5, "-%04d", tm->tm_year);
715  else
716  sprintf(str + 5, "-%04d %s", -(tm->tm_year - 1), "BC");
717  break;
718  }
719 }
Assert(fmt[strlen(fmt) - 1] !='\n')
#define USE_SQL_DATES
Definition: miscadmin.h:231
#define USE_POSTGRES_DATES
Definition: miscadmin.h:229
#define USE_ISO_DATES
Definition: miscadmin.h:230
#define USE_GERMAN_DATES
Definition: miscadmin.h:232
#define sprintf
Definition: port.h:227

References Assert(), MONTHS_PER_YEAR, sprintf, generate_unaccent_rules::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 753 of file dt_common.c.

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

References date2j(), days, MAXTZLEN, MINS_PER_HOUR, months, SECS_PER_HOUR, sprintf, generate_unaccent_rules::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 2352 of file dt_common.c.

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

References fmt, and generate_unaccent_rules::str.

Referenced by pgtypes_defmt_scan().

◆ GetCurrentDateTime()

void GetCurrentDateTime ( struct tm tm)

Definition at line 1058 of file dt_common.c.

1059 {
1060  int tz;
1061 
1062  abstime2tm(time(NULL), &tz, tm, NULL);
1063 }
static void abstime2tm(AbsoluteTime _time, int *tzp, struct tm *tm, char **tzn)
Definition: dt_common.c:972

References abstime2tm(), and tm.

Referenced by DecodeDateTime().

◆ GetEpochTime()

int GetEpochTime ( struct tm tm)

Definition at line 949 of file dt_common.c.

950 {
951  struct tm *t0;
952  time_t epoch = 0;
953 
954  t0 = gmtime(&epoch);
955 
956  if (t0)
957  {
958  tm->tm_year = t0->tm_year + 1900;
959  tm->tm_mon = t0->tm_mon + 1;
960  tm->tm_mday = t0->tm_mday;
961  tm->tm_hour = t0->tm_hour;
962  tm->tm_min = t0->tm_min;
963  tm->tm_sec = t0->tm_sec;
964 
965  return 0;
966  }
967 
968  return -1;
969 } /* GetEpochTime() */
static const unsigned __int64 epoch
Definition: gettimeofday.c:34

References epoch, 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 606 of file dt_common.c.

607 {
608  unsigned int julian;
609  unsigned int quad;
610  unsigned int extra;
611  int y;
612 
613  julian = jd;
614  julian += 32044;
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;
622  y += quad * 4;
623  *year = y - 4800;
624  quad = julian * 2141 / 65536;
625  *day = julian - 7834 * quad / 256;
626  *month = (quad + 10) % 12 + 1;
627 } /* j2date() */

References y.

Referenced by DecodeDateTime(), and DecodeNumber().

◆ ParseDateTime()

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

Definition at line 1598 of file dt_common.c.

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

References DTK_DATE, DTK_NUMBER, DTK_SPECIAL, DTK_STRING, DTK_TIME, DTK_TZ, 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 2457 of file dt_common.c.

2458 {
2459  /*
2460  * scan everything between pstr and pstr_end. This is not including the
2461  * last character so we might set it to '\0' for the parsing
2462  */
2463 
2464  char last_char;
2465  int err = 0;
2466  char *pstr_end;
2467  char *strtol_end = NULL;
2468 
2469  while (**pstr == ' ')
2470  pstr++;
2471  pstr_end = find_end_token(*pstr, pfmt);
2472  if (!pstr_end)
2473  {
2474  /* there was an error, no match */
2475  return 1;
2476  }
2477  last_char = *pstr_end;
2478  *pstr_end = '\0';
2479 
2480  switch (scan_type)
2481  {
2482  case PGTYPES_TYPE_UINT:
2483 
2484  /*
2485  * numbers may be blank-padded, this is the only deviation from
2486  * the fmt-string we accept
2487  */
2488  while (**pstr == ' ')
2489  (*pstr)++;
2490  errno = 0;
2491  scan_val->uint_val = (unsigned int) strtol(*pstr, &strtol_end, 10);
2492  if (errno)
2493  err = 1;
2494  break;
2496  while (**pstr == ' ')
2497  (*pstr)++;
2498  errno = 0;
2499  scan_val->luint_val = (unsigned long int) strtol(*pstr, &strtol_end, 10);
2500  if (errno)
2501  err = 1;
2502  break;
2504  scan_val->str_val = pgtypes_strdup(*pstr);
2505  if (scan_val->str_val == NULL)
2506  err = 1;
2507  break;
2508  }
2509  if (strtol_end && *strtol_end)
2510  *pstr = strtol_end;
2511  else
2512  *pstr = pstr_end;
2513  *pstr_end = last_char;
2514  return err;
2515 }
static char * find_end_token(char *str, char *fmt)
Definition: dt_common.c:2352
char * pgtypes_strdup(const char *str)
Definition: common.c:20
#define PGTYPES_TYPE_UINT
#define PGTYPES_TYPE_STRING_MALLOCED
#define PGTYPES_TYPE_UINT_LONG
unsigned int uint_val
unsigned long int luint_val

References find_end_token(), un_fmt_comb::luint_val, pgtypes_strdup(), PGTYPES_TYPE_STRING_MALLOCED, PGTYPES_TYPE_UINT, PGTYPES_TYPE_UINT_LONG, un_fmt_comb::str_val, and un_fmt_comb::uint_val.

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 2519 of file dt_common.c.

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

References datetktbl, day_tab, days, DecodeTimezone(), DTZ, fmt, free, isleap, j, un_fmt_comb::luint_val, 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(), generate_unaccent_rules::str, un_fmt_comb::str_val, 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, generate_unaccent_rules::type, TZ, un_fmt_comb::uint_val, and datetkn::value.

Referenced by PGTYPEStimestamp_defmt_asc(), and PGTYPEStimestamp_defmt_scan().

◆ TrimTrailingZeros()

void TrimTrailingZeros ( char *  str)

Definition at line 722 of file dt_common.c.

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

References len, and generate_unaccent_rules::str.

Referenced by AppendSeconds(), and EncodeDateTime().

Variable Documentation

◆ datecache

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

Definition at line 489 of file dt_common.c.

Referenced by DecodeSpecial().

◆ datetktbl

const datetkn datetktbl[]
static

Definition at line 20 of file dt_common.c.

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.

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.

Referenced by EncodeDateTime(), and PGTYPEStimestamp_defmt_scan().

◆ deltacache

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

Definition at line 491 of file dt_common.c.

Referenced by DecodeUnits().

◆ deltatktbl

const datetkn deltatktbl[]
static

Definition at line 421 of file dt_common.c.

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.

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}

◆ pgtypes_date_weekdays_short

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

◆ 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().