PostgreSQL Source Code  git master
datetime.h File Reference
#include "utils/timestamp.h"
Include dependency graph for datetime.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  datetkn
 
struct  TimeZoneAbbrevTable
 
struct  DynamicZoneAbbrev
 
struct  DateTimeErrorExtra
 

Macros

#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 abbreviation */
 
#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 WEEK   24
 
#define DECADE   25
 
#define CENTURY   26
 
#define MILLENNIUM   27
 
#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_ALL_SECS_M)
 
#define MAXDATELEN   128
 
#define MAXDATEFIELDS   25
 
#define TOKMAXLEN   10
 
#define FMODULO(t, q, u)
 
#define TMODULO(t, q, u)
 
#define isleap(y)   (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
 
#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 DTERR_BAD_TIMEZONE   (-6)
 
#define DTERR_BAD_ZONE_ABBREV   (-7)
 
#define TZNAME_FIXED_OFFSET   0
 
#define TZNAME_DYNTZ   1
 
#define TZNAME_ZONE   2
 

Typedefs

typedef struct TimeZoneAbbrevTable TimeZoneAbbrevTable
 
typedef struct DynamicZoneAbbrev DynamicZoneAbbrev
 
typedef struct DateTimeErrorExtra DateTimeErrorExtra
 

Functions

void GetCurrentDateTime (struct pg_tm *tm)
 
void GetCurrentTimeUsec (struct pg_tm *tm, fsec_t *fsec, int *tzp)
 
void j2date (int jd, int *year, int *month, int *day)
 
int date2j (int year, int month, int day)
 
int ParseDateTime (const char *timestr, char *workbuf, size_t buflen, char **field, int *ftype, int maxfields, int *numfields)
 
int DecodeDateTime (char **field, int *ftype, int nf, int *dtype, struct pg_tm *tm, fsec_t *fsec, int *tzp, DateTimeErrorExtra *extra)
 
int DecodeTimezone (const char *str, int *tzp)
 
int DecodeTimeOnly (char **field, int *ftype, int nf, int *dtype, struct pg_tm *tm, fsec_t *fsec, int *tzp, DateTimeErrorExtra *extra)
 
int DecodeInterval (char **field, int *ftype, int nf, int range, int *dtype, struct pg_itm_in *itm_in)
 
int DecodeISO8601Interval (char *str, int *dtype, struct pg_itm_in *itm_in)
 
void DateTimeParseError (int dterr, DateTimeErrorExtra *extra, const char *str, const char *datatype, struct Node *escontext)
 
int DetermineTimeZoneOffset (struct pg_tm *tm, pg_tz *tzp)
 
int DetermineTimeZoneAbbrevOffset (struct pg_tm *tm, const char *abbr, pg_tz *tzp)
 
int DetermineTimeZoneAbbrevOffsetTS (TimestampTz ts, const char *abbr, pg_tz *tzp, int *isdst)
 
void EncodeDateOnly (struct pg_tm *tm, int style, char *str)
 
void EncodeTimeOnly (struct pg_tm *tm, fsec_t fsec, bool print_tz, int tz, int style, char *str)
 
void EncodeDateTime (struct pg_tm *tm, fsec_t fsec, bool print_tz, int tz, const char *tzn, int style, char *str)
 
void EncodeInterval (struct pg_itm *itm, int style, char *str)
 
void EncodeSpecialTimestamp (Timestamp dt, char *str)
 
int ValidateDate (int fmask, bool isjulian, bool is2digits, bool bc, struct pg_tm *tm)
 
int DecodeTimezoneAbbrev (int field, const char *lowtoken, int *ftype, int *offset, pg_tz **tz, DateTimeErrorExtra *extra)
 
int DecodeSpecial (int field, const char *lowtoken, int *val)
 
int DecodeUnits (int field, const char *lowtoken, int *val)
 
int DecodeTimezoneName (const char *tzname, int *offset, pg_tz **tz)
 
pg_tzDecodeTimezoneNameToTz (const char *tzname)
 
int DecodeTimezoneAbbrevPrefix (const char *str, int *offset, pg_tz **tz)
 
int j2day (int date)
 
struct NodeTemporalSimplify (int32 max_precis, struct Node *node)
 
bool CheckDateTokenTables (void)
 
TimeZoneAbbrevTableConvertTimeZoneAbbrevs (struct tzEntry *abbrevs, int n)
 
void InstallTimeZoneAbbrevs (TimeZoneAbbrevTable *tbl)
 
bool AdjustTimestampForTypmod (Timestamp *time, int32 typmod, struct Node *escontext)
 

Variables

PGDLLIMPORT const char *const months []
 
PGDLLIMPORT const char *const days []
 
PGDLLIMPORT const int day_tab [2][13]
 

Macro Definition Documentation

◆ ABS_AFTER

#define ABS_AFTER   21

Definition at line 112 of file datetime.h.

◆ ABS_BEFORE

#define ABS_BEFORE   20

Definition at line 111 of file datetime.h.

◆ AD

#define AD   0

Definition at line 75 of file datetime.h.

◆ ADBC

#define ADBC   18

Definition at line 108 of file datetime.h.

◆ AGO

#define AGO   19

Definition at line 110 of file datetime.h.

◆ AM

#define AM   0

Definition at line 71 of file datetime.h.

◆ AMPM

#define AMPM   9

Definition at line 99 of file datetime.h.

◆ BC

#define BC   1

Definition at line 76 of file datetime.h.

◆ CENTURY

#define CENTURY   26

Definition at line 119 of file datetime.h.

◆ DA_D

#define DA_D   "ad"

Definition at line 60 of file datetime.h.

◆ DAGO

#define DAGO   "ago"

Definition at line 35 of file datetime.h.

◆ DAY

#define DAY   3

Definition at line 93 of file datetime.h.

◆ DB_C

#define DB_C   "bc"

Definition at line 61 of file datetime.h.

◆ DCENTURY

#define DCENTURY   "century"

Definition at line 58 of file datetime.h.

◆ DCURRENT

#define DCURRENT   "current"

Definition at line 36 of file datetime.h.

◆ DDAY

#define DDAY   "day"

Definition at line 52 of file datetime.h.

◆ DDECADE

#define DDECADE   "decade"

Definition at line 57 of file datetime.h.

◆ DECADE

#define DECADE   25

Definition at line 118 of file datetime.h.

◆ DHOUR

#define DHOUR   "hour"

Definition at line 51 of file datetime.h.

◆ DMICROSEC

#define DMICROSEC   "usecond"

Definition at line 47 of file datetime.h.

◆ DMILLENNIUM

#define DMILLENNIUM   "millennium"

Definition at line 59 of file datetime.h.

◆ DMILLISEC

#define DMILLISEC   "msecond"

Definition at line 48 of file datetime.h.

◆ DMINUTE

#define DMINUTE   "minute"

Definition at line 50 of file datetime.h.

◆ DMONTH

#define DMONTH   "month"

Definition at line 54 of file datetime.h.

◆ DOW

#define DOW   16

Definition at line 106 of file datetime.h.

◆ DOY

#define DOY   15

Definition at line 105 of file datetime.h.

◆ DQUARTER

#define DQUARTER   "quarter"

Definition at line 55 of file datetime.h.

◆ DSECOND

#define DSECOND   "second"

Definition at line 49 of file datetime.h.

◆ DTERR_BAD_FORMAT

#define DTERR_BAD_FORMAT   (-1)

Definition at line 282 of file datetime.h.

◆ DTERR_BAD_TIMEZONE

#define DTERR_BAD_TIMEZONE   (-6)

Definition at line 287 of file datetime.h.

◆ DTERR_BAD_ZONE_ABBREV

#define DTERR_BAD_ZONE_ABBREV   (-7)

Definition at line 288 of file datetime.h.

◆ DTERR_FIELD_OVERFLOW

#define DTERR_FIELD_OVERFLOW   (-2)

Definition at line 283 of file datetime.h.

◆ DTERR_INTERVAL_OVERFLOW

#define DTERR_INTERVAL_OVERFLOW   (-4)

Definition at line 285 of file datetime.h.

◆ DTERR_MD_FIELD_OVERFLOW

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

Definition at line 284 of file datetime.h.

◆ DTERR_TZDISP_OVERFLOW

#define DTERR_TZDISP_OVERFLOW   (-5)

Definition at line 286 of file datetime.h.

◆ DTIMEZONE

#define DTIMEZONE   "timezone"

Definition at line 62 of file datetime.h.

◆ DTK_AGO

#define DTK_AGO   5

Definition at line 147 of file datetime.h.

◆ DTK_ALL_SECS_M

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

Definition at line 190 of file datetime.h.

◆ DTK_CENTURY

#define DTK_CENTURY   27

Definition at line 169 of file datetime.h.

◆ DTK_DATE

#define DTK_DATE   2

Definition at line 144 of file datetime.h.

◆ DTK_DATE_M

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

Definition at line 191 of file datetime.h.

◆ DTK_DAY

#define DTK_DAY   21

Definition at line 163 of file datetime.h.

◆ DTK_DECADE

#define DTK_DECADE   26

Definition at line 168 of file datetime.h.

◆ DTK_DELTA

#define DTK_DELTA   17

Definition at line 159 of file datetime.h.

◆ DTK_DOW

#define DTK_DOW   32

Definition at line 175 of file datetime.h.

◆ DTK_DOY

#define DTK_DOY   33

Definition at line 176 of file datetime.h.

◆ DTK_EARLY

#define DTK_EARLY   9

Definition at line 150 of file datetime.h.

◆ DTK_EPOCH

#define DTK_EPOCH   11

Definition at line 152 of file datetime.h.

◆ DTK_HOUR

#define DTK_HOUR   20

Definition at line 162 of file datetime.h.

◆ DTK_ISODOW

#define DTK_ISODOW   37

Definition at line 180 of file datetime.h.

◆ DTK_ISOYEAR

#define DTK_ISOYEAR   36

Definition at line 179 of file datetime.h.

◆ DTK_JULIAN

#define DTK_JULIAN   31

Definition at line 173 of file datetime.h.

◆ DTK_LATE

#define DTK_LATE   10

Definition at line 151 of file datetime.h.

◆ DTK_M

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

Definition at line 187 of file datetime.h.

◆ DTK_MICROSEC

#define DTK_MICROSEC   30

Definition at line 172 of file datetime.h.

◆ DTK_MILLENNIUM

#define DTK_MILLENNIUM   28

Definition at line 170 of file datetime.h.

◆ DTK_MILLISEC

#define DTK_MILLISEC   29

Definition at line 171 of file datetime.h.

◆ DTK_MINUTE

#define DTK_MINUTE   19

Definition at line 161 of file datetime.h.

◆ DTK_MONTH

#define DTK_MONTH   23

Definition at line 165 of file datetime.h.

◆ DTK_NOW

#define DTK_NOW   12

Definition at line 153 of file datetime.h.

◆ DTK_NUMBER

#define DTK_NUMBER   0

Definition at line 141 of file datetime.h.

◆ DTK_QUARTER

#define DTK_QUARTER   24

Definition at line 166 of file datetime.h.

◆ DTK_SECOND

#define DTK_SECOND   18

Definition at line 160 of file datetime.h.

◆ DTK_SPECIAL

#define DTK_SPECIAL   6

Definition at line 149 of file datetime.h.

◆ DTK_STRING

#define DTK_STRING   1

Definition at line 142 of file datetime.h.

◆ DTK_TIME

#define DTK_TIME   3

Definition at line 145 of file datetime.h.

◆ DTK_TIME_M

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

Definition at line 192 of file datetime.h.

◆ DTK_TODAY

#define DTK_TODAY   14

Definition at line 155 of file datetime.h.

◆ DTK_TOMORROW

#define DTK_TOMORROW   15

Definition at line 156 of file datetime.h.

◆ DTK_TZ

#define DTK_TZ   4

Definition at line 146 of file datetime.h.

◆ DTK_TZ_HOUR

#define DTK_TZ_HOUR   34

Definition at line 177 of file datetime.h.

◆ DTK_TZ_MINUTE

#define DTK_TZ_MINUTE   35

Definition at line 178 of file datetime.h.

◆ DTK_WEEK

#define DTK_WEEK   22

Definition at line 164 of file datetime.h.

◆ DTK_YEAR

#define DTK_YEAR   25

Definition at line 167 of file datetime.h.

◆ DTK_YESTERDAY

#define DTK_YESTERDAY   13

Definition at line 154 of file datetime.h.

◆ DTK_ZULU

#define DTK_ZULU   16

Definition at line 157 of file datetime.h.

◆ DTZ

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

Definition at line 96 of file datetime.h.

◆ DTZMOD

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

Definition at line 122 of file datetime.h.

◆ DWEEK

#define DWEEK   "week"

Definition at line 53 of file datetime.h.

◆ DYEAR

#define DYEAR   "year"

Definition at line 56 of file datetime.h.

◆ DYNTZ

#define DYNTZ   7 /* dynamic timezone abbreviation */

Definition at line 97 of file datetime.h.

◆ EARLY

#define EARLY   "-infinity"

Definition at line 39 of file datetime.h.

◆ EPOCH

#define EPOCH   "epoch"

Definition at line 37 of file datetime.h.

◆ FMODULO

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

Definition at line 237 of file datetime.h.

◆ HOUR

#define HOUR   10

Definition at line 100 of file datetime.h.

◆ HR24

#define HR24   2

Definition at line 73 of file datetime.h.

◆ IGNORE_DTF

#define IGNORE_DTF   8

Definition at line 98 of file datetime.h.

◆ INVALID

#define INVALID   "invalid"

Definition at line 38 of file datetime.h.

◆ isleap

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

Definition at line 271 of file datetime.h.

◆ ISODATE

#define ISODATE   22

Definition at line 114 of file datetime.h.

◆ ISOTIME

#define ISOTIME   23

Definition at line 115 of file datetime.h.

◆ JULIAN

#define JULIAN   4

Definition at line 94 of file datetime.h.

◆ LATE

#define LATE   "infinity"

Definition at line 40 of file datetime.h.

◆ MAXDATEFIELDS

#define MAXDATEFIELDS   25

Definition at line 202 of file datetime.h.

◆ MAXDATELEN

#define MAXDATELEN   128

Definition at line 200 of file datetime.h.

◆ MICROSECOND

#define MICROSECOND   14

Definition at line 104 of file datetime.h.

◆ MILLENNIUM

#define MILLENNIUM   27

Definition at line 120 of file datetime.h.

◆ MILLISECOND

#define MILLISECOND   13

Definition at line 103 of file datetime.h.

◆ MINUTE

#define MINUTE   11

Definition at line 101 of file datetime.h.

◆ MONTH

#define MONTH   1

Definition at line 91 of file datetime.h.

◆ NOW

#define NOW   "now"

Definition at line 41 of file datetime.h.

◆ PM

#define PM   1

Definition at line 72 of file datetime.h.

◆ RESERV

#define RESERV   0

Definition at line 90 of file datetime.h.

◆ SECOND

#define SECOND   12

Definition at line 102 of file datetime.h.

◆ TMODULO

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

Definition at line 248 of file datetime.h.

◆ TODAY

#define TODAY   "today"

Definition at line 42 of file datetime.h.

◆ TOKMAXLEN

#define TOKMAXLEN   10

Definition at line 204 of file datetime.h.

◆ TOMORROW

#define TOMORROW   "tomorrow"

Definition at line 43 of file datetime.h.

◆ TZ

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

Definition at line 95 of file datetime.h.

◆ TZNAME_DYNTZ

#define TZNAME_DYNTZ   1

Definition at line 300 of file datetime.h.

◆ TZNAME_FIXED_OFFSET

#define TZNAME_FIXED_OFFSET   0

Definition at line 299 of file datetime.h.

◆ TZNAME_ZONE

#define TZNAME_ZONE   2

Definition at line 301 of file datetime.h.

◆ UNITS

#define UNITS   17

Definition at line 107 of file datetime.h.

◆ UNKNOWN_FIELD

#define UNKNOWN_FIELD   31

Definition at line 124 of file datetime.h.

◆ WEEK

#define WEEK   24

Definition at line 117 of file datetime.h.

◆ YEAR

#define YEAR   2

Definition at line 92 of file datetime.h.

◆ YESTERDAY

#define YESTERDAY   "yesterday"

Definition at line 44 of file datetime.h.

◆ ZULU

#define ZULU   "zulu"

Definition at line 45 of file datetime.h.

Typedef Documentation

◆ DateTimeErrorExtra

◆ DynamicZoneAbbrev

◆ TimeZoneAbbrevTable

Function Documentation

◆ AdjustTimestampForTypmod()

bool AdjustTimestampForTypmod ( Timestamp time,
int32  typmod,
struct Node escontext 
)

Definition at line 367 of file timestamp.c.

368 {
369  static const int64 TimestampScales[MAX_TIMESTAMP_PRECISION + 1] = {
370  INT64CONST(1000000),
371  INT64CONST(100000),
372  INT64CONST(10000),
373  INT64CONST(1000),
374  INT64CONST(100),
375  INT64CONST(10),
376  INT64CONST(1)
377  };
378 
379  static const int64 TimestampOffsets[MAX_TIMESTAMP_PRECISION + 1] = {
380  INT64CONST(500000),
381  INT64CONST(50000),
382  INT64CONST(5000),
383  INT64CONST(500),
384  INT64CONST(50),
385  INT64CONST(5),
386  INT64CONST(0)
387  };
388 
389  if (!TIMESTAMP_NOT_FINITE(*time)
390  && (typmod != -1) && (typmod != MAX_TIMESTAMP_PRECISION))
391  {
392  if (typmod < 0 || typmod > MAX_TIMESTAMP_PRECISION)
393  ereturn(escontext, false,
394  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
395  errmsg("timestamp(%d) precision must be between %d and %d",
396  typmod, 0, MAX_TIMESTAMP_PRECISION)));
397 
398  if (*time >= INT64CONST(0))
399  {
400  *time = ((*time + TimestampOffsets[typmod]) / TimestampScales[typmod]) *
401  TimestampScales[typmod];
402  }
403  else
404  {
405  *time = -((((-*time) + TimestampOffsets[typmod]) / TimestampScales[typmod])
406  * TimestampScales[typmod]);
407  }
408  }
409 
410  return true;
411 }
#define INT64CONST(x)
Definition: c.h:499
int64_t int64
Definition: c.h:482
#define MAX_TIMESTAMP_PRECISION
Definition: timestamp.h:92
#define TIMESTAMP_NOT_FINITE(j)
Definition: timestamp.h:169
int errcode(int sqlerrcode)
Definition: elog.c:853
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define ereturn(context, dummy_value,...)
Definition: elog.h:277

References ereturn, errcode(), errmsg(), INT64CONST, MAX_TIMESTAMP_PRECISION, and TIMESTAMP_NOT_FINITE.

Referenced by executeDateTimeMethod(), GetSQLCurrentTimestamp(), GetSQLLocalTimestamp(), parse_datetime(), timestamp_in(), timestamp_recv(), timestamp_scale(), timestamptz_in(), timestamptz_recv(), timestamptz_scale(), and to_timestamp().

◆ CheckDateTokenTables()

bool CheckDateTokenTables ( void  )

