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 "pgtypeslib_extern.h"
#include "dt.h"
#include "pgtypes_timestamp.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 973 of file dt_common.c.

References MAXTZLEN, PGTYPES_TS_BAD_TIMESTAMP, SECS_PER_HOUR, StrNCpy, TIMEZONE_GLOBAL, and TZNAME_GLOBAL.

Referenced by GetCurrentDateTime().

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

◆ date2j()

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

Definition at line 581 of file dt_common.c.

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

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() */

◆ datebsearch()

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

Definition at line 502 of file dt_common.c.

References TOKMAXLEN.

Referenced by DecodeSpecial(), and DecodeUnits().

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

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

References ADBC, BC, DecodeNumber(), DecodeSpecial(), DOY, DTK_DATE_M, DTK_M, i, IGNORE_DTF, MAXDATEFIELDS, MONTH, generate_unaccent_rules::str, generate_unaccent_rules::type, TZ, and val.

Referenced by DecodeDateTime().

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() */
#define IGNORE_DTF
Definition: datetime.h:99
#define TZ
Definition: datetime.h:96
#define ADBC
Definition: datetime.h:109
#define DTK_DATE_M
Definition: datetime.h:192
static struct pg_tm tm
Definition: localtime.c:108
int tm_mon
Definition: pgtime.h:31
int32 fsec_t
Definition: timestamp.h:41
#define MONTH
Definition: datetime.h:92
#define BC
Definition: datetime.h:77
static int DecodeSpecial(int field, char *lowtoken, int *val)
Definition: dt_common.c:637
#define MAXDATEFIELDS
Definition: datetime.h:203
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
int tm_year
Definition: pgtime.h:32
int i
#define DTK_M(t)
Definition: datetime.h:188
#define DOY
Definition: datetime.h:106
long val
Definition: informix.c:684

◆ DecodeDateTime()

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

Definition at line 1781 of file dt_common.c.

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(), generate_unaccent_rules::type, TZ, UNITS, USECS_PER_DAY, val, and YEAR.

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

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

References date2j(), DAY, DecodeNumberField(), DOY, DTK_DATE_M, DTK_M, j2date(), MONTH, MONTHS_PER_YEAR, strtoint(), val, and YEAR.

Referenced by DecodeDate(), and DecodeDateTime().

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() */
static int DecodeNumberField(int len, char *str, int fmask, int *tmask, struct tm *tm, fsec_t *fsec, bool *is2digits)
Definition: dt_common.c:1087
#define DAY
Definition: datetime.h:94
#define YEAR
Definition: datetime.h:93
int date2j(int y, int m, int d)
Definition: dt_common.c:581
#define DTK_DATE_M
Definition: datetime.h:192
static struct pg_tm tm
Definition: localtime.c:108
void j2date(int jd, int *year, int *month, int *day)
Definition: dt_common.c:606
#define MONTHS_PER_YEAR
Definition: timestamp.h:69
int tm_mday
Definition: pgtime.h:30
int tm_mon
Definition: pgtime.h:31
int strtoint(const char *pg_restrict str, char **pg_restrict endptr, int base)
Definition: string.c:50
#define MONTH
Definition: datetime.h:92
int tm_year
Definition: pgtime.h:32
#define DTK_M(t)
Definition: datetime.h:188
int tm_yday
Definition: pgtime.h:34
#define DOY
Definition: datetime.h:106
long val
Definition: informix.c:684

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

References DTK_DATE, DTK_DATE_M, DTK_TIME, DTK_TIME_M, i, and strtoint().

Referenced by DecodeDateTime(), and DecodeNumber().

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() */
int tm_hour
Definition: pgtime.h:29
#define DTK_TIME_M
Definition: datetime.h:193
#define DTK_DATE_M
Definition: datetime.h:192
static struct pg_tm tm
Definition: localtime.c:108
int tm_mday
Definition: pgtime.h:30
int tm_mon
Definition: pgtime.h:31
int strtoint(const char *pg_restrict str, char **pg_restrict endptr, int base)
Definition: string.c:50
#define DTK_TIME
Definition: datetime.h:146
int tm_year
Definition: pgtime.h:32
int i
int tm_sec
Definition: pgtime.h:27
int tm_min
Definition: pgtime.h:28
#define DTK_DATE
Definition: datetime.h:145

◆ DecodePosixTimezone()

static int DecodePosixTimezone ( char *  str,
int *  tzp 
)
static

Definition at line 1545 of file dt_common.c.

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

Referenced by DecodeDateTime().

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';
1562  type = DecodeSpecial(MAXDATEFIELDS - 1, str, &val);
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() */
#define TZ
Definition: datetime.h:96
static int DecodeTimezone(char *str, int *tzp)
Definition: dt_common.c:1500
static int DecodeSpecial(int field, char *lowtoken, int *val)
Definition: dt_common.c:637
#define MAXDATEFIELDS
Definition: datetime.h:203
#define DTZ
Definition: datetime.h:97
long val
Definition: informix.c:684

◆ DecodeSpecial()

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

Definition at line 637 of file dt_common.c.

