PostgreSQL Source Code  git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
dt.h File Reference
#include <pgtypes_timestamp.h>
#include <time.h>
Include dependency graph for dt.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  datetkn
 

Macros

#define MAXTZLEN   10
 
#define USE_POSTGRES_DATES   0
 
#define USE_ISO_DATES   1
 
#define USE_SQL_DATES   2
 
#define USE_GERMAN_DATES   3
 
#define INTSTYLE_POSTGRES   0
 
#define INTSTYLE_POSTGRES_VERBOSE   1
 
#define INTSTYLE_SQL_STANDARD   2
 
#define INTSTYLE_ISO_8601   3
 
#define INTERVAL_FULL_RANGE   (0x7FFF)
 
#define INTERVAL_MASK(b)   (1 << (b))
 
#define MAX_INTERVAL_PRECISION   6
 
#define DTERR_BAD_FORMAT   (-1)
 
#define DTERR_FIELD_OVERFLOW   (-2)
 
#define DTERR_MD_FIELD_OVERFLOW   (-3) /* triggers hint about DateStyle */
 
#define DTERR_INTERVAL_OVERFLOW   (-4)
 
#define DTERR_TZDISP_OVERFLOW   (-5)
 
#define DAGO   "ago"
 
#define DCURRENT   "current"
 
#define EPOCH   "epoch"
 
#define INVALID   "invalid"
 
#define EARLY   "-infinity"
 
#define LATE   "infinity"
 
#define NOW   "now"
 
#define TODAY   "today"
 
#define TOMORROW   "tomorrow"
 
#define YESTERDAY   "yesterday"
 
#define ZULU   "zulu"
 
#define DMICROSEC   "usecond"
 
#define DMILLISEC   "msecond"
 
#define DSECOND   "second"
 
#define DMINUTE   "minute"
 
#define DHOUR   "hour"
 
#define DDAY   "day"
 
#define DWEEK   "week"
 
#define DMONTH   "month"
 
#define DQUARTER   "quarter"
 
#define DYEAR   "year"
 
#define DDECADE   "decade"
 
#define DCENTURY   "century"
 
#define DMILLENNIUM   "millennium"
 
#define DA_D   "ad"
 
#define DB_C   "bc"
 
#define DTIMEZONE   "timezone"
 
#define AM   0
 
#define PM   1
 
#define HR24   2
 
#define AD   0
 
#define BC   1
 
#define RESERV   0
 
#define MONTH   1
 
#define YEAR   2
 
#define DAY   3
 
#define JULIAN   4
 
#define TZ   5 /* fixed-offset timezone abbreviation */
 
#define DTZ   6 /* fixed-offset timezone abbrev, DST */
 
#define DYNTZ   7 /* dynamic timezone abbr (unimplemented) */
 
#define IGNORE_DTF   8
 
#define AMPM   9
 
#define HOUR   10
 
#define MINUTE   11
 
#define SECOND   12
 
#define MILLISECOND   13
 
#define MICROSECOND   14
 
#define DOY   15
 
#define DOW   16
 
#define UNITS   17
 
#define ADBC   18
 
#define AGO   19
 
#define ABS_BEFORE   20
 
#define ABS_AFTER   21
 
#define ISODATE   22
 
#define ISOTIME   23
 
#define DTZMOD   28 /* "DST" as a separate word */
 
#define UNKNOWN_FIELD   31
 
#define DTK_NUMBER   0
 
#define DTK_STRING   1
 
#define DTK_DATE   2
 
#define DTK_TIME   3
 
#define DTK_TZ   4
 
#define DTK_AGO   5
 
#define DTK_SPECIAL   6
 
#define DTK_EARLY   9
 
#define DTK_LATE   10
 
#define DTK_EPOCH   11
 
#define DTK_NOW   12
 
#define DTK_YESTERDAY   13
 
#define DTK_TODAY   14
 
#define DTK_TOMORROW   15
 
#define DTK_ZULU   16
 
#define DTK_DELTA   17
 
#define DTK_SECOND   18
 
#define DTK_MINUTE   19
 
#define DTK_HOUR   20
 
#define DTK_DAY   21
 
#define DTK_WEEK   22
 
#define DTK_MONTH   23
 
#define DTK_QUARTER   24
 
#define DTK_YEAR   25
 
#define DTK_DECADE   26
 
#define DTK_CENTURY   27
 
#define DTK_MILLENNIUM   28
 
#define DTK_MILLISEC   29
 
#define DTK_MICROSEC   30
 
#define DTK_JULIAN   31
 
#define DTK_DOW   32
 
#define DTK_DOY   33
 
#define DTK_TZ_HOUR   34
 
#define DTK_TZ_MINUTE   35
 
#define DTK_ISOYEAR   36
 
#define DTK_ISODOW   37
 
#define DTK_M(t)   (0x01 << (t))
 
#define DTK_ALL_SECS_M   (DTK_M(SECOND) | DTK_M(MILLISECOND) | DTK_M(MICROSECOND))
 
#define DTK_DATE_M   (DTK_M(YEAR) | DTK_M(MONTH) | DTK_M(DAY))
 
#define DTK_TIME_M   (DTK_M(HOUR) | DTK_M(MINUTE) | DTK_M(SECOND))
 
#define MAXDATELEN   128
 
#define MAXDATEFIELDS   25
 
#define TOKMAXLEN   10
 
#define FMODULO(t, q, u)
 
#define TMODULO(t, q, u)
 
#define DAYS_PER_YEAR   365.25 /* assumes leap year every four years */
 
#define MONTHS_PER_YEAR   12
 
#define DAYS_PER_MONTH   30 /* assumes exactly 30 days per month */
 
#define HOURS_PER_DAY   24 /* assume no daylight savings time changes */
 
#define SECS_PER_YEAR   (36525 * 864) /* avoid floating-point computation */
 
#define SECS_PER_DAY   86400
 
#define SECS_PER_HOUR   3600
 
#define SECS_PER_MINUTE   60
 
#define MINS_PER_HOUR   60
 
#define USECS_PER_DAY   INT64CONST(86400000000)
 
#define USECS_PER_HOUR   INT64CONST(3600000000)
 
#define USECS_PER_MINUTE   INT64CONST(60000000)
 
#define USECS_PER_SEC   INT64CONST(1000000)
 
#define isleap(y)   (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
 
#define JULIAN_MINYEAR   (-4713)
 
#define JULIAN_MINMONTH   (11)
 
#define JULIAN_MINDAY   (24)
 
#define JULIAN_MAXYEAR   (5874898)
 
#define JULIAN_MAXMONTH   (6)
 
#define JULIAN_MAXDAY   (3)
 
#define IS_VALID_JULIAN(y, m, d)
 
#define MIN_TIMESTAMP   INT64CONST(-211813488000000000)
 
#define END_TIMESTAMP   INT64CONST(9223371331200000000)
 
#define IS_VALID_TIMESTAMP(t)   (MIN_TIMESTAMP <= (t) && (t) < END_TIMESTAMP)
 
#define UTIME_MINYEAR   (1901)
 
#define UTIME_MINMONTH   (12)
 
#define UTIME_MINDAY   (14)
 
#define UTIME_MAXYEAR   (2038)
 
#define UTIME_MAXMONTH   (01)
 
#define UTIME_MAXDAY   (18)
 
#define IS_VALID_UTIME(y, m, d)
 
#define DT_NOBEGIN   (-INT64CONST(0x7fffffffffffffff) - 1)
 
#define DT_NOEND   (INT64CONST(0x7fffffffffffffff))
 
#define TIMESTAMP_NOBEGIN(j)   do {(j) = DT_NOBEGIN;} while (0)
 
#define TIMESTAMP_NOEND(j)   do {(j) = DT_NOEND;} while (0)
 
#define TIMESTAMP_IS_NOBEGIN(j)   ((j) == DT_NOBEGIN)
 
