PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
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)
 
void ClearTimeZoneAbbrevCache (void)
 
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 368 of file timestamp.c.

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

4928{
4929 bool ok = true;
4930
4931 Assert(UNIX_EPOCH_JDATE == date2j(1970, 1, 1));
4932 Assert(POSTGRES_EPOCH_JDATE == date2j(2000, 1, 1));
4933
4934 ok &= CheckDateTokenTable("datetktbl", datetktbl, szdatetktbl);
4935 ok &= CheckDateTokenTable("deltatktbl", deltatktbl, szdeltatktbl);
4936 return ok;
4937}
static bool CheckDateTokenTable(const char *tablename, const datetkn *base, int nel)
Definition: datetime.c:4895
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:296
static const datetkn deltatktbl[]
Definition: datetime.c:187
#define UNIX_EPOCH_JDATE
Definition: timestamp.h:234
#define POSTGRES_EPOCH_JDATE
Definition: timestamp.h:235
Assert(PointerIsAligned(start, uint64))

◆ ClearTimeZoneAbbrevCache()

void ClearTimeZoneAbbrevCache ( void  )

Definition at line 3221 of file datetime.c.

3222{
3223 memset(tzabbrevcache, 0, sizeof(tzabbrevcache));
3224}
static TzAbbrevCache tzabbrevcache[MAXDATEFIELDS]
Definition: datetime.c:272

References tzabbrevcache.

Referenced by assign_timezone().

◆ ConvertTimeZoneAbbrevs()

TimeZoneAbbrevTable * ConvertTimeZoneAbbrevs ( struct tzEntry abbrevs,
int  n 
)

Definition at line 4989 of file datetime.c.

4990{
4992 Size tbl_size;
4993 int i;
4994
4995 /* Space for fixed fields and datetkn array */
4996 tbl_size = offsetof(TimeZoneAbbrevTable, abbrevs) +
4997 n * sizeof(datetkn);
4998 tbl_size = MAXALIGN(tbl_size);
4999 /* Count up space for dynamic abbreviations */
5000 for (i = 0; i < n; i++)
5001 {
5002 struct tzEntry *abbr = abbrevs + i;
5003
5004 if (abbr->zone != NULL)
5005 {
5006 Size dsize;
5007
5008 dsize = offsetof(DynamicZoneAbbrev, zone) +
5009 strlen(abbr->zone) + 1;
5010 tbl_size += MAXALIGN(dsize);
5011 }
5012 }
5013
5014 /* Alloc the result ... */
5015 tbl = guc_malloc(LOG, tbl_size);
5016 if (!tbl)
5017 return NULL;
5018
5019 /* ... and fill it in */
5020 tbl->tblsize = tbl_size;
5021 tbl->numabbrevs = n;
5022 /* in this loop, tbl_size reprises the space calculation above */
5023 tbl_size = offsetof(TimeZoneAbbrevTable, abbrevs) +
5024 n * sizeof(datetkn);
5025 tbl_size = MAXALIGN(tbl_size);
5026 for (i = 0; i < n; i++)
5027 {
5028 struct tzEntry *abbr = abbrevs + i;
5029 datetkn *dtoken = tbl->abbrevs + i;
5030
5031 /* use strlcpy to truncate name if necessary */
5032 strlcpy(dtoken->token, abbr->abbrev, TOKMAXLEN + 1);
5033 if (abbr->zone != NULL)
5034 {
5035 /* Allocate a DynamicZoneAbbrev for this abbreviation */
5036 DynamicZoneAbbrev *dtza;
5037 Size dsize;
5038
5039 dtza = (DynamicZoneAbbrev *) ((char *) tbl + tbl_size);
5040 dtza->tz = NULL;
5041 strcpy(dtza->zone, abbr->zone);
5042
5043 dtoken->type = DYNTZ;
5044 /* value is offset from table start to DynamicZoneAbbrev */
5045 dtoken->value = (int32) tbl_size;
5046
5047 dsize = offsetof(DynamicZoneAbbrev, zone) +
5048 strlen(abbr->zone) + 1;
5049 tbl_size += MAXALIGN(dsize);
5050 }
5051 else
5052 {
5053 dtoken->type = abbr->is_dst ? DTZ : TZ;
5054 dtoken->value = abbr->offset;
5055 }
5056 }
5057
5058 /* Assert the two loops above agreed on size calculations */
5059 Assert(tbl->tblsize == tbl_size);
5060
5061 /* Check the ordering, if testing */
5062 Assert(CheckDateTokenTable("timezone abbreviations", tbl->abbrevs, n));
5063
5064 return tbl;
5065}
#define MAXALIGN(LEN)
Definition: c.h:782
int32_t int32
Definition: c.h:498
size_t Size
Definition: c.h:576
#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:77
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 296 of file datetime.c.

297{
298 int julian;
299 int century;
300
301 if (month > 2)
302 {
303 month += 1;
304 year += 4800;
305 }
306 else
307 {
308 month += 13;
309 year += 4799;
310 }
311
312 century = year / 100;
313 julian = year * 365 - 32167;
314 julian += year / 4 - century + century / 4;
315 julian += 7834 * month / 256 + day;
316
317 return julian;
318} /* date2j() */

◆ DateTimeParseError()

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

Definition at line 4208 of file datetime.c.

4211{
4212 switch (dterr)
4213 {
4215 errsave(escontext,
4216 (errcode(ERRCODE_DATETIME_FIELD_OVERFLOW),
4217 errmsg("date/time field value out of range: \"%s\"",
4218 str)));
4219 break;
4221 /* <nanny>same as above, but add hint about DateStyle</nanny> */
4222 errsave(escontext,
4223 (errcode(ERRCODE_DATETIME_FIELD_OVERFLOW),
4224 errmsg("date/time field value out of range: \"%s\"",
4225 str),
4226 errhint("Perhaps you need a different \"DateStyle\" setting.")));
4227 break;
4229 errsave(escontext,
4230 (errcode(ERRCODE_INTERVAL_FIELD_OVERFLOW),
4231 errmsg("interval field value out of range: \"%s\"",
4232 str)));
4233 break;
4235 errsave(escontext,
4236 (errcode(ERRCODE_INVALID_TIME_ZONE_DISPLACEMENT_VALUE),
4237 errmsg("time zone displacement out of range: \"%s\"",
4238 str)));
4239 break;
4240 case DTERR_BAD_TIMEZONE:
4241 errsave(escontext,
4242 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
4243 errmsg("time zone \"%s\" not recognized",
4244 extra->dtee_timezone)));
4245 break;
4247 errsave(escontext,
4248 (errcode(ERRCODE_CONFIG_FILE_ERROR),
4249 errmsg("time zone \"%s\" not recognized",
4250 extra->dtee_timezone),
4251 errdetail("This time zone name appears in the configuration file for time zone abbreviation \"%s\".",
4252 extra->dtee_abbrev)));
4253 break;
4254 case DTERR_BAD_FORMAT:
4255 default:
4256 errsave(escontext,
4257 (errcode(ERRCODE_INVALID_DATETIME_FORMAT),
4258 errmsg("invalid input syntax for type %s: \"%s\"",
4259 datatype, str)));
4260 break;
4261 }
4262}
int errdetail(const char *fmt,...)
Definition: elog.c:1204
int errhint(const char *fmt,...)
Definition: elog.c:1318
#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_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 988 of file datetime.c.

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

