PostgreSQL Source Code  git master
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 **, int *, int, int *, struct tm *, fsec_t *)
 
int DecodeTime (char *, int *, struct tm *, fsec_t *)
 
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 *, fsec_t, int *, timestamp *)
 
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 *)
 
int ParseDateTime (char *, char *, char **, int *, int *, char **)
 
int DecodeDateTime (char **, int *, int, int *, struct tm *, fsec_t *, bool)
 
void j2date (int, int *, int *, int *)
 
void GetCurrentDateTime (struct tm *)
 
int date2j (int, int, int)
 
void TrimTrailingZeros (char *)
 
void dt2time (double, int *, int *, int *, fsec_t *)
 
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)
double rint(double x)
Definition: rint.c:21

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_MINYEAR
Definition: dt.h:273
#define JULIAN_MAXMONTH
Definition: dt.h:277
#define JULIAN_MINMONTH
Definition: dt.h:274
#define JULIAN_MAXYEAR
Definition: dt.h:276

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_MAXMONTH
Definition: dt.h:295
#define UTIME_MINYEAR
Definition: dt.h:291
#define UTIME_MAXDAY
Definition: dt.h:296
#define UTIME_MAXYEAR
Definition: dt.h:294
#define UTIME_MINMONTH
Definition: dt.h:292
#define UTIME_MINDAY
Definition: dt.h:293

Definition at line 298 of file dt.h.

Referenced by timestamp2tm().

◆ 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 4435 of file datetime.c.

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

Referenced by PostmasterMain().

4436 {
4437  bool ok = true;
4438 
4439  Assert(UNIX_EPOCH_JDATE == date2j(1970, 1, 1));
4440  Assert(POSTGRES_EPOCH_JDATE == date2j(2000, 1, 1));
4441 
4442  ok &= CheckDateTokenTable("datetktbl", datetktbl, szdatetktbl);
4443  ok &= CheckDateTokenTable("deltatktbl", deltatktbl, szdeltatktbl);
4444  return ok;
4445 }
static const datetkn datetktbl[]
Definition: datetime.c:89
static const int szdeltatktbl
Definition: datetime.c:235
static bool CheckDateTokenTable(const char *tablename, const datetkn *base, int nel)
Definition: datetime.c:4403
static const datetkn deltatktbl[]
Definition: datetime.c:170
int date2j(int y, int m, int d)
Definition: datetime.c:269
#define Assert(condition)
Definition: c.h:739
static const int szdatetktbl
Definition: datetime.c:164
#define UNIX_EPOCH_JDATE
Definition: timestamp.h:162
#define POSTGRES_EPOCH_JDATE
Definition: timestamp.h:163

◆ date2j()

int date2j ( int  ,
int  ,
int   
)

Definition at line 269 of file datetime.c.

Referenced by CheckDateTokenTables(), date2isoweek(), date2isoyear(), date2isoyearday(), date_in(), DCH_to_char(), DecodeDateTime(), DecodeNumber(), DetermineTimeZoneOffsetInternal(), EncodeDateTime(), 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(), timestamp_pl_interval(), timestamp_to_char(), timestamptz_date(), timestamptz_part(), timestamptz_pl_interval(), timestamptz_to_char(), tm2timestamp(), to_date(), and ValidateDate().

270 {
271  int julian;
272  int century;
273 
274  if (m > 2)
275  {
276  m += 1;
277  y += 4800;
278  }
279  else
280  {
281  m += 13;
282  y += 4799;
283  }
284 
285  century = y / 100;
286  julian = y * 365 - 32167;
287  julian += y / 4 - century + century / 4;
288  julian += 7834 * m / 256 + d;
289 
290  return julian;
291 } /* date2j() */

◆ DecodeDateTime()

int DecodeDateTime ( char **  ,
int *  ,
int  ,
int *  ,
struct tm ,
fsec_t ,
bool   
)

Definition at line 1779 of file dt_common.c.

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

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

