PostgreSQL Source Code git master
Loading...
Searching...
No Matches
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

charpgtypes_date_weekdays_short []
 
charpgtypes_date_months []
 
charmonths []
 
chardays []
 
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 246 of file dt.h.

◆ DAYS_PER_YEAR

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

Definition at line 237 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 307 of file dt.h.

◆ DT_NOEND

#define DT_NOEND   (INT64CONST(0x7fffffffffffffff))

Definition at line 308 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 289 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)
static int fb(int x)

Definition at line 218 of file dt.h.

219 { \
220 (q) = (((t) < 0) ? ceil((t) / (u)): floor((t) / (u))); \
221 if ((q) != 0) (t) -= rint((q) * (u)); \
222} while(0)

◆ 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 247 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:279
#define JULIAN_MAXYEAR
Definition dt.h:278
#define JULIAN_MINMONTH
Definition dt.h:276
#define JULIAN_MINYEAR
Definition dt.h:275
int y
Definition isn.c:76

Definition at line 282 of file dt.h.

◆ IS_VALID_TIMESTAMP

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

Definition at line 291 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:294
#define UTIME_MINDAY
Definition dt.h:295
#define UTIME_MAXDAY
Definition dt.h:298
#define UTIME_MINYEAR
Definition dt.h:293
#define UTIME_MAXMONTH
Definition dt.h:297
#define UTIME_MAXYEAR
Definition dt.h:296

Definition at line 300 of file dt.h.

◆ isleap

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

Definition at line 269 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 280 of file dt.h.

◆ JULIAN_MAXMONTH

#define JULIAN_MAXMONTH   (6)

Definition at line 279 of file dt.h.

◆ JULIAN_MAXYEAR

#define JULIAN_MAXYEAR   (5874898)

Definition at line 278 of file dt.h.

◆ JULIAN_MINDAY

#define JULIAN_MINDAY   (24)

Definition at line 277 of file dt.h.

◆ JULIAN_MINMONTH

#define JULIAN_MINMONTH   (11)

Definition at line 276 of file dt.h.

◆ JULIAN_MINYEAR

#define JULIAN_MINYEAR   (-4713)

Definition at line 275 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 288 of file dt.h.

◆ MINS_PER_HOUR

#define MINS_PER_HOUR   60

Definition at line 258 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 238 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 255 of file dt.h.

◆ SECS_PER_HOUR

#define SECS_PER_HOUR   3600

Definition at line 256 of file dt.h.

◆ SECS_PER_MINUTE

#define SECS_PER_MINUTE   60

Definition at line 257 of file dt.h.

◆ SECS_PER_YEAR

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

Definition at line 254 of file dt.h.

◆ TIMESTAMP_IS_NOBEGIN

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

Definition at line 312 of file dt.h.

◆ TIMESTAMP_IS_NOEND

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

Definition at line 313 of file dt.h.

◆ TIMESTAMP_NOBEGIN

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

Definition at line 310 of file dt.h.

◆ TIMESTAMP_NOEND

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

Definition at line 311 of file dt.h.

◆ TIMESTAMP_NOT_FINITE

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

Definition at line 314 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 230 of file dt.h.

231 { \
232 (q) = ((t) / (u)); \
233 if ((q) != 0) (t) -= ((q) * (u)); \
234} while(0)

◆ 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 260 of file dt.h.

◆ USECS_PER_HOUR

#define USECS_PER_HOUR   INT64CONST(3600000000)

Definition at line 261 of file dt.h.

◆ USECS_PER_MINUTE

#define USECS_PER_MINUTE   INT64CONST(60000000)

Definition at line 262 of file dt.h.

◆ USECS_PER_SEC

#define USECS_PER_SEC   INT64CONST(1000000)

Definition at line 263 of file dt.h.

◆ UTIME_MAXDAY

#define UTIME_MAXDAY   (18)

Definition at line 298 of file dt.h.

◆ UTIME_MAXMONTH

#define UTIME_MAXMONTH   (01)

Definition at line 297 of file dt.h.

◆ UTIME_MAXYEAR

#define UTIME_MAXYEAR   (2038)

Definition at line 296 of file dt.h.

◆ UTIME_MINDAY

#define UTIME_MINDAY   (14)

Definition at line 295 of file dt.h.

◆ UTIME_MINMONTH

#define UTIME_MINMONTH   (12)

Definition at line 294 of file dt.h.

◆ UTIME_MINYEAR

#define UTIME_MINYEAR   (1901)

Definition at line 293 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

Definition at line 12 of file dt.h.

Function Documentation

◆ CheckDateTokenTables()

bool CheckDateTokenTables ( void  )

Definition at line 4966 of file datetime.c.

4967{
4968 bool ok = true;
4969
4970 Assert(UNIX_EPOCH_JDATE == date2j(1970, 1, 1));
4971 Assert(POSTGRES_EPOCH_JDATE == date2j(2000, 1, 1));
4972
4973 ok &= CheckDateTokenTable("datetktbl", datetktbl, szdatetktbl);
4974 ok &= CheckDateTokenTable("deltatktbl", deltatktbl, szdeltatktbl);
4975 return ok;
4976}
static bool CheckDateTokenTable(const char *tablename, const datetkn *base, int nel)
Definition datetime.c:4934
static const datetkn datetktbl[]
Definition datetime.c:106
static const int szdeltatktbl
Definition datetime.c:253
static const int szdatetktbl
Definition datetime.c:182
int date2j(int year, int month, int day)
Definition datetime.c:297
static const datetkn deltatktbl[]
Definition datetime.c:188
#define Assert(condition)
Definition c.h:943
#define UNIX_EPOCH_JDATE
Definition timestamp.h:234
#define POSTGRES_EPOCH_JDATE
Definition timestamp.h:235

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