#define TIMESTAMP_IS_NOEND(j)   ((j) == DT_NOEND)
 
#define TIMESTAMP_NOT_FINITE(j)   (TIMESTAMP_IS_NOBEGIN(j) || TIMESTAMP_IS_NOEND(j))
 

Typedefs

typedef int32 fsec_t
 

Functions

int DecodeInterval (char **field, int *ftype, int nf, int *dtype, struct tm *tm, fsec_t *fsec)
 
int DecodeTime (char *str, int *tmask, struct tm *tm, fsec_t *fsec)
 
void EncodeDateTime (struct tm *tm, fsec_t fsec, bool print_tz, int tz, const char *tzn, int style, char *str, bool EuroDates)
 
void EncodeInterval (struct tm *tm, fsec_t fsec, int style, char *str)
 
int tm2timestamp (struct tm *tm, fsec_t fsec, int *tzp, timestamp *result)
 
int DecodeUnits (int field, char *lowtoken, int *val)
 
bool CheckDateTokenTables (void)
 
void EncodeDateOnly (struct tm *tm, int style, char *str, bool EuroDates)
 
int GetEpochTime (struct tm *tm)
 
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)
 
void j2date (int jd, int *year, int *month, int *day)
 
void GetCurrentDateTime (struct tm *tm)
 
int date2j (int y, int m, int d)
 
void TrimTrailingZeros (char *str)
 
void dt2time (double jd, int *hour, int *min, int *sec, fsec_t *fsec)
 
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

char * pgtypes_date_weekdays_short []
 
char * pgtypes_date_months []
 
char * months []
 
char * days []
 
const int day_tab [2][13]
 

Macro Definition Documentation

◆ ABS_AFTER

#define ABS_AFTER   21

Definition at line 114 of file dt.h.

◆ ABS_BEFORE

#define ABS_BEFORE   20

Definition at line 113 of file dt.h.

◆ AD

#define AD   0

Definition at line 75 of file dt.h.

◆ ADBC

#define ADBC   18

Definition at line 110 of file dt.h.

◆ AGO

#define AGO   19

Definition at line 112 of file dt.h.

◆ AM

#define AM   0

Definition at line 71 of file dt.h.

◆ AMPM

#define AMPM   9

Definition at line 101 of file dt.h.

◆ BC

#define BC   1

Definition at line 76 of file dt.h.

◆ DA_D

#define DA_D   "ad"

Definition at line 60 of file dt.h.

◆ DAGO

#define DAGO   "ago"

Definition at line 35 of file dt.h.

◆ DAY

#define DAY   3

Definition at line 95 of file dt.h.

◆ DAYS_PER_MONTH

#define DAYS_PER_MONTH   30 /* assumes exactly 30 days per month */

Definition at line 244 of file dt.h.

◆ DAYS_PER_YEAR

#define DAYS_PER_YEAR   365.25 /* assumes leap year every four years */

Definition at line 235 of file dt.h.

◆ DB_C

#define DB_C   "bc"

Definition at line 61 of file dt.h.

◆ DCENTURY

#define DCENTURY   "century"

Definition at line 58 of file dt.h.

◆ DCURRENT

#define DCURRENT   "current"

Definition at line 36 of file dt.h.

◆ DDAY

#define DDAY   "day"

Definition at line 52 of file dt.h.

◆ DDECADE

#define DDECADE   "decade"

Definition at line 57 of file dt.h.

◆ DHOUR

#define DHOUR   "hour"

Definition at line 51 of file dt.h.

◆ DMICROSEC

#define DMICROSEC   "usecond"

Definition at line 47 of file dt.h.

◆ DMILLENNIUM

#define DMILLENNIUM   "millennium"

Definition at line 59 of file dt.h.

◆ DMILLISEC

#define DMILLISEC   "msecond"

Definition at line 48 of file dt.h.

◆ DMINUTE

#define DMINUTE   "minute"

Definition at line 50 of file dt.h.

◆ DMONTH

#define DMONTH   "month"

Definition at line 54 of file dt.h.

◆ DOW

#define DOW   16

Definition at line 108 of file dt.h.

◆ DOY

#define DOY   15

Definition at line 107 of file dt.h.

◆ DQUARTER

#define DQUARTER   "quarter"

Definition at line 55 of file dt.h.

◆ DSECOND

#define DSECOND   "second"

Definition at line 49 of file dt.h.

◆ DT_NOBEGIN

#define DT_NOBEGIN   (-INT64CONST(0x7fffffffffffffff) - 1)

Definition at line 305 of file dt.h.

◆ DT_NOEND

#define DT_NOEND   (INT64CONST(0x7fffffffffffffff))

Definition at line 306 of file dt.h.

◆ DTERR_BAD_FORMAT

#define DTERR_BAD_FORMAT   (-1)

Definition at line 28 of file dt.h.

◆ DTERR_FIELD_OVERFLOW

#define DTERR_FIELD_OVERFLOW   (-2)

Definition at line 29 of file dt.h.

◆ DTERR_INTERVAL_OVERFLOW

#define DTERR_INTERVAL_OVERFLOW   (-4)

Definition at line 31 of file dt.h.

◆ DTERR_MD_FIELD_OVERFLOW

#define DTERR_MD_FIELD_OVERFLOW   (-3) /* triggers hint about DateStyle */

Definition at line 30 of file dt.h.

◆ DTERR_TZDISP_OVERFLOW

#define DTERR_TZDISP_OVERFLOW   (-5)

Definition at line 32 of file dt.h.

◆ DTIMEZONE

#define DTIMEZONE   "timezone"

Definition at line 62 of file dt.h.

◆ DTK_AGO

#define DTK_AGO   5

Definition at line 145 of file dt.h.

◆ DTK_ALL_SECS_M

#define DTK_ALL_SECS_M   (DTK_M(SECOND) | DTK_M(MILLISECOND) | DTK_M(MICROSECOND))

Definition at line 186 of file dt.h.

◆ DTK_CENTURY

#define DTK_CENTURY   27

Definition at line 167 of file dt.h.

◆ DTK_DATE

#define DTK_DATE   2

Definition at line 142 of file dt.h.

◆ DTK_DATE_M

#define DTK_DATE_M   (DTK_M(YEAR) | DTK_M(MONTH) | DTK_M(DAY))

Definition at line 187 of file dt.h.

◆ DTK_DAY

#define DTK_DAY   21

Definition at line 161 of file dt.h.

◆ DTK_DECADE

#define DTK_DECADE   26

Definition at line 166 of file dt.h.

◆ DTK_DELTA

#define DTK_DELTA   17

Definition at line 157 of file dt.h.

◆ DTK_DOW

#define DTK_DOW   32

Definition at line 173 of file dt.h.

◆ DTK_DOY

#define DTK_DOY   33

Definition at line 174 of file dt.h.

◆ DTK_EARLY

#define DTK_EARLY   9

Definition at line 148 of file dt.h.

◆ DTK_EPOCH

#define DTK_EPOCH   11

Definition at line 150 of file dt.h.

◆ DTK_HOUR

#define DTK_HOUR   20

Definition at line 160 of file dt.h.

◆ DTK_ISODOW

#define DTK_ISODOW   37

Definition at line 178 of file dt.h.

◆ DTK_ISOYEAR

#define DTK_ISOYEAR   36

Definition at line 177 of file dt.h.

◆ DTK_JULIAN

#define DTK_JULIAN   31

Definition at line 171 of file dt.h.

◆ DTK_LATE

#define DTK_LATE   10

Definition at line 149 of file dt.h.

◆ DTK_M

#define DTK_M (   t)    (0x01 << (t))

Definition at line 185 of file dt.h.

◆ DTK_MICROSEC

#define DTK_MICROSEC   30

Definition at line 170 of file dt.h.

◆ DTK_MILLENNIUM

#define DTK_MILLENNIUM   28

Definition at line 168 of file dt.h.