3482{
3483 bool force_negative = false;
3484 bool is_before = false;
3485 bool parsing_unit_val = false;
3486 char *cp;
3487 int fmask = 0,
3488 tmask,
3489 type,
3490 uval;
3491 int i;
3492 int dterr;
3493 int64 val;
3494 double fval;
3495
3496 *dtype = DTK_DELTA;
3497 type = IGNORE_DTF;
3498 ClearPgItmIn(itm_in);
3499
3500 /*----------
3501 * The SQL standard defines the interval literal
3502 * '-1 1:00:00'
3503 * to mean "negative 1 days and negative 1 hours", while Postgres
3504 * traditionally treats this as meaning "negative 1 days and positive
3505 * 1 hours". In SQL_STANDARD intervalstyle, we apply the leading sign
3506 * to all fields if there are no other explicit signs.
3507 *
3508 * We leave the signs alone if there are additional explicit signs.
3509 * This protects us against misinterpreting postgres-style dump output,
3510 * since the postgres-style output code has always put an explicit sign on
3511 * all fields following a negative field. But note that SQL-spec output
3512 * is ambiguous and can be misinterpreted on load! (So it's best practice
3513 * to dump in postgres style, not SQL style.)
3514 *----------
3515 */
3516 if (IntervalStyle == INTSTYLE_SQL_STANDARD && nf > 0 && *field[0] == '-')
3517 {
3518 force_negative = true;
3519 /* Check for additional explicit signs */
3520 for (i = 1; i < nf; i++)
3521 {
3522 if (*field[i] == '-' || *field[i] == '+')
3523 {
3524 force_negative = false;
3525 break;
3526 }
3527 }
3528 }
3529
3530 /* read through list backwards to pick up units before values */
3531 for (i = nf - 1; i >= 0; i--)
3532 {
3533 switch (ftype[i])
3534 {
3535 case DTK_TIME:
3536 dterr = DecodeTimeForInterval(field[i], fmask, range,
3537 &tmask, itm_in);
3538 if (dterr)
3539 return dterr;
3540 if (force_negative &&
3541 itm_in->tm_usec > 0)
3542 itm_in->tm_usec = -itm_in->tm_usec;
3543 type = DTK_DAY;
3544 parsing_unit_val = false;
3545 break;
3546
3547 case DTK_TZ:
3548
3549 /*
3550 * Timezone means a token with a leading sign character and at
3551 * least one digit; there could be ':', '.', '-' embedded in
3552 * it as well.
3553 */
3554 Assert(*field[i] == '-' || *field[i] == '+');
3555
3556 /*
3557 * Check for signed hh:mm or hh:mm:ss. If so, process exactly
3558 * like DTK_TIME case above, plus handling the sign.
3559 */
3560 if (strchr(field[i] + 1, ':') != NULL &&
3561 DecodeTimeForInterval(field[i] + 1, fmask, range,
3562 &tmask, itm_in) == 0)
3563 {
3564 if (*field[i] == '-')
3565 {
3566 /* flip the sign on time field */
3567 if (itm_in->tm_usec == PG_INT64_MIN)
3568 return DTERR_FIELD_OVERFLOW;
3569 itm_in->tm_usec = -itm_in->tm_usec;
3570 }
3571
3572 if (force_negative &&
3573 itm_in->tm_usec > 0)
3574 itm_in->tm_usec = -itm_in->tm_usec;
3575
3576 /*
3577 * Set the next type to be a day, if units are not
3578 * specified. This handles the case of '1 +02:03' since we
3579 * are reading right to left.
3580 */
3581 type = DTK_DAY;
3582 parsing_unit_val = false;
3583 break;
3584 }
3585
3586 /*
3587 * Otherwise, fall through to DTK_NUMBER case, which can
3588 * handle signed float numbers and signed year-month values.
3589 */
3590
3591 /* FALLTHROUGH */
3592
3593 case DTK_DATE:
3594 case DTK_NUMBER:
3595 if (type == IGNORE_DTF)
3596 {
3597 /* use typmod to decide what rightmost field is */
3598 switch (range)
3599 {
3600 case INTERVAL_MASK(YEAR):
3601 type = DTK_YEAR;
3602 break;
3603 case INTERVAL_MASK(MONTH):
3605 type = DTK_MONTH;
3606 break;
3607 case INTERVAL_MASK(DAY):
3608 type = DTK_DAY;
3609 break;
3610 case INTERVAL_MASK(HOUR):
3612 type = DTK_HOUR;
3613 break;
3614 case INTERVAL_MASK(MINUTE):
3617 type = DTK_MINUTE;
3618 break;
3619 case INTERVAL_MASK(SECOND):
3623 type = DTK_SECOND;
3624 break;
3625 default:
3626 type = DTK_SECOND;
3627 break;
3628 }
3629 }
3630
3631 errno = 0;
3632 val = strtoi64(field[i], &cp, 10);
3633 if (errno == ERANGE)
3634 return DTERR_FIELD_OVERFLOW;
3635
3636 if (*cp == '-')
3637 {
3638 /* SQL "years-months" syntax */
3639 int val2;
3640
3641 val2 = strtoint(cp + 1, &cp, 10);
3642 if (errno == ERANGE || val2 < 0 || val2 >= MONTHS_PER_YEAR)
3643 return DTERR_FIELD_OVERFLOW;
3644 if (*cp != '\0')
3645 return DTERR_BAD_FORMAT;
3646 type = DTK_MONTH;
3647 if (*field[i] == '-')
3648 val2 = -val2;
3650 return DTERR_FIELD_OVERFLOW;
3651 if (pg_add_s64_overflow(val, val2, &val))
3652 return DTERR_FIELD_OVERFLOW;
3653 fval = 0;
3654 }
3655 else if (*cp == '.')
3656 {
3657 dterr = ParseFraction(cp, &fval);
3658 if (dterr)
3659 return dterr;
3660 if (*field[i] == '-')
3661 fval = -fval;
3662 }
3663 else if (*cp == '\0')
3664 fval = 0;
3665 else
3666 return DTERR_BAD_FORMAT;
3667
3668 tmask = 0; /* DTK_M(type); */
3669
3670 if (force_negative)
3671 {
3672 /* val and fval should be of same sign, but test anyway */
3673 if (val > 0)
3674 val = -val;
3675 if (fval > 0)
3676 fval = -fval;
3677 }
3678
3679 switch (type)
3680 {
3681 case DTK_MICROSEC:
3682 if (!AdjustMicroseconds(val, fval, 1, itm_in))
3683 return DTERR_FIELD_OVERFLOW;
3684 tmask = DTK_M(MICROSECOND);
3685 break;
3686
3687 case DTK_MILLISEC:
3688 if (!AdjustMicroseconds(val, fval, 1000, itm_in))
3689 return DTERR_FIELD_OVERFLOW;
3690 tmask = DTK_M(MILLISECOND);
3691 break;
3692
3693 case DTK_SECOND:
3694 if (!AdjustMicroseconds(val, fval, USECS_PER_SEC, itm_in))
3695 return DTERR_FIELD_OVERFLOW;
3696
3697 /*
3698 * If any subseconds were specified, consider this
3699 * microsecond and millisecond input as well.
3700 */
3701 if (fval == 0)
3702 tmask = DTK_M(SECOND);
3703 else
3704 tmask = DTK_ALL_SECS_M;
3705 break;
3706
3707 case DTK_MINUTE:
3708 if (!AdjustMicroseconds(val, fval, USECS_PER_MINUTE, itm_in))
3709 return DTERR_FIELD_OVERFLOW;
3710 tmask = DTK_M(MINUTE);
3711 break;
3712
3713 case DTK_HOUR:
3714 if (!AdjustMicroseconds(val, fval, USECS_PER_HOUR, itm_in))
3715 return DTERR_FIELD_OVERFLOW;
3716 tmask = DTK_M(HOUR);
3717 type = DTK_DAY; /* set for next field */
3718 break;
3719
3720 case DTK_DAY:
3721 if (!AdjustDays(val, 1, itm_in) ||
3722 !AdjustFractMicroseconds(fval, USECS_PER_DAY, itm_in))
3723 return DTERR_FIELD_OVERFLOW;
3724 tmask = DTK_M(DAY);
3725 break;
3726
3727 case DTK_WEEK:
3728 if (!AdjustDays(val, 7, itm_in) ||
3729 !AdjustFractDays(fval, 7, itm_in))
3730 return DTERR_FIELD_OVERFLOW;
3731 tmask = DTK_M(WEEK);
3732 break;
3733
3734 case DTK_MONTH:
3735 if (!AdjustMonths(val, itm_in) ||
3736 !AdjustFractDays(fval, DAYS_PER_MONTH, itm_in))
3737 return DTERR_FIELD_OVERFLOW;
3738 tmask = DTK_M(MONTH);
3739 break;
3740
3741 case DTK_YEAR:
3742 if (!AdjustYears(val, 1, itm_in) ||
3743 !AdjustFractYears(fval, 1, itm_in))
3744 return DTERR_FIELD_OVERFLOW;
3745 tmask = DTK_M(YEAR);
3746 break;
3747
3748 case DTK_DECADE:
3749 if (!AdjustYears(val, 10, itm_in) ||
3750 !AdjustFractYears(fval, 10, itm_in))
3751 return DTERR_FIELD_OVERFLOW;
3752 tmask = DTK_M(DECADE);
3753 break;
3754
3755 case DTK_CENTURY:
3756 if (!AdjustYears(val, 100, itm_in) ||
3757 !AdjustFractYears(fval, 100, itm_in))
3758 return DTERR_FIELD_OVERFLOW;
3759 tmask = DTK_M(CENTURY);
3760 break;
3761
3762 case DTK_MILLENNIUM:
3763 if (!AdjustYears(val, 1000, itm_in) ||
3764 !AdjustFractYears(fval, 1000, itm_in))
3765 return DTERR_FIELD_OVERFLOW;
3766 tmask = DTK_M(MILLENNIUM);
3767 break;
3768
3769 default:
3770 return DTERR_BAD_FORMAT;
3771 }
3772 parsing_unit_val = false;
3773 break;
3774
3775 case DTK_STRING:
3776 case DTK_SPECIAL:
3777 /* reject consecutive unhandled units */
3778 if (parsing_unit_val)
3779 return DTERR_BAD_FORMAT;
3780 type = DecodeUnits(i, field[i], &uval);
3781 if (type == UNKNOWN_FIELD)
3782 type = DecodeSpecial(i, field[i], &uval);
3783 if (type == IGNORE_DTF)
3784 continue;
3785
3786 tmask = 0; /* DTK_M(type); */
3787 switch (type)
3788 {
3789 case UNITS:
3790 type = uval;
3791 parsing_unit_val = true;
3792 break;
3793
3794 case AGO:
3795
3796 /*
3797 * "ago" is only allowed to appear at the end of the
3798 * interval.
3799 */
3800 if (i != nf - 1)
3801 return DTERR_BAD_FORMAT;
3802 is_before = true;
3803 type = uval;
3804 break;
3805
3806 case RESERV:
3807 tmask = (DTK_DATE_M | DTK_TIME_M);
3808
3809 /*
3810 * Only reserved words corresponding to infinite
3811 * intervals are accepted.
3812 */
3813 if (uval != DTK_LATE && uval != DTK_EARLY)
3814 return DTERR_BAD_FORMAT;
3815
3816 /*
3817 * Infinity cannot be followed by anything else. We
3818 * could allow "ago" to reverse the sign of infinity
3819 * but using signed infinity is more intuitive.
3820 */
3821 if (i != nf - 1)
3822 return DTERR_BAD_FORMAT;
3823
3824 *dtype = uval;
3825 break;
3826
3827 default:
3828 return DTERR_BAD_FORMAT;
3829 }
3830 break;
3831
3832 default:
3833 return DTERR_BAD_FORMAT;
3834 }
3835
3836 if (tmask & fmask)
3837 return DTERR_BAD_FORMAT;
3838 fmask |= tmask;
3839 }
3840
3841 /* ensure that at least one time field has been found */
3842 if (fmask == 0)
3843 return DTERR_BAD_FORMAT;
3844
3845 /* reject if unit appeared and was never handled */
3846 if (parsing_unit_val)
3847 return DTERR_BAD_FORMAT;
3848
3849 /* finally, AGO negates everything */
3850 if (is_before)
3851 {
3852 if (itm_in->tm_usec == PG_INT64_MIN ||
3853 itm_in->tm_mday == INT_MIN ||
3854 itm_in->tm_mon == INT_MIN ||
3855 itm_in->tm_year == INT_MIN)
3856 return DTERR_FIELD_OVERFLOW;
3857
3858 itm_in->tm_usec = -itm_in->tm_usec;
3859 itm_in->tm_mday = -itm_in->tm_mday;
3860 itm_in->tm_mon = -itm_in->tm_mon;
3861 itm_in->tm_year = -itm_in->tm_year;
3862 }
3863
3864 return 0;
3865}
int DecodeUnits(int field, const char *lowtoken, int *val)
Definition: datetime.c:4163
static bool AdjustDays(int64 val, int scale, struct pg_itm_in *itm_in)
Definition: datetime.c:643
static bool AdjustFractYears(double frac, int scale, struct pg_itm_in *itm_in)
Definition: datetime.c:611
static bool AdjustMicroseconds(int64 val, double fval, int64 scale, struct pg_itm_in *itm_in)
Definition: datetime.c:628
static int DecodeTimeForInterval(char *str, int fmask, int range, int *tmask, struct pg_itm_in *itm_in)
Definition: datetime.c:2745
static bool AdjustFractMicroseconds(double frac, int64 scale, struct pg_itm_in *itm_in)
Definition: datetime.c:547
static bool AdjustYears(int64 val, int scale, struct pg_itm_in *itm_in)
Definition: datetime.c:671
static bool AdjustMonths(int64 val, struct pg_itm_in *itm_in)
Definition: datetime.c:659
static bool AdjustFractDays(double frac, int scale, struct pg_itm_in *itm_in)
Definition: datetime.c:579
static void ClearPgItmIn(struct pg_itm_in *itm_in)
Definition: datetime.c:3456
#define PG_INT64_MIN
Definition: c.h:562
#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:128
#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:293
static bool pg_add_s64_overflow(int64 a, int64 b, int64 *result)
Definition: int.h:235
#define INTSTYLE_SQL_STANDARD
Definition: miscadmin.h:259
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 3945 of file datetime.c.