References datebsearch(), szdatetktbl, TOKMAXLEN, datetkn::type, generate_unaccent_rules::type, UNKNOWN_FIELD, and datetkn::value.

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

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)
650  tp = datebsearch(lowtoken, datetktbl, szdatetktbl);
651  }
652  datecache[field] = tp;
653  if (tp == NULL)
654  {
655  type = UNKNOWN_FIELD;
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
#define UNKNOWN_FIELD
Definition: datetime.h:125
int32 value
Definition: datetime.h:212
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
char type
Definition: datetime.h:211
#define TOKMAXLEN
Definition: datetime.h:205
long val
Definition: informix.c:684

◆ DecodeTime()

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

Definition at line 1435 of file dt_common.c.

References DTK_TIME_M, i, strtoint(), and USECS_PER_SEC.

Referenced by DecodeDateTime().

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:94
int tm_hour
Definition: pgtime.h:29
#define DTK_TIME_M
Definition: datetime.h:193
static struct pg_tm tm
Definition: localtime.c:108
int strtoint(const char *pg_restrict str, char **pg_restrict endptr, int base)
Definition: string.c:50
int i
int tm_sec
Definition: pgtime.h:27
int tm_min
Definition: pgtime.h:28

◆ DecodeTimezone()

static int DecodeTimezone ( char *  str,
int *  tzp 
)
static

Definition at line 1500 of file dt_common.c.

References MINS_PER_HOUR, SECS_PER_MINUTE, and strtoint().

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

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:89
#define SECS_PER_MINUTE
Definition: timestamp.h:88
int strtoint(const char *pg_restrict str, char **pg_restrict endptr, int base)
Definition: string.c:50

◆ DecodeUnits()

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

Definition at line 536 of file dt_common.c.

References datebsearch(), szdeltatktbl, datetkn::token, TOKMAXLEN, datetkn::type, generate_unaccent_rules::type, UNKNOWN_FIELD, and datetkn::value.

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  {
550  type = UNKNOWN_FIELD;
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 deltatktbl[]
Definition: dt_common.c:421
#define UNKNOWN_FIELD
Definition: datetime.h:125
int32 value
Definition: datetime.h:212
static const unsigned int szdeltatktbl
Definition: dt_common.c:487
static const datetkn * datebsearch(const char *key, const datetkn *base, unsigned int nel)
Definition: dt_common.c:502
char type
Definition: datetime.h:211
#define TOKMAXLEN
Definition: datetime.h:205
static const datetkn * deltacache[MAXDATEFIELDS]
Definition: dt_common.c:491
long val
Definition: informix.c:684

◆ dt2time()

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

Definition at line 1066 of file dt_common.c.

References USECS_PER_HOUR, USECS_PER_MINUTE, and USECS_PER_SEC.

Referenced by DecodeDateTime().

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_SEC
Definition: timestamp.h:94
#define USECS_PER_MINUTE
Definition: timestamp.h:93
#define USECS_PER_HOUR
Definition: timestamp.h:92

◆ EncodeDateOnly()

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

Definition at line 671 of file dt_common.c.

References Assert, MONTHS_PER_YEAR, sprintf, USE_GERMAN_DATES, USE_ISO_DATES, USE_POSTGRES_DATES, and USE_SQL_DATES.

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

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

References date2j(), days, MAXTZLEN, MINS_PER_HOUR, months, SECS_PER_HOUR, sprintf, TrimTrailingZeros(), USE_GERMAN_DATES, USE_ISO_DATES, USE_POSTGRES_DATES, and USE_SQL_DATES.

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

◆ find_end_token()

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

Definition at line 2353 of file dt_common.c.

Referenced by pgtypes_defmt_scan().

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

◆ GetCurrentDateTime()

void GetCurrentDateTime ( struct tm tm)

Definition at line 1058 of file dt_common.c.

References abstime2tm().

Referenced by DecodeDateTime().

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

◆ GetEpochTime()

int GetEpochTime ( struct tm tm)

Definition at line 950 of file dt_common.c.

References epoch.

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

◆ j2date()

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

Definition at line 606 of file dt_common.c.

References MONTHS_PER_YEAR.

Referenced by DecodeDateTime(), and DecodeNumber().

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 
628  return;
629 } /* j2date() */

◆ ParseDateTime()

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

Definition at line 1598 of file dt_common.c.

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

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  }
1745  /* otherwise, something is not right... */
1746  else
1747  return -1;
1748 
1749  /* force in a delimiter after each field */
1750  *lp++ = '\0';
1751  nf++;
1752  }
1753 
1754  *numfields = nf;
1755 
1756  return 0;
1757 } /* ParseDateTime() */
unsigned char pg_tolower(unsigned char ch)
Definition: pgstrcasecmp.c:122
#define DTK_TZ
Definition: datetime.h:147
#define DTK_NUMBER
Definition: datetime.h:142
#define DTK_TIME
Definition: datetime.h:146
#define MAXDATEFIELDS
Definition: datetime.h:203
#define DTK_STRING
Definition: datetime.h:143
#define DTK_DATE
Definition: datetime.h:145
#define DTK_SPECIAL
Definition: datetime.h:150

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

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

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

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

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

Referenced by PGTYPEStimestamp_defmt_asc().

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

◆ TrimTrailingZeros()

void TrimTrailingZeros ( char *  str)

Definition at line 724 of file dt_common.c.

Referenced by AppendSeconds(), and EncodeDateTime().

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

Variable Documentation

◆ datecache

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

Definition at line 489 of file dt_common.c.

◆ datetktbl

const datetkn datetktbl[]
static

Definition at line 20 of file dt_common.c.

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

◆ deltatktbl

const datetkn deltatktbl[]
static

Definition at line 421 of file dt_common.c.

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