Definition at line 4811 of file datetime.c.

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

◆ ConvertTimeZoneAbbrevs()

TimeZoneAbbrevTable* ConvertTimeZoneAbbrevs ( struct tzEntry abbrevs,
int  n 
)

Definition at line 4873 of file datetime.c.

4874 {
4875  TimeZoneAbbrevTable *tbl;
4876  Size tbl_size;
4877  int i;
4878 
4879  /* Space for fixed fields and datetkn array */
4880  tbl_size = offsetof(TimeZoneAbbrevTable, abbrevs) +
4881  n * sizeof(datetkn);
4882  tbl_size = MAXALIGN(tbl_size);
4883  /* Count up space for dynamic abbreviations */
4884  for (i = 0; i < n; i++)
4885  {
4886  struct tzEntry *abbr = abbrevs + i;
4887 
4888  if (abbr->zone != NULL)
4889  {
4890  Size dsize;
4891 
4892  dsize = offsetof(DynamicZoneAbbrev, zone) +
4893  strlen(abbr->zone) + 1;
4894  tbl_size += MAXALIGN(dsize);
4895  }
4896  }
4897 
4898  /* Alloc the result ... */
4899  tbl = guc_malloc(LOG, tbl_size);
4900  if (!tbl)
4901  return NULL;
4902 
4903  /* ... and fill it in */
4904  tbl->tblsize = tbl_size;
4905  tbl->numabbrevs = n;
4906  /* in this loop, tbl_size reprises the space calculation above */
4907  tbl_size = offsetof(TimeZoneAbbrevTable, abbrevs) +
4908  n * sizeof(datetkn);
4909  tbl_size = MAXALIGN(tbl_size);
4910  for (i = 0; i < n; i++)
4911  {
4912  struct tzEntry *abbr = abbrevs + i;
4913  datetkn *dtoken = tbl->abbrevs + i;
4914 
4915  /* use strlcpy to truncate name if necessary */
4916  strlcpy(dtoken->token, abbr->abbrev, TOKMAXLEN + 1);
4917  if (abbr->zone != NULL)
4918  {
4919  /* Allocate a DynamicZoneAbbrev for this abbreviation */
4920  DynamicZoneAbbrev *dtza;
4921  Size dsize;
4922 
4923  dtza = (DynamicZoneAbbrev *) ((char *) tbl + tbl_size);
4924  dtza->tz = NULL;
4925  strcpy(dtza->zone, abbr->zone);
4926 
4927  dtoken->type = DYNTZ;
4928  /* value is offset from table start to DynamicZoneAbbrev */
4929  dtoken->value = (int32) tbl_size;
4930 
4931  dsize = offsetof(DynamicZoneAbbrev, zone) +
4932  strlen(abbr->zone) + 1;
4933  tbl_size += MAXALIGN(dsize);
4934  }
4935  else
4936  {
4937  dtoken->type = abbr->is_dst ? DTZ : TZ;
4938  dtoken->value = abbr->offset;
4939  }
4940  }
4941 
4942  /* Assert the two loops above agreed on size calculations */
4943  Assert(tbl->tblsize == tbl_size);
4944 
4945  /* Check the ordering, if testing */
4946  Assert(CheckDateTokenTable("timezone abbreviations", tbl->abbrevs, n));
4947 
4948  return tbl;
4949 }
#define MAXALIGN(LEN)
Definition: c.h:765
int32_t int32
Definition: c.h:481
size_t Size
Definition: c.h:559
#define LOG
Definition: elog.h:31
void * guc_malloc(int elevel, size_t size)
Definition: guc.c:638
#define TZ
Definition: datetime.h:95
#define DTZ
Definition: datetime.h:96
#define TOKMAXLEN
Definition: datetime.h:204
#define DYNTZ
Definition: datetime.h:97
int i
Definition: isn.c:72
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
char zone[FLEXIBLE_ARRAY_MEMBER]
Definition: datetime.h:227
datetkn abbrevs[FLEXIBLE_ARRAY_MEMBER]
Definition: datetime.h:219
char token[TOKMAXLEN+1]
Definition: datetime.h:209
int32 value
Definition: datetime.h:211
char type
Definition: datetime.h:210
char * zone
Definition: tzparser.h:27
int offset
Definition: tzparser.h:29
char * abbrev
Definition: tzparser.h:26
bool is_dst
Definition: tzparser.h:30
Definition: zic.c:94

References tzEntry::abbrev, TimeZoneAbbrevTable::abbrevs, Assert, CheckDateTokenTable(), DTZ, DYNTZ, guc_malloc(), i, tzEntry::is_dst, LOG, MAXALIGN, TimeZoneAbbrevTable::numabbrevs, tzEntry::offset, strlcpy(), TimeZoneAbbrevTable::tblsize, datetkn::token, TOKMAXLEN, datetkn::type, TZ, DynamicZoneAbbrev::tz, datetkn::value, DynamicZoneAbbrev::zone, and tzEntry::zone.

Referenced by load_tzoffsets().

◆ date2j()

int date2j ( int  year,
int  month,
int  day 
)

Definition at line 286 of file datetime.c.

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

◆ DateTimeParseError()

void DateTimeParseError ( int  dterr,
DateTimeErrorExtra extra,
const char *  str,
const char *  datatype,
struct Node escontext 
)

Definition at line 4092 of file datetime.c.

4095 {
4096  switch (dterr)
4097  {
4098  case DTERR_FIELD_OVERFLOW:
4099  errsave(escontext,
4100  (errcode(ERRCODE_DATETIME_FIELD_OVERFLOW),
4101  errmsg("date/time field value out of range: \"%s\"",
4102  str)));
4103  break;
4105  /* <nanny>same as above, but add hint about DateStyle</nanny> */
4106  errsave(escontext,
4107  (errcode(ERRCODE_DATETIME_FIELD_OVERFLOW),
4108  errmsg("date/time field value out of range: \"%s\"",
4109  str),
4110  errhint("Perhaps you need a different \"DateStyle\" setting.")));
4111  break;
4113  errsave(escontext,
4114  (errcode(ERRCODE_INTERVAL_FIELD_OVERFLOW),
4115  errmsg("interval field value out of range: \"%s\"",
4116  str)));
4117  break;
4118  case DTERR_TZDISP_OVERFLOW:
4119  errsave(escontext,
4120  (errcode(ERRCODE_INVALID_TIME_ZONE_DISPLACEMENT_VALUE),
4121  errmsg("time zone displacement out of range: \"%s\"",
4122  str)));
4123  break;
4124  case DTERR_BAD_TIMEZONE:
4125  errsave(escontext,
4126  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4127  errmsg("time zone \"%s\" not recognized",
4128  extra->dtee_timezone)));
4129  break;
4130  case DTERR_BAD_ZONE_ABBREV:
4131  errsave(escontext,
4132  (errcode(ERRCODE_CONFIG_FILE_ERROR),
4133  errmsg("time zone \"%s\" not recognized",
4134  extra->dtee_timezone),
4135  errdetail("This time zone name appears in the configuration file for time zone abbreviation \"%s\".",
4136  extra->dtee_abbrev)));
4137  break;
4138  case DTERR_BAD_FORMAT:
4139  default:
4140  errsave(escontext,
4141  (errcode(ERRCODE_INVALID_DATETIME_FORMAT),
4142  errmsg("invalid input syntax for type %s: \"%s\"",
4143  datatype, str)));
4144  break;
4145  }
4146 }
int errdetail(const char *fmt,...)
Definition: elog.c:1203
int errhint(const char *fmt,...)
Definition: elog.c:1317
#define errsave(context,...)
Definition: elog.h:261
const char * str
#define DTERR_BAD_ZONE_ABBREV
Definition: datetime.h:288
#define DTERR_INTERVAL_OVERFLOW
Definition: datetime.h:285
#define DTERR_BAD_TIMEZONE
Definition: datetime.h:287
#define DTERR_BAD_FORMAT
Definition: datetime.h:282
#define DTERR_TZDISP_OVERFLOW
Definition: datetime.h:286
#define DTERR_FIELD_OVERFLOW
Definition: datetime.h:283
#define DTERR_MD_FIELD_OVERFLOW
Definition: datetime.h:284
const char * dtee_timezone
Definition: datetime.h:293
const char * dtee_abbrev
Definition: datetime.h:295

References DateTimeErrorExtra::dtee_abbrev, DateTimeErrorExtra::dtee_timezone, DTERR_BAD_FORMAT, DTERR_BAD_TIMEZONE, DTERR_BAD_ZONE_ABBREV, DTERR_FIELD_OVERFLOW, DTERR_INTERVAL_OVERFLOW, DTERR_MD_FIELD_OVERFLOW, DTERR_TZDISP_OVERFLOW, errcode(), errdetail(), errhint(), errmsg(), errsave, and str.

Referenced by date_in(), DecodeTimezoneName(), do_to_timestamp(), interval_in(), pg_timezone_abbrevs(), time_in(), timestamp_in(), timestamptz_in(), and timetz_in().

◆ DecodeDateTime()

int DecodeDateTime ( char **  field,
int *  ftype,
int  nf,
int *  dtype,
struct pg_tm tm,
fsec_t fsec,
int *  tzp,
DateTimeErrorExtra extra 
)

Definition at line 978 of file datetime.c.