◆ DTK_MILLISEC

#define DTK_MILLISEC   29

Definition at line 169 of file dt.h.

◆ DTK_MINUTE

#define DTK_MINUTE   19

Definition at line 159 of file dt.h.

◆ DTK_MONTH

#define DTK_MONTH   23

Definition at line 163 of file dt.h.

◆ DTK_NOW

#define DTK_NOW   12

Definition at line 151 of file dt.h.

◆ DTK_NUMBER

#define DTK_NUMBER   0

Definition at line 139 of file dt.h.

◆ DTK_QUARTER

#define DTK_QUARTER   24

Definition at line 164 of file dt.h.

◆ DTK_SECOND

#define DTK_SECOND   18

Definition at line 158 of file dt.h.

◆ DTK_SPECIAL

#define DTK_SPECIAL   6

Definition at line 147 of file dt.h.

◆ DTK_STRING

#define DTK_STRING   1

Definition at line 140 of file dt.h.

◆ DTK_TIME

#define DTK_TIME   3

Definition at line 143 of file dt.h.

◆ DTK_TIME_M

#define DTK_TIME_M   (DTK_M(HOUR) | DTK_M(MINUTE) | DTK_M(SECOND))

Definition at line 188 of file dt.h.

◆ DTK_TODAY

#define DTK_TODAY   14

Definition at line 153 of file dt.h.

◆ DTK_TOMORROW

#define DTK_TOMORROW   15

Definition at line 154 of file dt.h.

◆ DTK_TZ

#define DTK_TZ   4

Definition at line 144 of file dt.h.

◆ DTK_TZ_HOUR

#define DTK_TZ_HOUR   34

Definition at line 175 of file dt.h.

◆ DTK_TZ_MINUTE

#define DTK_TZ_MINUTE   35

Definition at line 176 of file dt.h.

◆ DTK_WEEK

#define DTK_WEEK   22

Definition at line 162 of file dt.h.

◆ DTK_YEAR

#define DTK_YEAR   25

Definition at line 165 of file dt.h.

◆ DTK_YESTERDAY

#define DTK_YESTERDAY   13

Definition at line 152 of file dt.h.

◆ DTK_ZULU

#define DTK_ZULU   16

Definition at line 155 of file dt.h.

◆ DTZ

#define DTZ   6 /* fixed-offset timezone abbrev, DST */

Definition at line 98 of file dt.h.

◆ DTZMOD

#define DTZMOD   28 /* "DST" as a separate word */

Definition at line 119 of file dt.h.

◆ DWEEK

#define DWEEK   "week"

Definition at line 53 of file dt.h.

◆ DYEAR

#define DYEAR   "year"

Definition at line 56 of file dt.h.

◆ DYNTZ

#define DYNTZ   7 /* dynamic timezone abbr (unimplemented) */

Definition at line 99 of file dt.h.

◆ EARLY

#define EARLY   "-infinity"

Definition at line 39 of file dt.h.

◆ END_TIMESTAMP

#define END_TIMESTAMP   INT64CONST(9223371331200000000)

Definition at line 287 of file dt.h.

◆ EPOCH

#define EPOCH   "epoch"

Definition at line 37 of file dt.h.

◆ FMODULO

#define FMODULO (   t,
  q,
 
)
Value:
do { \
(q) = (((t) < 0) ? ceil((t) / (u)): floor((t) / (u))); \
if ((q) != 0) (t) -= rint((q) * (u)); \
} while(0)

Definition at line 217 of file dt.h.

◆ HOUR

#define HOUR   10

Definition at line 102 of file dt.h.

◆ HOURS_PER_DAY

#define HOURS_PER_DAY   24 /* assume no daylight savings time changes */

Definition at line 245 of file dt.h.

◆ HR24

#define HR24   2

Definition at line 73 of file dt.h.

◆ IGNORE_DTF

#define IGNORE_DTF   8

Definition at line 100 of file dt.h.

◆ INTERVAL_FULL_RANGE

#define INTERVAL_FULL_RANGE   (0x7FFF)

Definition at line 24 of file dt.h.

◆ INTERVAL_MASK

#define INTERVAL_MASK (   b)    (1 << (b))

Definition at line 25 of file dt.h.

◆ INTSTYLE_ISO_8601

#define INTSTYLE_ISO_8601   3

Definition at line 22 of file dt.h.

◆ INTSTYLE_POSTGRES

#define INTSTYLE_POSTGRES   0

Definition at line 19 of file dt.h.

◆ INTSTYLE_POSTGRES_VERBOSE

#define INTSTYLE_POSTGRES_VERBOSE   1

Definition at line 20 of file dt.h.

◆ INTSTYLE_SQL_STANDARD

#define INTSTYLE_SQL_STANDARD   2

Definition at line 21 of file dt.h.

◆ INVALID

#define INVALID   "invalid"

Definition at line 38 of file dt.h.

◆ IS_VALID_JULIAN

#define IS_VALID_JULIAN (   y,
  m,
 
)
Value:
(((y) > JULIAN_MINYEAR || \
((y) == JULIAN_MINYEAR && ((m) >= JULIAN_MINMONTH))) && \
((y) < JULIAN_MAXYEAR || \
((y) == JULIAN_MAXYEAR && ((m) < JULIAN_MAXMONTH))))
#define JULIAN_MAXMONTH
Definition: dt.h:277
#define JULIAN_MAXYEAR
Definition: dt.h:276
#define JULIAN_MINMONTH
Definition: dt.h:274
#define JULIAN_MINYEAR
Definition: dt.h:273
int y
Definition: isn.c:71

Definition at line 280 of file dt.h.

◆ IS_VALID_TIMESTAMP

#define IS_VALID_TIMESTAMP (   t)    (MIN_TIMESTAMP <= (t) && (t) < END_TIMESTAMP)

Definition at line 289 of file dt.h.

◆ IS_VALID_UTIME

#define IS_VALID_UTIME (   y,
  m,
 
)
Value:
((((y) > UTIME_MINYEAR) \
|| (((y) == UTIME_MINYEAR) && (((m) > UTIME_MINMONTH) \
|| (((m) == UTIME_MINMONTH) && ((d) >= UTIME_MINDAY))))) \
&& (((y) < UTIME_MAXYEAR) \
|| (((y) == UTIME_MAXYEAR) && (((m) < UTIME_MAXMONTH) \
|| (((m) == UTIME_MAXMONTH) && ((d) <= UTIME_MAXDAY))))))
#define UTIME_MINMONTH
Definition: dt.h:292
#define UTIME_MINDAY
Definition: dt.h:293
#define UTIME_MAXDAY
Definition: dt.h:296
#define UTIME_MINYEAR
Definition: dt.h:291
#define UTIME_MAXMONTH
Definition: dt.h:295
#define UTIME_MAXYEAR
Definition: dt.h:294

Definition at line 298 of file dt.h.

◆ isleap

#define isleap (   y)    (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))

Definition at line 267 of file dt.h.

◆ ISODATE

#define ISODATE   22

Definition at line 116 of file dt.h.

◆ ISOTIME

#define ISOTIME   23

Definition at line 117 of file dt.h.

◆ JULIAN

#define JULIAN   4

Definition at line 96 of file dt.h.

◆ JULIAN_MAXDAY

#define JULIAN_MAXDAY   (3)

Definition at line 278 of file dt.h.

◆ JULIAN_MAXMONTH

#define JULIAN_MAXMONTH   (6)

Definition at line 277 of file dt.h.

◆ JULIAN_MAXYEAR

#define JULIAN_MAXYEAR   (5874898)

Definition at line 276 of file dt.h.

◆ JULIAN_MINDAY

#define JULIAN_MINDAY   (24)

Definition at line 275 of file dt.h.

◆ JULIAN_MINMONTH

#define JULIAN_MINMONTH   (11)

Definition at line 274 of file dt.h.

◆ JULIAN_MINYEAR