Referenced by PostmasterMain().

◆ date2j()

◆ DecodeDateTime()

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

Definition at line 1794 of file dt_common.c.

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

◆ DecodeInterval()

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

Definition at line 327 of file interval.c.

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

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

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

Referenced by DecodeDateTime().

◆ DecodeUnits()

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

Definition at line 537 of file dt_common.c.

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

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

◆ dt2time()

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

Definition at line 1072 of file dt_common.c.

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

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

Referenced by DecodeDateTime().

◆ EncodeDateOnly()

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

Definition at line 672 of file dt_common.c.

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

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

◆ EncodeDateTime()

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

Definition at line 757 of file dt_common.c.

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

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

◆ EncodeInterval()

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

Definition at line 761 of file interval.c.

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

References AddISO8601IntPart(), AddPostgresIntPart(), AddVerboseIntPart(), AppendSeconds(), fb(), 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 1064 of file dt_common.c.

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

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

Referenced by DecodeDateTime().

◆ GetEpochTime()

int GetEpochTime ( struct tm tm)

Definition at line 953 of file dt_common.c.

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

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

◆ j2date()

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

Definition at line 322 of file datetime.c.

323{
324 unsigned int julian;
325 unsigned int quad;
326 unsigned int extra;
327 int y;
328
329 julian = jd;
330 julian += 32044;
331 quad = julian / 146097;
332 extra = (julian - quad * 146097) * 4 + 3;
333 julian += 60 + quad * 3 + extra / 146097;
334 quad = julian / 1461;
335 julian -= quad * 1461;
336 y = julian * 4 / 1461;
337 julian = ((y != 0) ? ((julian + 305) % 365) : ((julian + 306) % 366))
338 + 123;
339 y += quad * 4;
340 *year = y - 4800;
341 quad = julian * 2141 / 65536;
342 *day = julian - 7834 * quad / 256;
343 *month = (quad + 10) % MONTHS_PER_YEAR + 1;
344} /* j2date() */

References fb(), MONTHS_PER_YEAR, and y.

Referenced by date2timestamptz_safe(), date_out(), DecodeDateTime(), 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(), 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 1611 of file dt_common.c.

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

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

◆ PGTYPEStimestamp_defmt_scan()

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

Definition at line 2535 of file dt_common.c.

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

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

Referenced by PGTYPEStimestamp_defmt_asc(), and PGTYPEStimestamp_defmt_scan().

◆ tm2timestamp()

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

Definition at line 38 of file timestamp.c.

39{
40 int dDate;
41 int64 time;
42
43 /* Prevent overflow in Julian-day routines */
45 return -1;
46
47 dDate = date2j(tm->tm_year, tm->tm_mon, tm->tm_mday) - date2j(2000, 1, 1);
48 time = time2t(tm->tm_hour, tm->tm_min, tm->tm_sec, fsec);
51 return -1;
52 if (tzp != NULL)
53 *result = dt2local(*result, -(*tzp));
54
55 /* final range check catches just-out-of-range timestamps */
57 return -1;
58
59 return 0;
60} /* tm2timestamp() */
#define unlikely(x)
Definition c.h:438
uint32 result
#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:293
static bool pg_add_s64_overflow(int64 a, int64 b, int64 *result)
Definition int.h:235
static timestamp dt2local(timestamp dt, int tz)
Definition timestamp.c:23
static int64 time2t(const int hour, const int min, const int sec, const fsec_t fsec)
Definition timestamp.c:17

References date2j(), dt2local(), fb(), IS_VALID_JULIAN, IS_VALID_TIMESTAMP, pg_add_s64_overflow(), pg_mul_s64_overflow(), result, 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.

Referenced by PGTYPEStimestamp_add_interval(), PGTYPEStimestamp_current(), PGTYPEStimestamp_from_asc(), SetEpochTimestamp(), SetEpochTimestamp(), timestamp_in(), timestamp_pl_interval(), timestamp_trunc(), timestamp_zone(), timestamptz2timestamp_safe(), timestamptz_in(), timestamptz_pl_interval_internal(), timestamptz_trunc_internal(), and timestamptz_zone().

◆ TrimTrailingZeros()

void TrimTrailingZeros ( char str)

Definition at line 725 of file dt_common.c.

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

References fb(), len, and str.

Referenced by AppendSeconds(), and EncodeDateTime().

Variable Documentation

◆ day_tab

const int day_tab[2][13]
extern

Definition at line 76 of file datetime.c.

77{
78 {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 0},
79 {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 0}
80};

Referenced by DecodeDateTime(), PGTYPEStimestamp_add_interval(), PGTYPEStimestamp_defmt_scan(), timestamp_age(), timestamp_pl_interval(), timestamptz_age(), timestamptz_pl_interval_internal(), and ValidateDate().

◆ days

char* days[]
extern

◆ months

char* months[]
extern

Definition at line 82 of file datetime.c.

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

Referenced by DCH_from_char(), DCH_to_char(), dttofmtasc_replace(), EncodeDateTime(), EncodeDateTime(), make_interval(), PGTYPESdate_defmt_asc(), PGTYPESdate_fmt_asc(), and PGTYPEStimestamp_defmt_scan().

◆ pgtypes_date_months

char* pgtypes_date_months[]
extern

Definition at line 499 of file dt_common.c.

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

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

◆ pgtypes_date_weekdays_short

char* pgtypes_date_weekdays_short[]
extern

Definition at line 497 of file dt_common.c.

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

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