3947{
3948 bool datepart = true;
3949 bool havefield = false;
3950
3951 *dtype = DTK_DELTA;
3952 ClearPgItmIn(itm_in);
3953
3954 if (strlen(str) < 2 || str[0] != 'P')
3955 return DTERR_BAD_FORMAT;
3956
3957 str++;
3958 while (*str)
3959 {
3960 char *fieldstart;
3961 int64 val;
3962 double fval;
3963 char unit;
3964 int dterr;
3965
3966 if (*str == 'T') /* T indicates the beginning of the time part */
3967 {
3968 datepart = false;
3969 havefield = false;
3970 str++;
3971 continue;
3972 }
3973
3974 fieldstart = str;
3975 dterr = ParseISO8601Number(str, &str, &val, &fval);
3976 if (dterr)
3977 return dterr;
3978
3979 /*
3980 * Note: we could step off the end of the string here. Code below
3981 * *must* exit the loop if unit == '\0'.
3982 */
3983 unit = *str++;
3984
3985 if (datepart)
3986 {
3987 switch (unit) /* before T: Y M W D */
3988 {
3989 case 'Y':
3990 if (!AdjustYears(val, 1, itm_in) ||
3991 !AdjustFractYears(fval, 1, itm_in))
3992 return DTERR_FIELD_OVERFLOW;
3993 break;
3994 case 'M':
3995 if (!AdjustMonths(val, itm_in) ||
3996 !AdjustFractDays(fval, DAYS_PER_MONTH, itm_in))
3997 return DTERR_FIELD_OVERFLOW;
3998 break;
3999 case 'W':
4000 if (!AdjustDays(val, 7, itm_in) ||
4001 !AdjustFractDays(fval, 7, itm_in))
4002 return DTERR_FIELD_OVERFLOW;
4003 break;
4004 case 'D':
4005 if (!AdjustDays(val, 1, itm_in) ||
4006 !AdjustFractMicroseconds(fval, USECS_PER_DAY, itm_in))
4007 return DTERR_FIELD_OVERFLOW;
4008 break;
4009 case 'T': /* ISO 8601 4.4.3.3 Alternative Format / Basic */
4010 case '\0':
4011 if (ISO8601IntegerWidth(fieldstart) == 8 && !havefield)
4012 {
4013 if (!AdjustYears(val / 10000, 1, itm_in) ||
4014 !AdjustMonths((val / 100) % 100, itm_in) ||
4015 !AdjustDays(val % 100, 1, itm_in) ||
4016 !AdjustFractMicroseconds(fval, USECS_PER_DAY, itm_in))
4017 return DTERR_FIELD_OVERFLOW;
4018 if (unit == '\0')
4019 return 0;
4020 datepart = false;
4021 havefield = false;
4022 continue;
4023 }
4024 /* Else fall through to extended alternative format */
4025 /* FALLTHROUGH */
4026 case '-': /* ISO 8601 4.4.3.3 Alternative Format,
4027 * Extended */
4028 if (havefield)
4029 return DTERR_BAD_FORMAT;
4030
4031 if (!AdjustYears(val, 1, itm_in) ||
4032 !AdjustFractYears(fval, 1, itm_in))
4033 return DTERR_FIELD_OVERFLOW;
4034 if (unit == '\0')
4035 return 0;
4036 if (unit == 'T')
4037 {
4038 datepart = false;
4039 havefield = false;
4040 continue;
4041 }
4042
4043 dterr = ParseISO8601Number(str, &str, &val, &fval);
4044 if (dterr)
4045 return dterr;
4046 if (!AdjustMonths(val, itm_in) ||
4047 !AdjustFractDays(fval, DAYS_PER_MONTH, itm_in))
4048 return DTERR_FIELD_OVERFLOW;
4049 if (*str == '\0')
4050 return 0;
4051 if (*str == 'T')
4052 {
4053 datepart = false;
4054 havefield = false;
4055 continue;
4056 }
4057 if (*str != '-')
4058 return DTERR_BAD_FORMAT;
4059 str++;
4060
4061 dterr = ParseISO8601Number(str, &str, &val, &fval);
4062 if (dterr)
4063 return dterr;
4064 if (!AdjustDays(val, 1, itm_in) ||
4065 !AdjustFractMicroseconds(fval, USECS_PER_DAY, itm_in))
4066 return DTERR_FIELD_OVERFLOW;
4067 if (*str == '\0')
4068 return 0;
4069 if (*str == 'T')
4070 {
4071 datepart = false;
4072 havefield = false;
4073 continue;
4074 }
4075 return DTERR_BAD_FORMAT;
4076 default:
4077 /* not a valid date unit suffix */
4078 return DTERR_BAD_FORMAT;
4079 }
4080 }
4081 else
4082 {
4083 switch (unit) /* after T: H M S */
4084 {
4085 case 'H':
4086 if (!AdjustMicroseconds(val, fval, USECS_PER_HOUR, itm_in))
4087 return DTERR_FIELD_OVERFLOW;
4088 break;
4089 case 'M':
4090 if (!AdjustMicroseconds(val, fval, USECS_PER_MINUTE, itm_in))
4091 return DTERR_FIELD_OVERFLOW;
4092 break;
4093 case 'S':
4094 if (!AdjustMicroseconds(val, fval, USECS_PER_SEC, itm_in))
4095 return DTERR_FIELD_OVERFLOW;
4096 break;
4097 case '\0': /* ISO 8601 4.4.3.3 Alternative Format */
4098 if (ISO8601IntegerWidth(fieldstart) == 6 && !havefield)
4099 {
4100 if (!AdjustMicroseconds(val / 10000, 0, USECS_PER_HOUR, itm_in) ||
4101 !AdjustMicroseconds((val / 100) % 100, 0, USECS_PER_MINUTE, itm_in) ||
4102 !AdjustMicroseconds(val % 100, 0, USECS_PER_SEC, itm_in) ||
4103 !AdjustFractMicroseconds(fval, 1, itm_in))
4104 return DTERR_FIELD_OVERFLOW;
4105 return 0;
4106 }
4107 /* Else fall through to extended alternative format */
4108 /* FALLTHROUGH */
4109 case ':': /* ISO 8601 4.4.3.3 Alternative Format,
4110 * Extended */
4111 if (havefield)
4112 return DTERR_BAD_FORMAT;
4113
4114 if (!AdjustMicroseconds(val, fval, USECS_PER_HOUR, itm_in))
4115 return DTERR_FIELD_OVERFLOW;
4116 if (unit == '\0')
4117 return 0;
4118
4119 dterr = ParseISO8601Number(str, &str, &val, &fval);
4120 if (dterr)
4121 return dterr;
4122 if (!AdjustMicroseconds(val, fval, USECS_PER_MINUTE, itm_in))
4123 return DTERR_FIELD_OVERFLOW;
4124 if (*str == '\0')
4125 return 0;
4126 if (*str != ':')
4127 return DTERR_BAD_FORMAT;
4128 str++;
4129
4130 dterr = ParseISO8601Number(str, &str, &val, &fval);
4131 if (dterr)
4132 return dterr;
4133 if (!AdjustMicroseconds(val, fval, USECS_PER_SEC, itm_in))
4134 return DTERR_FIELD_OVERFLOW;
4135 if (*str == '\0')
4136 return 0;
4137 return DTERR_BAD_FORMAT;
4138
4139 default:
4140 /* not a valid time unit suffix */
4141 return DTERR_BAD_FORMAT;
4142 }
4143 }
4144
4145 havefield = true;
4146 }
4147
4148 return 0;
4149}
static int ISO8601IntegerWidth(char *fieldstart)
Definition: datetime.c:3918
static int ParseISO8601Number(char *str, char **endptr, int64 *ipart, double *fpart)
Definition: datetime.c:3876

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