◆ DecodeInterval()

int DecodeInterval ( char **  ,
int *  ,
int  ,
int *  ,
struct tm ,
fsec_t  
)

Definition at line 326 of file interval.c.

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, rint(), SECOND, SECS_PER_DAY, SECS_PER_HOUR, SECS_PER_MINUTE, strtoint(), generate_unaccent_rules::type, TZ, UNITS, USECS_PER_SEC, val, and YEAR.

Referenced by PGTYPESinterval_from_asc().

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  if (fval != 0)
532  tm->tm_mon += 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  if (fval != 0)
539  tm->tm_mon += fval * MONTHS_PER_YEAR * 10;
540  tmask = (fmask & DTK_M(YEAR)) ? 0 : DTK_M(YEAR);
541  break;
542 
543  case DTK_CENTURY:
544  tm->tm_year += val * 100;
545  if (fval != 0)
546  tm->tm_mon += fval * MONTHS_PER_YEAR * 100;
547  tmask = (fmask & DTK_M(YEAR)) ? 0 : DTK_M(YEAR);
548  break;
549 
550  case DTK_MILLENNIUM:
551  tm->tm_year += val * 1000;
552  if (fval != 0)
553  tm->tm_mon += fval * MONTHS_PER_YEAR * 1000;
554  tmask = (fmask & DTK_M(YEAR)) ? 0 : DTK_M(YEAR);
555  break;
556 
557  default:
558  return DTERR_BAD_FORMAT;
559  }
560  break;
561 
562  case DTK_STRING:
563  case DTK_SPECIAL:
564  type = DecodeUnits(i, field[i], &val);
565  if (type == IGNORE_DTF)
566  continue;
567 
568  tmask = 0; /* DTK_M(type); */
569  switch (type)
570  {
571  case UNITS:
572  type = val;
573  break;
574 
575  case AGO:
576  is_before = true;
577  type = val;
578  break;
579 
580  case RESERV:
581  tmask = (DTK_DATE_M | DTK_TIME_M);
582  *dtype = val;
583  break;
584 
585  default:
586  return DTERR_BAD_FORMAT;
587  }
588  break;
589 
590  default:
591  return DTERR_BAD_FORMAT;
592  }
593 
594  if (tmask & fmask)
595  return DTERR_BAD_FORMAT;
596  fmask |= tmask;
597  }
598 
599  /* ensure that at least one time field has been found */
600  if (fmask == 0)
601  return DTERR_BAD_FORMAT;
602 
603  /* ensure fractional seconds are fractional */
604  if (*fsec != 0)
605  {
606  int sec;
607 
608  sec = *fsec / USECS_PER_SEC;
609  *fsec -= sec * USECS_PER_SEC;
610  tm->tm_sec += sec;
611  }
612 
613  /*----------
614  * The SQL standard defines the interval literal
615  * '-1 1:00:00'
616  * to mean "negative 1 days and negative 1 hours", while Postgres
617  * traditionally treats this as meaning "negative 1 days and positive
618  * 1 hours". In SQL_STANDARD intervalstyle, we apply the leading sign
619  * to all fields if there are no other explicit signs.
620  *
621  * We leave the signs alone if there are additional explicit signs.
622  * This protects us against misinterpreting postgres-style dump output,
623  * since the postgres-style output code has always put an explicit sign on
624  * all fields following a negative field. But note that SQL-spec output
625  * is ambiguous and can be misinterpreted on load! (So it's best practice
626  * to dump in postgres style, not SQL style.)
627  *----------
628  */
629  if (IntervalStyle == INTSTYLE_SQL_STANDARD && *field[0] == '-')
630  {
631  /* Check for additional explicit signs */
632  bool more_signs = false;
633 
634  for (i = 1; i < nf; i++)
635  {
636  if (*field[i] == '-' || *field[i] == '+')
637  {
638  more_signs = true;
639  break;
640  }
641  }
642 
643  if (!more_signs)
644  {
645  /*
646  * Rather than re-determining which field was field[0], just force
647  * 'em all negative.
648  */
649  if (*fsec > 0)
650  *fsec = -(*fsec);
651  if (tm->tm_sec > 0)
652  tm->tm_sec = -tm->tm_sec;
653  if (tm->tm_min > 0)
654  tm->tm_min = -tm->tm_min;
655  if (tm->tm_hour > 0)
656  tm->tm_hour = -tm->tm_hour;
657  if (tm->tm_mday > 0)
658  tm->tm_mday = -tm->tm_mday;
659  if (tm->tm_mon > 0)
660  tm->tm_mon = -tm->tm_mon;
661  if (tm->tm_year > 0)
662  tm->tm_year = -tm->tm_year;
663  }
664  }
665 
666  /* finally, AGO negates everything */
667  if (is_before)
668  {
669  *fsec = -(*fsec);
670  tm->tm_sec = -tm->tm_sec;
671  tm->tm_min = -tm->tm_min;
672  tm->tm_hour = -tm->tm_hour;
673  tm->tm_mday = -tm->tm_mday;
674  tm->tm_mon = -tm->tm_mon;
675  tm->tm_year = -tm->tm_year;
676  }
677 
678  return 0;
679 }
#define INTSTYLE_POSTGRES_VERBOSE
Definition: miscadmin.h:235
static void AdjustFractDays(double frac, struct tm *tm, fsec_t *fsec, int scale)
Definition: interval.c:41
#define DTERR_BAD_FORMAT
Definition: datetime.h:280
#define DTK_CENTURY
Definition: datetime.h:170
#define DAY
Definition: datetime.h:94
#define UNITS
Definition: datetime.h:108
#define IGNORE_DTF
Definition: datetime.h:99
#define DTK_WEEK
Definition: datetime.h:165
#define DTK_YEAR
Definition: datetime.h:168
#define USECS_PER_SEC
Definition: timestamp.h:94
#define YEAR
Definition: datetime.h:93
#define DTK_DELTA
Definition: datetime.h:160
int tm_hour
Definition: pgtime.h:29
#define AGO
Definition: datetime.h:111
#define DTK_TIME_M
Definition: datetime.h:193
#define DTK_MILLENNIUM
Definition: datetime.h:171
int IntervalStyle
Definition: globals.c:117
#define TZ
Definition: datetime.h:96
#define SECOND
Definition: datetime.h:103
int DecodeUnits(int field, char *lowtoken, int *val)
Definition: datetime.c:3699
#define INTERVAL_FULL_RANGE
Definition: timestamp.h:48
#define DTK_DATE_M
Definition: datetime.h:192
static struct pg_tm tm
Definition: localtime.c:108
static void ClearPgTm(struct tm *tm, fsec_t *fsec)
Definition: interval.c:94
#define DTK_MONTH
Definition: datetime.h:166
#define DTK_MILLISEC
Definition: datetime.h:172
#define MONTHS_PER_YEAR
Definition: timestamp.h:69
#define DTK_DECADE
Definition: datetime.h:169
#define DTK_TZ
Definition: datetime.h:147
#define DTK_HOUR
Definition: datetime.h:163
#define DTERR_FIELD_OVERFLOW
Definition: datetime.h:281
#define MILLISECOND
Definition: datetime.h:104
#define SECS_PER_DAY
Definition: timestamp.h:86
#define DTK_SECOND
Definition: datetime.h:161
int tm_mday
Definition: pgtime.h:30
int tm_mon
Definition: pgtime.h:31
#define SECS_PER_MINUTE
Definition: timestamp.h:88
int strtoint(const char *pg_restrict str, char **pg_restrict endptr, int base)
Definition: string.c:50
#define DTK_NUMBER
Definition: datetime.h:142
#define MINUTE
Definition: datetime.h:102
static struct cvec * range(struct vars *v, chr a, chr b, int cases)
Definition: regc_locale.c:416
double rint(double x)
Definition: rint.c:21
#define SECS_PER_HOUR
Definition: timestamp.h:87
#define MONTH
Definition: datetime.h:92
#define DTK_MINUTE
Definition: datetime.h:162
#define DTK_MICROSEC
Definition: datetime.h:173
#define DAYS_PER_MONTH
Definition: timestamp.h:77
#define DTK_TIME
Definition: datetime.h:146
static int DecodeTime(char *str, int fmask, int range, int *tmask, struct pg_tm *tm, fsec_t *fsec)
Definition: datetime.c:2527
static void AdjustFractSeconds(double frac, struct tm *tm, fsec_t *fsec, int scale)
Definition: interval.c:23
#define Assert(condition)
Definition: c.h:739
#define DTK_STRING
Definition: datetime.h:143
#define DTK_DAY
Definition: datetime.h:164
#define RESERV
Definition: datetime.h:91
#define DTK_ALL_SECS_M
Definition: datetime.h:191
#define INTERVAL_MASK(b)
Definition: timestamp.h:45
#define INTSTYLE_SQL_STANDARD
Definition: miscadmin.h:236
int tm_year
Definition: pgtime.h:32
int i
#define DTK_M(t)
Definition: datetime.h:188
#define HOUR
Definition: datetime.h:101
int tm_sec
Definition: pgtime.h:27
int tm_min
Definition: pgtime.h:28
long val
Definition: informix.c:664
#define DTK_DATE
Definition: datetime.h:145
#define DTK_SPECIAL
Definition: datetime.h:150
#define MICROSECOND
Definition: datetime.h:105