#define JULIAN_MINYEAR   (-4713)

Definition at line 273 of file dt.h.

◆ LATE

#define LATE   "infinity"

Definition at line 40 of file dt.h.

◆ MAX_INTERVAL_PRECISION

#define MAX_INTERVAL_PRECISION   6

Definition at line 26 of file dt.h.

◆ MAXDATEFIELDS

#define MAXDATEFIELDS   25

Definition at line 198 of file dt.h.

◆ MAXDATELEN

#define MAXDATELEN   128

Definition at line 196 of file dt.h.

◆ MAXTZLEN

#define MAXTZLEN   10

Definition at line 10 of file dt.h.

◆ MICROSECOND

#define MICROSECOND   14

Definition at line 106 of file dt.h.

◆ MILLISECOND

#define MILLISECOND   13

Definition at line 105 of file dt.h.

◆ MIN_TIMESTAMP

#define MIN_TIMESTAMP   INT64CONST(-211813488000000000)

Definition at line 286 of file dt.h.

◆ MINS_PER_HOUR

#define MINS_PER_HOUR   60

Definition at line 256 of file dt.h.

◆ MINUTE

#define MINUTE   11

Definition at line 103 of file dt.h.

◆ MONTH

#define MONTH   1

Definition at line 93 of file dt.h.

◆ MONTHS_PER_YEAR

#define MONTHS_PER_YEAR   12

Definition at line 236 of file dt.h.

◆ NOW

#define NOW   "now"

Definition at line 41 of file dt.h.

◆ PM

#define PM   1

Definition at line 72 of file dt.h.

◆ RESERV

#define RESERV   0

Definition at line 92 of file dt.h.

◆ SECOND

#define SECOND   12

Definition at line 104 of file dt.h.

◆ SECS_PER_DAY

#define SECS_PER_DAY   86400

Definition at line 253 of file dt.h.

◆ SECS_PER_HOUR

#define SECS_PER_HOUR   3600

Definition at line 254 of file dt.h.

◆ SECS_PER_MINUTE

#define SECS_PER_MINUTE   60

Definition at line 255 of file dt.h.

◆ SECS_PER_YEAR

#define SECS_PER_YEAR   (36525 * 864) /* avoid floating-point computation */

Definition at line 252 of file dt.h.

◆ TIMESTAMP_IS_NOBEGIN

#define TIMESTAMP_IS_NOBEGIN (   j)    ((j) == DT_NOBEGIN)

Definition at line 310 of file dt.h.

◆ TIMESTAMP_IS_NOEND

#define TIMESTAMP_IS_NOEND (   j)    ((j) == DT_NOEND)

Definition at line 311 of file dt.h.

◆ TIMESTAMP_NOBEGIN

#define TIMESTAMP_NOBEGIN (   j)    do {(j) = DT_NOBEGIN;} while (0)

Definition at line 308 of file dt.h.

◆ TIMESTAMP_NOEND

#define TIMESTAMP_NOEND (   j)    do {(j) = DT_NOEND;} while (0)

Definition at line 309 of file dt.h.

◆ TIMESTAMP_NOT_FINITE

#define TIMESTAMP_NOT_FINITE (   j)    (TIMESTAMP_IS_NOBEGIN(j) || TIMESTAMP_IS_NOEND(j))

Definition at line 312 of file dt.h.

◆ TMODULO

#define TMODULO (   t,
  q,
 
)
Value:
do { \
(q) = ((t) / (u)); \
if ((q) != 0) (t) -= ((q) * (u)); \
} while(0)

Definition at line 228 of file dt.h.

◆ TODAY

#define TODAY   "today"

Definition at line 42 of file dt.h.

◆ TOKMAXLEN

#define TOKMAXLEN   10

Definition at line 200 of file dt.h.

◆ TOMORROW

#define TOMORROW   "tomorrow"

Definition at line 43 of file dt.h.

◆ TZ

#define TZ   5 /* fixed-offset timezone abbreviation */

Definition at line 97 of file dt.h.

◆ UNITS

#define UNITS   17

Definition at line 109 of file dt.h.

◆ UNKNOWN_FIELD

#define UNKNOWN_FIELD   31

Definition at line 121 of file dt.h.

◆ USE_GERMAN_DATES

#define USE_GERMAN_DATES   3

Definition at line 17 of file dt.h.

◆ USE_ISO_DATES

#define USE_ISO_DATES   1

Definition at line 15 of file dt.h.

◆ USE_POSTGRES_DATES

#define USE_POSTGRES_DATES   0

Definition at line 14 of file dt.h.

◆ USE_SQL_DATES

#define USE_SQL_DATES   2

Definition at line 16 of file dt.h.

◆ USECS_PER_DAY

#define USECS_PER_DAY   INT64CONST(86400000000)

Definition at line 258 of file dt.h.

◆ USECS_PER_HOUR

#define USECS_PER_HOUR   INT64CONST(3600000000)

Definition at line 259 of file dt.h.

◆ USECS_PER_MINUTE

#define USECS_PER_MINUTE   INT64CONST(60000000)

Definition at line 260 of file dt.h.

◆ USECS_PER_SEC

#define USECS_PER_SEC   INT64CONST(1000000)

Definition at line 261 of file dt.h.

◆ UTIME_MAXDAY

#define UTIME_MAXDAY   (18)

Definition at line 296 of file dt.h.

◆ UTIME_MAXMONTH

#define UTIME_MAXMONTH   (01)

Definition at line 295 of file dt.h.

◆ UTIME_MAXYEAR

#define UTIME_MAXYEAR   (2038)

Definition at line 294 of file dt.h.

◆ UTIME_MINDAY

#define UTIME_MINDAY   (14)

Definition at line 293 of file dt.h.

◆ UTIME_MINMONTH

#define UTIME_MINMONTH   (12)

Definition at line 292 of file dt.h.

◆ UTIME_MINYEAR

#define UTIME_MINYEAR   (1901)

Definition at line 291 of file dt.h.

◆ YEAR

#define YEAR   2

Definition at line 94 of file dt.h.

◆ YESTERDAY

#define YESTERDAY   "yesterday"

Definition at line 44 of file dt.h.

◆ ZULU

#define ZULU   "zulu"

Definition at line 45 of file dt.h.

Typedef Documentation

◆ fsec_t

typedef int32 fsec_t

Definition at line 12 of file dt.h.

Function Documentation

◆ CheckDateTokenTables()

bool CheckDateTokenTables ( void  )

Definition at line 4811 of file datetime.c.

4812 {
4813  bool ok = true;
4814 
4815  Assert(UNIX_EPOCH_JDATE == date2j(1970, 1, 1));
4816  Assert(POSTGRES_EPOCH_JDATE == date2j(2000, 1, 1));
4817 
4818  ok &= CheckDateTokenTable("datetktbl", datetktbl, szdatetktbl);
4819  ok &= CheckDateTokenTable("deltatktbl", deltatktbl, szdeltatktbl);
4820  return ok;
4821 }
static bool CheckDateTokenTable(const char *tablename, const datetkn *base, int nel)
Definition: datetime.c:4779
static const datetkn datetktbl[]
Definition: datetime.c:105
static const int szdeltatktbl
Definition: datetime.c:252
static const int szdatetktbl
Definition: datetime.c:181
int date2j(int year, int month, int day)
Definition: datetime.c:286
static const datetkn deltatktbl[]
Definition: datetime.c:187
#define Assert(condition)
Definition: c.h:837
#define UNIX_EPOCH_JDATE
Definition: timestamp.h:234
#define POSTGRES_EPOCH_JDATE
Definition: timestamp.h:235

References Assert, CheckDateTokenTable(), date2j(), datetktbl, deltatktbl, POSTGRES_EPOCH_JDATE, szdatetktbl, szdeltatktbl, and UNIX_EPOCH_JDATE.

Referenced by PostmasterMain().