3241{
3242 int type;
3243 const datetkn *tp;
3244
3245 tp = datecache[field];
3246 /* use strncmp so that we match truncated tokens */
3247 if (tp == NULL || strncmp(lowtoken, tp->token, TOKMAXLEN) != 0)
3248 {
3249 tp = datebsearch(lowtoken, datetktbl, szdatetktbl);
3250 }
3251 if (tp == NULL)
3252 {
3254 *val = 0;
3255 }
3256 else
3257 {
3258 datecache[field] = tp;
3259 type = tp->type;
3260 *val = tp->value;
3261 }
3262
3263 return type;
3264}
static const datetkn * datecache[MAXDATEFIELDS]
Definition: datetime.c:258
static const datetkn * datebsearch(const char *key, const datetkn *base, int nel)
Definition: datetime.c:4269

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

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

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

3052{
3053 int tz;
3054 int hr,
3055 min,
3056 sec = 0;
3057 char *cp;
3058
3059 /* leading character must be "+" or "-" */
3060 if (*str != '+' && *str != '-')
3061 return DTERR_BAD_FORMAT;
3062
3063 errno = 0;
3064 hr = strtoint(str + 1, &cp, 10);
3065 if (errno == ERANGE)
3066 return DTERR_TZDISP_OVERFLOW;
3067
3068 /* explicit delimiter? */
3069 if (*cp == ':')
3070 {
3071 errno = 0;
3072 min = strtoint(cp + 1, &cp, 10);
3073 if (errno == ERANGE)
3074 return DTERR_TZDISP_OVERFLOW;
3075 if (*cp == ':')
3076 {
3077 errno = 0;
3078 sec = strtoint(cp + 1, &cp, 10);
3079 if (errno == ERANGE)
3080 return DTERR_TZDISP_OVERFLOW;
3081 }
3082 }
3083 /* otherwise, might have run things together... */
3084 else if (*cp == '\0' && strlen(str) > 3)
3085 {
3086 min = hr % 100;
3087 hr = hr / 100;
3088 /* we could, but don't, support a run-together hhmmss format */
3089 }
3090 else
3091 min = 0;
3092
3093 /* Range-check the values; see notes in datatype/timestamp.h */
3094 if (hr < 0 || hr > MAX_TZDISP_HOUR)
3095 return DTERR_TZDISP_OVERFLOW;
3096 if (min < 0 || min >= MINS_PER_HOUR)
3097 return DTERR_TZDISP_OVERFLOW;
3098 if (sec < 0 || sec >= SECS_PER_MINUTE)
3099 return DTERR_TZDISP_OVERFLOW;
3100
3101 tz = (hr * MINS_PER_HOUR + min) * SECS_PER_MINUTE + sec;
3102 if (*str == '-')
3103 tz = -tz;
3104
3105 *tzp = -tz;
3106
3107 if (*cp != '\0')
3108 return DTERR_BAD_FORMAT;
3109
3110 return 0;
3111}
#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 3135 of file datetime.c.