981 {
982  int fmask = 0,
983  tmask,
984  type;
985  int ptype = 0; /* "prefix type" for ISO and Julian formats */
986  int i;
987  int val;
988  int dterr;
989  int mer = HR24;
990  bool haveTextMonth = false;
991  bool isjulian = false;
992  bool is2digits = false;
993  bool bc = false;
994  pg_tz *namedTz = NULL;
995  pg_tz *abbrevTz = NULL;
996  pg_tz *valtz;
997  char *abbrev = NULL;
998  struct pg_tm cur_tm;
999 
1000  /*
1001  * We'll insist on at least all of the date fields, but initialize the
1002  * remaining fields in case they are not set later...
1003  */
1004  *dtype = DTK_DATE;
1005  tm->tm_hour = 0;
1006  tm->tm_min = 0;
1007  tm->tm_sec = 0;
1008  *fsec = 0;
1009  /* don't know daylight savings time status apriori */
1010  tm->tm_isdst = -1;
1011  if (tzp != NULL)
1012  *tzp = 0;
1013 
1014  for (i = 0; i < nf; i++)
1015  {
1016  switch (ftype[i])
1017  {
1018  case DTK_DATE:
1019 
1020  /*
1021  * Integral julian day with attached time zone? All other
1022  * forms with JD will be separated into distinct fields, so we
1023  * handle just this case here.
1024  */
1025  if (ptype == DTK_JULIAN)
1026  {
1027  char *cp;
1028  int jday;
1029 
1030  if (tzp == NULL)
1031  return DTERR_BAD_FORMAT;
1032 
1033  errno = 0;
1034  jday = strtoint(field[i], &cp, 10);
1035  if (errno == ERANGE || jday < 0)
1036  return DTERR_FIELD_OVERFLOW;
1037 
1038  j2date(jday, &tm->tm_year, &tm->tm_mon, &tm->tm_mday);
1039  isjulian = true;
1040 
1041  /* Get the time zone from the end of the string */
1042  dterr = DecodeTimezone(cp, tzp);
1043  if (dterr)
1044  return dterr;
1045 
1046  tmask = DTK_DATE_M | DTK_TIME_M | DTK_M(TZ);
1047  ptype = 0;
1048  break;
1049  }
1050 
1051  /*
1052  * Already have a date? Then this might be a time zone name
1053  * with embedded punctuation (e.g. "America/New_York") or a
1054  * run-together time with trailing time zone (e.g. hhmmss-zz).
1055  * - thomas 2001-12-25
1056  *
1057  * We consider it a time zone if we already have month & day.
1058  * This is to allow the form "mmm dd hhmmss tz year", which
1059  * we've historically accepted.
1060  */
1061  else if (ptype != 0 ||
1062  ((fmask & (DTK_M(MONTH) | DTK_M(DAY))) ==
1063  (DTK_M(MONTH) | DTK_M(DAY))))
1064  {
1065  /* No time zone accepted? Then quit... */
1066  if (tzp == NULL)
1067  return DTERR_BAD_FORMAT;
1068 
1069  if (isdigit((unsigned char) *field[i]) || ptype != 0)
1070  {
1071  char *cp;
1072 
1073  /*
1074  * Allow a preceding "t" field, but no other units.
1075  */
1076  if (ptype != 0)
1077  {
1078  /* Sanity check; should not fail this test */
1079  if (ptype != DTK_TIME)
1080  return DTERR_BAD_FORMAT;
1081  ptype = 0;
1082  }
1083 
1084  /*
1085  * Starts with a digit but we already have a time
1086  * field? Then we are in trouble with a date and time
1087  * already...
1088  */
1089  if ((fmask & DTK_TIME_M) == DTK_TIME_M)
1090  return DTERR_BAD_FORMAT;
1091 
1092  if ((cp = strchr(field[i], '-')) == NULL)
1093  return DTERR_BAD_FORMAT;
1094 
1095  /* Get the time zone from the end of the string */
1096  dterr = DecodeTimezone(cp, tzp);
1097  if (dterr)
1098  return dterr;
1099  *cp = '\0';
1100 
1101  /*
1102  * Then read the rest of the field as a concatenated
1103  * time
1104  */
1105  dterr = DecodeNumberField(strlen(field[i]), field[i],
1106  fmask,
1107  &tmask, tm,
1108  fsec, &is2digits);
1109  if (dterr < 0)
1110  return dterr;
1111 
1112  /*
1113  * modify tmask after returning from
1114  * DecodeNumberField()
1115  */
1116  tmask |= DTK_M(TZ);
1117  }
1118  else
1119  {
1120  namedTz = pg_tzset(field[i]);
1121  if (!namedTz)
1122  {
1123  extra->dtee_timezone = field[i];
1124  return DTERR_BAD_TIMEZONE;
1125  }
1126  /* we'll apply the zone setting below */
1127  tmask = DTK_M(TZ);
1128  }
1129  }
1130  else
1131  {
1132  dterr = DecodeDate(field[i], fmask,
1133  &tmask, &is2digits, tm);
1134  if (dterr)
1135  return dterr;
1136  }
1137  break;
1138 
1139  case DTK_TIME:
1140 
1141  /*
1142  * This might be an ISO time following a "t" field.
1143  */
1144  if (ptype != 0)
1145  {
1146  /* Sanity check; should not fail this test */
1147  if (ptype != DTK_TIME)
1148  return DTERR_BAD_FORMAT;
1149  ptype = 0;
1150  }
1151  dterr = DecodeTime(field[i], fmask, INTERVAL_FULL_RANGE,
1152  &tmask, tm, fsec);
1153  if (dterr)
1154  return dterr;
1155 
1156  /* check for time overflow */
1158  *fsec))
1159  return DTERR_FIELD_OVERFLOW;
1160  break;
1161 
1162  case DTK_TZ:
1163  {
1164  int tz;
1165 
1166  if (tzp == NULL)
1167  return DTERR_BAD_FORMAT;
1168 
1169  dterr = DecodeTimezone(field[i], &tz);
1170  if (dterr)
1171  return dterr;
1172  *tzp = tz;
1173  tmask = DTK_M(TZ);
1174  }
1175  break;
1176 
1177  case DTK_NUMBER:
1178 
1179  /*
1180  * Deal with cases where previous field labeled this one
1181  */
1182  if (ptype != 0)
1183  {
1184  char *cp;
1185  int value;
1186 
1187  errno = 0;
1188  value = strtoint(field[i], &cp, 10);
1189  if (errno == ERANGE)
1190  return DTERR_FIELD_OVERFLOW;
1191  if (*cp != '.' && *cp != '\0')
1192  return DTERR_BAD_FORMAT;
1193 
1194  switch (ptype)
1195  {
1196  case DTK_JULIAN:
1197  /* previous field was a label for "julian date" */
1198  if (value < 0)
1199  return DTERR_FIELD_OVERFLOW;
1200  tmask = DTK_DATE_M;
1201  j2date(value, &tm->tm_year, &tm->tm_mon, &tm->tm_mday);
1202  isjulian = true;
1203 
1204  /* fractional Julian Day? */
1205  if (*cp == '.')
1206  {
1207  double time;
1208 
1209  dterr = ParseFraction(cp, &time);
1210  if (dterr)
1211  return dterr;
1212  time *= USECS_PER_DAY;
1213  dt2time(time,
1214  &tm->tm_hour, &tm->tm_min,
1215  &tm->tm_sec, fsec);
1216  tmask |= DTK_TIME_M;
1217  }
1218  break;
1219 
1220  case DTK_TIME:
1221  /* previous field was "t" for ISO time */
1222  dterr = DecodeNumberField(strlen(field[i]), field[i],
1223  (fmask | DTK_DATE_M),
1224  &tmask, tm,
1225  fsec, &is2digits);
1226  if (dterr < 0)
1227  return dterr;
1228  if (tmask != DTK_TIME_M)
1229  return DTERR_BAD_FORMAT;
1230  break;
1231 
1232  default:
1233  return DTERR_BAD_FORMAT;
1234  break;
1235  }
1236 
1237  ptype = 0;
1238  *dtype = DTK_DATE;
1239  }
1240  else
1241  {
1242  char *cp;
1243  int flen;
1244 
1245  flen = strlen(field[i]);
1246  cp = strchr(field[i], '.');
1247 
1248  /* Embedded decimal and no date yet? */
1249  if (cp != NULL && !(fmask & DTK_DATE_M))
1250  {
1251  dterr = DecodeDate(field[i], fmask,
1252  &tmask, &is2digits, tm);
1253  if (dterr)
1254  return dterr;
1255  }
1256  /* embedded decimal and several digits before? */
1257  else if (cp != NULL && flen - strlen(cp) > 2)
1258  {
1259  /*
1260  * Interpret as a concatenated date or time Set the
1261  * type field to allow decoding other fields later.
1262  * Example: 20011223 or 040506
1263  */
1264  dterr = DecodeNumberField(flen, field[i], fmask,
1265  &tmask, tm,
1266  fsec, &is2digits);
1267  if (dterr < 0)
1268  return dterr;
1269  }
1270 
1271  /*
1272  * Is this a YMD or HMS specification, or a year number?
1273  * YMD and HMS are required to be six digits or more, so
1274  * if it is 5 digits, it is a year. If it is six or more
1275  * digits, we assume it is YMD or HMS unless no date and
1276  * no time values have been specified. This forces 6+
1277  * digit years to be at the end of the string, or to use
1278  * the ISO date specification.
1279  */
1280  else if (flen >= 6 && (!(fmask & DTK_DATE_M) ||
1281  !(fmask & DTK_TIME_M)))
1282  {
1283  dterr = DecodeNumberField(flen, field[i], fmask,
1284  &tmask, tm,
1285  fsec, &is2digits);
1286  if (dterr < 0)
1287  return dterr;
1288  }
1289  /* otherwise it is a single date/time field... */
1290  else
1291  {
1292  dterr = DecodeNumber(flen, field[i],
1293  haveTextMonth, fmask,
1294  &tmask, tm,
1295  fsec, &is2digits);
1296  if (dterr)
1297  return dterr;
1298  }
1299  }
1300  break;
1301 
1302  case DTK_STRING:
1303  case DTK_SPECIAL:
1304  /* timezone abbrevs take precedence over built-in tokens */
1305  dterr = DecodeTimezoneAbbrev(i, field[i],
1306  &type, &val, &valtz, extra);
1307  if (dterr)
1308  return dterr;
1309  if (type == UNKNOWN_FIELD)
1310  type = DecodeSpecial(i, field[i], &val);
1311  if (type == IGNORE_DTF)
1312  continue;
1313 
1314  tmask = DTK_M(type);
1315  switch (type)
1316  {
1317  case RESERV:
1318  switch (val)
1319  {
1320  case DTK_NOW:
1321  tmask = (DTK_DATE_M | DTK_TIME_M | DTK_M(TZ));
1322  *dtype = DTK_DATE;
1323  GetCurrentTimeUsec(tm, fsec, tzp);
1324  break;
1325 
1326  case DTK_YESTERDAY:
1327  tmask = DTK_DATE_M;
1328  *dtype = DTK_DATE;
1329  GetCurrentDateTime(&cur_tm);
1330  j2date(date2j(cur_tm.tm_year, cur_tm.tm_mon, cur_tm.tm_mday) - 1,
1331  &tm->tm_year, &tm->tm_mon, &tm->tm_mday);
1332  break;
1333 
1334  case DTK_TODAY:
1335  tmask = DTK_DATE_M;
1336  *dtype = DTK_DATE;
1337  GetCurrentDateTime(&cur_tm);
1338  tm->tm_year = cur_tm.tm_year;
1339  tm->tm_mon = cur_tm.tm_mon;
1340  tm->tm_mday = cur_tm.tm_mday;
1341  break;
1342 
1343  case DTK_TOMORROW:
1344  tmask = DTK_DATE_M;
1345  *dtype = DTK_DATE;
1346  GetCurrentDateTime(&cur_tm);
1347  j2date(date2j(cur_tm.tm_year, cur_tm.tm_mon, cur_tm.tm_mday) + 1,
1348  &tm->tm_year, &tm->tm_mon, &tm->tm_mday);
1349  break;
1350 
1351  case DTK_ZULU:
1352  tmask = (DTK_TIME_M | DTK_M(TZ));
1353  *dtype = DTK_DATE;
1354  tm->tm_hour = 0;
1355  tm->tm_min = 0;
1356  tm->tm_sec = 0;
1357  if (tzp != NULL)
1358  *tzp = 0;
1359  break;
1360 
1361  case DTK_EPOCH:
1362  case DTK_LATE:
1363  case DTK_EARLY:
1364  tmask = (DTK_DATE_M | DTK_TIME_M | DTK_M(TZ));
1365  *dtype = val;
1366  /* caller ignores tm for these dtype codes */
1367  break;
1368 
1369  default:
1370  elog(ERROR, "unrecognized RESERV datetime token: %d",
1371  val);
1372  }
1373 
1374  break;
1375 
1376  case MONTH:
1377 
1378  /*
1379  * already have a (numeric) month? then see if we can
1380  * substitute...
1381  */
1382  if ((fmask & DTK_M(MONTH)) && !haveTextMonth &&
1383  !(fmask & DTK_M(DAY)) && tm->tm_mon >= 1 &&
1384  tm->tm_mon <= 31)
1385  {
1386  tm->tm_mday = tm->tm_mon;
1387  tmask = DTK_M(DAY);
1388  }
1389  haveTextMonth = true;
1390  tm->tm_mon = val;
1391  break;
1392 
1393  case DTZMOD:
1394 
1395  /*
1396  * daylight savings time modifier (solves "MET DST"
1397  * syntax)
1398  */
1399  tmask |= DTK_M(DTZ);
1400  tm->tm_isdst = 1;
1401  if (tzp == NULL)
1402  return DTERR_BAD_FORMAT;
1403  *tzp -= val;
1404  break;
1405 
1406  case DTZ:
1407 
1408  /*
1409  * set mask for TZ here _or_ check for DTZ later when
1410  * getting default timezone
1411  */
1412  tmask |= DTK_M(TZ);
1413  tm->tm_isdst = 1;
1414  if (tzp == NULL)
1415  return DTERR_BAD_FORMAT;
1416  *tzp = -val;
1417  break;
1418 
1419  case TZ:
1420  tm->tm_isdst = 0;
1421  if (tzp == NULL)
1422  return DTERR_BAD_FORMAT;
1423  *tzp = -val;
1424  break;
1425 
1426  case DYNTZ:
1427  tmask |= DTK_M(TZ);
1428  if (tzp == NULL)
1429  return DTERR_BAD_FORMAT;
1430  /* we'll determine the actual offset later */
1431  abbrevTz = valtz;
1432  abbrev = field[i];
1433  break;
1434 
1435  case AMPM:
1436  mer = val;
1437  break;
1438 
1439  case ADBC:
1440  bc = (val == BC);
1441  break;
1442 
1443  case DOW:
1444  tm->tm_wday = val;
1445  break;
1446 
1447  case UNITS:
1448  tmask = 0;
1449  /* reject consecutive unhandled units */
1450  if (ptype != 0)
1451  return DTERR_BAD_FORMAT;
1452  ptype = val;
1453  break;
1454 
1455  case ISOTIME:
1456 
1457  /*
1458  * This is a filler field "t" indicating that the next
1459  * field is time. Try to verify that this is sensible.
1460  */
1461  tmask = 0;
1462 
1463  /* No preceding date? Then quit... */
1464  if ((fmask & DTK_DATE_M) != DTK_DATE_M)
1465  return DTERR_BAD_FORMAT;
1466 
1467  /* reject consecutive unhandled units */
1468  if (ptype != 0)
1469  return DTERR_BAD_FORMAT;
1470  ptype = val;
1471  break;
1472 
1473  case UNKNOWN_FIELD:
1474 
1475  /*
1476  * Before giving up and declaring error, check to see
1477  * if it is an all-alpha timezone name.
1478  */
1479  namedTz = pg_tzset(field[i]);
1480  if (!namedTz)
1481  return DTERR_BAD_FORMAT;
1482  /* we'll apply the zone setting below */
1483  tmask = DTK_M(TZ);
1484  break;
1485 
1486  default:
1487  return DTERR_BAD_FORMAT;
1488  }
1489  break;
1490 
1491  default:
1492  return DTERR_BAD_FORMAT;
1493  }
1494 
1495  if (tmask & fmask)
1496  return DTERR_BAD_FORMAT;
1497  fmask |= tmask;
1498  } /* end loop over fields */
1499 
1500  /* reject if prefix type appeared and was never handled */
1501  if (ptype != 0)
1502  return DTERR_BAD_FORMAT;
1503 
1504  /* do additional checking for normal date specs (but not "infinity" etc) */
1505  if (*dtype == DTK_DATE)
1506  {
1507  /* do final checking/adjustment of Y/M/D fields */
1508  dterr = ValidateDate(fmask, isjulian, is2digits, bc, tm);
1509  if (dterr)
1510  return dterr;
1511 
1512  /* handle AM/PM */
1513  if (mer != HR24 && tm->tm_hour > HOURS_PER_DAY / 2)
1514  return DTERR_FIELD_OVERFLOW;
1515  if (mer == AM && tm->tm_hour == HOURS_PER_DAY / 2)
1516  tm->tm_hour = 0;
1517  else if (mer == PM && tm->tm_hour != HOURS_PER_DAY / 2)
1518  tm->tm_hour += HOURS_PER_DAY / 2;
1519 
1520  /* check for incomplete input */
1521  if ((fmask & DTK_DATE_M) != DTK_DATE_M)
1522  {
1523  if ((fmask & DTK_TIME_M) == DTK_TIME_M)
1524  return 1;
1525  return DTERR_BAD_FORMAT;
1526  }
1527 
1528  /*
1529  * If we had a full timezone spec, compute the offset (we could not do
1530  * it before, because we need the date to resolve DST status).
1531  */
1532  if (namedTz != NULL)
1533  {
1534  /* daylight savings time modifier disallowed with full TZ */
1535  if (fmask & DTK_M(DTZMOD))
1536  return DTERR_BAD_FORMAT;
1537 
1538  *tzp = DetermineTimeZoneOffset(tm, namedTz);
1539  }
1540 
1541  /*
1542  * Likewise, if we had a dynamic timezone abbreviation, resolve it
1543  * now.
1544  */
1545  if (abbrevTz != NULL)
1546  {
1547  /* daylight savings time modifier disallowed with dynamic TZ */
1548  if (fmask & DTK_M(DTZMOD))
1549  return DTERR_BAD_FORMAT;
1550 
1551  *tzp = DetermineTimeZoneAbbrevOffset(tm, abbrev, abbrevTz);
1552  }
1553 
1554  /* timezone not specified? then use session timezone */
1555  if (tzp != NULL && !(fmask & DTK_M(TZ)))
1556  {
1557  /*
1558  * daylight savings time modifier but no standard timezone? then
1559  * error
1560  */
1561  if (fmask & DTK_M(DTZMOD))
1562  return DTERR_BAD_FORMAT;
1563 
1565  }
1566  }
1567 
1568  return 0;
1569 }
static int DecodeDate(char *str, int fmask, int *tmask, bool *is2digits, struct pg_tm *tm)
Definition: datetime.c:2398
static int DecodeNumberField(int len, char *str, int fmask, int *tmask, struct pg_tm *tm, fsec_t *fsec, bool *is2digits)
Definition: datetime.c:2912
static int ParseFraction(char *cp, double *frac)
Definition: datetime.c:680
static int DecodeNumber(int flen, char *str, bool haveTextMonth, int fmask, int *tmask, struct pg_tm *tm, fsec_t *fsec, bool *is2digits)
Definition: datetime.c:2727
int DetermineTimeZoneOffset(struct pg_tm *tm, pg_tz *tzp)
Definition: datetime.c:1585
int DecodeTimezoneAbbrev(int field, const char *lowtoken, int *ftype, int *offset, pg_tz **tz, DateTimeErrorExtra *extra)
Definition: datetime.c:3091
static int DecodeTime(char *str, int fmask, int range, int *tmask, struct pg_tm *tm, fsec_t *fsec)
Definition: datetime.c:2672
int ValidateDate(int fmask, bool isjulian, bool is2digits, bool bc, struct pg_tm *tm)
Definition: datetime.c:2508
int DecodeSpecial(int field, const char *lowtoken, int *val)
Definition: datetime.c:3148
void j2date(int jd, int *year, int *month, int *day)
Definition: datetime.c:311
void GetCurrentDateTime(struct pg_tm *tm)
Definition: datetime.c:366
int DecodeTimezone(const char *str, int *tzp)
Definition: datetime.c:3007
void GetCurrentTimeUsec(struct pg_tm *tm, fsec_t *fsec, int *tzp)
Definition: datetime.c:387
int DetermineTimeZoneAbbrevOffset(struct pg_tm *tm, const char *abbr, pg_tz *tzp)
Definition: datetime.c:1746
void dt2time(Timestamp jd, int *hour, int *min, int *sec, fsec_t *fsec)
Definition: timestamp.c:1864
#define USECS_PER_DAY
Definition: timestamp.h:131
#define HOURS_PER_DAY
Definition: timestamp.h:118
bool time_overflows(int hour, int min, int sec, fsec_t fsec)
Definition: date.c:1439
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:225
#define DTK_TOMORROW
Definition: datetime.h:156
#define DTK_EPOCH
Definition: datetime.h:152
#define DTK_SPECIAL
Definition: datetime.h:149
#define AMPM
Definition: datetime.h:99
#define DTK_TIME
Definition: datetime.h:145
#define UNKNOWN_FIELD
Definition: datetime.h:124
#define PM
Definition: datetime.h:72
#define DTK_NUMBER
Definition: datetime.h:141
#define DTK_STRING
Definition: datetime.h:142
#define DTK_JULIAN
Definition: datetime.h:173
#define MONTH
Definition: datetime.h:91
#define IGNORE_DTF
Definition: datetime.h:98
#define DTK_TIME_M
Definition: datetime.h:192
#define DTK_M(t)
Definition: datetime.h:187
#define DAY
Definition: datetime.h:93
#define ADBC
Definition: datetime.h:108
#define DTK_LATE
Definition: datetime.h:151
#define DTK_DATE
Definition: datetime.h:144
#define RESERV
Definition: datetime.h:90
#define BC
Definition: datetime.h:76
#define HR24
Definition: datetime.h:73
#define DTK_DATE_M
Definition: datetime.h:191
#define DTK_EARLY
Definition: datetime.h:150
#define DTZMOD
Definition: datetime.h:122
#define DTK_TZ
Definition: datetime.h:146
#define DOW
Definition: datetime.h:106
#define ISOTIME
Definition: datetime.h:115
#define AM
Definition: datetime.h:71
#define DTK_YESTERDAY
Definition: datetime.h:154
#define DTK_ZULU
Definition: datetime.h:157
#define UNITS
Definition: datetime.h:107
#define DTK_TODAY
Definition: datetime.h:155
#define DTK_NOW
Definition: datetime.h:153
static struct @160 value
long val
Definition: informix.c:689
static struct pg_tm tm
Definition: localtime.c:104
PGDLLIMPORT pg_tz * session_timezone
Definition: pgtz.c:28
pg_tz * pg_tzset(const char *tzname)
Definition: pgtz.c:234
int strtoint(const char *pg_restrict str, char **pg_restrict endptr, int base)
Definition: string.c:50
Definition: pgtime.h:35
int tm_hour
Definition: pgtime.h:38
int tm_mday
Definition: pgtime.h:39
int tm_mon
Definition: pgtime.h:40
int tm_min
Definition: pgtime.h:37
int tm_wday
Definition: pgtime.h:42
int tm_sec
Definition: pgtime.h:36
int tm_isdst
Definition: pgtime.h:44
int tm_year
Definition: pgtime.h:41
Definition: pgtz.h:66
#define INTERVAL_FULL_RANGE
Definition: timestamp.h:76
const char * type

References ADBC, AM, AMPM, BC, date2j(), DAY, DecodeDate(), DecodeNumber(), DecodeNumberField(), DecodeSpecial(), DecodeTime(), DecodeTimezone(), DecodeTimezoneAbbrev(), DetermineTimeZoneAbbrevOffset(), DetermineTimeZoneOffset(), DOW, dt2time(), DateTimeErrorExtra::dtee_timezone, DTERR_BAD_FORMAT, DTERR_BAD_TIMEZONE, DTERR_FIELD_OVERFLOW, DTK_DATE, DTK_DATE_M, DTK_EARLY, DTK_EPOCH, DTK_JULIAN, DTK_LATE, DTK_M, DTK_NOW, DTK_NUMBER, DTK_SPECIAL, DTK_STRING, DTK_TIME, DTK_TIME_M, DTK_TODAY, DTK_TOMORROW, DTK_TZ, DTK_YESTERDAY, DTK_ZULU, DTZ, DTZMOD, DYNTZ, elog, ERROR, GetCurrentDateTime(), GetCurrentTimeUsec(), HOURS_PER_DAY, HR24, i, IGNORE_DTF, INTERVAL_FULL_RANGE, ISOTIME, j2date(), MONTH, ParseFraction(), pg_tzset(), PM, RESERV, session_timezone, strtoint(), time_overflows(), tm, pg_tm::tm_hour, pg_tm::tm_isdst, pg_tm::tm_mday, pg_tm::tm_min, pg_tm::tm_mon, pg_tm::tm_sec, pg_tm::tm_wday, pg_tm::tm_year, type, TZ, UNITS, UNKNOWN_FIELD, USECS_PER_DAY, val, ValidateDate(), and value.

Referenced by check_recovery_target_time(), date_in(), PGTYPESdate_from_asc(), PGTYPEStimestamp_from_asc(), timestamp_in(), and timestamptz_in().

◆ DecodeInterval()

int DecodeInterval ( char **  field,
int *  ftype,
int  nf,
int  range,
int *  dtype,
struct pg_itm_in itm_in 
)

Definition at line 3364 of file datetime.c.

