PostgreSQL Source Code  git master
dt_common.c File Reference
#include "postgres_fe.h"
#include <time.h>
#include <ctype.h>
#include <math.h>
#include "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 datetkndatebsearch (char *key, 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

int day_tab [2][13]
 
static datetkn datetktbl []
 
static datetkn deltatktbl []
 
static const unsigned int szdatetktbl = lengthof(datetktbl)
 
static const unsigned int szdeltatktbl = lengthof(deltatktbl)
 
static datetkndatecache [MAXDATEFIELDS] = {NULL}
 
static 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

Definition at line 17 of file dt_common.c.

Function Documentation

◆ abstime2tm()

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

Definition at line 977 of file dt_common.c.

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

Referenced by GetCurrentDateTime().

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

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

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

◆ datebsearch()

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

Definition at line 506 of file dt_common.c.

References TOKMAXLEN.

Referenced by DecodeSpecial(), and DecodeUnits().

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

◆ DecodeDate()

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

Definition at line 1310 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().

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

◆ DecodeDateTime()

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

Definition at line 1785 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, generate_unaccent_rules::type, TZ, UNITS, USECS_PER_DAY, val, and YEAR.

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

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

Referenced by DecodeDate(), and DecodeDateTime().

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

◆ DecodeNumberField()

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

Definition at line 1091 of file dt_common.c.

References DTK_DATE, DTK_DATE_M, DTK_TIME, DTK_TIME_M, and i.

Referenced by DecodeDateTime(), and DecodeNumber().

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

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

Referenced by DecodeDateTime().

1550 {
1551  int val,
1552  tz;
1553  int type;
1554  char *cp;
1555  char delim;
1556 
1557  cp = str;
1558  while (*cp != '\0' && isalpha((unsigned char) *cp))
1559  cp++;
1560 
1561  if (DecodeTimezone(cp, &tz) != 0)
1562  return -1;
1563 
1564  delim = *cp;
1565  *cp = '\0';
1566  type = DecodeSpecial(MAXDATEFIELDS - 1, str, &val);
1567  *cp = delim;
1568 
1569  switch (type)
1570  {
1571  case DTZ:
1572  case TZ:
1573  *tzp = -(val + tz);
1574  break;
1575 
1576  default:
1577  return -1;
1578  }
1579 
1580  return 0;
1581 } /* DecodePosixTimezone() */
#define TZ
Definition: datetime.h:96
static int DecodeTimezone(char *str, int *tzp)
Definition: dt_common.c:1504
static int DecodeSpecial(int field, char *lowtoken, int *val)
Definition: dt_common.c:641
#define MAXDATEFIELDS
Definition: datetime.h:205
#define DTZ
Definition: datetime.h:97
long val
Definition: informix.c:689

◆ DecodeSpecial()

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

Definition at line 641 of file dt_common.c.

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

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

642 {
643  int type;
644  datetkn *tp;
645 
646  /* use strncmp so that we match truncated tokens */
647  if (datecache[field] != NULL &&
648  strncmp(lowtoken, datecache[field]->token, TOKMAXLEN) == 0)
649  tp = datecache[field];
650  else
651  {
652  tp = NULL;
653  if (!tp)
654  tp = datebsearch(lowtoken, datetktbl, szdatetktbl);
655  }
656  datecache[field] = tp;
657  if (tp == NULL)
658  {
659  type = UNKNOWN_FIELD;
660  *val = 0;
661  }
662  else
663  {
664  type = tp->type;
665  *val = tp->value;
666  }
667 
668  return type;
669 } /* DecodeSpecial() */
static const unsigned int szdatetktbl
Definition: dt_common.c:490
#define UNKNOWN_FIELD
Definition: datetime.h:125
int32 value
Definition: datetime.h:214
static datetkn * datecache[MAXDATEFIELDS]
Definition: dt_common.c:493
char type
Definition: datetime.h:213
#define TOKMAXLEN
Definition: datetime.h:207
static datetkn datetktbl[]
Definition: dt_common.c:19
static datetkn * datebsearch(char *key, datetkn *base, unsigned int nel)
Definition: dt_common.c:506
long val
Definition: informix.c:689

◆ DecodeTime()

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

Definition at line 1439 of file dt_common.c.

References DTK_TIME_M, i, and USECS_PER_SEC.

Referenced by DecodeDateTime().

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

References MINS_PER_HOUR, and SECS_PER_MINUTE.

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

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

◆ DecodeUnits()

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

Definition at line 540 of file dt_common.c.

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