◆ DecodeTime()

int DecodeTime ( char *  ,
int *  ,
struct tm ,
fsec_t  
)

Definition at line 1433 of file dt_common.c.

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

Referenced by DecodeDateTime().

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

◆ DecodeUnits()

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

Definition at line 3699 of file datetime.c.

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

Referenced by DecodeInterval(), interval_part(), interval_trunc(), time_part(), timestamp_part(), timestamp_trunc(), timestamptz_part(), timestamptz_trunc_internal(), and timetz_part().

3700 {
3701  int type;
3702  const datetkn *tp;
3703 
3704  tp = deltacache[field];
3705  /* use strncmp so that we match truncated tokens */
3706  if (tp == NULL || strncmp(lowtoken, tp->token, TOKMAXLEN) != 0)
3707  {
3708  tp = datebsearch(lowtoken, deltatktbl, szdeltatktbl);
3709  }
3710  if (tp == NULL)
3711  {
3712  type = UNKNOWN_FIELD;
3713  *val = 0;
3714  }
3715  else
3716  {
3717  deltacache[field] = tp;
3718  type = tp->type;
3719  *val = tp->value;
3720  }
3721 
3722  return type;
3723 } /* DecodeUnits() */
static const datetkn * deltacache[MAXDATEFIELDS]
Definition: datetime.c:243
#define UNKNOWN_FIELD
Definition: datetime.h:125
int32 value
Definition: datetime.h:212
static const int szdeltatktbl
Definition: datetime.c:235
char token[TOKMAXLEN+1]
Definition: datetime.h:210
static const datetkn * datebsearch(const char *key, const datetkn *base, int nel)
Definition: datetime.c:3781
static const datetkn deltatktbl[]
Definition: datetime.c:170
char type
Definition: datetime.h:211
#define TOKMAXLEN
Definition: datetime.h:205
long val
Definition: informix.c:664