3366 {
3367  bool force_negative = false;
3368  bool is_before = false;
3369  bool parsing_unit_val = false;
3370  char *cp;
3371  int fmask = 0,
3372  tmask,
3373  type,
3374  uval;
3375  int i;
3376  int dterr;
3377  int64 val;
3378  double fval;
3379 
3380  *dtype = DTK_DELTA;
3381  type = IGNORE_DTF;
3382  ClearPgItmIn(itm_in);
3383 
3384  /*----------
3385  * The SQL standard defines the interval literal
3386  * '-1 1:00:00'
3387  * to mean "negative 1 days and negative 1 hours", while Postgres
3388  * traditionally treats this as meaning "negative 1 days and positive
3389  * 1 hours". In SQL_STANDARD intervalstyle, we apply the leading sign
3390  * to all fields if there are no other explicit signs.
3391  *
3392  * We leave the signs alone if there are additional explicit signs.
3393  * This protects us against misinterpreting postgres-style dump output,
3394  * since the postgres-style output code has always put an explicit sign on
3395  * all fields following a negative field. But note that SQL-spec output
3396  * is ambiguous and can be misinterpreted on load! (So it's best practice
3397  * to dump in postgres style, not SQL style.)
3398  *----------
3399  */
3400  if (IntervalStyle == INTSTYLE_SQL_STANDARD && nf > 0 && *field[0] == '-')
3401  {
3402  force_negative = true;
3403  /* Check for additional explicit signs */
3404  for (i = 1; i < nf; i++)
3405  {
3406  if (*field[i] == '-' || *field[i] == '+')
3407  {
3408  force_negative = false;
3409  break;
3410  }
3411  }
3412  }
3413 
3414  /* read through list backwards to pick up units before values */
3415  for (i = nf - 1; i >= 0; i--)
3416  {
3417  switch (ftype[i])
3418  {
3419  case DTK_TIME:
3420  dterr = DecodeTimeForInterval(field[i], fmask, range,
3421  &tmask, itm_in);
3422  if (dterr)
3423  return dterr;
3424  if (force_negative &&
3425  itm_in->tm_usec > 0)
3426  itm_in->tm_usec = -itm_in->tm_usec;
3427  type = DTK_DAY;
3428  parsing_unit_val = false;
3429  break;
3430 
3431  case DTK_TZ:
3432 
3433  /*
3434  * Timezone means a token with a leading sign character and at
3435  * least one digit; there could be ':', '.', '-' embedded in
3436  * it as well.
3437  */
3438  Assert(*field[i] == '-' || *field[i] == '+');
3439 
3440  /*
3441  * Check for signed hh:mm or hh:mm:ss. If so, process exactly
3442  * like DTK_TIME case above, plus handling the sign.
3443  */
3444  if (strchr(field[i] + 1, ':') != NULL &&
3445  DecodeTimeForInterval(field[i] + 1, fmask, range,
3446  &tmask, itm_in) == 0)
3447  {
3448  if (*field[i] == '-')
3449  {
3450  /* flip the sign on time field */
3451  if (itm_in->tm_usec == PG_INT64_MIN)
3452  return DTERR_FIELD_OVERFLOW;
3453  itm_in->tm_usec = -itm_in->tm_usec;
3454  }
3455 
3456  if (force_negative &&
3457  itm_in->tm_usec > 0)
3458  itm_in->tm_usec = -itm_in->tm_usec;
3459 
3460  /*
3461  * Set the next type to be a day, if units are not
3462  * specified. This handles the case of '1 +02:03' since we
3463  * are reading right to left.
3464  */
3465  type = DTK_DAY;
3466  parsing_unit_val = false;
3467  break;
3468  }
3469 
3470  /*
3471  * Otherwise, fall through to DTK_NUMBER case, which can
3472  * handle signed float numbers and signed year-month values.
3473  */
3474 
3475  /* FALLTHROUGH */
3476 
3477  case DTK_DATE:
3478  case DTK_NUMBER:
3479  if (type == IGNORE_DTF)
3480  {
3481  /* use typmod to decide what rightmost field is */
3482  switch (range)
3483  {
3484  case INTERVAL_MASK(YEAR):
3485  type = DTK_YEAR;
3486  break;
3487  case INTERVAL_MASK(MONTH):
3489  type = DTK_MONTH;
3490  break;
3491  case INTERVAL_MASK(DAY):
3492  type = DTK_DAY;
3493  break;
3494  case INTERVAL_MASK(HOUR):
3496  type = DTK_HOUR;
3497  break;
3498  case INTERVAL_MASK(MINUTE):
3501  type = DTK_MINUTE;
3502  break;
3503  case INTERVAL_MASK(SECOND):
3507  type = DTK_SECOND;
3508  break;
3509  default:
3510  type = DTK_SECOND;
3511  break;
3512  }
3513  }
3514 
3515  errno = 0;
3516  val = strtoi64(field[i], &cp, 10);
3517  if (errno == ERANGE)
3518  return DTERR_FIELD_OVERFLOW;
3519 
3520  if (*cp == '-')
3521  {
3522  /* SQL "years-months" syntax */
3523  int val2;
3524 
3525  val2 = strtoint(cp + 1, &cp, 10);
3526  if (errno == ERANGE || val2 < 0 || val2 >= MONTHS_PER_YEAR)
3527  return DTERR_FIELD_OVERFLOW;
3528  if (*cp != '\0')
3529  return DTERR_BAD_FORMAT;
3530  type = DTK_MONTH;
3531  if (*field[i] == '-')
3532  val2 = -val2;
3534  return DTERR_FIELD_OVERFLOW;
3535  if (pg_add_s64_overflow(val, val2, &val))
3536  return DTERR_FIELD_OVERFLOW;
3537  fval = 0;
3538  }
3539  else if (*cp == '.')
3540  {
3541  dterr = ParseFraction(cp, &fval);
3542  if (dterr)
3543  return dterr;
3544  if (*field[i] == '-')
3545  fval = -fval;
3546  }
3547  else if (*cp == '\0')
3548  fval = 0;
3549  else
3550  return DTERR_BAD_FORMAT;
3551 
3552  tmask = 0; /* DTK_M(type); */
3553 
3554  if (force_negative)
3555  {
3556  /* val and fval should be of same sign, but test anyway */
3557  if (val > 0)
3558  val = -val;
3559  if (fval > 0)
3560  fval = -fval;
3561  }
3562 
3563  switch (type)
3564  {
3565  case DTK_MICROSEC:
3566  if (!AdjustMicroseconds(val, fval, 1, itm_in))
3567  return DTERR_FIELD_OVERFLOW;
3568  tmask = DTK_M(MICROSECOND);
3569  break;
3570 
3571  case DTK_MILLISEC:
3572  if (!AdjustMicroseconds(val, fval, 1000, itm_in))
3573  return DTERR_FIELD_OVERFLOW;
3574  tmask = DTK_M(MILLISECOND);
3575  break;
3576 
3577  case DTK_SECOND:
3578  if (!AdjustMicroseconds(val, fval, USECS_PER_SEC, itm_in))
3579  return DTERR_FIELD_OVERFLOW;
3580 
3581  /*
3582  * If any subseconds were specified, consider this
3583  * microsecond and millisecond input as well.
3584  */
3585  if (fval == 0)
3586  tmask = DTK_M(SECOND);
3587  else
3588  tmask = DTK_ALL_SECS_M;
3589  break;
3590 
3591  case DTK_MINUTE:
3592  if (!AdjustMicroseconds(val, fval, USECS_PER_MINUTE, itm_in))
3593  return DTERR_FIELD_OVERFLOW;
3594  tmask = DTK_M(MINUTE);
3595  break;
3596 
3597  case DTK_HOUR:
3598  if (!AdjustMicroseconds(val, fval, USECS_PER_HOUR, itm_in))
3599  return DTERR_FIELD_OVERFLOW;
3600  tmask = DTK_M(HOUR);
3601  type = DTK_DAY; /* set for next field */
3602  break;
3603 
3604  case DTK_DAY:
3605  if (!AdjustDays(val, 1, itm_in) ||
3606  !AdjustFractMicroseconds(fval, USECS_PER_DAY, itm_in))
3607  return DTERR_FIELD_OVERFLOW;
3608  tmask = DTK_M(DAY);
3609  break;
3610 
3611  case DTK_WEEK:
3612  if (!AdjustDays(val, 7, itm_in) ||
3613  !AdjustFractDays(fval, 7, itm_in))
3614  return DTERR_FIELD_OVERFLOW;
3615  tmask = DTK_M(WEEK);
3616  break;
3617 
3618  case DTK_MONTH:
3619  if (!AdjustMonths(val, itm_in) ||
3620  !AdjustFractDays(fval, DAYS_PER_MONTH, itm_in))
3621  return DTERR_FIELD_OVERFLOW;
3622  tmask = DTK_M(MONTH);
3623  break;
3624 
3625  case DTK_YEAR:
3626  if (!AdjustYears(val, 1, itm_in) ||
3627  !AdjustFractYears(fval, 1, itm_in))
3628  return DTERR_FIELD_OVERFLOW;
3629  tmask = DTK_M(YEAR);
3630  break;
3631 
3632  case DTK_DECADE:
3633  if (!AdjustYears(val, 10, itm_in) ||
3634  !AdjustFractYears(fval, 10, itm_in))
3635  return DTERR_FIELD_OVERFLOW;
3636  tmask = DTK_M(DECADE);
3637  break;
3638 
3639  case DTK_CENTURY:
3640  if (!AdjustYears(val, 100, itm_in) ||
3641  !AdjustFractYears(fval, 100, itm_in))
3642  return DTERR_FIELD_OVERFLOW;
3643  tmask = DTK_M(CENTURY);
3644  break;
3645 
3646  case DTK_MILLENNIUM:
3647  if (!AdjustYears(val, 1000, itm_in) ||
3648  !AdjustFractYears(fval, 1000, itm_in))
3649  return DTERR_FIELD_OVERFLOW;
3650  tmask = DTK_M(MILLENNIUM);
3651  break;
3652 
3653  default:
3654  return DTERR_BAD_FORMAT;
3655  }
3656  parsing_unit_val = false;
3657  break;
3658 
3659  case DTK_STRING:
3660  case DTK_SPECIAL:
3661  /* reject consecutive unhandled units */
3662  if (parsing_unit_val)
3663  return DTERR_BAD_FORMAT;
3664  type = DecodeUnits(i, field[i], &uval);
3665  if (type == UNKNOWN_FIELD)
3666  type = DecodeSpecial(i, field[i], &uval);
3667  if (type == IGNORE_DTF)
3668  continue;
3669 
3670  tmask = 0; /* DTK_M(type); */
3671  switch (type)
3672  {
3673  case UNITS:
3674  type = uval;
3675  parsing_unit_val = true;
3676  break;
3677 
3678  case AGO:
3679 
3680  /*
3681  * "ago" is only allowed to appear at the end of the
3682  * interval.
3683  */
3684  if (i != nf - 1)
3685  return DTERR_BAD_FORMAT;
3686  is_before = true;
3687  type = uval;
3688  break;
3689 
3690  case RESERV:
3691  tmask = (DTK_DATE_M | DTK_TIME_M);
3692 
3693  /*
3694  * Only reserved words corresponding to infinite
3695  * intervals are accepted.
3696  */
3697  if (uval != DTK_LATE && uval != DTK_EARLY)
3698  return DTERR_BAD_FORMAT;
3699 
3700  /*
3701  * Infinity cannot be followed by anything else. We
3702  * could allow "ago" to reverse the sign of infinity
3703  * but using signed infinity is more intuitive.
3704  */
3705  if (i != nf - 1)
3706  return DTERR_BAD_FORMAT;
3707 
3708  *dtype = uval;
3709  break;
3710 
3711  default:
3712  return DTERR_BAD_FORMAT;
3713  }
3714  break;
3715 
3716  default:
3717  return DTERR_BAD_FORMAT;
3718  }
3719 
3720  if (tmask & fmask)
3721  return DTERR_BAD_FORMAT;
3722  fmask |= tmask;
3723  }
3724 
3725  /* ensure that at least one time field has been found */
3726  if (fmask == 0)
3727  return DTERR_BAD_FORMAT;
3728 
3729  /* reject if unit appeared and was never handled */
3730  if (parsing_unit_val)
3731  return DTERR_BAD_FORMAT;
3732 
3733  /* finally, AGO negates everything */
3734  if (is_before)
3735  {
3736  if (itm_in->tm_usec == PG_INT64_MIN ||
3737  itm_in->tm_mday == INT_MIN ||
3738  itm_in->tm_mon == INT_MIN ||
3739  itm_in->tm_year == INT_MIN)
3740  return DTERR_FIELD_OVERFLOW;
3741 
3742  itm_in->tm_usec = -itm_in->tm_usec;
3743  itm_in->tm_mday = -itm_in->tm_mday;
3744  itm_in->tm_mon = -itm_in->tm_mon;
3745  itm_in->tm_year = -itm_in->tm_year;
3746  }
3747 
3748  return 0;
3749 }
int DecodeUnits(int field, const char *lowtoken, int *val)
Definition: datetime.c:4047
static bool AdjustDays(int64 val, int scale, struct pg_itm_in *itm_in)
Definition: datetime.c:633
static bool AdjustFractYears(double frac, int scale, struct pg_itm_in *itm_in)
Definition: datetime.c:601
static bool AdjustMicroseconds(int64 val, double fval, int64 scale, struct pg_itm_in *itm_in)
Definition: datetime.c:618
static int DecodeTimeForInterval(char *str, int fmask, int range, int *tmask, struct pg_itm_in *itm_in)
Definition: datetime.c:2701
static bool AdjustFractMicroseconds(double frac, int64 scale, struct pg_itm_in *itm_in)
Definition: datetime.c:537
static bool AdjustYears(int64 val, int scale, struct pg_itm_in *itm_in)
Definition: datetime.c:661
static bool AdjustMonths(int64 val, struct pg_itm_in *itm_in)
Definition: datetime.c:649
static bool AdjustFractDays(double frac, int scale, struct pg_itm_in *itm_in)
Definition: datetime.c:569
static void ClearPgItmIn(struct pg_itm_in *itm_in)
Definition: datetime.c:3340
#define PG_INT64_MIN
Definition: c.h:545
#define USECS_PER_HOUR
Definition: timestamp.h:132
#define MONTHS_PER_YEAR
Definition: timestamp.h:108
#define USECS_PER_SEC
Definition: timestamp.h:134
#define USECS_PER_MINUTE
Definition: timestamp.h:133
#define DAYS_PER_MONTH
Definition: timestamp.h:116
int IntervalStyle
Definition: globals.c:126
#define MILLENNIUM
Definition: datetime.h:120
#define DTK_DECADE
Definition: datetime.h:168
#define DTK_SECOND
Definition: datetime.h:160
#define DTK_DELTA
Definition: datetime.h:159
#define MICROSECOND
Definition: datetime.h:104
#define HOUR
Definition: datetime.h:100
#define WEEK
Definition: datetime.h:117
#define DECADE
Definition: datetime.h:118
#define YEAR
Definition: datetime.h:92
#define DTK_CENTURY
Definition: datetime.h:169
#define MILLISECOND
Definition: datetime.h:103
#define DTK_DAY
Definition: datetime.h:163
#define CENTURY
Definition: datetime.h:119
#define DTK_MILLENNIUM
Definition: datetime.h:170
#define SECOND
Definition: datetime.h:102
#define DTK_ALL_SECS_M
Definition: datetime.h:190
#define DTK_HOUR
Definition: datetime.h:162
#define DTK_WEEK
Definition: datetime.h:164
#define MINUTE
Definition: datetime.h:101
#define DTK_MICROSEC
Definition: datetime.h:172
#define DTK_YEAR
Definition: datetime.h:167
#define AGO
Definition: datetime.h:110
#define DTK_MILLISEC
Definition: datetime.h:171
#define DTK_MONTH
Definition: datetime.h:165
#define DTK_MINUTE
Definition: datetime.h:161
static bool pg_mul_s64_overflow(int64 a, int64 b, int64 *result)
Definition: int.h:261
static bool pg_add_s64_overflow(int64 a, int64 b, int64 *result)
Definition: int.h:203
#define INTSTYLE_SQL_STANDARD
Definition: miscadmin.h:258
static struct cvec * range(struct vars *v, chr a, chr b, int cases)
Definition: regc_locale.c:412
int tm_mon
Definition: timestamp.h:86
int tm_year
Definition: timestamp.h:87
int tm_mday
Definition: timestamp.h:85
int64 tm_usec
Definition: timestamp.h:84
#define INTERVAL_MASK(b)
Definition: timestamp.h:73

References AdjustDays(), AdjustFractDays(), AdjustFractMicroseconds(), AdjustFractYears(), AdjustMicroseconds(), AdjustMonths(), AdjustYears(), AGO, Assert, CENTURY, ClearPgItmIn(), DAY, DAYS_PER_MONTH, DECADE, DecodeSpecial(), DecodeTimeForInterval(), 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_EARLY, DTK_HOUR, DTK_LATE, 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_MASK, IntervalStyle, INTSTYLE_SQL_STANDARD, MICROSECOND, MILLENNIUM, MILLISECOND, MINUTE, MONTH, MONTHS_PER_YEAR, ParseFraction(), pg_add_s64_overflow(), PG_INT64_MIN, pg_mul_s64_overflow(), range(), RESERV, SECOND, strtoint(), pg_itm_in::tm_mday, pg_itm_in::tm_mon, pg_itm_in::tm_usec, pg_itm_in::tm_year, type, UNITS, UNKNOWN_FIELD, USECS_PER_DAY, USECS_PER_HOUR, USECS_PER_MINUTE, USECS_PER_SEC, val, WEEK, and YEAR.

Referenced by interval_in().

◆ DecodeISO8601Interval()

int DecodeISO8601Interval ( char *  str,
int *  dtype,
struct pg_itm_in itm_in 
)

Definition at line 3829 of file datetime.c.

3831 {
3832  bool datepart = true;
3833  bool havefield = false;
3834 
3835  *dtype = DTK_DELTA;
3836  ClearPgItmIn(itm_in);
3837 
3838  if (strlen(str) < 2 || str[0] != 'P')
3839  return DTERR_BAD_FORMAT;
3840 
3841  str++;
3842  while (*str)
3843  {
3844  char *fieldstart;
3845  int64 val;
3846  double fval;
3847  char unit;
3848  int dterr;
3849 
3850  if (*str == 'T') /* T indicates the beginning of the time part */
3851  {
3852  datepart = false;
3853  havefield = false;
3854  str++;
3855  continue;
3856  }
3857 
3858  fieldstart = str;
3859  dterr = ParseISO8601Number(str, &str, &val, &fval);
3860  if (dterr)
3861  return dterr;
3862 
3863  /*
3864  * Note: we could step off the end of the string here. Code below
3865  * *must* exit the loop if unit == '\0'.
3866  */
3867  unit = *str++;
3868 
3869  if (datepart)
3870  {
3871  switch (unit) /* before T: Y M W D */
3872  {
3873  case 'Y':
3874  if (!AdjustYears(val, 1, itm_in) ||
3875  !AdjustFractYears(fval, 1, itm_in))
3876  return DTERR_FIELD_OVERFLOW;
3877  break;
3878  case 'M':
3879  if (!AdjustMonths(val, itm_in) ||
3880  !AdjustFractDays(fval, DAYS_PER_MONTH, itm_in))
3881  return DTERR_FIELD_OVERFLOW;
3882  break;
3883  case 'W':
3884  if (!AdjustDays(val, 7, itm_in) ||
3885  !AdjustFractDays(fval, 7, itm_in))
3886  return DTERR_FIELD_OVERFLOW;
3887  break;
3888  case 'D':
3889  if (!AdjustDays(val, 1, itm_in) ||
3890  !AdjustFractMicroseconds(fval, USECS_PER_DAY, itm_in))
3891  return DTERR_FIELD_OVERFLOW;
3892  break;
3893  case 'T': /* ISO 8601 4.4.3.3 Alternative Format / Basic */
3894  case '\0':
3895  if (ISO8601IntegerWidth(fieldstart) == 8 && !havefield)
3896  {
3897  if (!AdjustYears(val / 10000, 1, itm_in) ||
3898  !AdjustMonths((val / 100) % 100, itm_in) ||
3899  !AdjustDays(val % 100, 1, itm_in) ||
3900  !AdjustFractMicroseconds(fval, USECS_PER_DAY, itm_in))
3901  return DTERR_FIELD_OVERFLOW;
3902  if (unit == '\0')
3903  return 0;
3904  datepart = false;
3905  havefield = false;
3906  continue;
3907  }
3908  /* Else fall through to extended alternative format */
3909  /* FALLTHROUGH */
3910  case '-': /* ISO 8601 4.4.3.3 Alternative Format,
3911  * Extended */
3912  if (havefield)
3913  return DTERR_BAD_FORMAT;
3914 
3915  if (!AdjustYears(val, 1, itm_in) ||
3916  !AdjustFractYears(fval, 1, itm_in))
3917  return DTERR_FIELD_OVERFLOW;
3918  if (unit == '\0')
3919  return 0;
3920  if (unit == 'T')
3921  {
3922  datepart = false;
3923  havefield = false;
3924  continue;
3925  }
3926 
3927  dterr = ParseISO8601Number(str, &str, &val, &fval);
3928  if (dterr)
3929  return dterr;
3930  if (!AdjustMonths(val, itm_in) ||
3931  !AdjustFractDays(fval, DAYS_PER_MONTH, itm_in))
3932  return DTERR_FIELD_OVERFLOW;
3933  if (*str == '\0')
3934  return 0;
3935  if (*str == 'T')
3936  {
3937  datepart = false;
3938  havefield = false;
3939  continue;
3940  }
3941  if (*str != '-')
3942  return DTERR_BAD_FORMAT;
3943  str++;
3944 
3945  dterr = ParseISO8601Number(str, &str, &val, &fval);
3946  if (dterr)
3947  return dterr;
3948  if (!AdjustDays(val, 1, itm_in) ||
3949  !AdjustFractMicroseconds(fval, USECS_PER_DAY, itm_in))
3950  return DTERR_FIELD_OVERFLOW;
3951  if (*str == '\0')
3952  return 0;
3953  if (*str == 'T')
3954  {
3955  datepart = false;
3956  havefield = false;
3957  continue;
3958  }
3959  return DTERR_BAD_FORMAT;
3960  default:
3961  /* not a valid date unit suffix */
3962  return DTERR_BAD_FORMAT;
3963  }
3964  }
3965  else
3966  {
3967  switch (unit) /* after T: H M S */
3968  {
3969  case 'H':
3970  if (!AdjustMicroseconds(val, fval, USECS_PER_HOUR, itm_in))
3971  return DTERR_FIELD_OVERFLOW;
3972  break;
3973  case 'M':
3974  if (!AdjustMicroseconds(val, fval, USECS_PER_MINUTE, itm_in))
3975  return DTERR_FIELD_OVERFLOW;
3976  break;
3977  case 'S':
3978  if (!AdjustMicroseconds(val, fval, USECS_PER_SEC, itm_in))
3979  return DTERR_FIELD_OVERFLOW;
3980  break;
3981  case '\0': /* ISO 8601 4.4.3.3 Alternative Format */
3982  if (ISO8601IntegerWidth(fieldstart) == 6 && !havefield)
3983  {
3984  if (!AdjustMicroseconds(val / 10000, 0, USECS_PER_HOUR, itm_in) ||
3985  !AdjustMicroseconds((val / 100) % 100, 0, USECS_PER_MINUTE, itm_in) ||
3986  !AdjustMicroseconds(val % 100, 0, USECS_PER_SEC, itm_in) ||
3987  !AdjustFractMicroseconds(fval, 1, itm_in))
3988  return DTERR_FIELD_OVERFLOW;
3989  return 0;
3990  }
3991  /* Else fall through to extended alternative format */
3992  /* FALLTHROUGH */
3993  case ':': /* ISO 8601 4.4.3.3 Alternative Format,
3994  * Extended */
3995  if (havefield)
3996  return DTERR_BAD_FORMAT;
3997 
3998  if (!AdjustMicroseconds(val, fval, USECS_PER_HOUR, itm_in))
3999  return DTERR_FIELD_OVERFLOW;
4000  if (unit == '\0')
4001  return 0;
4002 
4003  dterr = ParseISO8601Number(str, &str, &val, &fval);
4004  if (dterr)
4005  return dterr;
4006  if (!AdjustMicroseconds(val, fval, USECS_PER_MINUTE, itm_in))
4007  return DTERR_FIELD_OVERFLOW;
4008  if (*str == '\0')
4009  return 0;
4010  if (*str != ':')
4011  return DTERR_BAD_FORMAT;
4012  str++;
4013 
4014  dterr = ParseISO8601Number(str, &str, &val, &fval);
4015  if (dterr)
4016  return dterr;
4017  if (!AdjustMicroseconds(val, fval, USECS_PER_SEC, itm_in))
4018  return DTERR_FIELD_OVERFLOW;
4019  if (*str == '\0')
4020  return 0;
4021  return DTERR_BAD_FORMAT;
4022 
4023  default:
4024  /* not a valid time unit suffix */
4025  return DTERR_BAD_FORMAT;
4026  }
4027  }
4028 
4029  havefield = true;
4030  }
4031 
4032  return 0;
4033 }
static int ISO8601IntegerWidth(char *fieldstart)
Definition: datetime.c:3802
static int ParseISO8601Number(char *str, char **endptr, int64 *ipart, double *fpart)
Definition: datetime.c:3760