◆ date2j()

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

Definition at line 286 of file datetime.c.

287 {
288  int julian;
289  int century;
290 
291  if (month > 2)
292  {
293  month += 1;
294  year += 4800;
295  }
296  else
297  {
298  month += 13;
299  year += 4799;
300  }
301 
302  century = year / 100;
303  julian = year * 365 - 32167;
304  julian += year / 4 - century + century / 4;
305  julian += 7834 * month / 256 + day;
306 
307  return julian;
308 } /* date2j() */

References y.

Referenced by CheckDateTokenTables(), date2isoweek(), date2isoyear(), date2isoyearday(), date_in(), DCH_to_char(), DecodeDateTime(), DecodeNumber(), DetermineTimeZoneOffsetInternal(), EncodeDateTime(), extract_date(), GetSQLCurrentDate(), isoweek2j(), make_date(), make_timestamp_internal(), parse_datetime(), PGTYPESdate_dayofweek(), PGTYPESdate_defmt_asc(), PGTYPESdate_fmt_asc(), PGTYPESdate_from_asc(), PGTYPESdate_julmdy(), PGTYPESdate_mdyjul(), PGTYPESdate_to_asc(), PGTYPESdate_today(), timestamp2tm(), timestamp_date(), timestamp_part_common(), timestamp_pl_interval(), timestamp_to_char(), timestamptz_date(), timestamptz_part_common(), timestamptz_pl_interval_internal(), timestamptz_to_char(), tm2timestamp(), to_date(), and ValidateDate().

◆ DecodeDateTime()

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

Definition at line 1782 of file dt_common.c.

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

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

◆ DecodeInterval()

int DecodeInterval ( char **  field,
int *  ftype,
int  nf,
int *  dtype,
struct tm tm,
fsec_t fsec 
)

Definition at line 326 of file interval.c.