◆ dt2time()

void dt2time ( double  ,
int *  ,
int *  ,
int *  ,
fsec_t  
)

Definition at line 1064 of file dt_common.c.

References USECS_PER_HOUR, USECS_PER_MINUTE, and USECS_PER_SEC.

Referenced by DecodeDateTime().

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

◆ EncodeDateOnly()

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

Definition at line 669 of file dt_common.c.

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

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:215
#define USE_ISO_DATES
Definition: miscadmin.h:214
static struct pg_tm tm
Definition: localtime.c:108
#define MONTHS_PER_YEAR
Definition: timestamp.h:69
#define sprintf
Definition: port.h:194
int tm_mday
Definition: pgtime.h:30
int tm_mon
Definition: pgtime.h:31
#define USE_POSTGRES_DATES
Definition: miscadmin.h:213
#define Assert(condition)
Definition: c.h:739
int tm_year
Definition: pgtime.h:32
#define USE_GERMAN_DATES
Definition: miscadmin.h:216

◆ EncodeDateTime()

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

Definition at line 753 of file dt_common.c.

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

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. However, all
830  * TZ abbreviations in the IANA database are plain ASCII.
831  */
832 
833  if (print_tz)
834  {
835  if (tzn)
836  sprintf(str + strlen(str), " %.*s", MAXTZLEN, tzn);
837  else
838  {
839  hour = -(tz / SECS_PER_HOUR);
840  min = (abs(tz) / MINS_PER_HOUR) % MINS_PER_HOUR;
841  if (min != 0)
842  sprintf(str + strlen(str), "%+03d:%02d", hour, min);
843  else
844  sprintf(str + strlen(str), "%+03d", hour);
845  }
846  }
847  break;
848 
849  case USE_GERMAN_DATES:
850  /* German variant on European style */
851 
852  sprintf(str, "%02d.%02d", tm->tm_mday, tm->tm_mon);
853 
854  sprintf(str + 5, ".%04d %02d:%02d",
855  (tm->tm_year > 0) ? tm->tm_year : -(tm->tm_year - 1),
856  tm->tm_hour, tm->tm_min);
857 
858  /*
859  * Print fractional seconds if any. The field widths here should
860  * be at least equal to MAX_TIMESTAMP_PRECISION.
861  */
862  if (fsec != 0)
863  {
864  sprintf(str + strlen(str), ":%02d.%06d", tm->tm_sec, fsec);
866  }
867  else
868  sprintf(str + strlen(str), ":%02d", tm->tm_sec);
869 
870  if (tm->tm_year <= 0)
871  sprintf(str + strlen(str), " BC");
872 
873  if (print_tz)
874  {
875  if (tzn)
876  sprintf(str + strlen(str), " %.*s", MAXTZLEN, tzn);
877  else
878  {
879  hour = -(tz / SECS_PER_HOUR);
880  min = (abs(tz) / MINS_PER_HOUR) % MINS_PER_HOUR;
881  if (min != 0)
882  sprintf(str + strlen(str), "%+03d:%02d", hour, min);
883  else
884  sprintf(str + strlen(str), "%+03d", hour);
885  }
886  }
887  break;
888 
889  case USE_POSTGRES_DATES:
890  default:
891  /* Backward-compatible with traditional Postgres abstime dates */
892 
893  day = date2j(tm->tm_year, tm->tm_mon, tm->tm_mday);
894  tm->tm_wday = (int) ((day + date2j(2000, 1, 1) + 1) % 7);
895 
896  memcpy(str, days[tm->tm_wday], 3);
897  strcpy(str + 3, " ");
898 
899  if (EuroDates)
900  sprintf(str + 4, "%02d %3s", tm->tm_mday, months[tm->tm_mon - 1]);
901  else
902  sprintf(str + 4, "%3s %02d", months[tm->tm_mon - 1], tm->tm_mday);
903 
904  sprintf(str + 10, " %02d:%02d", tm->tm_hour, tm->tm_min);
905 
906  /*
907  * Print fractional seconds if any. The field widths here should
908  * be at least equal to MAX_TIMESTAMP_PRECISION.
909  */
910  if (fsec != 0)
911  {
912  sprintf(str + strlen(str), ":%02d.%06d", tm->tm_sec, fsec);
914  }
915  else
916  sprintf(str + strlen(str), ":%02d", tm->tm_sec);
917 
918  sprintf(str + strlen(str), " %04d",
919  (tm->tm_year > 0) ? tm->tm_year : -(tm->tm_year - 1));
920  if (tm->tm_year <= 0)
921  sprintf(str + strlen(str), " BC");
922 
923  if (print_tz)
924  {
925  if (tzn)
926  sprintf(str + strlen(str), " %.*s", MAXTZLEN, tzn);
927  else
928  {
929  /*
930  * We have a time zone, but no string version. Use the
931  * numeric form, but be sure to include a leading space to
932  * avoid formatting something which would be rejected by
933  * the date/time parser later. - thomas 2001-10-19
934  */
935  hour = -(tz / SECS_PER_HOUR);
936  min = (abs(tz) / MINS_PER_HOUR) % MINS_PER_HOUR;
937  if (min != 0)
938  sprintf(str + strlen(str), " %+03d:%02d", hour, min);
939  else
940  sprintf(str + strlen(str), " %+03d", hour);
941  }
942  }
943  break;
944  }
945 }
char * days[]
Definition: dt_common.c:495
int tm_wday
Definition: pgtime.h:33
int tm_isdst
Definition: pgtime.h:35
int tm_hour
Definition: pgtime.h:29
#define USE_SQL_DATES
Definition: miscadmin.h:215
#define MAXTZLEN
Definition: miscadmin.h:241
int date2j(int y, int m, int d)
Definition: dt_common.c:581
#define MINS_PER_HOUR
Definition: timestamp.h:89
#define USE_ISO_DATES
Definition: miscadmin.h:214
static struct pg_tm tm
Definition: localtime.c:108
#define sprintf
Definition: port.h:194
char * months[]
Definition: dt_common.c:493
int tm_mday
Definition: pgtime.h:30
int tm_mon
Definition: pgtime.h:31
#define USE_POSTGRES_DATES
Definition: miscadmin.h:213
#define SECS_PER_HOUR
Definition: timestamp.h:87
int tm_year
Definition: pgtime.h:32
#define USE_GERMAN_DATES
Definition: miscadmin.h:216
int tm_sec
Definition: pgtime.h:27
void TrimTrailingZeros(char *str)
Definition: dt_common.c:722
int tm_min
Definition: pgtime.h:28