3138{
3139 TzAbbrevCache *tzc = &tzabbrevcache[field];
3140 bool isfixed;
3141 int isdst;
3142 const datetkn *tp;
3143
3144 /*
3145 * Do we have a cached result? Use strncmp so that we match truncated
3146 * names, although we shouldn't really see that happen with normal
3147 * abbreviations.
3148 */
3149 if (strncmp(lowtoken, tzc->abbrev, TOKMAXLEN) == 0)
3150 {
3151 *ftype = tzc->ftype;
3152 *offset = tzc->offset;
3153 *tz = tzc->tz;
3154 return 0;
3155 }
3156
3157 /*
3158 * See if the current session_timezone recognizes it. Checking this
3159 * before zoneabbrevtbl allows us to correctly handle abbreviations whose
3160 * meaning varies across zones, such as "LMT".
3161 */
3162 if (session_timezone &&
3164 &isfixed, offset, &isdst))
3165 {
3166 *ftype = (isfixed ? (isdst ? DTZ : TZ) : DYNTZ);
3167 *tz = (isfixed ? NULL : session_timezone);
3168 /* flip sign to agree with the convention used in zoneabbrevtbl */
3169 *offset = -(*offset);
3170 /* cache result; use strlcpy to truncate name if necessary */
3171 strlcpy(tzc->abbrev, lowtoken, TOKMAXLEN + 1);
3172 tzc->ftype = *ftype;
3173 tzc->offset = *offset;
3174 tzc->tz = *tz;
3175 return 0;
3176 }
3177
3178 /* Nope, so look in zoneabbrevtbl */
3179 if (zoneabbrevtbl)
3180 tp = datebsearch(lowtoken, zoneabbrevtbl->abbrevs,
3182 else
3183 tp = NULL;
3184 if (tp == NULL)
3185 {
3186 *ftype = UNKNOWN_FIELD;
3187 *offset = 0;
3188 *tz = NULL;
3189 /* failure results are not cached */
3190 }
3191 else
3192 {
3193 *ftype = tp->type;
3194 if (tp->type == DYNTZ)
3195 {
3196 *offset = 0;
3197 *tz = FetchDynamicTimeZone(zoneabbrevtbl, tp, extra);
3198 if (*tz == NULL)
3199 return DTERR_BAD_ZONE_ABBREV;
3200 }
3201 else
3202 {
3203 *offset = tp->value;
3204 *tz = NULL;
3205 }
3206
3207 /* cache result; use strlcpy to truncate name if necessary */
3208 strlcpy(tzc->abbrev, lowtoken, TOKMAXLEN + 1);
3209 tzc->ftype = *ftype;
3210 tzc->offset = *offset;
3211 tzc->tz = *tz;
3212 }
3213
3214 return 0;
3215}
static TimeZoneAbbrevTable * zoneabbrevtbl
Definition: datetime.c:254
static bool TimeZoneAbbrevIsKnown(const char *abbr, pg_tz *tzp, bool *isfixed, int *offset, int *isdst)
Definition: datetime.c:1865
static pg_tz * FetchDynamicTimeZone(TimeZoneAbbrevTable *tbl, const datetkn *tp, DateTimeErrorExtra *extra)
Definition: datetime.c:5086
pg_tz * tz
Definition: datetime.c:269
char abbrev[TOKMAXLEN+1]
Definition: datetime.c:266

References TzAbbrevCache::abbrev, TimeZoneAbbrevTable::abbrevs, datebsearch(), DTERR_BAD_ZONE_ABBREV, DTZ, DYNTZ, FetchDynamicTimeZone(), TzAbbrevCache::ftype, TimeZoneAbbrevTable::numabbrevs, TzAbbrevCache::offset, session_timezone, strlcpy(), TimeZoneAbbrevIsKnown(), TOKMAXLEN, datetkn::type, TzAbbrevCache::tz, TZ, tzabbrevcache, 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 3365 of file datetime.c.

3366{
3367 char lowtoken[TOKMAXLEN + 1];
3368 int len;
3369
3370 *offset = 0; /* avoid uninitialized vars on failure */
3371 *tz = NULL;
3372
3373 /* Downcase as much of the string as we could need */
3374 for (len = 0; len < TOKMAXLEN; len++)
3375 {
3376 if (*str == '\0' || !isalpha((unsigned char) *str))
3377 break;
3378 lowtoken[len] = pg_tolower((unsigned char) *str++);
3379 }
3380 lowtoken[len] = '\0';
3381
3382 /*
3383 * We could avoid doing repeated binary searches if we cared to duplicate
3384 * datebsearch here, but it's not clear that such an optimization would be
3385 * worth the trouble. In common cases there's probably not anything after
3386 * the zone abbrev anyway. So just search with successively truncated
3387 * strings.
3388 */
3389 while (len > 0)
3390 {
3391 bool isfixed;
3392 int isdst;
3393 const datetkn *tp;
3394
3395 /* See if the current session_timezone recognizes it. */
3396 if (session_timezone &&
3398 &isfixed, offset, &isdst))
3399 {
3400 if (isfixed)
3401 {
3402 /* flip sign to agree with the convention in zoneabbrevtbl */
3403 *offset = -(*offset);
3404 }
3405 else
3406 {
3407 /* Caller must resolve the abbrev's current meaning */
3408 *tz = session_timezone;
3409 }
3410 return len;
3411 }
3412
3413 /* Known in zoneabbrevtbl? */
3414 if (zoneabbrevtbl)
3415 tp = datebsearch(lowtoken, zoneabbrevtbl->abbrevs,
3417 else
3418 tp = NULL;
3419 if (tp != NULL)
3420 {
3421 if (tp->type == DYNTZ)
3422 {
3423 DateTimeErrorExtra extra;
3425 &extra);
3426
3427 if (tzp != NULL)
3428 {
3429 /* Caller must resolve the abbrev's current meaning */
3430 *tz = tzp;
3431 return len;
3432 }
3433 }
3434 else
3435 {
3436 /* Fixed-offset zone abbrev, so it's easy */
3437 *offset = tp->value;
3438 return len;
3439 }
3440 }
3441
3442 /* Nope, try the next shorter string. */
3443 lowtoken[--len] = '\0';
3444 }
3445
3446 /* Did not find a match */
3447 return -1;
3448}
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(), session_timezone, str, TimeZoneAbbrevIsKnown(), 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 3282 of file datetime.c.

3283{
3284 char *lowzone;
3285 int dterr,
3286 type;
3287 DateTimeErrorExtra extra;
3288
3289 /*
3290 * First we look in the timezone abbreviation table (to handle cases like
3291 * "EST"), and if that fails, we look in the timezone database (to handle
3292 * cases like "America/New_York"). This matches the order in which
3293 * timestamp input checks the cases; it's important because the timezone
3294 * database unwisely uses a few zone names that are identical to offset
3295 * abbreviations.
3296 */
3297
3298 /* DecodeTimezoneAbbrev requires lowercase input */
3299 lowzone = downcase_truncate_identifier(tzname,
3300 strlen(tzname),
3301 false);
3302
3303 dterr = DecodeTimezoneAbbrev(0, lowzone, &type, offset, tz, &extra);
3304 if (dterr)
3305 DateTimeParseError(dterr, &extra, NULL, NULL, NULL);
3306
3307 if (type == TZ || type == DTZ)
3308 {
3309 /* fixed-offset abbreviation, return the offset */
3310 return TZNAME_FIXED_OFFSET;
3311 }
3312 else if (type == DYNTZ)
3313 {
3314 /* dynamic-offset abbreviation, return its referenced timezone */
3315 return TZNAME_DYNTZ;
3316 }
3317 else
3318 {
3319 /* try it as a full zone name */
3320 *tz = pg_tzset(tzname);
3321 if (*tz == NULL)
3322 ereport(ERROR,
3323 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
3324 errmsg("time zone \"%s\" not recognized", tzname)));
3325 return TZNAME_ZONE;
3326 }
3327}
void DateTimeParseError(int dterr, DateTimeErrorExtra *extra, const char *str, const char *datatype, Node *escontext)
Definition: datetime.c:4208
#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 3337 of file datetime.c.

3338{
3339 pg_tz *result;
3340 int offset;
3341
3342 if (DecodeTimezoneName(tzname, &offset, &result) == TZNAME_FIXED_OFFSET)
3343 {
3344 /* fixed-offset abbreviation, get a pg_tz descriptor for that */
3345 result = pg_tzset_offset(-offset); /* flip to POSIX sign convention */
3346 }
3347 return result;
3348}
int DecodeTimezoneName(const char *tzname, int *offset, pg_tz **tz)
Definition: datetime.c:3282
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 4163 of file datetime.c.