328 {
331  bool is_before = false;
332  char *cp;
333  int fmask = 0,
334  tmask,
335  type;
336  int i;
337  int dterr;
338  int val;
339  double fval;
340 
341  *dtype = DTK_DELTA;
342  type = IGNORE_DTF;
343  ClearPgTm(tm, fsec);
344 
345  /* read through list backwards to pick up units before values */
346  for (i = nf - 1; i >= 0; i--)
347  {
348  switch (ftype[i])
349  {
350  case DTK_TIME:
351  dterr = DecodeTime(field[i], /* range, */
352  &tmask, tm, fsec);
353  if (dterr)
354  return dterr;
355  type = DTK_DAY;
356  break;
357 
358  case DTK_TZ:
359 
360  /*
361  * Timezone is a token with a leading sign character and at
362  * least one digit; there could be ':', '.', '-' embedded in
363  * it as well.
364  */
365  Assert(*field[i] == '-' || *field[i] == '+');
366 
367  /*
368  * Try for hh:mm or hh:mm:ss. If not, fall through to
369  * DTK_NUMBER case, which can handle signed float numbers and
370  * signed year-month values.
371  */
372  if (strchr(field[i] + 1, ':') != NULL &&
373  DecodeTime(field[i] + 1, /* INTERVAL_FULL_RANGE, */
374  &tmask, tm, fsec) == 0)
375  {
376  if (*field[i] == '-')
377  {
378  /* flip the sign on all fields */
379  tm->tm_hour = -tm->tm_hour;
380  tm->tm_min = -tm->tm_min;
381  tm->tm_sec = -tm->tm_sec;
382  *fsec = -(*fsec);
383  }
384 
385  /*
386  * Set the next type to be a day, if units are not
387  * specified. This handles the case of '1 +02:03' since we
388  * are reading right to left.
389  */
390  type = DTK_DAY;
391  tmask = DTK_M(TZ);
392  break;
393  }
394  /* FALL THROUGH */
395 
396  case DTK_DATE:
397  case DTK_NUMBER:
398  if (type == IGNORE_DTF)
399  {
400  /* use typmod to decide what rightmost field is */
401  switch (range)
402  {
403  case INTERVAL_MASK(YEAR):
404  type = DTK_YEAR;
405  break;
406  case INTERVAL_MASK(MONTH):
408  type = DTK_MONTH;
409  break;
410  case INTERVAL_MASK(DAY):
411  type = DTK_DAY;
412  break;
413  case INTERVAL_MASK(HOUR):
417  type = DTK_HOUR;
418  break;
419  case INTERVAL_MASK(MINUTE):
421  type = DTK_MINUTE;
422  break;
423  case INTERVAL_MASK(SECOND):
426  type = DTK_SECOND;
427  break;
428  default:
429  type = DTK_SECOND;
430  break;
431  }
432  }
433 
434  errno = 0;
435  val = strtoint(field[i], &cp, 10);
436  if (errno == ERANGE)
437  return DTERR_FIELD_OVERFLOW;
438 
439  if (*cp == '-')
440  {
441  /* SQL "years-months" syntax */
442  int val2;
443 
444  val2 = strtoint(cp + 1, &cp, 10);
445  if (errno == ERANGE || val2 < 0 || val2 >= MONTHS_PER_YEAR)
446  return DTERR_FIELD_OVERFLOW;
447  if (*cp != '\0')
448  return DTERR_BAD_FORMAT;
449  type = DTK_MONTH;
450  if (*field[i] == '-')
451  val2 = -val2;
452  val = val * MONTHS_PER_YEAR + val2;
453  fval = 0;
454  }
455  else if (*cp == '.')
456  {
457  errno = 0;
458  fval = strtod(cp, &cp);
459  if (*cp != '\0' || errno != 0)
460  return DTERR_BAD_FORMAT;
461 
462  if (*field[i] == '-')
463  fval = -fval;
464  }
465  else if (*cp == '\0')
466  fval = 0;
467  else
468  return DTERR_BAD_FORMAT;
469 
470  tmask = 0; /* DTK_M(type); */
471 
472  switch (type)
473  {
474  case DTK_MICROSEC:
475  *fsec += rint(val + fval);
476  tmask = DTK_M(MICROSECOND);
477  break;
478 
479  case DTK_MILLISEC:
480  *fsec += rint((val + fval) * 1000);
481  tmask = DTK_M(MILLISECOND);
482  break;
483 
484  case DTK_SECOND:
485  tm->tm_sec += val;
486  *fsec += rint(fval * 1000000);
487 
488  /*
489  * If any subseconds were specified, consider this
490  * microsecond and millisecond input as well.
491  */
492  if (fval == 0)
493  tmask = DTK_M(SECOND);
494  else
495  tmask = DTK_ALL_SECS_M;
496  break;
497 
498  case DTK_MINUTE:
499  tm->tm_min += val;
500  AdjustFractSeconds(fval, tm, fsec, SECS_PER_MINUTE);
501  tmask = DTK_M(MINUTE);
502  break;
503 
504  case DTK_HOUR:
505  tm->tm_hour += val;
506  AdjustFractSeconds(fval, tm, fsec, SECS_PER_HOUR);
507  tmask = DTK_M(HOUR);
508  type = DTK_DAY;
509  break;
510 
511  case DTK_DAY:
512  tm->tm_mday += val;
513  AdjustFractSeconds(fval, tm, fsec, SECS_PER_DAY);
514  tmask = (fmask & DTK_M(DAY)) ? 0 : DTK_M(DAY);
515  break;
516 
517  case DTK_WEEK:
518  tm->tm_mday += val * 7;
519  AdjustFractDays(fval, tm, fsec, 7);
520  tmask = (fmask & DTK_M(DAY)) ? 0 : DTK_M(DAY);
521  break;
522 
523  case DTK_MONTH:
524  tm->tm_mon += val;
525  AdjustFractDays(fval, tm, fsec, DAYS_PER_MONTH);
526  tmask = DTK_M(MONTH);
527  break;
528 
529  case DTK_YEAR:
530  tm->tm_year += val;
531  tm->tm_mon += rint(fval * MONTHS_PER_YEAR);
532  tmask = (fmask & DTK_M(YEAR)) ? 0 : DTK_M(YEAR);
533  break;
534 
535  case DTK_DECADE:
536  tm->tm_year += val * 10;
537  tm->tm_mon += rint(fval * MONTHS_PER_YEAR * 10);
538  tmask = (fmask & DTK_M(YEAR)) ? 0 : DTK_M(YEAR);
539  break;
540 
541  case DTK_CENTURY:
542  tm->tm_year += val * 100;
543  tm->tm_mon += rint(fval * MONTHS_PER_YEAR * 100);
544  tmask = (fmask & DTK_M(YEAR)) ? 0 : DTK_M(YEAR);
545  break;
546 
547  case DTK_MILLENNIUM:
548  tm->tm_year += val * 1000;
549  tm->tm_mon += rint(fval * MONTHS_PER_YEAR * 1000);
550  tmask = (fmask & DTK_M(YEAR)) ? 0 : DTK_M(YEAR);
551  break;
552 
553  default:
554  return DTERR_BAD_FORMAT;
555  }
556  break;
557 
558  case DTK_STRING:
559  case DTK_SPECIAL:
560  type = DecodeUnits(i, field[i], &val);
561  if (type == IGNORE_DTF)
562  continue;
563 
564  tmask = 0; /* DTK_M(type); */
565  switch (type)
566  {
567  case UNITS:
568  type = val;
569  break;
570 
571  case AGO:
572  is_before = true;
573  type = val;
574  break;
575 
576  case RESERV:
577  tmask = (DTK_DATE_M | DTK_TIME_M);
578  *dtype = val;
579  break;
580 
581  default:
582  return DTERR_BAD_FORMAT;
583  }
584  break;
585 
586  default:
587  return DTERR_BAD_FORMAT;
588  }
589 
590  if (tmask & fmask)
591  return DTERR_BAD_FORMAT;
592  fmask |= tmask;
593  }
594 
595  /* ensure that at least one time field has been found */
596  if (fmask == 0)
597  return DTERR_BAD_FORMAT;
598 
599  /* ensure fractional seconds are fractional */
600  if (*fsec != 0)
601  {
602  int sec;
603 
604  sec = *fsec / USECS_PER_SEC;
605  *fsec -= sec * USECS_PER_SEC;
606  tm->tm_sec += sec;
607  }
608 
609  /*----------
610  * The SQL standard defines the interval literal
611  * '-1 1:00:00'
612  * to mean "negative 1 days and negative 1 hours", while Postgres
613  * traditionally treats this as meaning "negative 1 days and positive
614  * 1 hours". In SQL_STANDARD intervalstyle, we apply the leading sign
615  * to all fields if there are no other explicit signs.
616  *
617  * We leave the signs alone if there are additional explicit signs.
618  * This protects us against misinterpreting postgres-style dump output,
619  * since the postgres-style output code has always put an explicit sign on
620  * all fields following a negative field. But note that SQL-spec output
621  * is ambiguous and can be misinterpreted on load! (So it's best practice
622  * to dump in postgres style, not SQL style.)
623  *----------
624  */
625  if (IntervalStyle == INTSTYLE_SQL_STANDARD && *field[0] == '-')
626  {
627  /* Check for additional explicit signs */
628  bool more_signs = false;
629 
630  for (i = 1; i < nf; i++)
631  {
632  if (*field[i] == '-' || *field[i] == '+')
633  {
634  more_signs = true;
635  break;
636  }
637  }
638 
639  if (!more_signs)
640  {
641  /*
642  * Rather than re-determining which field was field[0], just force
643  * 'em all negative.
644  */
645  if (*fsec > 0)
646  *fsec = -(*fsec);
647  if (tm->tm_sec > 0)
648  tm->tm_sec = -tm->tm_sec;
649  if (tm->tm_min > 0)
650  tm->tm_min = -tm->tm_min;
651  if (tm->tm_hour > 0)
652  tm->tm_hour = -tm->tm_hour;
653  if (tm->tm_mday > 0)
654  tm->tm_mday = -tm->tm_mday;
655  if (tm->tm_mon > 0)
656  tm->tm_mon = -tm->tm_mon;
657  if (tm->tm_year > 0)
658  tm->tm_year = -tm->tm_year;
659  }
660  }
661 
662  /* finally, AGO negates everything */
663  if (is_before)
664  {
665  *fsec = -(*fsec);
666  tm->tm_sec = -tm->tm_sec;
667  tm->tm_min = -tm->tm_min;
668  tm->tm_hour = -tm->tm_hour;
669  tm->tm_mday = -tm->tm_mday;
670  tm->tm_mon = -tm->tm_mon;
671  tm->tm_year = -tm->tm_year;
672  }
673 
674  return 0;
675 }
int DecodeUnits(int field, const char *lowtoken, int *val)
Definition: datetime.c:4047
static int DecodeTime(char *str, int fmask, int range, int *tmask, struct pg_tm *tm, fsec_t *fsec)
Definition: datetime.c:2672
#define SECS_PER_HOUR
Definition: timestamp.h:127
#define MONTHS_PER_YEAR
Definition: timestamp.h:108
#define SECS_PER_MINUTE
Definition: timestamp.h:128
#define USECS_PER_SEC
Definition: timestamp.h:134
#define DAYS_PER_MONTH
Definition: timestamp.h:116
#define SECS_PER_DAY
Definition: timestamp.h:126
int IntervalStyle
Definition: globals.c:126
#define DTK_DECADE
Definition: datetime.h:168
#define DTK_DELTA
Definition: datetime.h:159
#define MICROSECOND
Definition: datetime.h:104
#define DTK_CENTURY
Definition: datetime.h:169
#define MILLISECOND
Definition: datetime.h:103
#define DTERR_BAD_FORMAT
Definition: datetime.h:282
#define DTK_MILLENNIUM
Definition: datetime.h:170
#define DTK_ALL_SECS_M
Definition: datetime.h:190
#define DTK_WEEK
Definition: datetime.h:164
#define DTK_MICROSEC
Definition: datetime.h:172
#define AGO
Definition: datetime.h:110
#define DTK_MILLISEC
Definition: datetime.h:171
#define DTERR_FIELD_OVERFLOW
Definition: datetime.h:283
static void AdjustFractDays(double frac, struct tm *tm, fsec_t *fsec, int scale)
Definition: interval.c:41
static void ClearPgTm(struct tm *tm, fsec_t *fsec)
Definition: interval.c:94
static void AdjustFractSeconds(double frac, struct tm *tm, fsec_t *fsec, int scale)
Definition: interval.c:23
#define INTSTYLE_SQL_STANDARD
Definition: miscadmin.h:258
#define INTSTYLE_POSTGRES_VERBOSE
Definition: miscadmin.h:257
static struct cvec * range(struct vars *v, chr a, chr b, int cases)
Definition: regc_locale.c:412
#define INTERVAL_FULL_RANGE
Definition: timestamp.h:76
#define INTERVAL_MASK(b)
Definition: timestamp.h:73

References AdjustFractDays(), AdjustFractSeconds(), AGO, Assert, ClearPgTm(), DAY, DAYS_PER_MONTH, DecodeTime(), DecodeUnits(), DTERR_BAD_FORMAT, DTERR_FIELD_OVERFLOW, DTK_ALL_SECS_M, DTK_CENTURY, DTK_DATE, DTK_DATE_M, DTK_DAY, DTK_DECADE, DTK_DELTA, DTK_HOUR, DTK_M, DTK_MICROSEC, DTK_MILLENNIUM, DTK_MILLISEC, DTK_MINUTE, DTK_MONTH, DTK_NUMBER, DTK_SECOND, DTK_SPECIAL, DTK_STRING, DTK_TIME, DTK_TIME_M, DTK_TZ, DTK_WEEK, DTK_YEAR, HOUR, i, IGNORE_DTF, INTERVAL_FULL_RANGE, INTERVAL_MASK, IntervalStyle, INTSTYLE_POSTGRES_VERBOSE, INTSTYLE_SQL_STANDARD, MICROSECOND, MILLISECOND, MINUTE, MONTH, MONTHS_PER_YEAR, range(), RESERV, SECOND, SECS_PER_DAY, SECS_PER_HOUR, SECS_PER_MINUTE, strtoint(), tm, pg_tm::tm_hour, pg_tm::tm_mday, pg_tm::tm_min, pg_tm::tm_mon, pg_tm::tm_sec, pg_tm::tm_year, type, TZ, UNITS, USECS_PER_SEC, val, and YEAR.