◆ EncodeInterval()

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

Definition at line 763 of file interval.c.

References AddISO8601IntPart(), AddPostgresIntPart(), AddVerboseIntPart(), AppendSeconds(), INTSTYLE_ISO_8601, INTSTYLE_POSTGRES, INTSTYLE_POSTGRES_VERBOSE, INTSTYLE_SQL_STANDARD, MAX_INTERVAL_PRECISION, sprintf, and generate_unaccent_rules::str.

Referenced by PGTYPESinterval_to_asc().

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

◆ GetCurrentDateTime()

void GetCurrentDateTime ( struct tm )

Definition at line 1056 of file dt_common.c.

References abstime2tm().

Referenced by DecodeDateTime().

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

◆ GetEpochTime()

int GetEpochTime ( struct tm )

Definition at line 948 of file dt_common.c.

References epoch.

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

◆ j2date()

void j2date ( int  ,
int *  ,
int *  ,
int *   
)

Definition at line 294 of file datetime.c.

References MONTHS_PER_YEAR.

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

295 {
296  unsigned int julian;
297  unsigned int quad;
298  unsigned int extra;
299  int y;
300 
301  julian = jd;
302  julian += 32044;
303  quad = julian / 146097;
304  extra = (julian - quad * 146097) * 4 + 3;
305  julian += 60 + quad * 3 + extra / 146097;
306  quad = julian / 1461;
307  julian -= quad * 1461;
308  y = julian * 4 / 1461;
309  julian = ((y != 0) ? ((julian + 305) % 365) : ((julian + 306) % 366))
310  + 123;
311  y += quad * 4;
312  *year = y - 4800;
313  quad = julian * 2141 / 65536;
314  *day = julian - 7834 * quad / 256;
315  *month = (quad + 10) % MONTHS_PER_YEAR + 1;
316 } /* j2date() */
#define MONTHS_PER_YEAR
Definition: timestamp.h:69