4164{
4165 int type;
4166 const datetkn *tp;
4167
4168 tp = deltacache[field];
4169 /* use strncmp so that we match truncated tokens */
4170 if (tp == NULL || strncmp(lowtoken, tp->token, TOKMAXLEN) != 0)
4171 {
4172 tp = datebsearch(lowtoken, deltatktbl, szdeltatktbl);
4173 }
4174 if (tp == NULL)
4175 {
4177 *val = 0;
4178 }
4179 else
4180 {
4181 deltacache[field] = tp;
4182 type = tp->type;
4183 *val = tp->value;
4184 }
4185
4186 return type;
4187} /* 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 1756 of file datetime.c.

1757{
1758 pg_time_t t;
1759 int zone_offset;
1760 int abbr_offset;
1761 int abbr_isdst;
1762
1763 /*
1764 * Compute the UTC time we want to probe at. (In event of overflow, we'll
1765 * probe at the epoch, which is a bit random but probably doesn't matter.)
1766 */
1767 zone_offset = DetermineTimeZoneOffsetInternal(tm, tzp, &t);
1768
1769 /*
1770 * Try to match the abbreviation to something in the zone definition.
1771 */
1773 &abbr_offset, &abbr_isdst))
1774 {
1775 /* Success, so use the abbrev-specific answers. */
1776 tm->tm_isdst = abbr_isdst;
1777 return abbr_offset;
1778 }
1779
1780 /*
1781 * No match, so use the answers we already got from
1782 * DetermineTimeZoneOffsetInternal.
1783 */
1784 return zone_offset;
1785}
static int DetermineTimeZoneOffsetInternal(struct pg_tm *tm, pg_tz *tzp, pg_time_t *tp)
Definition: datetime.c:1617
static bool DetermineTimeZoneAbbrevOffsetInternal(pg_time_t t, const char *abbr, pg_tz *tzp, int *offset, int *isdst)
Definition: datetime.c:1831
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 1794 of file datetime.c.

1796{
1798 int zone_offset;
1799 int abbr_offset;
1800 int tz;
1801 struct pg_tm tm;
1802 fsec_t fsec;
1803
1804 /*
1805 * If the abbrev matches anything in the zone data, this is pretty easy.
1806 */
1808 &abbr_offset, isdst))
1809 return abbr_offset;
1810
1811 /*
1812 * Else, break down the timestamp so we can use DetermineTimeZoneOffset.
1813 */
1814 if (timestamp2tm(ts, &tz, &tm, &fsec, NULL, tzp) != 0)
1815 ereport(ERROR,
1816 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1817 errmsg("timestamp out of range")));
1818
1819 zone_offset = DetermineTimeZoneOffset(&tm, tzp);
1820 *isdst = tm.tm_isdst;
1821 return zone_offset;
1822}
int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm *tm, fsec_t *fsec, const char **tzn, pg_tz *attimezone)
Definition: timestamp.c:1910
pg_time_t timestamptz_to_time_t(TimestampTz t)
Definition: timestamp.c:1842
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_abbrevs(), timestamptz_zone(), and timetz_zone().

◆ DetermineTimeZoneOffset()

◆ EncodeDateOnly()

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

Definition at line 4343 of file datetime.c.

4344{
4345 Assert(tm->tm_mon >= 1 && tm->tm_mon <= MONTHS_PER_YEAR);
4346
4347 switch (style)
4348 {
4349 case USE_ISO_DATES:
4350 case USE_XSD_DATES:
4351 /* compatible with ISO date formats */
4353 (tm->tm_year > 0) ? tm->tm_year : -(tm->tm_year - 1), 4);
4354 *str++ = '-';
4356 *str++ = '-';
4358 break;
4359
4360 case USE_SQL_DATES:
4361 /* compatible with Oracle/Ingres date formats */
4362 if (DateOrder == DATEORDER_DMY)
4363 {
4365 *str++ = '/';
4367 }
4368 else
4369 {
4371 *str++ = '/';
4373 }
4374 *str++ = '/';
4376 (tm->tm_year > 0) ? tm->tm_year : -(tm->tm_year - 1), 4);
4377 break;
4378
4379 case USE_GERMAN_DATES:
4380 /* German-style date format */
4382 *str++ = '.';
4384 *str++ = '.';
4386 (tm->tm_year > 0) ? tm->tm_year : -(tm->tm_year - 1), 4);
4387 break;
4388
4389 case USE_POSTGRES_DATES:
4390 default:
4391 /* traditional date-only style for Postgres */
4392 if (DateOrder == DATEORDER_DMY)
4393 {
4395 *str++ = '-';
4397 }
4398 else
4399 {
4401 *str++ = '-';
4403 }
4404 *str++ = '-';
4406 (tm->tm_year > 0) ? tm->tm_year : -(tm->tm_year - 1), 4);
4407 break;
4408 }
4409
4410 if (tm->tm_year <= 0)
4411 {
4412 memcpy(str, " BC", 3); /* Don't copy NUL */
4413 str += 3;
4414 }
4415 *str = '\0';
4416}
int DateOrder
Definition: globals.c:127
#define USE_SQL_DATES
Definition: miscadmin.h:238
#define USE_POSTGRES_DATES
Definition: miscadmin.h:236
#define USE_ISO_DATES
Definition: miscadmin.h:237
#define DATEORDER_DMY
Definition: miscadmin.h:244
#define USE_XSD_DATES
Definition: miscadmin.h:240
#define USE_GERMAN_DATES
Definition: miscadmin.h:239
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 4458 of file datetime.c.