Referenced by PGTYPESinterval_from_asc().

◆ DecodeTime()

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

Definition at line 1437 of file dt_common.c.

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

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

Referenced by DecodeDateTime().

◆ DecodeUnits()

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

Definition at line 536 of file dt_common.c.

537 {
538  int type;
539  const datetkn *tp;
540 
541  /* use strncmp so that we match truncated tokens */
542  if (deltacache[field] != NULL &&
543  strncmp(lowtoken, deltacache[field]->token, TOKMAXLEN) == 0)
544  tp = deltacache[field];
545  else
546  tp = datebsearch(lowtoken, deltatktbl, szdeltatktbl);
547  deltacache[field] = tp;
548  if (tp == NULL)
549  {
551  *val = 0;
552  }
553  else
554  {
555  type = tp->type;
556  *val = tp->value;
557  }
558 
559  return type;
560 } /* DecodeUnits() */
static const datetkn * deltacache[MAXDATEFIELDS]
Definition: dt_common.c:491
static const unsigned int szdeltatktbl
Definition: dt_common.c:487
static const datetkn deltatktbl[]
Definition: dt_common.c:421
static const datetkn * datebsearch(const char *key, const datetkn *base, unsigned int nel)
Definition: dt_common.c:502
#define UNKNOWN_FIELD
Definition: datetime.h:124
#define TOKMAXLEN
Definition: datetime.h:204
#define token
Definition: indent_globs.h:126
int32 value
Definition: datetime.h:211
char type
Definition: datetime.h:210

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

◆ dt2time()

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

Definition at line 1068 of file dt_common.c.

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

References USECS_PER_HOUR, USECS_PER_MINUTE, and USECS_PER_SEC.

Referenced by DecodeDateTime().

◆ EncodeDateOnly()

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

Definition at line 669 of file dt_common.c.

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

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

◆ EncodeDateTime()

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

Definition at line 753 of file dt_common.c.

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

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

◆ EncodeInterval()

void EncodeInterval ( struct tm tm,
fsec_t  fsec,
int  style,
char *  str 
)

Definition at line 759 of file interval.c.

760 {
761  char *cp = str;
762  int year = tm->tm_year;
763  int mon = tm->tm_mon;
764  int mday = tm->tm_mday;
765  int hour = tm->tm_hour;
766  int min = tm->tm_min;
767  int sec = tm->tm_sec;
768  bool is_before = false;
769  bool is_zero = true;
770 
771  /*
772  * The sign of year and month are guaranteed to match, since they are
773  * stored internally as "month". But we'll need to check for is_before and
774  * is_zero when determining the signs of day and hour/minute/seconds
775  * fields.
776  */
777  switch (style)
778  {
779  /* SQL Standard interval format */
781  {
782  bool has_negative = year < 0 || mon < 0 ||
783  mday < 0 || hour < 0 ||
784  min < 0 || sec < 0 || fsec < 0;
785  bool has_positive = year > 0 || mon > 0 ||
786  mday > 0 || hour > 0 ||
787  min > 0 || sec > 0 || fsec > 0;
788  bool has_year_month = year != 0 || mon != 0;
789  bool has_day_time = mday != 0 || hour != 0 ||
790  min != 0 || sec != 0 || fsec != 0;
791  bool has_day = mday != 0;
792  bool sql_standard_value = !(has_negative && has_positive) &&
793  !(has_year_month && has_day_time);
794 
795  /*
796  * SQL Standard wants only 1 "<sign>" preceding the whole
797  * interval ... but can't do that if mixed signs.
798  */
799  if (has_negative && sql_standard_value)
800  {
801  *cp++ = '-';
802  year = -year;
803  mon = -mon;
804  mday = -mday;
805  hour = -hour;
806  min = -min;
807  sec = -sec;
808  fsec = -fsec;
809  }
810 
811  if (!has_negative && !has_positive)
812  {
813  sprintf(cp, "0");
814  }
815  else if (!sql_standard_value)
816  {
817  /*
818  * For non sql-standard interval values, force outputting
819  * the signs to avoid ambiguities with intervals with
820  * mixed sign components.
821  */
822  char year_sign = (year < 0 || mon < 0) ? '-' : '+';
823  char day_sign = (mday < 0) ? '-' : '+';
824  char sec_sign = (hour < 0 || min < 0 ||
825  sec < 0 || fsec < 0) ? '-' : '+';
826 
827  sprintf(cp, "%c%d-%d %c%d %c%d:%02d:",
828  year_sign, abs(year), abs(mon),
829  day_sign, abs(mday),
830  sec_sign, abs(hour), abs(min));
831  cp += strlen(cp);
832  AppendSeconds(cp, sec, fsec, MAX_INTERVAL_PRECISION, true);
833  }
834  else if (has_year_month)
835  {
836  sprintf(cp, "%d-%d", year, mon);
837  }
838  else if (has_day)
839  {
840  sprintf(cp, "%d %d:%02d:", mday, hour, min);
841  cp += strlen(cp);
842  AppendSeconds(cp, sec, fsec, MAX_INTERVAL_PRECISION, true);
843  }
844  else
845  {
846  sprintf(cp, "%d:%02d:", hour, min);
847  cp += strlen(cp);
848  AppendSeconds(cp, sec, fsec, MAX_INTERVAL_PRECISION, true);
849  }
850  }
851  break;
852 
853  /* ISO 8601 "time-intervals by duration only" */
854  case INTSTYLE_ISO_8601:
855  /* special-case zero to avoid printing nothing */
856  if (year == 0 && mon == 0 && mday == 0 &&
857  hour == 0 && min == 0 && sec == 0 && fsec == 0)
858  {
859  sprintf(cp, "PT0S");
860  break;
861  }
862  *cp++ = 'P';
863  cp = AddISO8601IntPart(cp, year, 'Y');
864  cp = AddISO8601IntPart(cp, mon, 'M');
865  cp = AddISO8601IntPart(cp, mday, 'D');
866  if (hour != 0 || min != 0 || sec != 0 || fsec != 0)
867  *cp++ = 'T';
868  cp = AddISO8601IntPart(cp, hour, 'H');
869  cp = AddISO8601IntPart(cp, min, 'M');
870  if (sec != 0 || fsec != 0)
871  {
872  if (sec < 0 || fsec < 0)
873  *cp++ = '-';
874  AppendSeconds(cp, sec, fsec, MAX_INTERVAL_PRECISION, false);
875  cp += strlen(cp);
876  *cp++ = 'S';
877  *cp = '\0';
878  }
879  break;
880 
881  /* Compatible with postgresql < 8.4 when DateStyle = 'iso' */
882  case INTSTYLE_POSTGRES:
883  cp = AddPostgresIntPart(cp, year, "year", &is_zero, &is_before);
884  cp = AddPostgresIntPart(cp, mon, "mon", &is_zero, &is_before);
885  cp = AddPostgresIntPart(cp, mday, "day", &is_zero, &is_before);
886  if (is_zero || hour != 0 || min != 0 || sec != 0 || fsec != 0)
887  {
888  bool minus = (hour < 0 || min < 0 || sec < 0 || fsec < 0);
889 
890  sprintf(cp, "%s%s%02d:%02d:",
891  is_zero ? "" : " ",
892  (minus ? "-" : (is_before ? "+" : "")),
893  abs(hour), abs(min));
894  cp += strlen(cp);
895  AppendSeconds(cp, sec, fsec, MAX_INTERVAL_PRECISION, true);
896  }
897  break;
898 
899  /* Compatible with postgresql < 8.4 when DateStyle != 'iso' */
901  default:
902  strcpy(cp, "@");
903  cp++;
904  cp = AddVerboseIntPart(cp, year, "year", &is_zero, &is_before);
905  cp = AddVerboseIntPart(cp, mon, "mon", &is_zero, &is_before);
906  cp = AddVerboseIntPart(cp, mday, "day", &is_zero, &is_before);
907  cp = AddVerboseIntPart(cp, hour, "hour", &is_zero, &is_before);
908  cp = AddVerboseIntPart(cp, min, "min", &is_zero, &is_before);
909  if (sec != 0 || fsec != 0)
910  {
911  *cp++ = ' ';
912  if (sec < 0 || (sec == 0 && fsec < 0))
913  {
914  if (is_zero)
915  is_before = true;
916  else if (!is_before)
917  *cp++ = '-';
918  }
919  else if (is_before)
920  *cp++ = '-';
921  AppendSeconds(cp, sec, fsec, MAX_INTERVAL_PRECISION, false);
922  cp += strlen(cp);
923  /* We output "ago", not negatives, so use abs(). */
924  sprintf(cp, " sec%s",
925  (abs(sec) != 1 || fsec != 0) ? "s" : "");
926  is_zero = false;
927  }
928  /* identically zero? then put in a unitless zero... */
929  if (is_zero)
930  strcat(cp, " 0");
931  if (is_before)
932  strcat(cp, " ago");
933  break;
934  }
935 }
#define MAX_INTERVAL_PRECISION
Definition: timestamp.h:93
static char * AddPostgresIntPart(char *cp, int value, const char *units, bool *is_zero, bool *is_before)
Definition: interval.c:700
static char * AddISO8601IntPart(char *cp, int value, char units)
Definition: interval.c:723
static char * AddVerboseIntPart(char *cp, int value, const char *units, bool *is_zero, bool *is_before)
Definition: interval.c:680
static void AppendSeconds(char *cp, int sec, fsec_t fsec, int precision, bool fillzeros)
Definition: interval.c:733
#define INTSTYLE_ISO_8601
Definition: miscadmin.h:259
#define INTSTYLE_POSTGRES
Definition: miscadmin.h:256