541 {
542  int type;
543  datetkn *tp;
544 
545  /* use strncmp so that we match truncated tokens */
546  if (deltacache[field] != NULL &&
547  strncmp(lowtoken, deltacache[field]->token, TOKMAXLEN) == 0)
548  tp = deltacache[field];
549  else
550  tp = datebsearch(lowtoken, deltatktbl, szdeltatktbl);
551  deltacache[field] = tp;
552  if (tp == NULL)
553  {
554  type = UNKNOWN_FIELD;
555  *val = 0;
556  }
557  else
558  {
559  type = tp->type;
560  *val = tp->value;
561  }
562 
563  return type;
564 } /* DecodeUnits() */
static datetkn deltatktbl[]
Definition: dt_common.c:423
#define UNKNOWN_FIELD
Definition: datetime.h:125
int32 value
Definition: datetime.h:214
static const unsigned int szdeltatktbl
Definition: dt_common.c:491
static datetkn * deltacache[MAXDATEFIELDS]
Definition: dt_common.c:495
char type
Definition: datetime.h:213
#define TOKMAXLEN
Definition: datetime.h:207
static datetkn * datebsearch(char *key, datetkn *base, unsigned int nel)
Definition: dt_common.c:506
long val
Definition: informix.c:689

◆ dt2time()

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

Definition at line 1070 of file dt_common.c.

References USECS_PER_HOUR, USECS_PER_MINUTE, and USECS_PER_SEC.

Referenced by DecodeDateTime().

1071 {
1072  int64 time;
1073 
1074  time = jd;
1075  *hour = time / USECS_PER_HOUR;
1076  time -= (*hour) * USECS_PER_HOUR;
1077  *min = time / USECS_PER_MINUTE;
1078  time -= (*min) * USECS_PER_MINUTE;
1079  *sec = time / USECS_PER_SEC;
1080  *fsec = time - (*sec * USECS_PER_SEC);
1081 } /* 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 675 of file dt_common.c.

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

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

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

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

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

◆ find_end_token()

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

Definition at line 2357 of file dt_common.c.

Referenced by pgtypes_defmt_scan().

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

◆ GetCurrentDateTime()

void GetCurrentDateTime ( struct tm tm)

Definition at line 1062 of file dt_common.c.

References abstime2tm().

Referenced by DecodeDateTime().

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

◆ GetEpochTime()

int GetEpochTime ( struct tm tm)

Definition at line 954 of file dt_common.c.

References epoch.

955 {
956  struct tm *t0;
957  time_t epoch = 0;
958 
959  t0 = gmtime(&epoch);
960 
961  if (t0)
962  {
963  tm->tm_year = t0->tm_year + 1900;
964  tm->tm_mon = t0->tm_mon + 1;
965  tm->tm_mday = t0->tm_mday;
966  tm->tm_hour = t0->tm_hour;
967  tm->tm_min = t0->tm_min;
968  tm->tm_sec = t0->tm_sec;
969 
970  return 0;
971  }
972 
973  return -1;
974 } /* GetEpochTime() */
int tm_hour
Definition: pgtime.h:29
static struct pg_tm tm
Definition: localtime.c:107
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 610 of file dt_common.c.

References MONTHS_PER_YEAR.

Referenced by DecodeDateTime(), and DecodeNumber().

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

◆ ParseDateTime()

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

Definition at line 1602 of file dt_common.c.

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

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

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

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

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

◆ TrimTrailingZeros()

void TrimTrailingZeros ( char *  str)

Definition at line 728 of file dt_common.c.

Referenced by AppendSeconds(), and EncodeDateTime().

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

Variable Documentation

◆ datecache

datetkn* datecache[MAXDATEFIELDS] = {NULL}
static

Definition at line 493 of file dt_common.c.

◆ datetktbl

datetkn datetktbl[]
static

Definition at line 19 of file dt_common.c.

◆ day_tab

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

Referenced by EncodeDateTime(), and PGTYPEStimestamp_defmt_scan().

◆ deltacache

datetkn* deltacache[MAXDATEFIELDS] = {NULL}
static

Definition at line 495 of file dt_common.c.

◆ deltatktbl

datetkn deltatktbl[]
static

Definition at line 423 of file dt_common.c.

◆ months

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

Definition at line 497 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 490 of file dt_common.c.

Referenced by DecodeSpecial(), and PGTYPEStimestamp_defmt_scan().

◆ szdeltatktbl

const unsigned int szdeltatktbl = lengthof(deltatktbl)
static

Definition at line 491 of file dt_common.c.

Referenced by DecodeUnits().