References AdjustDays(), AdjustFractDays(), AdjustFractMicroseconds(), AdjustFractYears(), AdjustMicroseconds(), AdjustMonths(), AdjustYears(), ClearPgItmIn(), DAYS_PER_MONTH, DTERR_BAD_FORMAT, DTERR_FIELD_OVERFLOW, DTK_DELTA, ISO8601IntegerWidth(), ParseISO8601Number(), str, USECS_PER_DAY, USECS_PER_HOUR, USECS_PER_MINUTE, USECS_PER_SEC, and val.

Referenced by interval_in().

◆ DecodeSpecial()

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

Definition at line 3148 of file datetime.c.

3149 {
3150  int type;
3151  const datetkn *tp;
3152 
3153  tp = datecache[field];
3154  /* use strncmp so that we match truncated tokens */
3155  if (tp == NULL || strncmp(lowtoken, tp->token, TOKMAXLEN) != 0)
3156  {
3157  tp = datebsearch(lowtoken, datetktbl, szdatetktbl);
3158  }
3159  if (tp == NULL)
3160  {
3161  type = UNKNOWN_FIELD;
3162  *val = 0;
3163  }
3164  else
3165  {
3166  datecache[field] = tp;
3167  type = tp->type;
3168  *val = tp->value;
3169  }
3170 
3171  return type;
3172 }
static const datetkn * datecache[MAXDATEFIELDS]
Definition: datetime.c:258
static const datetkn * datebsearch(const char *key, const datetkn *base, int nel)
Definition: datetime.c:4153

References datebsearch(), datecache, datetktbl, szdatetktbl, datetkn::token, TOKMAXLEN, type, datetkn::type, UNKNOWN_FIELD, val, and datetkn::value.

Referenced by DecodeDate(), DecodeDateTime(), DecodeInterval(), DecodeTimeOnly(), extract_date(), interval_part_common(), time_part_common(), timestamp_part_common(), timestamptz_part_common(), and timetz_part_common().

◆ DecodeTimeOnly()

int DecodeTimeOnly ( char **  field,
int *  ftype,
int  nf,
int *  dtype,
struct pg_tm tm,
fsec_t fsec,
int *  tzp,
DateTimeErrorExtra extra 
)

Definition at line 1864 of file datetime.c.

1867 {
1868  int fmask = 0,
1869  tmask,
1870  type;
1871  int ptype = 0; /* "prefix type" for ISO and Julian formats */
1872  int i;
1873  int val;
1874  int dterr;
1875  bool isjulian = false;
1876  bool is2digits = false;
1877  bool bc = false;
1878  int mer = HR24;
1879  pg_tz *namedTz = NULL;
1880  pg_tz *abbrevTz = NULL;
1881  char *abbrev = NULL;
1882  pg_tz *valtz;
1883 
1884  *dtype = DTK_TIME;
1885  tm->tm_hour = 0;
1886  tm->tm_min = 0;
1887  tm->tm_sec = 0;
1888  *fsec = 0;
1889  /* don't know daylight savings time status apriori */
1890  tm->tm_isdst = -1;
1891 
1892  if (tzp != NULL)
1893  *tzp = 0;
1894 
1895  for (i = 0; i < nf; i++)
1896  {
1897  switch (ftype[i])
1898  {
1899  case DTK_DATE:
1900 
1901  /*
1902  * Time zone not allowed? Then should not accept dates or time
1903  * zones no matter what else!
1904  */
1905  if (tzp == NULL)
1906  return DTERR_BAD_FORMAT;
1907 
1908  /* Under limited circumstances, we will accept a date... */
1909  if (i == 0 && nf >= 2 &&
1910  (ftype[nf - 1] == DTK_DATE || ftype[1] == DTK_TIME))
1911  {
1912  dterr = DecodeDate(field[i], fmask,
1913  &tmask, &is2digits, tm);
1914  if (dterr)
1915  return dterr;
1916  }
1917  /* otherwise, this is a time and/or time zone */
1918  else
1919  {
1920  if (isdigit((unsigned char) *field[i]))
1921  {
1922  char *cp;
1923 
1924  /*
1925  * Starts with a digit but we already have a time
1926  * field? Then we are in trouble with time already...
1927  */
1928  if ((fmask & DTK_TIME_M) == DTK_TIME_M)
1929  return DTERR_BAD_FORMAT;
1930 
1931  /*
1932  * Should not get here and fail. Sanity check only...
1933  */
1934  if ((cp = strchr(field[i], '-')) == NULL)
1935  return DTERR_BAD_FORMAT;
1936 
1937  /* Get the time zone from the end of the string */
1938  dterr = DecodeTimezone(cp, tzp);
1939  if (dterr)
1940  return dterr;
1941  *cp = '\0';
1942 
1943  /*
1944  * Then read the rest of the field as a concatenated
1945  * time
1946  */
1947  dterr = DecodeNumberField(strlen(field[i]), field[i],
1948  (fmask | DTK_DATE_M),
1949  &tmask, tm,
1950  fsec, &is2digits);
1951  if (dterr < 0)
1952  return dterr;
1953  ftype[i] = dterr;
1954 
1955  tmask |= DTK_M(TZ);
1956  }
1957  else
1958  {
1959  namedTz = pg_tzset(field[i]);
1960  if (!namedTz)
1961  {
1962  extra->dtee_timezone = field[i];
1963  return DTERR_BAD_TIMEZONE;
1964  }
1965  /* we'll apply the zone setting below */
1966  ftype[i] = DTK_TZ;
1967  tmask = DTK_M(TZ);
1968  }
1969  }
1970  break;
1971 
1972  case DTK_TIME:
1973 
1974  /*
1975  * This might be an ISO time following a "t" field.
1976  */
1977  if (ptype != 0)
1978  {
1979  if (ptype != DTK_TIME)
1980  return DTERR_BAD_FORMAT;
1981  ptype = 0;
1982  }
1983 
1984  dterr = DecodeTime(field[i], (fmask | DTK_DATE_M),
1986  &tmask, tm, fsec);
1987  if (dterr)
1988  return dterr;
1989  break;
1990 
1991  case DTK_TZ:
1992  {
1993  int tz;
1994 
1995  if (tzp == NULL)
1996  return DTERR_BAD_FORMAT;
1997 
1998  dterr = DecodeTimezone(field[i], &tz);
1999  if (dterr)
2000  return dterr;
2001  *tzp = tz;
2002  tmask = DTK_M(TZ);
2003  }
2004  break;
2005 
2006  case DTK_NUMBER:
2007 
2008  /*
2009  * Deal with cases where previous field labeled this one
2010  */
2011  if (ptype != 0)
2012  {
2013  char *cp;
2014  int value;
2015 
2016  errno = 0;
2017  value = strtoint(field[i], &cp, 10);
2018  if (errno == ERANGE)
2019  return DTERR_FIELD_OVERFLOW;
2020  if (*cp != '.' && *cp != '\0')
2021  return DTERR_BAD_FORMAT;
2022 
2023  switch (ptype)
2024  {
2025  case DTK_JULIAN:
2026  /* previous field was a label for "julian date" */
2027  if (tzp == NULL)
2028  return DTERR_BAD_FORMAT;
2029  if (value < 0)
2030  return DTERR_FIELD_OVERFLOW;
2031  tmask = DTK_DATE_M;
2032  j2date(value, &tm->tm_year, &tm->tm_mon, &tm->tm_mday);
2033  isjulian = true;
2034 
2035  if (*cp == '.')
2036  {
2037  double time;
2038 
2039  dterr = ParseFraction(cp, &time);
2040  if (dterr)
2041  return dterr;
2042  time *= USECS_PER_DAY;
2043  dt2time(time,
2044  &tm->tm_hour, &tm->tm_min,
2045  &tm->tm_sec, fsec);
2046  tmask |= DTK_TIME_M;
2047  }
2048  break;
2049 
2050  case DTK_TIME:
2051  /* previous field was "t" for ISO time */
2052  dterr = DecodeNumberField(strlen(field[i]), field[i],
2053  (fmask | DTK_DATE_M),
2054  &tmask, tm,
2055  fsec, &is2digits);
2056  if (dterr < 0)
2057  return dterr;
2058  ftype[i] = dterr;
2059 
2060  if (tmask != DTK_TIME_M)
2061  return DTERR_BAD_FORMAT;
2062  break;
2063 
2064  default:
2065  return DTERR_BAD_FORMAT;
2066  break;
2067  }
2068 
2069  ptype = 0;
2070  *dtype = DTK_DATE;
2071  }
2072  else
2073  {
2074  char *cp;
2075  int flen;
2076 
2077  flen = strlen(field[i]);
2078  cp = strchr(field[i], '.');
2079 
2080  /* Embedded decimal? */
2081  if (cp != NULL)
2082  {
2083  /*
2084  * Under limited circumstances, we will accept a
2085  * date...
2086  */
2087  if (i == 0 && nf >= 2 && ftype[nf - 1] == DTK_DATE)
2088  {
2089  dterr = DecodeDate(field[i], fmask,
2090  &tmask, &is2digits, tm);
2091  if (dterr)
2092  return dterr;
2093  }
2094  /* embedded decimal and several digits before? */
2095  else if (flen - strlen(cp) > 2)
2096  {
2097  /*
2098  * Interpret as a concatenated date or time Set
2099  * the type field to allow decoding other fields
2100  * later. Example: 20011223 or 040506
2101  */
2102  dterr = DecodeNumberField(flen, field[i],
2103  (fmask | DTK_DATE_M),
2104  &tmask, tm,
2105  fsec, &is2digits);
2106  if (dterr < 0)
2107  return dterr;
2108  ftype[i] = dterr;
2109  }
2110  else
2111  return DTERR_BAD_FORMAT;
2112  }
2113  else if (flen > 4)
2114  {
2115  dterr = DecodeNumberField(flen, field[i],
2116  (fmask | DTK_DATE_M),
2117  &tmask, tm,
2118  fsec, &is2digits);
2119  if (dterr < 0)
2120  return dterr;
2121  ftype[i] = dterr;
2122  }
2123  /* otherwise it is a single date/time field... */
2124  else
2125  {
2126  dterr = DecodeNumber(flen, field[i],
2127  false,
2128  (fmask | DTK_DATE_M),
2129  &tmask, tm,
2130  fsec, &is2digits);
2131  if (dterr)
2132  return dterr;
2133  }
2134  }
2135  break;
2136 
2137  case DTK_STRING:
2138  case DTK_SPECIAL:
2139  /* timezone abbrevs take precedence over built-in tokens */
2140  dterr = DecodeTimezoneAbbrev(i, field[i],
2141  &type, &val, &valtz, extra);
2142  if (dterr)
2143  return dterr;
2144  if (type == UNKNOWN_FIELD)
2145  type = DecodeSpecial(i, field[i], &val);
2146  if (type == IGNORE_DTF)
2147  continue;
2148 
2149  tmask = DTK_M(type);
2150  switch (type)
2151  {
2152  case RESERV:
2153  switch (val)
2154  {
2155  case DTK_NOW:
2156  tmask = DTK_TIME_M;
2157  *dtype = DTK_TIME;
2158  GetCurrentTimeUsec(tm, fsec, NULL);
2159  break;
2160 
2161  case DTK_ZULU:
2162  tmask = (DTK_TIME_M | DTK_M(TZ));
2163  *dtype = DTK_TIME;
2164  tm->tm_hour = 0;
2165  tm->tm_min = 0;
2166  tm->tm_sec = 0;
2167  tm->tm_isdst = 0;
2168  break;
2169 
2170  default:
2171  return DTERR_BAD_FORMAT;
2172  }
2173 
2174  break;
2175 
2176  case DTZMOD:
2177 
2178  /*
2179  * daylight savings time modifier (solves "MET DST"
2180  * syntax)
2181  */
2182  tmask |= DTK_M(DTZ);
2183  tm->tm_isdst = 1;
2184  if (tzp == NULL)
2185  return DTERR_BAD_FORMAT;
2186  *tzp -= val;
2187  break;
2188 
2189  case DTZ:
2190 
2191  /*
2192  * set mask for TZ here _or_ check for DTZ later when
2193  * getting default timezone
2194  */
2195  tmask |= DTK_M(TZ);
2196  tm->tm_isdst = 1;
2197  if (tzp == NULL)
2198  return DTERR_BAD_FORMAT;
2199  *tzp = -val;
2200  ftype[i] = DTK_TZ;
2201  break;
2202 
2203  case TZ:
2204  tm->tm_isdst = 0;
2205  if (tzp == NULL)
2206  return DTERR_BAD_FORMAT;
2207  *tzp = -val;
2208  ftype[i] = DTK_TZ;
2209  break;
2210 
2211  case DYNTZ:
2212  tmask |= DTK_M(TZ);
2213  if (tzp == NULL)
2214  return DTERR_BAD_FORMAT;
2215  /* we'll determine the actual offset later */
2216  abbrevTz = valtz;
2217  abbrev = field[i];
2218  ftype[i] = DTK_TZ;
2219  break;
2220 
2221  case AMPM:
2222  mer = val;
2223  break;
2224 
2225  case ADBC:
2226  bc = (val == BC);
2227  break;
2228 
2229  case UNITS:
2230  tmask = 0;
2231  /* reject consecutive unhandled units */
2232  if (ptype != 0)
2233  return DTERR_BAD_FORMAT;
2234  ptype = val;
2235  break;
2236 
2237  case ISOTIME:
2238  tmask = 0;
2239  /* reject consecutive unhandled units */
2240  if (ptype != 0)
2241  return DTERR_BAD_FORMAT;
2242  ptype = val;
2243  break;
2244 
2245  case UNKNOWN_FIELD:
2246 
2247  /*
2248  * Before giving up and declaring error, check to see
2249  * if it is an all-alpha timezone name.
2250  */
2251  namedTz = pg_tzset(field[i]);
2252  if (!namedTz)
2253  return DTERR_BAD_FORMAT;
2254  /* we'll apply the zone setting below */
2255  tmask = DTK_M(TZ);
2256  break;
2257 
2258  default:
2259  return DTERR_BAD_FORMAT;
2260  }
2261  break;
2262 
2263  default:
2264  return DTERR_BAD_FORMAT;
2265  }
2266 
2267  if (tmask & fmask)
2268  return DTERR_BAD_FORMAT;
2269  fmask |= tmask;
2270  } /* end loop over fields */
2271 
2272  /* reject if prefix type appeared and was never handled */
2273  if (ptype != 0)
2274  return DTERR_BAD_FORMAT;
2275 
2276  /* do final checking/adjustment of Y/M/D fields */
2277  dterr = ValidateDate(fmask, isjulian, is2digits, bc, tm);
2278  if (dterr)
2279  return dterr;
2280 
2281  /* handle AM/PM */
2282  if (mer != HR24 && tm->tm_hour > HOURS_PER_DAY / 2)
2283  return DTERR_FIELD_OVERFLOW;
2284  if (mer == AM && tm->tm_hour == HOURS_PER_DAY / 2)
2285  tm->tm_hour = 0;
2286  else if (mer == PM && tm->tm_hour != HOURS_PER_DAY / 2)
2287  tm->tm_hour += HOURS_PER_DAY / 2;
2288 
2289  /* check for time overflow */
2290  if (time_overflows(tm->tm_hour, tm->tm_min, tm->tm_sec, *fsec))
2291  return DTERR_FIELD_OVERFLOW;
2292 
2293  if ((fmask & DTK_TIME_M) != DTK_TIME_M)
2294  return DTERR_BAD_FORMAT;
2295 
2296  /*
2297  * If we had a full timezone spec, compute the offset (we could not do it
2298  * before, because we may need the date to resolve DST status).
2299  */
2300  if (namedTz != NULL)
2301  {
2302  long int gmtoff;
2303 
2304  /* daylight savings time modifier disallowed with full TZ */
2305  if (fmask & DTK_M(DTZMOD))
2306  return DTERR_BAD_FORMAT;
2307 
2308  /* if non-DST zone, we do not need to know the date */
2309  if (pg_get_timezone_offset(namedTz, &gmtoff))
2310  {
2311  *tzp = -(int) gmtoff;
2312  }
2313  else
2314  {
2315  /* a date has to be specified */
2316  if ((fmask & DTK_DATE_M) != DTK_DATE_M)
2317  return DTERR_BAD_FORMAT;
2318  *tzp = DetermineTimeZoneOffset(tm, namedTz);
2319  }
2320  }
2321 
2322  /*
2323  * Likewise, if we had a dynamic timezone abbreviation, resolve it now.
2324  */
2325  if (abbrevTz != NULL)
2326  {
2327  struct pg_tm tt,
2328  *tmp = &tt;
2329 
2330  /*
2331  * daylight savings time modifier but no standard timezone? then error
2332  */
2333  if (fmask & DTK_M(DTZMOD))
2334  return DTERR_BAD_FORMAT;
2335 
2336  if ((fmask & DTK_DATE_M) == 0)
2337  GetCurrentDateTime(tmp);
2338  else
2339  {
2340  /* a date has to be specified */
2341  if ((fmask & DTK_DATE_M) != DTK_DATE_M)
2342  return DTERR_BAD_FORMAT;
2343  tmp->tm_year = tm->tm_year;
2344  tmp->tm_mon = tm->tm_mon;
2345  tmp->tm_mday = tm->tm_mday;
2346  }
2347  tmp->tm_hour = tm->tm_hour;
2348  tmp->tm_min = tm->tm_min;
2349  tmp->tm_sec = tm->tm_sec;
2350  *tzp = DetermineTimeZoneAbbrevOffset(tmp, abbrev, abbrevTz);
2351  tm->tm_isdst = tmp->tm_isdst;
2352  }
2353 
2354  /* timezone not specified? then use session timezone */
2355  if (tzp != NULL && !(fmask & DTK_M(TZ)))
2356  {
2357  struct pg_tm tt,
2358  *tmp = &tt;
2359 
2360  /*
2361  * daylight savings time modifier but no standard timezone? then error
2362  */
2363  if (fmask & DTK_M(DTZMOD))
2364  return DTERR_BAD_FORMAT;
2365 
2366  if ((fmask & DTK_DATE_M) == 0)
2367  GetCurrentDateTime(tmp);
2368  else
2369  {
2370  /* a date has to be specified */
2371  if ((fmask & DTK_DATE_M) != DTK_DATE_M)
2372  return DTERR_BAD_FORMAT;
2373  tmp->tm_year = tm->tm_year;
2374  tmp->tm_mon = tm->tm_mon;
2375  tmp->tm_mday = tm->tm_mday;
2376  }
2377  tmp->tm_hour = tm->tm_hour;
2378  tmp->tm_min = tm->tm_min;
2379  tmp->tm_sec = tm->tm_sec;
2381  tm->tm_isdst = tmp->tm_isdst;
2382  }
2383 
2384  return 0;
2385 }
bool pg_get_timezone_offset(const pg_tz *tz, long int *gmtoff)
Definition: localtime.c:1851