References AddISO8601IntPart(), AddPostgresIntPart(), AddVerboseIntPart(), AppendSeconds(), INTSTYLE_ISO_8601, INTSTYLE_POSTGRES, INTSTYLE_POSTGRES_VERBOSE, INTSTYLE_SQL_STANDARD, MAX_INTERVAL_PRECISION, sprintf, str, tm, pg_tm::tm_hour, pg_tm::tm_mday, pg_tm::tm_min, pg_tm::tm_mon, pg_tm::tm_sec, and pg_tm::tm_year.

Referenced by PGTYPESinterval_to_asc().

◆ GetCurrentDateTime()

void GetCurrentDateTime ( struct tm tm)

Definition at line 1060 of file dt_common.c.

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

References abstime2tm(), and tm.

Referenced by DecodeDateTime().

◆ GetEpochTime()

int GetEpochTime ( struct tm tm)

Definition at line 949 of file dt_common.c.

950 {
951  struct tm *t0;
952  struct tm tmbuf;
953  time_t epoch = 0;
954 
955  t0 = gmtime_r(&epoch, &tmbuf);
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() */
static const unsigned __int64 epoch

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

◆ j2date()

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

Definition at line 311 of file datetime.c.

312 {
313  unsigned int julian;
314  unsigned int quad;
315  unsigned int extra;
316  int y;
317 
318  julian = jd;
319  julian += 32044;
320  quad = julian / 146097;
321  extra = (julian - quad * 146097) * 4 + 3;
322  julian += 60 + quad * 3 + extra / 146097;
323  quad = julian / 1461;
324  julian -= quad * 1461;
325  y = julian * 4 / 1461;
326  julian = ((y != 0) ? ((julian + 305) % 365) : ((julian + 306) % 366))
327  + 123;
328  y += quad * 4;
329  *year = y - 4800;
330  quad = julian * 2141 / 65536;
331  *day = julian - 7834 * quad / 256;
332  *month = (quad + 10) % MONTHS_PER_YEAR + 1;
333 } /* j2date() */

References MONTHS_PER_YEAR, and y.

Referenced by date2timestamptz_opt_overflow(), date_out(), DecodeDateTime(), DecodeNumber(), DecodeTimeOnly(), do_to_timestamp(), executeDateTimeMethod(), extract_date(), isoweek2date(), isoweekdate2date(), JsonEncodeDateTime(), map_sql_value_to_xml_value(), PGTYPESdate_fmt_asc(), PGTYPESdate_julmdy(), PGTYPESdate_to_asc(), timestamp2tm(), timestamp_pl_interval(), timestamptz_pl_interval_internal(), and ValidateDate().

◆ ParseDateTime()

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

Definition at line 1600 of file dt_common.c.

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

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

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

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

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

Referenced by PGTYPEStimestamp_defmt_asc(), and PGTYPEStimestamp_defmt_scan().

◆ tm2timestamp()

int tm2timestamp ( struct tm tm,
fsec_t  fsec,
int *  tzp,
timestamp result 
)

Definition at line 41 of file timestamp.c.

42 {
43  int dDate;
44  int64 time;
45 
46  /* Prevent overflow in Julian-day routines */
48  return -1;
49 
50  dDate = date2j(tm->tm_year, tm->tm_mon, tm->tm_mday) - date2j(2000, 1, 1);
51  time = time2t(tm->tm_hour, tm->tm_min, tm->tm_sec, fsec);
52  if (unlikely(pg_mul_s64_overflow(dDate, USECS_PER_DAY, result) ||
53  pg_add_s64_overflow(*result, time, result)))
54  return -1;
55  if (tzp != NULL)
56  *result = dt2local(*result, -(*tzp));
57 
58  /* final range check catches just-out-of-range timestamps */
59  if (!IS_VALID_TIMESTAMP(*result))
60  return -1;
61 
62  return 0;
63 } /* tm2timestamp() */
#define unlikely(x)
Definition: c.h:326
#define IS_VALID_JULIAN(y, m, d)
Definition: timestamp.h:227
#define IS_VALID_TIMESTAMP(t)
Definition: timestamp.h:267
static bool pg_mul_s64_overflow(int64 a, int64 b, int64 *result)
Definition: int.h:261
static bool pg_add_s64_overflow(int64 a, int64 b, int64 *result)
Definition: int.h:203
static timestamp dt2local(timestamp dt, int tz)
Definition: timestamp.c:27
static int64 time2t(const int hour, const int min, const int sec, const fsec_t fsec)
Definition: timestamp.c:21

References date2j(), dt2local(), IS_VALID_JULIAN, IS_VALID_TIMESTAMP, pg_add_s64_overflow(), pg_mul_s64_overflow(), time2t(), tm, pg_tm::tm_hour, pg_tm::tm_mday, pg_tm::tm_min, pg_tm::tm_mon, pg_tm::tm_sec, pg_tm::tm_year, unlikely, and USECS_PER_DAY.

◆ TrimTrailingZeros()

void TrimTrailingZeros ( char *  str)

Definition at line 722 of file dt_common.c.

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

References len, and str.

Referenced by AppendSeconds(), and EncodeDateTime().

Variable Documentation

◆ day_tab

◆ days

◆ months

◆ pgtypes_date_months

char* pgtypes_date_months[]
extern

◆ pgtypes_date_weekdays_short

char* pgtypes_date_weekdays_short[]
extern