◆ ParseDateTime()

int ParseDateTime ( char *  ,
char *  ,
char **  ,
int *  ,
int *  ,
char **   
)

Definition at line 1596 of file dt_common.c.

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

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

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

References day_tab, days, DecodeTimezone(), DTZ, free, isleap, un_fmt_comb::luint_val, months, MONTHS_PER_YEAR, pg_strcasecmp(), pgtypes_alloc(), pgtypes_date_months, pgtypes_date_weekdays_short, pgtypes_defmt_scan(), PGTYPES_TYPE_STRING_MALLOCED, PGTYPES_TYPE_UINT, PGTYPES_TYPE_UINT_LONG, generate_unaccent_rules::str, un_fmt_comb::str_val, szdatetktbl, tm, tm2timestamp(), generate_unaccent_rules::type, TZ, un_fmt_comb::uint_val, and datetkn::value.

Referenced by PGTYPEStimestamp_defmt_asc().

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

◆ tm2timestamp()

int tm2timestamp ( struct tm ,
fsec_t  ,
int *  ,
timestamp  
)

Definition at line 40 of file timestamp.c.

References date2j(), dt2local(), IS_VALID_JULIAN, IS_VALID_TIMESTAMP, time2t(), and USECS_PER_DAY.

41 {
42  int dDate;
43  int64 time;
44 
45  /* Prevent overflow in Julian-day routines */
47  return -1;
48 
49  dDate = date2j(tm->tm_year, tm->tm_mon, tm->tm_mday) - date2j(2000, 1, 1);
50  time = time2t(tm->tm_hour, tm->tm_min, tm->tm_sec, fsec);
51  *result = (dDate * USECS_PER_DAY) + time;
52  /* check for major overflow */
53  if ((*result - time) / USECS_PER_DAY != dDate)
54  return -1;
55  /* check for just-barely overflow (okay except time-of-day wraps) */
56  /* caution: we want to allow 1999-12-31 24:00:00 */
57  if ((*result < 0 && dDate > 0) ||
58  (*result > 0 && dDate < -1))
59  return -1;
60  if (tzp != NULL)
61  *result = dt2local(*result, -(*tzp));
62 
63  /* final range check catches just-out-of-range timestamps */
64  if (!IS_VALID_TIMESTAMP(*result))
65  return -1;
66 
67  return 0;
68 } /* tm2timestamp() */
static int64 time2t(const int hour, const int min, const int sec, const fsec_t fsec)
Definition: timestamp.c:20
int tm_hour
Definition: pgtime.h:29
static timestamp dt2local(timestamp dt, int tz)
Definition: timestamp.c:26
static struct pg_tm tm
Definition: localtime.c:108
int tm_mday
Definition: pgtime.h:30
int tm_mon
Definition: pgtime.h:31
#define IS_VALID_JULIAN(y, m, d)
Definition: timestamp.h:155
#define USECS_PER_DAY
Definition: timestamp.h:91
int date2j(int y, int m, int d)
Definition: datetime.c:269
#define IS_VALID_TIMESTAMP(t)
Definition: timestamp.h:195
int tm_year
Definition: pgtime.h:32
int tm_sec
Definition: pgtime.h:27
int tm_min
Definition: pgtime.h:28

◆ TrimTrailingZeros()

void TrimTrailingZeros ( char *  )

Definition at line 722 of file dt_common.c.

Referenced by AppendSeconds(), and EncodeDateTime().

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 }

Variable Documentation

◆ day_tab

◆ days

◆ months

◆ pgtypes_date_months

char* pgtypes_date_months[]

◆ pgtypes_date_weekdays_short

char* pgtypes_date_weekdays_short[]