References ADBC, AM, AMPM, BC, DecodeDate(), DecodeNumber(), DecodeNumberField(), DecodeSpecial(), DecodeTime(), DecodeTimezone(), DecodeTimezoneAbbrev(), DetermineTimeZoneAbbrevOffset(), DetermineTimeZoneOffset(), dt2time(), DateTimeErrorExtra::dtee_timezone, DTERR_BAD_FORMAT, DTERR_BAD_TIMEZONE, DTERR_FIELD_OVERFLOW, DTK_DATE, DTK_DATE_M, DTK_JULIAN, DTK_M, DTK_NOW, DTK_NUMBER, DTK_SPECIAL, DTK_STRING, DTK_TIME, DTK_TIME_M, DTK_TZ, DTK_ZULU, DTZ, DTZMOD, DYNTZ, GetCurrentDateTime(), GetCurrentTimeUsec(), HOURS_PER_DAY, HR24, i, IGNORE_DTF, INTERVAL_FULL_RANGE, ISOTIME, j2date(), ParseFraction(), pg_get_timezone_offset(), pg_tzset(), PM, RESERV, session_timezone, strtoint(), time_overflows(), tm, pg_tm::tm_hour, pg_tm::tm_isdst, pg_tm::tm_mday, pg_tm::tm_min, pg_tm::tm_mon, pg_tm::tm_sec, pg_tm::tm_year, type, TZ, UNITS, UNKNOWN_FIELD, USECS_PER_DAY, val, ValidateDate(), and value.

Referenced by time_in(), and timetz_in().

◆ DecodeTimezone()

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

Definition at line 3007 of file datetime.c.

3008 {
3009  int tz;
3010  int hr,
3011  min,
3012  sec = 0;
3013  char *cp;
3014 
3015  /* leading character must be "+" or "-" */
3016  if (*str != '+' && *str != '-')
3017  return DTERR_BAD_FORMAT;
3018 
3019  errno = 0;
3020  hr = strtoint(str + 1, &cp, 10);
3021  if (errno == ERANGE)
3022  return DTERR_TZDISP_OVERFLOW;
3023 
3024  /* explicit delimiter? */
3025  if (*cp == ':')
3026  {
3027  errno = 0;
3028  min = strtoint(cp + 1, &cp, 10);
3029  if (errno == ERANGE)
3030  return DTERR_TZDISP_OVERFLOW;
3031  if (*cp == ':')
3032  {
3033  errno = 0;
3034  sec = strtoint(cp + 1, &cp, 10);
3035  if (errno == ERANGE)
3036  return DTERR_TZDISP_OVERFLOW;
3037  }
3038  }
3039  /* otherwise, might have run things together... */
3040  else if (*cp == '\0' && strlen(str) > 3)
3041  {
3042  min = hr % 100;
3043  hr = hr / 100;
3044  /* we could, but don't, support a run-together hhmmss format */
3045  }
3046  else
3047  min = 0;
3048 
3049  /* Range-check the values; see notes in datatype/timestamp.h */
3050  if (hr < 0 || hr > MAX_TZDISP_HOUR)
3051  return DTERR_TZDISP_OVERFLOW;
3052  if (min < 0 || min >= MINS_PER_HOUR)
3053  return DTERR_TZDISP_OVERFLOW;
3054  if (sec < 0 || sec >= SECS_PER_MINUTE)
3055  return DTERR_TZDISP_OVERFLOW;
3056 
3057  tz = (hr * MINS_PER_HOUR + min) * SECS_PER_MINUTE + sec;
3058  if (*str == '-')
3059  tz = -tz;
3060 
3061  *tzp = -tz;
3062 
3063  if (*cp != '\0')
3064  return DTERR_BAD_FORMAT;
3065 
3066  return 0;
3067 }
#define MAX_TZDISP_HOUR
Definition: timestamp.h:143
#define MINS_PER_HOUR
Definition: timestamp.h:129
#define SECS_PER_MINUTE
Definition: timestamp.h:128

References DTERR_BAD_FORMAT, DTERR_TZDISP_OVERFLOW, MAX_TZDISP_HOUR, MINS_PER_HOUR, SECS_PER_MINUTE, str, and strtoint().

Referenced by DecodeDateTime(), DecodeTimeOnly(), and parse_sane_timezone().

◆ DecodeTimezoneAbbrev()

int DecodeTimezoneAbbrev ( int  field,
const char *  lowtoken,
int *  ftype,
int *  offset,
pg_tz **  tz,
DateTimeErrorExtra extra 
)

Definition at line 3091 of file datetime.c.

3094 {
3095  const datetkn *tp;
3096 
3097  tp = abbrevcache[field];
3098  /* use strncmp so that we match truncated tokens */
3099  if (tp == NULL || strncmp(lowtoken, tp->token, TOKMAXLEN) != 0)
3100  {
3101  if (zoneabbrevtbl)
3102  tp = datebsearch(lowtoken, zoneabbrevtbl->abbrevs,
3104  else
3105  tp = NULL;
3106  }
3107  if (tp == NULL)
3108  {
3109  *ftype = UNKNOWN_FIELD;
3110  *offset = 0;
3111  *tz = NULL;
3112  }
3113  else
3114  {
3115  abbrevcache[field] = tp;
3116  *ftype = tp->type;
3117  if (tp->type == DYNTZ)
3118  {
3119  *offset = 0;
3120  *tz = FetchDynamicTimeZone(zoneabbrevtbl, tp, extra);
3121  if (*tz == NULL)
3122  return DTERR_BAD_ZONE_ABBREV;
3123  }
3124  else
3125  {
3126  *offset = tp->value;
3127  *tz = NULL;
3128  }
3129  }
3130 
3131  return 0;
3132 }
static const datetkn * abbrevcache[MAXDATEFIELDS]
Definition: datetime.c:262
static TimeZoneAbbrevTable * zoneabbrevtbl
Definition: datetime.c:254
static pg_tz * FetchDynamicTimeZone(TimeZoneAbbrevTable *tbl, const datetkn *tp, DateTimeErrorExtra *extra)
Definition: datetime.c:4970

References abbrevcache, TimeZoneAbbrevTable::abbrevs, datebsearch(), DTERR_BAD_ZONE_ABBREV, DYNTZ, FetchDynamicTimeZone(), TimeZoneAbbrevTable::numabbrevs, datetkn::token, TOKMAXLEN, datetkn::type, UNKNOWN_FIELD, datetkn::value, and zoneabbrevtbl.

Referenced by DecodeDateTime(), DecodeTimeOnly(), and DecodeTimezoneName().

◆ DecodeTimezoneAbbrevPrefix()

int DecodeTimezoneAbbrevPrefix ( const char *  str,
int *  offset,
pg_tz **  tz 
)

Definition at line 3273 of file datetime.c.

3274 {
3275  char lowtoken[TOKMAXLEN + 1];
3276  int len;
3277 
3278  *offset = 0; /* avoid uninitialized vars on failure */
3279  *tz = NULL;
3280 
3281  if (!zoneabbrevtbl)
3282  return -1; /* no abbrevs known, so fail immediately */
3283 
3284  /* Downcase as much of the string as we could need */
3285  for (len = 0; len < TOKMAXLEN; len++)
3286  {
3287  if (*str == '\0' || !isalpha((unsigned char) *str))
3288  break;
3289  lowtoken[len] = pg_tolower((unsigned char) *str++);
3290  }
3291  lowtoken[len] = '\0';
3292 
3293  /*
3294  * We could avoid doing repeated binary searches if we cared to duplicate
3295  * datebsearch here, but it's not clear that such an optimization would be
3296  * worth the trouble. In common cases there's probably not anything after
3297  * the zone abbrev anyway. So just search with successively truncated
3298  * strings.
3299  */
3300  while (len > 0)
3301  {
3302  const datetkn *tp = datebsearch(lowtoken, zoneabbrevtbl->abbrevs,
3304 
3305  if (tp != NULL)
3306  {
3307  if (tp->type == DYNTZ)
3308  {
3309  DateTimeErrorExtra extra;
3311  &extra);
3312 
3313  if (tzp != NULL)
3314  {
3315  /* Caller must resolve the abbrev's current meaning */
3316  *tz = tzp;
3317  return len;
3318  }
3319  }
3320  else
3321  {
3322  /* Fixed-offset zone abbrev, so it's easy */
3323  *offset = tp->value;
3324  return len;
3325  }
3326  }
3327  lowtoken[--len] = '\0';
3328  }
3329 
3330  /* Did not find a match */
3331  return -1;
3332 }
const void size_t len
unsigned char pg_tolower(unsigned char ch)
Definition: pgstrcasecmp.c:122

References TimeZoneAbbrevTable::abbrevs, datebsearch(), DYNTZ, FetchDynamicTimeZone(), len, TimeZoneAbbrevTable::numabbrevs, pg_tolower(), str, TOKMAXLEN, datetkn::type, datetkn::value, and zoneabbrevtbl.

Referenced by DCH_from_char().

◆ DecodeTimezoneName()

int DecodeTimezoneName ( const char *  tzname,
int *  offset,
pg_tz **  tz 
)

Definition at line 3190 of file datetime.c.

3191 {
3192  char *lowzone;
3193  int dterr,
3194  type;
3195  DateTimeErrorExtra extra;
3196 
3197  /*
3198  * First we look in the timezone abbreviation table (to handle cases like
3199  * "EST"), and if that fails, we look in the timezone database (to handle
3200  * cases like "America/New_York"). This matches the order in which
3201  * timestamp input checks the cases; it's important because the timezone
3202  * database unwisely uses a few zone names that are identical to offset
3203  * abbreviations.
3204  */
3205 
3206  /* DecodeTimezoneAbbrev requires lowercase input */
3207  lowzone = downcase_truncate_identifier(tzname,
3208  strlen(tzname),
3209  false);
3210 
3211  dterr = DecodeTimezoneAbbrev(0, lowzone, &type, offset, tz, &extra);
3212  if (dterr)
3213  DateTimeParseError(dterr, &extra, NULL, NULL, NULL);
3214 
3215  if (type == TZ || type == DTZ)
3216  {
3217  /* fixed-offset abbreviation, return the offset */
3218  return TZNAME_FIXED_OFFSET;
3219  }
3220  else if (type == DYNTZ)
3221  {
3222  /* dynamic-offset abbreviation, return its referenced timezone */
3223  return TZNAME_DYNTZ;
3224  }
3225  else
3226  {
3227  /* try it as a full zone name */
3228  *tz = pg_tzset(tzname);
3229  if (*tz == NULL)
3230  ereport(ERROR,
3231  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
3232  errmsg("time zone \"%s\" not recognized", tzname)));
3233  return TZNAME_ZONE;
3234  }
3235 }
void DateTimeParseError(int dterr, DateTimeErrorExtra *extra, const char *str, const char *datatype, Node *escontext)
Definition: datetime.c:4092
#define ereport(elevel,...)
Definition: elog.h:149
#define TZNAME_ZONE
Definition: datetime.h:301
#define TZNAME_FIXED_OFFSET
Definition: datetime.h:299
#define TZNAME_DYNTZ
Definition: datetime.h:300
char * downcase_truncate_identifier(const char *ident, int len, bool warn)
Definition: scansup.c:37

References DateTimeParseError(), DecodeTimezoneAbbrev(), downcase_truncate_identifier(), DTZ, DYNTZ, ereport, errcode(), errmsg(), ERROR, pg_tzset(), type, TZ, TZNAME_DYNTZ, TZNAME_FIXED_OFFSET, and TZNAME_ZONE.

Referenced by DecodeTimezoneNameToTz(), parse_sane_timezone(), timestamp_zone(), timestamptz_zone(), and timetz_zone().

◆ DecodeTimezoneNameToTz()

pg_tz* DecodeTimezoneNameToTz ( const char *  tzname)

Definition at line 3245 of file datetime.c.

3246 {
3247  pg_tz *result;
3248  int offset;
3249 
3250  if (DecodeTimezoneName(tzname, &offset, &result) == TZNAME_FIXED_OFFSET)
3251  {
3252  /* fixed-offset abbreviation, get a pg_tz descriptor for that */
3253  result = pg_tzset_offset(-offset); /* flip to POSIX sign convention */
3254  }
3255  return result;
3256 }
int DecodeTimezoneName(const char *tzname, int *offset, pg_tz **tz)
Definition: datetime.c:3190
pg_tz * pg_tzset_offset(long gmtoffset)
Definition: pgtz.c:320

References DecodeTimezoneName(), pg_tzset_offset(), and TZNAME_FIXED_OFFSET.

Referenced by lookup_timezone().

◆ DecodeUnits()

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

Definition at line 4047 of file datetime.c.

4048 {
4049  int type;
4050  const datetkn *tp;
4051 
4052  tp = deltacache[field];
4053  /* use strncmp so that we match truncated tokens */
4054  if (tp == NULL || strncmp(lowtoken, tp->token, TOKMAXLEN) != 0)
4055  {
4056  tp = datebsearch(lowtoken, deltatktbl, szdeltatktbl);
4057  }
4058  if (tp == NULL)
4059  {
4060  type = UNKNOWN_FIELD;
4061  *val = 0;
4062  }
4063  else
4064  {
4065  deltacache[field] = tp;
4066  type = tp->type;
4067  *val = tp->value;
4068  }
4069 
4070  return type;
4071 } /* DecodeUnits() */
static const datetkn * deltacache[MAXDATEFIELDS]
Definition: datetime.c:260

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

Referenced by DecodeInterval(), extract_date(), interval_part_common(), interval_trunc(), time_part_common(), timestamp_part_common(), timestamp_trunc(), timestamptz_part_common(), timestamptz_trunc_internal(), and timetz_part_common().

◆ DetermineTimeZoneAbbrevOffset()

int DetermineTimeZoneAbbrevOffset ( struct pg_tm tm,
const char *  abbr,
pg_tz tzp 
)

Definition at line 1746 of file datetime.c.

1747 {
1748  pg_time_t t;
1749  int zone_offset;
1750  int abbr_offset;
1751  int abbr_isdst;
1752 
1753  /*
1754  * Compute the UTC time we want to probe at. (In event of overflow, we'll
1755  * probe at the epoch, which is a bit random but probably doesn't matter.)
1756  */
1757  zone_offset = DetermineTimeZoneOffsetInternal(tm, tzp, &t);
1758 
1759  /*
1760  * Try to match the abbreviation to something in the zone definition.
1761  */
1762  if (DetermineTimeZoneAbbrevOffsetInternal(t, abbr, tzp,
1763  &abbr_offset, &abbr_isdst))
1764  {
1765  /* Success, so use the abbrev-specific answers. */
1766  tm->tm_isdst = abbr_isdst;
1767  return abbr_offset;
1768  }
1769 
1770  /*
1771  * No match, so use the answers we already got from
1772  * DetermineTimeZoneOffsetInternal.
1773  */
1774  return zone_offset;
1775 }
static int DetermineTimeZoneOffsetInternal(struct pg_tm *tm, pg_tz *tzp, pg_time_t *tp)
Definition: datetime.c:1607
static bool DetermineTimeZoneAbbrevOffsetInternal(pg_time_t t, const char *abbr, pg_tz *tzp, int *offset, int *isdst)
Definition: datetime.c:1821
int64 pg_time_t
Definition: pgtime.h:23

References DetermineTimeZoneAbbrevOffsetInternal(), DetermineTimeZoneOffsetInternal(), tm, and pg_tm::tm_isdst.

Referenced by DecodeDateTime(), DecodeTimeOnly(), do_to_timestamp(), parse_sane_timezone(), and timestamp_zone().

◆ DetermineTimeZoneAbbrevOffsetTS()

int DetermineTimeZoneAbbrevOffsetTS ( TimestampTz  ts,
const char *  abbr,
pg_tz tzp,
int *  isdst 
)

Definition at line 1784 of file datetime.c.