4459{
4460 int day;
4461
4462 Assert(tm->tm_mon >= 1 && tm->tm_mon <= MONTHS_PER_YEAR);
4463
4464 /*
4465 * Negative tm_isdst means we have no valid time zone translation.
4466 */
4467 if (tm->tm_isdst < 0)
4468 print_tz = false;
4469
4470 switch (style)
4471 {
4472 case USE_ISO_DATES:
4473 case USE_XSD_DATES:
4474 /* Compatible with ISO-8601 date formats */
4476 (tm->tm_year > 0) ? tm->tm_year : -(tm->tm_year - 1), 4);
4477 *str++ = '-';
4479 *str++ = '-';
4481 *str++ = (style == USE_ISO_DATES) ? ' ' : 'T';
4483 *str++ = ':';
4485 *str++ = ':';
4486 str = AppendTimestampSeconds(str, tm, fsec);
4487 if (print_tz)
4488 str = EncodeTimezone(str, tz, style);
4489 break;
4490
4491 case USE_SQL_DATES:
4492 /* Compatible with Oracle/Ingres date formats */
4493 if (DateOrder == DATEORDER_DMY)
4494 {
4496 *str++ = '/';
4498 }
4499 else
4500 {
4502 *str++ = '/';
4504 }
4505 *str++ = '/';
4507 (tm->tm_year > 0) ? tm->tm_year : -(tm->tm_year - 1), 4);
4508 *str++ = ' ';
4510 *str++ = ':';
4512 *str++ = ':';
4513 str = AppendTimestampSeconds(str, tm, fsec);
4514
4515 /*
4516 * Note: the uses of %.*s in this function would be risky if the
4517 * timezone names ever contain non-ASCII characters, since we are
4518 * not being careful to do encoding-aware clipping. However, all
4519 * TZ abbreviations in the IANA database are plain ASCII.
4520 */
4521 if (print_tz)
4522 {
4523 if (tzn)
4524 {
4525 sprintf(str, " %.*s", MAXTZLEN, tzn);
4526 str += strlen(str);
4527 }
4528 else
4529 str = EncodeTimezone(str, tz, style);
4530 }
4531 break;
4532
4533 case USE_GERMAN_DATES:
4534 /* German variant on European style */
4536 *str++ = '.';
4538 *str++ = '.';
4540 (tm->tm_year > 0) ? tm->tm_year : -(tm->tm_year - 1), 4);
4541 *str++ = ' ';
4543 *str++ = ':';
4545 *str++ = ':';
4546 str = AppendTimestampSeconds(str, tm, fsec);
4547
4548 if (print_tz)
4549 {
4550 if (tzn)
4551 {
4552 sprintf(str, " %.*s", MAXTZLEN, tzn);
4553 str += strlen(str);
4554 }
4555 else
4556 str = EncodeTimezone(str, tz, style);
4557 }
4558 break;
4559
4560 case USE_POSTGRES_DATES:
4561 default:
4562 /* Backward-compatible with traditional Postgres abstime dates */
4563 day = date2j(tm->tm_year, tm->tm_mon, tm->tm_mday);
4564 tm->tm_wday = j2day(day);
4565 memcpy(str, days[tm->tm_wday], 3);
4566 str += 3;
4567 *str++ = ' ';
4568 if (DateOrder == DATEORDER_DMY)
4569 {
4571 *str++ = ' ';
4572 memcpy(str, months[tm->tm_mon - 1], 3);
4573 str += 3;
4574 }
4575 else
4576 {
4577 memcpy(str, months[tm->tm_mon - 1], 3);
4578 str += 3;
4579 *str++ = ' ';
4581 }
4582 *str++ = ' ';
4584 *str++ = ':';
4586 *str++ = ':';
4587 str = AppendTimestampSeconds(str, tm, fsec);
4588 *str++ = ' ';
4590 (tm->tm_year > 0) ? tm->tm_year : -(tm->tm_year - 1), 4);
4591
4592 if (print_tz)
4593 {
4594 if (tzn)
4595 {
4596 sprintf(str, " %.*s", MAXTZLEN, tzn);
4597 str += strlen(str);
4598 }
4599 else
4600 {
4601 /*
4602 * We have a time zone, but no string version. Use the
4603 * numeric form, but be sure to include a leading space to
4604 * avoid formatting something which would be rejected by
4605 * the date/time parser later. - thomas 2001-10-19
4606 */
4607 *str++ = ' ';
4608 str = EncodeTimezone(str, tz, style);
4609 }
4610 }
4611 break;
4612 }
4613
4614 if (tm->tm_year <= 0)
4615 {
4616 memcpy(str, " BC", 3); /* Don't copy NUL */
4617 str += 3;
4618 }
4619 *str = '\0';
4620}
static char * EncodeTimezone(char *str, int tz, int style)
Definition: datetime.c:4305
int j2day(int date)
Definition: datetime.c:354
const char *const months[]
Definition: datetime.c:81
static char * AppendTimestampSeconds(char *cp, struct pg_tm *tm, fsec_t fsec)
Definition: datetime.c:521
const char *const days[]
Definition: datetime.c:84
#define MAXTZLEN
Definition: miscadmin.h:264
#define sprintf
Definition: port.h:241

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

4702{
4703 char *cp = str;
4704 int year = itm->tm_year;
4705 int mon = itm->tm_mon;
4706 int64 mday = itm->tm_mday; /* tm_mday could be INT_MIN */
4707 int64 hour = itm->tm_hour;
4708 int min = itm->tm_min;
4709 int sec = itm->tm_sec;
4710 int fsec = itm->tm_usec;
4711 bool is_before = false;
4712 bool is_zero = true;
4713
4714 /*
4715 * The sign of year and month are guaranteed to match, since they are
4716 * stored internally as "month". But we'll need to check for is_before and
4717 * is_zero when determining the signs of day and hour/minute/seconds
4718 * fields.
4719 */
4720 switch (style)
4721 {
4722 /* SQL Standard interval format */
4724 {
4725 bool has_negative = year < 0 || mon < 0 ||
4726 mday < 0 || hour < 0 ||
4727 min < 0 || sec < 0 || fsec < 0;
4728 bool has_positive = year > 0 || mon > 0 ||
4729 mday > 0 || hour > 0 ||
4730 min > 0 || sec > 0 || fsec > 0;
4731 bool has_year_month = year != 0 || mon != 0;
4732 bool has_day_time = mday != 0 || hour != 0 ||
4733 min != 0 || sec != 0 || fsec != 0;
4734 bool has_day = mday != 0;
4735 bool sql_standard_value = !(has_negative && has_positive) &&
4736 !(has_year_month && has_day_time);
4737
4738 /*
4739 * SQL Standard wants only 1 "<sign>" preceding the whole
4740 * interval ... but can't do that if mixed signs.
4741 */
4742 if (has_negative && sql_standard_value)
4743 {
4744 *cp++ = '-';
4745 year = -year;
4746 mon = -mon;
4747 mday = -mday;
4748 hour = -hour;
4749 min = -min;
4750 sec = -sec;
4751 fsec = -fsec;
4752 }
4753
4754 if (!has_negative && !has_positive)
4755 {
4756 sprintf(cp, "0");
4757 }
4758 else if (!sql_standard_value)
4759 {
4760 /*
4761 * For non sql-standard interval values, force outputting
4762 * the signs to avoid ambiguities with intervals with
4763 * mixed sign components.
4764 */
4765 char year_sign = (year < 0 || mon < 0) ? '-' : '+';
4766 char day_sign = (mday < 0) ? '-' : '+';
4767 char sec_sign = (hour < 0 || min < 0 ||
4768 sec < 0 || fsec < 0) ? '-' : '+';
4769
4770 sprintf(cp, "%c%d-%d %c%" PRId64 " %c%" PRId64 ":%02d:",
4771 year_sign, abs(year), abs(mon),
4772 day_sign, i64abs(mday),
4773 sec_sign, i64abs(hour), abs(min));
4774 cp += strlen(cp);
4775 cp = AppendSeconds(cp, sec, fsec, MAX_INTERVAL_PRECISION, true);
4776 *cp = '\0';
4777 }
4778 else if (has_year_month)
4779 {
4780 sprintf(cp, "%d-%d", year, mon);
4781 }
4782 else if (has_day)
4783 {
4784 sprintf(cp, "%" PRId64 " %" PRId64 ":%02d:",
4785 mday, hour, min);
4786 cp += strlen(cp);
4787 cp = AppendSeconds(cp, sec, fsec, MAX_INTERVAL_PRECISION, true);
4788 *cp = '\0';
4789 }
4790 else
4791 {
4792 sprintf(cp, "%" PRId64 ":%02d:", hour, min);
4793 cp += strlen(cp);
4794 cp = AppendSeconds(cp, sec, fsec, MAX_INTERVAL_PRECISION, true);
4795 *cp = '\0';
4796 }
4797 }
4798 break;
4799
4800 /* ISO 8601 "time-intervals by duration only" */
4801 case INTSTYLE_ISO_8601:
4802 /* special-case zero to avoid printing nothing */
4803 if (year == 0 && mon == 0 && mday == 0 &&
4804 hour == 0 && min == 0 && sec == 0 && fsec == 0)
4805 {
4806 sprintf(cp, "PT0S");
4807 break;
4808 }
4809 *cp++ = 'P';
4810 cp = AddISO8601IntPart(cp, year, 'Y');
4811 cp = AddISO8601IntPart(cp, mon, 'M');
4812 cp = AddISO8601IntPart(cp, mday, 'D');
4813 if (hour != 0 || min != 0 || sec != 0 || fsec != 0)
4814 *cp++ = 'T';
4815 cp = AddISO8601IntPart(cp, hour, 'H');
4816 cp = AddISO8601IntPart(cp, min, 'M');
4817 if (sec != 0 || fsec != 0)
4818 {
4819 if (sec < 0 || fsec < 0)
4820 *cp++ = '-';
4821 cp = AppendSeconds(cp, sec, fsec, MAX_INTERVAL_PRECISION, false);
4822 *cp++ = 'S';
4823 *cp++ = '\0';
4824 }
4825 break;
4826
4827 /* Compatible with postgresql < 8.4 when DateStyle = 'iso' */
4828 case INTSTYLE_POSTGRES:
4829 cp = AddPostgresIntPart(cp, year, "year", &is_zero, &is_before);
4830
4831 /*
4832 * Ideally we should spell out "month" like we do for "year" and
4833 * "day". However, for backward compatibility, we can't easily
4834 * fix this. bjm 2011-05-24
4835 */
4836 cp = AddPostgresIntPart(cp, mon, "mon", &is_zero, &is_before);
4837 cp = AddPostgresIntPart(cp, mday, "day", &is_zero, &is_before);
4838 if (is_zero || hour != 0 || min != 0 || sec != 0 || fsec != 0)
4839 {
4840 bool minus = (hour < 0 || min < 0 || sec < 0 || fsec < 0);
4841
4842 sprintf(cp, "%s%s%02" PRId64 ":%02d:",
4843 is_zero ? "" : " ",
4844 (minus ? "-" : (is_before ? "+" : "")),
4845 i64abs(hour), abs(min));
4846 cp += strlen(cp);
4847 cp = AppendSeconds(cp, sec, fsec, MAX_INTERVAL_PRECISION, true);
4848 *cp = '\0';
4849 }
4850 break;
4851
4852 /* Compatible with postgresql < 8.4 when DateStyle != 'iso' */
4854 default:
4855 strcpy(cp, "@");
4856 cp++;
4857 cp = AddVerboseIntPart(cp, year, "year", &is_zero, &is_before);
4858 cp = AddVerboseIntPart(cp, mon, "mon", &is_zero, &is_before);
4859 cp = AddVerboseIntPart(cp, mday, "day", &is_zero, &is_before);
4860 cp = AddVerboseIntPart(cp, hour, "hour", &is_zero, &is_before);
4861 cp = AddVerboseIntPart(cp, min, "min", &is_zero, &is_before);
4862 if (sec != 0 || fsec != 0)
4863 {
4864 *cp++ = ' ';
4865 if (sec < 0 || (sec == 0 && fsec < 0))
4866 {
4867 if (is_zero)
4868 is_before = true;
4869 else if (!is_before)
4870 *cp++ = '-';
4871 }
4872 else if (is_before)
4873 *cp++ = '-';
4874 cp = AppendSeconds(cp, sec, fsec, MAX_INTERVAL_PRECISION, false);
4875 /* We output "ago", not negatives, so use abs(). */
4876 sprintf(cp, " sec%s",
4877 (abs(sec) != 1 || fsec != 0) ? "s" : "");
4878 is_zero = false;
4879 }
4880 /* identically zero? then put in a unitless zero... */
4881 if (is_zero)
4882 strcat(cp, " 0");
4883 if (is_before)
4884 strcat(cp, " ago");
4885 break;
4886 }
4887}
static char * AddPostgresIntPart(char *cp, int64 value, const char *units, bool *is_zero, bool *is_before)
Definition: datetime.c:4639
static char * AppendSeconds(char *cp, int sec, fsec_t fsec, int precision, bool fillzeros)
Definition: datetime.c:458
static char * AddISO8601IntPart(char *cp, int64 value, char units)
Definition: datetime.c:4629
static char * AddVerboseIntPart(char *cp, int64 value, const char *units, bool *is_zero, bool *is_before)
Definition: datetime.c:4662
#define MAX_INTERVAL_PRECISION
Definition: timestamp.h:93
#define INTSTYLE_POSTGRES_VERBOSE
Definition: miscadmin.h:258
#define INTSTYLE_ISO_8601
Definition: miscadmin.h:260
#define INTSTYLE_POSTGRES
Definition: miscadmin.h:257
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 1587 of file timestamp.c.