1786 {
1788  int zone_offset;
1789  int abbr_offset;
1790  int tz;
1791  struct pg_tm tm;
1792  fsec_t fsec;
1793 
1794  /*
1795  * If the abbrev matches anything in the zone data, this is pretty easy.
1796  */
1797  if (DetermineTimeZoneAbbrevOffsetInternal(t, abbr, tzp,
1798  &abbr_offset, isdst))
1799  return abbr_offset;
1800 
1801  /*
1802  * Else, break down the timestamp so we can use DetermineTimeZoneOffset.
1803  */
1804  if (timestamp2tm(ts, &tz, &tm, &fsec, NULL, tzp) != 0)
1805  ereport(ERROR,
1806  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1807  errmsg("timestamp out of range")));
1808 
1809  zone_offset = DetermineTimeZoneOffset(&tm, tzp);
1810  *isdst = tm.tm_isdst;
1811  return zone_offset;
1812 }
int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm *tm, fsec_t *fsec, const char **tzn, pg_tz *attimezone)
Definition: timestamp.c:1891
pg_time_t timestamptz_to_time_t(TimestampTz t)
Definition: timestamp.c:1823
int32 fsec_t
Definition: timestamp.h:41

References DetermineTimeZoneAbbrevOffsetInternal(), DetermineTimeZoneOffset(), ereport, errcode(), errmsg(), ERROR, timestamp2tm(), timestamptz_to_time_t(), tm, and pg_tm::tm_isdst.

Referenced by pg_timezone_abbrevs(), timestamptz_zone(), and timetz_zone().

◆ DetermineTimeZoneOffset()

◆ EncodeDateOnly()

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

Definition at line 4227 of file datetime.c.

4228 {
4229  Assert(tm->tm_mon >= 1 && tm->tm_mon <= MONTHS_PER_YEAR);
4230 
4231  switch (style)
4232  {
4233  case USE_ISO_DATES:
4234  case USE_XSD_DATES:
4235  /* compatible with ISO date formats */
4237  (tm->tm_year > 0) ? tm->tm_year : -(tm->tm_year - 1), 4);
4238  *str++ = '-';
4240  *str++ = '-';
4242  break;
4243 
4244  case USE_SQL_DATES:
4245  /* compatible with Oracle/Ingres date formats */
4246  if (DateOrder == DATEORDER_DMY)
4247  {
4249  *str++ = '/';
4251  }
4252  else
4253  {
4255  *str++ = '/';
4257  }
4258  *str++ = '/';
4260  (tm->tm_year > 0) ? tm->tm_year : -(tm->tm_year - 1), 4);
4261  break;
4262 
4263  case USE_GERMAN_DATES:
4264  /* German-style date format */
4266  *str++ = '.';
4268  *str++ = '.';
4270  (tm->tm_year > 0) ? tm->tm_year : -(tm->tm_year - 1), 4);
4271  break;
4272 
4273  case USE_POSTGRES_DATES:
4274  default:
4275  /* traditional date-only style for Postgres */
4276  if (DateOrder == DATEORDER_DMY)
4277  {
4279  *str++ = '-';
4281  }
4282  else
4283  {
4285  *str++ = '-';
4287  }
4288  *str++ = '-';
4290  (tm->tm_year > 0) ? tm->tm_year : -(tm->tm_year - 1), 4);
4291  break;
4292  }
4293 
4294  if (tm->tm_year <= 0)
4295  {
4296  memcpy(str, " BC", 3); /* Don't copy NUL */
4297  str += 3;
4298  }
4299  *str = '\0';
4300 }
int DateOrder
Definition: globals.c:125
#define USE_SQL_DATES
Definition: miscadmin.h:237
#define USE_POSTGRES_DATES
Definition: miscadmin.h:235
#define USE_ISO_DATES
Definition: miscadmin.h:236
#define DATEORDER_DMY
Definition: miscadmin.h:243
#define USE_XSD_DATES
Definition: miscadmin.h:239
#define USE_GERMAN_DATES
Definition: miscadmin.h:238
char * pg_ultostr_zeropad(char *str, uint32 value, int32 minwidth)
Definition: numutils.c:1267

References Assert, DateOrder, DATEORDER_DMY, MONTHS_PER_YEAR, pg_ultostr_zeropad(), str, tm, pg_tm::tm_mday, pg_tm::tm_mon, pg_tm::tm_year, USE_GERMAN_DATES, USE_ISO_DATES, USE_POSTGRES_DATES, USE_SQL_DATES, and USE_XSD_DATES.

Referenced by date_out(), JsonEncodeDateTime(), map_sql_value_to_xml_value(), and PGTYPESdate_to_asc().

◆ EncodeDateTime()

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

Definition at line 4342 of file datetime.c.

4343 {
4344  int day;
4345 
4346  Assert(tm->tm_mon >= 1 && tm->tm_mon <= MONTHS_PER_YEAR);
4347 
4348  /*
4349  * Negative tm_isdst means we have no valid time zone translation.
4350  */
4351  if (tm->tm_isdst < 0)
4352  print_tz = false;
4353 
4354  switch (style)
4355  {
4356  case USE_ISO_DATES:
4357  case USE_XSD_DATES:
4358  /* Compatible with ISO-8601 date formats */
4360  (tm->tm_year > 0) ? tm->tm_year : -(tm->tm_year - 1), 4);
4361  *str++ = '-';
4363  *str++ = '-';
4365  *str++ = (style == USE_ISO_DATES) ? ' ' : 'T';
4367  *str++ = ':';
4369  *str++ = ':';
4370  str = AppendTimestampSeconds(str, tm, fsec);
4371  if (print_tz)
4372  str = EncodeTimezone(str, tz, style);
4373  break;
4374 
4375  case USE_SQL_DATES:
4376  /* Compatible with Oracle/Ingres date formats */
4377  if (DateOrder == DATEORDER_DMY)
4378  {
4380  *str++ = '/';
4382  }
4383  else
4384  {
4386  *str++ = '/';
4388  }
4389  *str++ = '/';
4391  (tm->tm_year > 0) ? tm->tm_year : -(tm->tm_year - 1), 4);
4392  *str++ = ' ';
4394  *str++ = ':';
4396  *str++ = ':';
4397  str = AppendTimestampSeconds(str, tm, fsec);
4398 
4399  /*
4400  * Note: the uses of %.*s in this function would be risky if the
4401  * timezone names ever contain non-ASCII characters, since we are
4402  * not being careful to do encoding-aware clipping. However, all
4403  * TZ abbreviations in the IANA database are plain ASCII.
4404  */
4405  if (print_tz)
4406  {
4407  if (tzn)
4408  {
4409  sprintf(str, " %.*s", MAXTZLEN, tzn);
4410  str += strlen(str);
4411  }
4412  else
4413  str = EncodeTimezone(str, tz, style);
4414  }
4415  break;
4416 
4417  case USE_GERMAN_DATES:
4418  /* German variant on European style */
4420  *str++ = '.';
4422  *str++ = '.';
4424  (tm->tm_year > 0) ? tm->tm_year : -(tm->tm_year - 1), 4);
4425  *str++ = ' ';
4427  *str++ = ':';
4429  *str++ = ':';
4430  str = AppendTimestampSeconds(str, tm, fsec);
4431 
4432  if (print_tz)
4433  {
4434  if (tzn)
4435  {
4436  sprintf(str, " %.*s", MAXTZLEN, tzn);
4437  str += strlen(str);
4438  }
4439  else
4440  str = EncodeTimezone(str, tz, style);
4441  }
4442  break;
4443 
4444  case USE_POSTGRES_DATES:
4445  default:
4446  /* Backward-compatible with traditional Postgres abstime dates */
4447  day = date2j(tm->tm_year, tm->tm_mon, tm->tm_mday);
4448  tm->tm_wday = j2day(day);
4449  memcpy(str, days[tm->tm_wday], 3);
4450  str += 3;
4451  *str++ = ' ';
4452  if (DateOrder == DATEORDER_DMY)
4453  {
4455  *str++ = ' ';
4456  memcpy(str, months[tm->tm_mon - 1], 3);
4457  str += 3;
4458  }
4459  else
4460  {
4461  memcpy(str, months[tm->tm_mon - 1], 3);
4462  str += 3;
4463  *str++ = ' ';
4465  }
4466  *str++ = ' ';
4468  *str++ = ':';
4470  *str++ = ':';
4471  str = AppendTimestampSeconds(str, tm, fsec);
4472  *str++ = ' ';
4474  (tm->tm_year > 0) ? tm->tm_year : -(tm->tm_year - 1), 4);
4475 
4476  if (print_tz)
4477  {
4478  if (tzn)
4479  {
4480  sprintf(str, " %.*s", MAXTZLEN, tzn);
4481  str += strlen(str);
4482  }
4483  else
4484  {
4485  /*
4486  * We have a time zone, but no string version. Use the
4487  * numeric form, but be sure to include a leading space to
4488  * avoid formatting something which would be rejected by
4489  * the date/time parser later. - thomas 2001-10-19
4490  */
4491  *str++ = ' ';
4492  str = EncodeTimezone(str, tz, style);
4493  }
4494  }
4495  break;
4496  }
4497 
4498  if (tm->tm_year <= 0)
4499  {
4500  memcpy(str, " BC", 3); /* Don't copy NUL */
4501  str += 3;
4502  }
4503  *str = '\0';
4504 }
static char * EncodeTimezone(char *str, int tz, int style)
Definition: datetime.c:4189
int j2day(int date)
Definition: datetime.c:344
const char *const months[]
Definition: datetime.c:81
static char * AppendTimestampSeconds(char *cp, struct pg_tm *tm, fsec_t fsec)
Definition: datetime.c:511
const char *const days[]
Definition: datetime.c:84
#define MAXTZLEN
Definition: miscadmin.h:263
#define sprintf
Definition: port.h:240

References AppendTimestampSeconds(), Assert, date2j(), DateOrder, DATEORDER_DMY, days, EncodeTimezone(), j2day(), MAXTZLEN, months, MONTHS_PER_YEAR, pg_ultostr_zeropad(), sprintf, str, tm, pg_tm::tm_hour, pg_tm::tm_isdst, pg_tm::tm_mday, pg_tm::tm_min, pg_tm::tm_mon, pg_tm::tm_wday, pg_tm::tm_year, USE_GERMAN_DATES, USE_ISO_DATES, USE_POSTGRES_DATES, USE_SQL_DATES, and USE_XSD_DATES.

Referenced by JsonEncodeDateTime(), map_sql_value_to_xml_value(), PGTYPEStimestamp_to_asc(), timestamp_out(), timestamptz_out(), and timestamptz_to_str().

◆ EncodeInterval()

void EncodeInterval ( struct pg_itm itm,
int  style,
char *  str 
)

Definition at line 4585 of file datetime.c.

4586 {
4587  char *cp = str;
4588  int year = itm->tm_year;
4589  int mon = itm->tm_mon;
4590  int64 mday = itm->tm_mday; /* tm_mday could be INT_MIN */
4591  int64 hour = itm->tm_hour;
4592  int min = itm->tm_min;
4593  int sec = itm->tm_sec;
4594  int fsec = itm->tm_usec;
4595  bool is_before = false;
4596  bool is_zero = true;
4597 
4598  /*
4599  * The sign of year and month are guaranteed to match, since they are
4600  * stored internally as "month". But we'll need to check for is_before and
4601  * is_zero when determining the signs of day and hour/minute/seconds
4602  * fields.
4603  */
4604  switch (style)
4605  {
4606  /* SQL Standard interval format */
4607  case INTSTYLE_SQL_STANDARD:
4608  {
4609  bool has_negative = year < 0 || mon < 0 ||
4610  mday < 0 || hour < 0 ||
4611  min < 0 || sec < 0 || fsec < 0;
4612  bool has_positive = year > 0 || mon > 0 ||
4613  mday > 0 || hour > 0 ||
4614  min > 0 || sec > 0 || fsec > 0;
4615  bool has_year_month = year != 0 || mon != 0;
4616  bool has_day_time = mday != 0 || hour != 0 ||
4617  min != 0 || sec != 0 || fsec != 0;
4618  bool has_day = mday != 0;
4619  bool sql_standard_value = !(has_negative && has_positive) &&
4620  !(has_year_month && has_day_time);
4621 
4622  /*
4623  * SQL Standard wants only 1 "<sign>" preceding the whole
4624  * interval ... but can't do that if mixed signs.
4625  */
4626  if (has_negative && sql_standard_value)
4627  {
4628  *cp++ = '-';
4629  year = -year;
4630  mon = -mon;
4631  mday = -mday;
4632  hour = -hour;
4633  min = -min;
4634  sec = -sec;
4635  fsec = -fsec;
4636  }
4637 
4638  if (!has_negative && !has_positive)
4639  {
4640  sprintf(cp, "0");
4641  }
4642  else if (!sql_standard_value)
4643  {
4644  /*
4645  * For non sql-standard interval values, force outputting
4646  * the signs to avoid ambiguities with intervals with
4647  * mixed sign components.
4648  */
4649  char year_sign = (year < 0 || mon < 0) ? '-' : '+';
4650  char day_sign = (mday < 0) ? '-' : '+';
4651  char sec_sign = (hour < 0 || min < 0 ||
4652  sec < 0 || fsec < 0) ? '-' : '+';
4653 
4654  sprintf(cp, "%c%d-%d %c%lld %c%lld:%02d:",
4655  year_sign, abs(year), abs(mon),
4656  day_sign, (long long) i64abs(mday),
4657  sec_sign, (long long) i64abs(hour), abs(min));
4658  cp += strlen(cp);
4659  cp = AppendSeconds(cp, sec, fsec, MAX_INTERVAL_PRECISION, true);
4660  *cp = '\0';
4661  }
4662  else if (has_year_month)
4663  {
4664  sprintf(cp, "%d-%d", year, mon);
4665  }
4666  else if (has_day)
4667  {
4668  sprintf(cp, "%lld %lld:%02d:",
4669  (long long) mday, (long long) hour, min);
4670  cp += strlen(cp);
4671  cp = AppendSeconds(cp, sec, fsec, MAX_INTERVAL_PRECISION, true);
4672  *cp = '\0';
4673  }
4674  else
4675  {
4676  sprintf(cp, "%lld:%02d:", (long long) hour, min);
4677  cp += strlen(cp);
4678  cp = AppendSeconds(cp, sec, fsec, MAX_INTERVAL_PRECISION, true);
4679  *cp = '\0';
4680  }
4681  }
4682  break;
4683 
4684  /* ISO 8601 "time-intervals by duration only" */
4685  case INTSTYLE_ISO_8601:
4686  /* special-case zero to avoid printing nothing */
4687  if (year == 0 && mon == 0 && mday == 0 &&
4688  hour == 0 && min == 0 && sec == 0 && fsec == 0)
4689  {
4690  sprintf(cp, "PT0S");
4691  break;
4692  }
4693  *cp++ = 'P';
4694  cp = AddISO8601IntPart(cp, year, 'Y');
4695  cp = AddISO8601IntPart(cp, mon, 'M');
4696  cp = AddISO8601IntPart(cp, mday, 'D');
4697  if (hour != 0 || min != 0 || sec != 0 || fsec != 0)
4698  *cp++ = 'T';
4699  cp = AddISO8601IntPart(cp, hour, 'H');
4700  cp = AddISO8601IntPart(cp, min, 'M');
4701  if (sec != 0 || fsec != 0)
4702  {
4703  if (sec < 0 || fsec < 0)
4704  *cp++ = '-';
4705  cp = AppendSeconds(cp, sec, fsec, MAX_INTERVAL_PRECISION, false);
4706  *cp++ = 'S';
4707  *cp++ = '\0';
4708  }
4709  break;
4710 
4711  /* Compatible with postgresql < 8.4 when DateStyle = 'iso' */
4712  case INTSTYLE_POSTGRES:
4713  cp = AddPostgresIntPart(cp, year, "year", &is_zero, &is_before);
4714 
4715  /*
4716  * Ideally we should spell out "month" like we do for "year" and
4717  * "day". However, for backward compatibility, we can't easily
4718  * fix this. bjm 2011-05-24
4719  */
4720  cp = AddPostgresIntPart(cp, mon, "mon", &is_zero, &is_before);
4721  cp = AddPostgresIntPart(cp, mday, "day", &is_zero, &is_before);
4722  if (is_zero || hour != 0 || min != 0 || sec != 0 || fsec != 0)
4723  {
4724  bool minus = (hour < 0 || min < 0 || sec < 0 || fsec < 0);
4725 
4726  sprintf(cp, "%s%s%02lld:%02d:",
4727  is_zero ? "" : " ",
4728  (minus ? "-" : (is_before ? "+" : "")),
4729  (long long) i64abs(hour), abs(min));
4730  cp += strlen(cp);
4731  cp = AppendSeconds(cp, sec, fsec, MAX_INTERVAL_PRECISION, true);
4732  *cp = '\0';
4733  }
4734  break;
4735 
4736  /* Compatible with postgresql < 8.4 when DateStyle != 'iso' */
4738  default:
4739  strcpy(cp, "@");
4740  cp++;
4741  cp = AddVerboseIntPart(cp, year, "year", &is_zero, &is_before);
4742  cp = AddVerboseIntPart(cp, mon, "mon", &is_zero, &is_before);
4743  cp = AddVerboseIntPart(cp, mday, "day", &is_zero, &is_before);
4744  cp = AddVerboseIntPart(cp, hour, "hour", &is_zero, &is_before);
4745  cp = AddVerboseIntPart(cp, min, "min", &is_zero, &is_before);
4746  if (sec != 0 || fsec != 0)
4747  {
4748  *cp++ = ' ';
4749  if (sec < 0 || (sec == 0 && fsec < 0))
4750  {
4751  if (is_zero)
4752  is_before = true;
4753  else if (!is_before)
4754  *cp++ = '-';
4755  }
4756  else if (is_before)
4757  *cp++ = '-';
4758  cp = AppendSeconds(cp, sec, fsec, MAX_INTERVAL_PRECISION, false);
4759  /* We output "ago", not negatives, so use abs(). */
4760  sprintf(cp, " sec%s",
4761  (abs(sec) != 1 || fsec != 0) ? "s" : "");
4762  is_zero = false;
4763  }
4764  /* identically zero? then put in a unitless zero... */
4765  if (is_zero)
4766  strcat(cp, " 0");
4767  if (is_before)
4768  strcat(cp, " ago");
4769  break;
4770  }
4771 }
static char * AddVerboseIntPart(char *cp, int64 value, const char *units, bool *is_zero, bool *is_before)
Definition: datetime.c:4546
static char * AddPostgresIntPart(char *cp, int64 value, const char *units, bool *is_zero, bool *is_before)
Definition: datetime.c:4523
static char * AddISO8601IntPart(char *cp, int64 value, char units)
Definition: datetime.c:4513
static char * AppendSeconds(char *cp, int sec, fsec_t fsec, int precision, bool fillzeros)
Definition: datetime.c:448
#define MAX_INTERVAL_PRECISION
Definition: timestamp.h:93
#define INTSTYLE_POSTGRES_VERBOSE
Definition: miscadmin.h:257
#define INTSTYLE_ISO_8601
Definition: miscadmin.h:259
#define INTSTYLE_POSTGRES
Definition: miscadmin.h:256
int64 tm_hour
Definition: timestamp.h:70
int tm_year
Definition: timestamp.h:73
int tm_mon
Definition: timestamp.h:72
int tm_mday
Definition: timestamp.h:71
int tm_sec
Definition: timestamp.h:68
int tm_min
Definition: timestamp.h:69
int tm_usec
Definition: timestamp.h:67

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