1588{
1589 if (TIMESTAMP_IS_NOBEGIN(dt))
1590 strcpy(str, EARLY);
1591 else if (TIMESTAMP_IS_NOEND(dt))
1592 strcpy(str, LATE);
1593 else /* shouldn't happen */
1594 elog(ERROR, "invalid argument for EncodeSpecialTimestamp");
1595}
#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 4428 of file datetime.c.

4429{
4431 *str++ = ':';
4433 *str++ = ':';
4434 str = AppendSeconds(str, tm->tm_sec, fsec, MAX_TIME_PRECISION, true);
4435 if (print_tz)
4436 str = EncodeTimezone(str, tz, style);
4437 *str = '\0';
4438}
#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 376 of file datetime.c.

377{
378 fsec_t fsec;
379
380 GetCurrentTimeUsec(tm, &fsec, NULL);
381}

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

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

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

5074{
5075 zoneabbrevtbl = tbl;
5076 /* reset tzabbrevcache, which may contain results from old table */
5077 memset(tzabbrevcache, 0, sizeof(tzabbrevcache));
5078}

References tzabbrevcache, and zoneabbrevtbl.

Referenced by assign_timezone_abbreviations().

◆ j2date()

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

Definition at line 321 of file datetime.c.

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

◆ j2day()

int j2day ( int  date)

Definition at line 354 of file datetime.c.

355{
356 date += 1;
357 date %= 7;
358 /* Cope if division truncates towards zero, as it probably does */
359 if (date < 0)
360 date += 7;
361
362 return date;
363} /* 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 764 of file datetime.c.

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

4957{
4958 FuncExpr *expr = castNode(FuncExpr, node);
4959 Node *ret = NULL;
4960 Node *typmod;
4961
4962 Assert(list_length(expr->args) >= 2);
4963
4964 typmod = (Node *) lsecond(expr->args);
4965
4966 if (IsA(typmod, Const) && !((Const *) typmod)->constisnull)
4967 {
4968 Node *source = (Node *) linitial(expr->args);
4969 int32 old_precis = exprTypmod(source);
4970 int32 new_precis = DatumGetInt32(((Const *) typmod)->constvalue);
4971
4972 if (new_precis < 0 || new_precis == max_precis ||
4973 (old_precis >= 0 && new_precis >= old_precis))
4974 ret = relabel_to_typmod(source, new_precis);
4975 }
4976
4977 return ret;
4978}
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:301
Node * relabel_to_typmod(Node *expr, int32 typmod)
Definition: nodeFuncs.c:689
#define IsA(nodeptr, _type_)
Definition: nodes.h:164
#define castNode(_type_, nodeptr)
Definition: nodes.h:182
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:207
List * args
Definition: primnodes.h:785
Definition: nodes.h:135

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

2554{
2555 if (fmask & DTK_M(YEAR))
2556 {
2557 if (isjulian)
2558 {
2559 /* tm_year is correct and should not be touched */
2560 }
2561 else if (bc)
2562 {
2563 /* there is no year zero in AD/BC notation */
2564 if (tm->tm_year <= 0)
2565 return DTERR_FIELD_OVERFLOW;
2566 /* internally, we represent 1 BC as year zero, 2 BC as -1, etc */
2567 tm->tm_year = -(tm->tm_year - 1);
2568 }
2569 else if (is2digits)
2570 {
2571 /* process 1 or 2-digit input as 1970-2069 AD, allow '0' and '00' */
2572 if (tm->tm_year < 0) /* just paranoia */
2573 return DTERR_FIELD_OVERFLOW;
2574 if (tm->tm_year < 70)
2575 tm->tm_year += 2000;
2576 else if (tm->tm_year < 100)
2577 tm->tm_year += 1900;
2578 }
2579 else
2580 {
2581 /* there is no year zero in AD/BC notation */
2582 if (tm->tm_year <= 0)
2583 return DTERR_FIELD_OVERFLOW;
2584 }
2585 }
2586
2587 /* now that we have correct year, decode DOY */
2588 if (fmask & DTK_M(DOY))
2589 {
2590 j2date(date2j(tm->tm_year, 1, 1) + tm->tm_yday - 1,
2591 &tm->tm_year, &tm->tm_mon, &tm->tm_mday);
2592 }
2593
2594 /* check for valid month */
2595 if (fmask & DTK_M(MONTH))
2596 {
2597 if (tm->tm_mon < 1 || tm->tm_mon > MONTHS_PER_YEAR)
2599 }
2600
2601 /* minimal check for valid day */
2602 if (fmask & DTK_M(DAY))
2603 {
2604 if (tm->tm_mday < 1 || tm->tm_mday > 31)
2606 }
2607
2608 if ((fmask & DTK_DATE_M) == DTK_DATE_M)
2609 {
2610 /*
2611 * Check for valid day of month, now that we know for sure the month
2612 * and year. Note we don't use MD_FIELD_OVERFLOW here, since it seems
2613 * unlikely that "Feb 29" is a YMD-order error.
2614 */
2615 if (tm->tm_mday > day_tab[isleap(tm->tm_year)][tm->tm_mon - 1])
2616 return DTERR_FIELD_OVERFLOW;
2617 }
2618
2619 return 0;
2620}
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.