Referenced by interval_out().

◆ EncodeSpecialTimestamp()

void EncodeSpecialTimestamp ( Timestamp  dt,
char *  str 
)

Definition at line 1586 of file timestamp.c.

1587 {
1588  if (TIMESTAMP_IS_NOBEGIN(dt))
1589  strcpy(str, EARLY);
1590  else if (TIMESTAMP_IS_NOEND(dt))
1591  strcpy(str, LATE);
1592  else /* shouldn't happen */
1593  elog(ERROR, "invalid argument for EncodeSpecialTimestamp");
1594 }
#define TIMESTAMP_IS_NOEND(j)
Definition: timestamp.h:167
#define TIMESTAMP_IS_NOBEGIN(j)
Definition: timestamp.h:162
#define EARLY
Definition: datetime.h:39
#define LATE
Definition: datetime.h:40

References EARLY, elog, ERROR, LATE, str, TIMESTAMP_IS_NOBEGIN, and TIMESTAMP_IS_NOEND.

Referenced by JsonEncodeDateTime(), PGTYPEStimestamp_to_asc(), timestamp_out(), timestamptz_out(), and timestamptz_to_str().

◆ EncodeTimeOnly()

void EncodeTimeOnly ( struct pg_tm tm,
fsec_t  fsec,
bool  print_tz,
int  tz,
int  style,
char *  str 
)

Definition at line 4312 of file datetime.c.

4313 {
4315  *str++ = ':';
4317  *str++ = ':';
4318  str = AppendSeconds(str, tm->tm_sec, fsec, MAX_TIME_PRECISION, true);
4319  if (print_tz)
4320  str = EncodeTimezone(str, tz, style);
4321  *str = '\0';
4322 }
#define MAX_TIME_PRECISION
Definition: date.h:45

References AppendSeconds(), EncodeTimezone(), MAX_TIME_PRECISION, pg_ultostr_zeropad(), str, tm, pg_tm::tm_hour, pg_tm::tm_min, and pg_tm::tm_sec.

Referenced by JsonEncodeDateTime(), time_out(), and timetz_out().

◆ GetCurrentDateTime()

void GetCurrentDateTime ( struct pg_tm tm)

Definition at line 366 of file datetime.c.

367 {
368  fsec_t fsec;
369 
370  GetCurrentTimeUsec(tm, &fsec, NULL);
371 }

References GetCurrentTimeUsec(), and tm.

Referenced by DecodeDateTime(), DecodeTimeOnly(), GetSQLCurrentDate(), PGTYPESdate_today(), PGTYPEStimestamp_current(), and time_timetz().

◆ GetCurrentTimeUsec()

void GetCurrentTimeUsec ( struct pg_tm tm,
fsec_t fsec,
int *  tzp 
)

Definition at line 387 of file datetime.c.

388 {
390 
391  /*
392  * The cache key must include both current time and current timezone. By
393  * representing the timezone by just a pointer, we're assuming that
394  * distinct timezone settings could never have the same pointer value.
395  * This is true by virtue of the hashtable used inside pg_tzset();
396  * however, it might need another look if we ever allow entries in that
397  * hash to be recycled.
398  */
399  static TimestampTz cache_ts = 0;
400  static pg_tz *cache_timezone = NULL;
401  static struct pg_tm cache_tm;
402  static fsec_t cache_fsec;
403  static int cache_tz;
404 
405  if (cur_ts != cache_ts || session_timezone != cache_timezone)
406  {
407  /*
408  * Make sure cache is marked invalid in case of error after partial
409  * update within timestamp2tm.
410  */
411  cache_timezone = NULL;
412 
413  /*
414  * Perform the computation, storing results into cache. We do not
415  * really expect any error here, since current time surely ought to be
416  * within range, but check just for sanity's sake.
417  */
418  if (timestamp2tm(cur_ts, &cache_tz, &cache_tm, &cache_fsec,
419  NULL, session_timezone) != 0)
420  ereport(ERROR,
421  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
422  errmsg("timestamp out of range")));
423 
424  /* OK, so mark the cache valid. */
425  cache_ts = cur_ts;
426  cache_timezone = session_timezone;
427  }
428 
429  *tm = cache_tm;
430  *fsec = cache_fsec;
431  if (tzp != NULL)
432  *tzp = cache_tz;
433 }
int64 TimestampTz
Definition: timestamp.h:39
TimestampTz GetCurrentTransactionStartTimestamp(void)
Definition: xact.c:869

References ereport, errcode(), errmsg(), ERROR, GetCurrentTransactionStartTimestamp(), session_timezone, timestamp2tm(), and tm.

Referenced by DecodeDateTime(), DecodeTimeOnly(), GetCurrentDateTime(), GetSQLCurrentTime(), and GetSQLLocalTime().

◆ InstallTimeZoneAbbrevs()

void InstallTimeZoneAbbrevs ( TimeZoneAbbrevTable tbl)

Definition at line 4957 of file datetime.c.

4958 {
4959  zoneabbrevtbl = tbl;
4960  /* reset abbrevcache, which may contain pointers into old table */
4961  memset(abbrevcache, 0, sizeof(abbrevcache));
4962 }

References abbrevcache, and zoneabbrevtbl.

Referenced by assign_timezone_abbreviations().

◆ j2date()

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

Definition at line 311 of file datetime.c.

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

◆ j2day()

int j2day ( int  date)

Definition at line 344 of file datetime.c.

345 {
346  date += 1;
347  date %= 7;
348  /* Cope if division truncates towards zero, as it probably does */
349  if (date < 0)
350  date += 7;
351 
352  return date;
353 } /* j2day() */
long date
Definition: pgtypes_date.h:9

Referenced by date2isoweek(), date2isoyear(), EncodeDateTime(), extract_date(), isoweek2j(), timestamp_part_common(), and timestamptz_part_common().

◆ ParseDateTime()

int ParseDateTime ( const char *  timestr,
char *  workbuf,
size_t  buflen,
char **  field,
int *  ftype,
int  maxfields,
int *  numfields 
)

Definition at line 754 of file datetime.c.

756 {
757  int nf = 0;
758  const char *cp = timestr;
759  char *bufp = workbuf;
760  const char *bufend = workbuf + buflen;
761 
762  /*
763  * Set the character pointed-to by "bufptr" to "newchar", and increment
764  * "bufptr". "end" gives the end of the buffer -- we return an error if
765  * there is no space left to append a character to the buffer. Note that
766  * "bufptr" is evaluated twice.
767  */
768 #define APPEND_CHAR(bufptr, end, newchar) \
769  do \
770  { \
771  if (((bufptr) + 1) >= (end)) \
772  return DTERR_BAD_FORMAT; \
773  *(bufptr)++ = newchar; \
774  } while (0)
775 
776  /* outer loop through fields */
777  while (*cp != '\0')
778  {
779  /* Ignore spaces between fields */
780  if (isspace((unsigned char) *cp))
781  {
782  cp++;
783  continue;
784  }
785 
786  /* Record start of current field */
787  if (nf >= maxfields)
788  return DTERR_BAD_FORMAT;
789  field[nf] = bufp;
790 
791  /* leading digit? then date or time */
792  if (isdigit((unsigned char) *cp))
793  {
794  APPEND_CHAR(bufp, bufend, *cp++);
795  while (isdigit((unsigned char) *cp))
796  APPEND_CHAR(bufp, bufend, *cp++);
797 
798  /* time field? */
799  if (*cp == ':')
800  {
801  ftype[nf] = DTK_TIME;
802  APPEND_CHAR(bufp, bufend, *cp++);
803  while (isdigit((unsigned char) *cp) ||
804  (*cp == ':') || (*cp == '.'))
805  APPEND_CHAR(bufp, bufend, *cp++);
806  }
807  /* date field? allow embedded text month */
808  else if (*cp == '-' || *cp == '/' || *cp == '.')
809  {
810  /* save delimiting character to use later */
811  char delim = *cp;
812 
813  APPEND_CHAR(bufp, bufend, *cp++);
814  /* second field is all digits? then no embedded text month */
815  if (isdigit((unsigned char) *cp))
816  {
817  ftype[nf] = ((delim == '.') ? DTK_NUMBER : DTK_DATE);
818  while (isdigit((unsigned char) *cp))
819  APPEND_CHAR(bufp, bufend, *cp++);
820 
821  /*
822  * insist that the delimiters match to get a three-field
823  * date.
824  */
825  if (*cp == delim)
826  {
827  ftype[nf] = DTK_DATE;
828  APPEND_CHAR(bufp, bufend, *cp++);
829  while (isdigit((unsigned char) *cp) || *cp == delim)
830  APPEND_CHAR(bufp, bufend, *cp++);
831  }
832  }
833  else
834  {
835  ftype[nf] = DTK_DATE;
836  while (isalnum((unsigned char) *cp) || *cp == delim)
837  APPEND_CHAR(bufp, bufend, pg_tolower((unsigned char) *cp++));
838  }
839  }
840 
841  /*
842  * otherwise, number only and will determine year, month, day, or
843  * concatenated fields later...
844  */
845  else
846  ftype[nf] = DTK_NUMBER;
847  }
848  /* Leading decimal point? Then fractional seconds... */
849  else if (*cp == '.')
850  {
851  APPEND_CHAR(bufp, bufend, *cp++);
852  while (isdigit((unsigned char) *cp))
853  APPEND_CHAR(bufp, bufend, *cp++);
854 
855  ftype[nf] = DTK_NUMBER;
856  }
857 
858  /*
859  * text? then date string, month, day of week, special, or timezone
860  */
861  else if (isalpha((unsigned char) *cp))
862  {
863  bool is_date;
864 
865  ftype[nf] = DTK_STRING;
866  APPEND_CHAR(bufp, bufend, pg_tolower((unsigned char) *cp++));
867  while (isalpha((unsigned char) *cp))
868  APPEND_CHAR(bufp, bufend, pg_tolower((unsigned char) *cp++));
869 
870  /*
871  * Dates can have embedded '-', '/', or '.' separators. It could
872  * also be a timezone name containing embedded '/', '+', '-', '_',
873  * or ':' (but '_' or ':' can't be the first punctuation). If the
874  * next character is a digit or '+', we need to check whether what
875  * we have so far is a recognized non-timezone keyword --- if so,
876  * don't believe that this is the start of a timezone.
877  */
878  is_date = false;
879  if (*cp == '-' || *cp == '/' || *cp == '.')
880  is_date = true;
881  else if (*cp == '+' || isdigit((unsigned char) *cp))
882  {
883  *bufp = '\0'; /* null-terminate current field value */
884  /* we need search only the core token table, not TZ names */
885  if (datebsearch(field[nf], datetktbl, szdatetktbl) == NULL)
886  is_date = true;
887  }
888  if (is_date)
889  {
890  ftype[nf] = DTK_DATE;
891  do
892  {
893  APPEND_CHAR(bufp, bufend, pg_tolower((unsigned char) *cp++));
894  } while (*cp == '+' || *cp == '-' ||
895  *cp == '/' || *cp == '_' ||
896  *cp == '.' || *cp == ':' ||
897  isalnum((unsigned char) *cp));
898  }
899  }
900  /* sign? then special or numeric timezone */
901  else if (*cp == '+' || *cp == '-')
902  {
903  APPEND_CHAR(bufp, bufend, *cp++);
904  /* soak up leading whitespace */
905  while (isspace((unsigned char) *cp))
906  cp++;
907  /* numeric timezone? */
908  /* note that "DTK_TZ" could also be a signed float or yyyy-mm */
909  if (isdigit((unsigned char) *cp))
910  {
911  ftype[nf] = DTK_TZ;
912  APPEND_CHAR(bufp, bufend, *cp++);
913  while (isdigit((unsigned char) *cp) ||
914  *cp == ':' || *cp == '.' || *cp == '-')
915  APPEND_CHAR(bufp, bufend, *cp++);
916  }
917  /* special? */
918  else if (isalpha((unsigned char) *cp))
919  {
920  ftype[nf] = DTK_SPECIAL;
921  APPEND_CHAR(bufp, bufend, pg_tolower((unsigned char) *cp++));
922  while (isalpha((unsigned char) *cp))
923  APPEND_CHAR(bufp, bufend, pg_tolower((unsigned char) *cp++));
924  }
925  /* otherwise something wrong... */
926  else
927  return DTERR_BAD_FORMAT;
928  }
929  /* ignore other punctuation but use as delimiter */
930  else if (ispunct((unsigned char) *cp))
931  {
932  cp++;
933  continue;
934  }
935  /* otherwise, something is not right... */
936  else
937  return DTERR_BAD_FORMAT;
938 
939  /* force in a delimiter after each field */
940  *bufp++ = '\0';
941  nf++;
942  }
943 
944  *numfields = nf;
945 
946  return 0;
947 }
#define APPEND_CHAR(bufptr, end, newchar)

References APPEND_CHAR, datebsearch(), datetktbl, DTERR_BAD_FORMAT, DTK_DATE, DTK_NUMBER, DTK_SPECIAL, DTK_STRING, DTK_TIME, DTK_TZ, pg_tolower(), and szdatetktbl.

Referenced by check_recovery_target_time(), date_in(), interval_in(), PGTYPESdate_from_asc(), PGTYPESinterval_from_asc(), PGTYPEStimestamp_from_asc(), time_in(), timestamp_in(), timestamptz_in(), and timetz_in().

◆ TemporalSimplify()

struct Node* TemporalSimplify ( int32  max_precis,
struct Node node 
)

Definition at line 4840 of file datetime.c.

4841 {
4842  FuncExpr *expr = castNode(FuncExpr, node);
4843  Node *ret = NULL;
4844  Node *typmod;
4845 
4846  Assert(list_length(expr->args) >= 2);
4847 
4848  typmod = (Node *) lsecond(expr->args);
4849 
4850  if (IsA(typmod, Const) && !((Const *) typmod)->constisnull)
4851  {
4852  Node *source = (Node *) linitial(expr->args);
4853  int32 old_precis = exprTypmod(source);
4854  int32 new_precis = DatumGetInt32(((Const *) typmod)->constvalue);
4855 
4856  if (new_precis < 0 || new_precis == max_precis ||
4857  (old_precis >= 0 && new_precis >= old_precis))
4858  ret = relabel_to_typmod(source, new_precis);
4859  }
4860 
4861  return ret;
4862 }
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:298
Node * relabel_to_typmod(Node *expr, int32 typmod)
Definition: nodeFuncs.c:684
#define IsA(nodeptr, _type_)
Definition: nodes.h:158
#define castNode(_type_, nodeptr)
Definition: nodes.h:176
static int list_length(const List *l)
Definition: pg_list.h:152
#define linitial(l)
Definition: pg_list.h:178
#define lsecond(l)
Definition: pg_list.h:183
static rewind_source * source
Definition: pg_rewind.c:89
static int32 DatumGetInt32(Datum X)
Definition: postgres.h:202
List * args
Definition: primnodes.h:768
Definition: nodes.h:129

References FuncExpr::args, Assert, castNode, DatumGetInt32(), exprTypmod(), IsA, linitial, list_length(), lsecond, relabel_to_typmod(), and source.

Referenced by time_support(), and timestamp_support().

◆ ValidateDate()

int ValidateDate ( int  fmask,
bool  isjulian,
bool  is2digits,
bool  bc,
struct pg_tm tm 
)

Definition at line 2508 of file datetime.c.

2510 {
2511  if (fmask & DTK_M(YEAR))
2512  {
2513  if (isjulian)
2514  {
2515  /* tm_year is correct and should not be touched */
2516  }
2517  else if (bc)
2518  {
2519  /* there is no year zero in AD/BC notation */
2520  if (tm->tm_year <= 0)
2521  return DTERR_FIELD_OVERFLOW;
2522  /* internally, we represent 1 BC as year zero, 2 BC as -1, etc */
2523  tm->tm_year = -(tm->tm_year - 1);
2524  }
2525  else if (is2digits)
2526  {
2527  /* process 1 or 2-digit input as 1970-2069 AD, allow '0' and '00' */
2528  if (tm->tm_year < 0) /* just paranoia */
2529  return DTERR_FIELD_OVERFLOW;
2530  if (tm->tm_year < 70)
2531  tm->tm_year += 2000;
2532  else if (tm->tm_year < 100)
2533  tm->tm_year += 1900;
2534  }
2535  else
2536  {
2537  /* there is no year zero in AD/BC notation */
2538  if (tm->tm_year <= 0)
2539  return DTERR_FIELD_OVERFLOW;
2540  }
2541  }
2542 
2543  /* now that we have correct year, decode DOY */
2544  if (fmask & DTK_M(DOY))
2545  {
2546  j2date(date2j(tm->tm_year, 1, 1) + tm->tm_yday - 1,
2547  &tm->tm_year, &tm->tm_mon, &tm->tm_mday);
2548  }
2549 
2550  /* check for valid month */
2551  if (fmask & DTK_M(MONTH))
2552  {
2553  if (tm->tm_mon < 1 || tm->tm_mon > MONTHS_PER_YEAR)
2554  return DTERR_MD_FIELD_OVERFLOW;
2555  }
2556 
2557  /* minimal check for valid day */
2558  if (fmask & DTK_M(DAY))
2559  {
2560  if (tm->tm_mday < 1 || tm->tm_mday > 31)
2561  return DTERR_MD_FIELD_OVERFLOW;
2562  }
2563 
2564  if ((fmask & DTK_DATE_M) == DTK_DATE_M)
2565  {
2566  /*
2567  * Check for valid day of month, now that we know for sure the month
2568  * and year. Note we don't use MD_FIELD_OVERFLOW here, since it seems
2569  * unlikely that "Feb 29" is a YMD-order error.
2570  */
2571  if (tm->tm_mday > day_tab[isleap(tm->tm_year)][tm->tm_mon - 1])
2572  return DTERR_FIELD_OVERFLOW;
2573  }
2574 
2575  return 0;
2576 }
const int day_tab[2][13]
Definition: datetime.c:75
#define isleap(y)
Definition: datetime.h:271
#define DOY
Definition: datetime.h:105
int tm_yday
Definition: pgtime.h:43

References date2j(), DAY, day_tab, DOY, DTERR_FIELD_OVERFLOW, DTERR_MD_FIELD_OVERFLOW, DTK_DATE_M, DTK_M, isleap, j2date(), MONTH, MONTHS_PER_YEAR, tm, pg_tm::tm_mday, pg_tm::tm_mon, pg_tm::tm_yday, pg_tm::tm_year, and YEAR.

Referenced by DecodeDateTime(), DecodeTimeOnly(), do_to_timestamp(), make_date(), and make_timestamp_internal().

Variable Documentation

◆ day_tab

PGDLLIMPORT const int day_tab[2][13]
extern

Definition at line 75 of file datetime.c.

◆ days

PGDLLIMPORT const char* const days[]
extern

Definition at line 84 of file datetime.c.

◆ months

PGDLLIMPORT const char* const months[]
extern

Definition at line 81 of file datetime.c.