PostgreSQL Source Code  git master
datetime.h File Reference
#include "nodes/nodes.h"
#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
 

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)
 

Typedefs

typedef struct TimeZoneAbbrevTable TimeZoneAbbrevTable
 
typedef struct DynamicZoneAbbrev DynamicZoneAbbrev
 

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)
 
int DecodeTimezone (char *str, int *tzp)
 
int DecodeTimeOnly (char **field, int *ftype, int nf, int *dtype, struct pg_tm *tm, fsec_t *fsec, int *tzp)
 
int DecodeInterval (char **field, int *ftype, int nf, int range, int *dtype, struct pg_tm *tm, fsec_t *fsec)
 
int DecodeISO8601Interval (char *str, int *dtype, struct pg_tm *tm, fsec_t *fsec)
 
void DateTimeParseError (int dterr, const char *str, const char *datatype) pg_attribute_noreturn()
 
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_tm *tm, fsec_t fsec, 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, char *lowtoken, int *offset, pg_tz **tz)
 
int DecodeSpecial (int field, char *lowtoken, int *val)
 
int DecodeUnits (int field, char *lowtoken, int *val)
 
int j2day (int jd)
 
NodeTemporalSimplify (int32 max_precis, Node *node)
 
bool CheckDateTokenTables (void)
 
TimeZoneAbbrevTableConvertTimeZoneAbbrevs (struct tzEntry *abbrevs, int n)
 
void InstallTimeZoneAbbrevs (TimeZoneAbbrevTable *tbl)
 
void AdjustTimestampForTypmod (Timestamp *time, int32 typmod)
 
bool AdjustTimestampForTypmodError (Timestamp *time, int32 typmod, bool *error)
 

Variables

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

Macro Definition Documentation

◆ ABS_AFTER

#define ABS_AFTER   21

Definition at line 113 of file datetime.h.

◆ ABS_BEFORE

#define ABS_BEFORE   20

Definition at line 112 of file datetime.h.

◆ AD

#define AD   0

Definition at line 76 of file datetime.h.

◆ ADBC

#define ADBC   18

Definition at line 109 of file datetime.h.

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

◆ AGO

#define AGO   19

Definition at line 111 of file datetime.h.

Referenced by DecodeInterval().

◆ AM

#define AM   0

Definition at line 72 of file datetime.h.

Referenced by DecodeDateTime(), and DecodeTimeOnly().

◆ AMPM

#define AMPM   9

Definition at line 100 of file datetime.h.

Referenced by DecodeDateTime(), and DecodeTimeOnly().

◆ BC

#define BC   1

Definition at line 77 of file datetime.h.

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

◆ CENTURY

#define CENTURY   26

Definition at line 120 of file datetime.h.

Referenced by DecodeInterval().

◆ DA_D

#define DA_D   "ad"

Definition at line 61 of file datetime.h.

◆ DAGO

#define DAGO   "ago"

Definition at line 36 of file datetime.h.

◆ DAY

◆ DB_C

#define DB_C   "bc"

Definition at line 62 of file datetime.h.

◆ DCENTURY

#define DCENTURY   "century"

Definition at line 59 of file datetime.h.

◆ DCURRENT

#define DCURRENT   "current"

Definition at line 37 of file datetime.h.

◆ DDAY

#define DDAY   "day"

Definition at line 53 of file datetime.h.

◆ DDECADE

#define DDECADE   "decade"

Definition at line 58 of file datetime.h.

◆ DECADE

#define DECADE   25

Definition at line 119 of file datetime.h.

Referenced by DecodeInterval().

◆ DHOUR

#define DHOUR   "hour"

Definition at line 52 of file datetime.h.

◆ DMICROSEC

#define DMICROSEC   "usecond"

Definition at line 48 of file datetime.h.

◆ DMILLENNIUM

#define DMILLENNIUM   "millennium"

Definition at line 60 of file datetime.h.

◆ DMILLISEC

#define DMILLISEC   "msecond"

Definition at line 49 of file datetime.h.

◆ DMINUTE

#define DMINUTE   "minute"

Definition at line 51 of file datetime.h.

◆ DMONTH

#define DMONTH   "month"

Definition at line 55 of file datetime.h.

◆ DOW

#define DOW   16

Definition at line 107 of file datetime.h.

Referenced by DecodeDateTime().

◆ DOY

#define DOY   15

Definition at line 106 of file datetime.h.

Referenced by DecodeDate(), DecodeNumber(), and ValidateDate().

◆ DQUARTER

#define DQUARTER   "quarter"

Definition at line 56 of file datetime.h.

◆ DSECOND

#define DSECOND   "second"

Definition at line 50 of file datetime.h.

◆ DTERR_BAD_FORMAT

◆ DTERR_FIELD_OVERFLOW

◆ DTERR_INTERVAL_OVERFLOW

#define DTERR_INTERVAL_OVERFLOW   (-4)

Definition at line 283 of file datetime.h.

Referenced by DateTimeParseError(), and interval_in().

◆ DTERR_MD_FIELD_OVERFLOW

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

Definition at line 282 of file datetime.h.

Referenced by DateTimeParseError(), and ValidateDate().

◆ DTERR_TZDISP_OVERFLOW

#define DTERR_TZDISP_OVERFLOW   (-5)

◆ DTIMEZONE

#define DTIMEZONE   "timezone"

Definition at line 63 of file datetime.h.

◆ DTK_AGO

#define DTK_AGO   5

Definition at line 148 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 191 of file datetime.h.

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

◆ DTK_CENTURY

◆ DTK_DATE

◆ DTK_DATE_M

◆ DTK_DAY

◆ DTK_DECADE

◆ DTK_DELTA

#define DTK_DELTA   17

◆ DTK_DOW

#define DTK_DOW   32

◆ DTK_DOY

#define DTK_DOY   33

◆ DTK_EARLY

#define DTK_EARLY   9

Definition at line 151 of file datetime.h.

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

◆ DTK_EPOCH

◆ DTK_HOUR

◆ DTK_ISODOW

#define DTK_ISODOW   37

◆ DTK_ISOYEAR

#define DTK_ISOYEAR   36

◆ DTK_JULIAN

◆ DTK_LATE

#define DTK_LATE   10

Definition at line 152 of file datetime.h.

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

◆ DTK_M

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

◆ DTK_MICROSEC

◆ DTK_MILLENNIUM

◆ DTK_MILLISEC

◆ DTK_MINUTE

◆ DTK_MONTH

◆ DTK_NOW

#define DTK_NOW   12

Definition at line 154 of file datetime.h.

Referenced by DecodeDateTime(), and DecodeTimeOnly().

◆ DTK_NUMBER

#define DTK_NUMBER   0

Definition at line 142 of file datetime.h.

Referenced by DecodeDateTime(), DecodeInterval(), DecodeTimeOnly(), and ParseDateTime().

◆ DTK_QUARTER

◆ DTK_SECOND

◆ DTK_SPECIAL

#define DTK_SPECIAL   6

Definition at line 150 of file datetime.h.

Referenced by DecodeDateTime(), DecodeInterval(), DecodeTimeOnly(), and ParseDateTime().

◆ DTK_STRING

#define DTK_STRING   1

Definition at line 143 of file datetime.h.

Referenced by DecodeDateTime(), DecodeInterval(), DecodeTimeOnly(), and ParseDateTime().

◆ DTK_TIME

#define DTK_TIME   3

◆ DTK_TIME_M

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

◆ DTK_TODAY

#define DTK_TODAY   14

Definition at line 156 of file datetime.h.

Referenced by DecodeDateTime().

◆ DTK_TOMORROW

#define DTK_TOMORROW   15

Definition at line 157 of file datetime.h.

Referenced by DecodeDateTime().

◆ DTK_TZ

◆ DTK_TZ_HOUR

#define DTK_TZ_HOUR   34

◆ DTK_TZ_MINUTE

#define DTK_TZ_MINUTE   35

◆ DTK_WEEK

◆ DTK_YEAR

◆ DTK_YESTERDAY

#define DTK_YESTERDAY   13

Definition at line 155 of file datetime.h.

Referenced by DecodeDateTime().

◆ DTK_ZULU

#define DTK_ZULU   16

Definition at line 158 of file datetime.h.

Referenced by DecodeDateTime(), and DecodeTimeOnly().

◆ DTZ

◆ DTZMOD

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

Definition at line 123 of file datetime.h.

Referenced by DecodeDateTime(), and DecodeTimeOnly().

◆ DWEEK

#define DWEEK   "week"

Definition at line 54 of file datetime.h.

◆ DYEAR

#define DYEAR   "year"

Definition at line 57 of file datetime.h.

◆ DYNTZ

◆ EARLY

#define EARLY   "-infinity"

Definition at line 40 of file datetime.h.

Referenced by EncodeSpecialDate(), and EncodeSpecialTimestamp().

◆ EPOCH

#define EPOCH   "epoch"

Definition at line 38 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 238 of file datetime.h.

◆ HOUR

◆ HR24

#define HR24   2

Definition at line 74 of file datetime.h.

Referenced by DecodeDateTime(), and DecodeTimeOnly().

◆ IGNORE_DTF

#define IGNORE_DTF   8

Definition at line 99 of file datetime.h.

Referenced by DecodeDate(), DecodeDateTime(), DecodeInterval(), and DecodeTimeOnly().

◆ INVALID

#define INVALID   "invalid"

Definition at line 39 of file datetime.h.

◆ isleap

◆ ISODATE

#define ISODATE   22

Definition at line 115 of file datetime.h.

◆ ISOTIME

#define ISOTIME   23

Definition at line 116 of file datetime.h.

Referenced by DecodeDateTime(), and DecodeTimeOnly().

◆ JULIAN

#define JULIAN   4

Definition at line 95 of file datetime.h.

◆ LATE

#define LATE   "infinity"

Definition at line 41 of file datetime.h.

Referenced by EncodeSpecialDate(), and EncodeSpecialTimestamp().

◆ MAXDATEFIELDS

◆ MAXDATELEN

◆ MICROSECOND

#define MICROSECOND   14

Definition at line 105 of file datetime.h.

Referenced by DecodeInterval().

◆ MILLENNIUM

#define MILLENNIUM   27

Definition at line 121 of file datetime.h.

Referenced by DecodeInterval().

◆ MILLISECOND

#define MILLISECOND   13

Definition at line 104 of file datetime.h.

Referenced by DecodeInterval().

◆ MINUTE

◆ MONTH

◆ NOW

#define NOW   "now"

Definition at line 42 of file datetime.h.

◆ PM

#define PM   1

Definition at line 73 of file datetime.h.

Referenced by DecodeDateTime(), and DecodeTimeOnly().

◆ RESERV

◆ SECOND

◆ TMODULO

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

Definition at line 249 of file datetime.h.

Referenced by interval_justify_hours(), interval_justify_interval(), and timestamp2tm().

◆ TODAY

#define TODAY   "today"

Definition at line 43 of file datetime.h.

◆ TOKMAXLEN

◆ TOMORROW

#define TOMORROW   "tomorrow"

Definition at line 44 of file datetime.h.

◆ TZ

◆ UNITS

◆ UNKNOWN_FIELD

◆ WEEK

#define WEEK   24

Definition at line 118 of file datetime.h.

Referenced by DecodeInterval().

◆ YEAR

◆ YESTERDAY

#define YESTERDAY   "yesterday"

Definition at line 45 of file datetime.h.

◆ ZULU

#define ZULU   "zulu"

Definition at line 46 of file datetime.h.

Typedef Documentation

◆ DynamicZoneAbbrev

◆ TimeZoneAbbrevTable

Function Documentation

◆ AdjustTimestampForTypmod()

void AdjustTimestampForTypmod ( Timestamp time,
int32  typmod 
)

Definition at line 394 of file timestamp.c.

References AdjustTimestampForTypmodError().

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

395 {
396  (void) AdjustTimestampForTypmodError(time, typmod, NULL);
397 }
bool AdjustTimestampForTypmodError(Timestamp *time, int32 typmod, bool *error)
Definition: timestamp.c:339

◆ AdjustTimestampForTypmodError()

bool AdjustTimestampForTypmodError ( Timestamp time,
int32  typmod,
bool error 
)

Definition at line 339 of file timestamp.c.

References ereport, errcode(), errmsg(), ERROR, MAX_TIMESTAMP_PRECISION, and TIMESTAMP_NOT_FINITE.

Referenced by AdjustTimestampForTypmod().

340 {
341  static const int64 TimestampScales[MAX_TIMESTAMP_PRECISION + 1] = {
342  INT64CONST(1000000),
343  INT64CONST(100000),
344  INT64CONST(10000),
345  INT64CONST(1000),
346  INT64CONST(100),
347  INT64CONST(10),
348  INT64CONST(1)
349  };
350 
351  static const int64 TimestampOffsets[MAX_TIMESTAMP_PRECISION + 1] = {
352  INT64CONST(500000),
353  INT64CONST(50000),
354  INT64CONST(5000),
355  INT64CONST(500),
356  INT64CONST(50),
357  INT64CONST(5),
358  INT64CONST(0)
359  };
360 
361  if (!TIMESTAMP_NOT_FINITE(*time)
362  && (typmod != -1) && (typmod != MAX_TIMESTAMP_PRECISION))
363  {
364  if (typmod < 0 || typmod > MAX_TIMESTAMP_PRECISION)
365  {
366  if (error)
367  {
368  *error = true;
369  return false;
370  }
371 
372  ereport(ERROR,
373  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
374  errmsg("timestamp(%d) precision must be between %d and %d",
375  typmod, 0, MAX_TIMESTAMP_PRECISION)));
376  }
377 
378  if (*time >= INT64CONST(0))
379  {
380  *time = ((*time + TimestampOffsets[typmod]) / TimestampScales[typmod]) *
381  TimestampScales[typmod];
382  }
383  else
384  {
385  *time = -((((-*time) + TimestampOffsets[typmod]) / TimestampScales[typmod])
386  * TimestampScales[typmod]);
387  }
388  }
389 
390  return true;
391 }
static void error(void)
Definition: sql-dyntest.c:147
int errcode(int sqlerrcode)
Definition: elog.c:698
#define TIMESTAMP_NOT_FINITE(j)
Definition: timestamp.h:122
#define ERROR
Definition: elog.h:46
#define MAX_TIMESTAMP_PRECISION
Definition: timestamp.h:53
#define ereport(elevel,...)
Definition: elog.h:157
int errmsg(const char *fmt,...)
Definition: elog.c:909

◆ CheckDateTokenTables()

bool CheckDateTokenTables ( void  )

Definition at line 4465 of file datetime.c.

4466 {
4467  bool ok = true;
4468 
4469  Assert(UNIX_EPOCH_JDATE == date2j(1970, 1, 1));
4470  Assert(POSTGRES_EPOCH_JDATE == date2j(2000, 1, 1));
4471 
4472  ok &= CheckDateTokenTable("datetktbl", datetktbl, szdatetktbl);
4473  ok &= CheckDateTokenTable("deltatktbl", deltatktbl, szdeltatktbl);
4474  return ok;
4475 }
static const datetkn datetktbl[]
Definition: datetime.c:89
static const int szdeltatktbl
Definition: datetime.c:235
static bool CheckDateTokenTable(const char *tablename, const datetkn *base, int nel)
Definition: datetime.c:4433
static const datetkn deltatktbl[]
Definition: datetime.c:170
int date2j(int y, int m, int d)
Definition: datetime.c:269
#define Assert(condition)
Definition: c.h:804
static const int szdatetktbl
Definition: datetime.c:164
#define UNIX_EPOCH_JDATE
Definition: timestamp.h:162
#define POSTGRES_EPOCH_JDATE
Definition: timestamp.h:163

◆ ConvertTimeZoneAbbrevs()

TimeZoneAbbrevTable* ConvertTimeZoneAbbrevs ( struct tzEntry abbrevs,
int  n 
)

Definition at line 4527 of file datetime.c.

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

Referenced by load_tzoffsets().

4528 {
4529  TimeZoneAbbrevTable *tbl;
4530  Size tbl_size;
4531  int i;
4532 
4533  /* Space for fixed fields and datetkn array */
4534  tbl_size = offsetof(TimeZoneAbbrevTable, abbrevs) +
4535  n * sizeof(datetkn);
4536  tbl_size = MAXALIGN(tbl_size);
4537  /* Count up space for dynamic abbreviations */
4538  for (i = 0; i < n; i++)
4539  {
4540  struct tzEntry *abbr = abbrevs + i;
4541 
4542  if (abbr->zone != NULL)
4543  {
4544  Size dsize;
4545 
4546  dsize = offsetof(DynamicZoneAbbrev, zone) +
4547  strlen(abbr->zone) + 1;
4548  tbl_size += MAXALIGN(dsize);
4549  }
4550  }
4551 
4552  /* Alloc the result ... */
4553  tbl = malloc(tbl_size);
4554  if (!tbl)
4555  return NULL;
4556 
4557  /* ... and fill it in */
4558  tbl->tblsize = tbl_size;
4559  tbl->numabbrevs = n;
4560  /* in this loop, tbl_size reprises the space calculation above */
4561  tbl_size = offsetof(TimeZoneAbbrevTable, abbrevs) +
4562  n * sizeof(datetkn);
4563  tbl_size = MAXALIGN(tbl_size);
4564  for (i = 0; i < n; i++)
4565  {
4566  struct tzEntry *abbr = abbrevs + i;
4567  datetkn *dtoken = tbl->abbrevs + i;
4568 
4569  /* use strlcpy to truncate name if necessary */
4570  strlcpy(dtoken->token, abbr->abbrev, TOKMAXLEN + 1);
4571  if (abbr->zone != NULL)
4572  {
4573  /* Allocate a DynamicZoneAbbrev for this abbreviation */
4574  DynamicZoneAbbrev *dtza;
4575  Size dsize;
4576 
4577  dtza = (DynamicZoneAbbrev *) ((char *) tbl + tbl_size);
4578  dtza->tz = NULL;
4579  strcpy(dtza->zone, abbr->zone);
4580 
4581  dtoken->type = DYNTZ;
4582  /* value is offset from table start to DynamicZoneAbbrev */
4583  dtoken->value = (int32) tbl_size;
4584 
4585  dsize = offsetof(DynamicZoneAbbrev, zone) +
4586  strlen(abbr->zone) + 1;
4587  tbl_size += MAXALIGN(dsize);
4588  }
4589  else
4590  {
4591  dtoken->type = abbr->is_dst ? DTZ : TZ;
4592  dtoken->value = abbr->offset;
4593  }
4594  }
4595 
4596  /* Assert the two loops above agreed on size calculations */
4597  Assert(tbl->tblsize == tbl_size);
4598 
4599  /* Check the ordering, if testing */
4600  Assert(CheckDateTokenTable("timezone abbreviations", tbl->abbrevs, n));
4601 
4602  return tbl;
4603 }
int offset
Definition: tzparser.h:29
#define TZ
Definition: datetime.h:96
int32 value
Definition: datetime.h:212
signed int int32
Definition: c.h:429
char * zone
Definition: tzparser.h:27
bool is_dst
Definition: tzparser.h:30
#define malloc(a)
Definition: header.h:50
char token[TOKMAXLEN+1]
Definition: datetime.h:210
static bool CheckDateTokenTable(const char *tablename, const datetkn *base, int nel)
Definition: datetime.c:4433
datetkn abbrevs[FLEXIBLE_ARRAY_MEMBER]
Definition: datetime.h:220
char * abbrev
Definition: tzparser.h:26
char zone[FLEXIBLE_ARRAY_MEMBER]
Definition: datetime.h:228
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
char type
Definition: datetime.h:211
#define Assert(condition)
Definition: c.h:804
size_t Size
Definition: c.h:540
#define MAXALIGN(LEN)
Definition: c.h:757
#define DTZ
Definition: datetime.h:97
Definition: zic.c:93
#define DYNTZ
Definition: datetime.h:98
int i
#define TOKMAXLEN
Definition: datetime.h:205
#define offsetof(type, field)
Definition: c.h:727

◆ date2j()

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

Definition at line 269 of file datetime.c.

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

◆ DateTimeParseError()

void DateTimeParseError ( int  dterr,
const char *  str,
const char *  datatype 
)

Definition at line 3764 of file datetime.c.

References DTERR_BAD_FORMAT, DTERR_FIELD_OVERFLOW, DTERR_INTERVAL_OVERFLOW, DTERR_MD_FIELD_OVERFLOW, DTERR_TZDISP_OVERFLOW, ereport, errcode(), errhint(), errmsg(), and ERROR.

Referenced by date_in(), do_to_timestamp(), interval_in(), parse_datetime(), time_in(), timestamp_in(), timestamptz_in(), timetz_in(), and to_timestamp().

3765 {
3766  switch (dterr)
3767  {
3768  case DTERR_FIELD_OVERFLOW:
3769  ereport(ERROR,
3770  (errcode(ERRCODE_DATETIME_FIELD_OVERFLOW),
3771  errmsg("date/time field value out of range: \"%s\"",
3772  str)));
3773  break;
3775  /* <nanny>same as above, but add hint about DateStyle</nanny> */
3776  ereport(ERROR,
3777  (errcode(ERRCODE_DATETIME_FIELD_OVERFLOW),
3778  errmsg("date/time field value out of range: \"%s\"",
3779  str),
3780  errhint("Perhaps you need a different \"datestyle\" setting.")));
3781  break;
3783  ereport(ERROR,
3784  (errcode(ERRCODE_INTERVAL_FIELD_OVERFLOW),
3785  errmsg("interval field value out of range: \"%s\"",
3786  str)));
3787  break;
3788  case DTERR_TZDISP_OVERFLOW:
3789  ereport(ERROR,
3790  (errcode(ERRCODE_INVALID_TIME_ZONE_DISPLACEMENT_VALUE),
3791  errmsg("time zone displacement out of range: \"%s\"",
3792  str)));
3793  break;
3794  case DTERR_BAD_FORMAT:
3795  default:
3796  ereport(ERROR,
3797  (errcode(ERRCODE_INVALID_DATETIME_FORMAT),
3798  errmsg("invalid input syntax for type %s: \"%s\"",
3799  datatype, str)));
3800  break;
3801  }
3802 }
#define DTERR_BAD_FORMAT
Definition: datetime.h:280
int errhint(const char *fmt,...)
Definition: elog.c:1156
int errcode(int sqlerrcode)
Definition: elog.c:698
#define ERROR
Definition: elog.h:46
#define DTERR_FIELD_OVERFLOW
Definition: datetime.h:281
#define DTERR_INTERVAL_OVERFLOW
Definition: datetime.h:283
#define DTERR_TZDISP_OVERFLOW
Definition: datetime.h:284
#define ereport(elevel,...)
Definition: elog.h:157
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define DTERR_MD_FIELD_OVERFLOW
Definition: datetime.h:282

◆ DecodeDateTime()

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

Definition at line 803 of file datetime.c.

References ADBC, AM, AMPM, BC, date2j(), DAY, DecodeDate(), DecodeNumber(), DecodeNumberField(), DecodeSpecial(), DecodeTime(), DecodeTimezone(), DecodeTimezoneAbbrev(), DetermineTimeZoneAbbrevOffset(), DetermineTimeZoneOffset(), DOW, dt2time(), DTERR_BAD_FORMAT, DTERR_FIELD_OVERFLOW, DTK_ALL_SECS_M, DTK_DATE, DTK_DATE_M, DTK_DAY, DTK_HOUR, DTK_JULIAN, DTK_M, DTK_MINUTE, DTK_MONTH, DTK_NOW, DTK_NUMBER, DTK_SECOND, DTK_SPECIAL, DTK_STRING, DTK_TIME, DTK_TIME_M, DTK_TODAY, DTK_TOMORROW, DTK_TZ, DTK_YEAR, DTK_YESTERDAY, DTK_ZULU, DTZ, DTZMOD, DYNTZ, ereport, errcode(), errmsg(), ERROR, GetCurrentDateTime(), GetCurrentTimeUsec(), HOUR, HOURS_PER_DAY, HR24, i, IGNORE_DTF, INTERVAL_FULL_RANGE, ISOTIME, j2date(), MINUTE, MONTH, ParseFractionalSecond(), pg_tzset(), PM, RESERV, SECOND, session_timezone, strtoint(), time_overflows(), 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, generate_unaccent_rules::type, TZ, UNITS, UNKNOWN_FIELD, USECS_PER_DAY, val, ValidateDate(), and YEAR.

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

805 {
806  int fmask = 0,
807  tmask,
808  type;
809  int ptype = 0; /* "prefix type" for ISO y2001m02d04 format */
810  int i;
811  int val;
812  int dterr;
813  int mer = HR24;
814  bool haveTextMonth = false;
815  bool isjulian = false;
816  bool is2digits = false;
817  bool bc = false;
818  pg_tz *namedTz = NULL;
819  pg_tz *abbrevTz = NULL;
820  pg_tz *valtz;
821  char *abbrev = NULL;
822  struct pg_tm cur_tm;
823 
824  /*
825  * We'll insist on at least all of the date fields, but initialize the
826  * remaining fields in case they are not set later...
827  */
828  *dtype = DTK_DATE;
829  tm->tm_hour = 0;
830  tm->tm_min = 0;
831  tm->tm_sec = 0;
832  *fsec = 0;
833  /* don't know daylight savings time status apriori */
834  tm->tm_isdst = -1;
835  if (tzp != NULL)
836  *tzp = 0;
837 
838  for (i = 0; i < nf; i++)
839  {
840  switch (ftype[i])
841  {
842  case DTK_DATE:
843 
844  /*
845  * Integral julian day with attached time zone? All other
846  * forms with JD will be separated into distinct fields, so we
847  * handle just this case here.
848  */
849  if (ptype == DTK_JULIAN)
850  {
851  char *cp;
852  int val;
853 
854  if (tzp == NULL)
855  return DTERR_BAD_FORMAT;
856 
857  errno = 0;
858  val = strtoint(field[i], &cp, 10);
859  if (errno == ERANGE || val < 0)
860  return DTERR_FIELD_OVERFLOW;
861 
862  j2date(val, &tm->tm_year, &tm->tm_mon, &tm->tm_mday);
863  isjulian = true;
864 
865  /* Get the time zone from the end of the string */
866  dterr = DecodeTimezone(cp, tzp);
867  if (dterr)
868  return dterr;
869 
870  tmask = DTK_DATE_M | DTK_TIME_M | DTK_M(TZ);
871  ptype = 0;
872  break;
873  }
874 
875  /*
876  * Already have a date? Then this might be a time zone name
877  * with embedded punctuation (e.g. "America/New_York") or a
878  * run-together time with trailing time zone (e.g. hhmmss-zz).
879  * - thomas 2001-12-25
880  *
881  * We consider it a time zone if we already have month & day.
882  * This is to allow the form "mmm dd hhmmss tz year", which
883  * we've historically accepted.
884  */
885  else if (ptype != 0 ||
886  ((fmask & (DTK_M(MONTH) | DTK_M(DAY))) ==
887  (DTK_M(MONTH) | DTK_M(DAY))))
888  {
889  /* No time zone accepted? Then quit... */
890  if (tzp == NULL)
891  return DTERR_BAD_FORMAT;
892 
893  if (isdigit((unsigned char) *field[i]) || ptype != 0)
894  {
895  char *cp;
896 
897  if (ptype != 0)
898  {
899  /* Sanity check; should not fail this test */
900  if (ptype != DTK_TIME)
901  return DTERR_BAD_FORMAT;
902  ptype = 0;
903  }
904 
905  /*
906  * Starts with a digit but we already have a time
907  * field? Then we are in trouble with a date and time
908  * already...
909  */
910  if ((fmask & DTK_TIME_M) == DTK_TIME_M)
911  return DTERR_BAD_FORMAT;
912 
913  if ((cp = strchr(field[i], '-')) == NULL)
914  return DTERR_BAD_FORMAT;
915 
916  /* Get the time zone from the end of the string */
917  dterr = DecodeTimezone(cp, tzp);
918  if (dterr)
919  return dterr;
920  *cp = '\0';
921 
922  /*
923  * Then read the rest of the field as a concatenated
924  * time
925  */
926  dterr = DecodeNumberField(strlen(field[i]), field[i],
927  fmask,
928  &tmask, tm,
929  fsec, &is2digits);
930  if (dterr < 0)
931  return dterr;
932 
933  /*
934  * modify tmask after returning from
935  * DecodeNumberField()
936  */
937  tmask |= DTK_M(TZ);
938  }
939  else
940  {
941  namedTz = pg_tzset(field[i]);
942  if (!namedTz)
943  {
944  /*
945  * We should return an error code instead of
946  * ereport'ing directly, but then there is no way
947  * to report the bad time zone name.
948  */
949  ereport(ERROR,
950  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
951  errmsg("time zone \"%s\" not recognized",
952  field[i])));
953  }
954  /* we'll apply the zone setting below */
955  tmask = DTK_M(TZ);
956  }
957  }
958  else
959  {
960  dterr = DecodeDate(field[i], fmask,
961  &tmask, &is2digits, tm);
962  if (dterr)
963  return dterr;
964  }
965  break;
966 
967  case DTK_TIME:
968 
969  /*
970  * This might be an ISO time following a "t" field.
971  */
972  if (ptype != 0)
973  {
974  /* Sanity check; should not fail this test */
975  if (ptype != DTK_TIME)
976  return DTERR_BAD_FORMAT;
977  ptype = 0;
978  }
979  dterr = DecodeTime(field[i], fmask, INTERVAL_FULL_RANGE,
980  &tmask, tm, fsec);
981  if (dterr)
982  return dterr;
983 
984  /* check for time overflow */
985  if (time_overflows(tm->tm_hour, tm->tm_min, tm->tm_sec,
986  *fsec))
987  return DTERR_FIELD_OVERFLOW;
988  break;
989 
990  case DTK_TZ:
991  {
992  int tz;
993 
994  if (tzp == NULL)
995  return DTERR_BAD_FORMAT;
996 
997  dterr = DecodeTimezone(field[i], &tz);
998  if (dterr)
999  return dterr;
1000  *tzp = tz;
1001  tmask = DTK_M(TZ);
1002  }
1003  break;
1004 
1005  case DTK_NUMBER:
1006 
1007  /*
1008  * Was this an "ISO date" with embedded field labels? An
1009  * example is "y2001m02d04" - thomas 2001-02-04
1010  */
1011  if (ptype != 0)
1012  {
1013  char *cp;
1014  int val;
1015 
1016  errno = 0;
1017  val = strtoint(field[i], &cp, 10);
1018  if (errno == ERANGE)
1019  return DTERR_FIELD_OVERFLOW;
1020 
1021  /*
1022  * only a few kinds are allowed to have an embedded
1023  * decimal
1024  */
1025  if (*cp == '.')
1026  switch (ptype)
1027  {
1028  case DTK_JULIAN:
1029  case DTK_TIME:
1030  case DTK_SECOND:
1031  break;
1032  default:
1033  return DTERR_BAD_FORMAT;
1034  break;
1035  }
1036  else if (*cp != '\0')
1037  return DTERR_BAD_FORMAT;
1038 
1039  switch (ptype)
1040  {
1041  case DTK_YEAR:
1042  tm->tm_year = val;
1043  tmask = DTK_M(YEAR);
1044  break;
1045 
1046  case DTK_MONTH:
1047 
1048  /*
1049  * already have a month and hour? then assume
1050  * minutes
1051  */
1052  if ((fmask & DTK_M(MONTH)) != 0 &&
1053  (fmask & DTK_M(HOUR)) != 0)
1054  {
1055  tm->tm_min = val;
1056  tmask = DTK_M(MINUTE);
1057  }
1058  else
1059  {
1060  tm->tm_mon = val;
1061  tmask = DTK_M(MONTH);
1062  }
1063  break;
1064 
1065  case DTK_DAY:
1066  tm->tm_mday = val;
1067  tmask = DTK_M(DAY);
1068  break;
1069 
1070  case DTK_HOUR:
1071  tm->tm_hour = val;
1072  tmask = DTK_M(HOUR);
1073  break;
1074 
1075  case DTK_MINUTE:
1076  tm->tm_min = val;
1077  tmask = DTK_M(MINUTE);
1078  break;
1079 
1080  case DTK_SECOND:
1081  tm->tm_sec = val;
1082  tmask = DTK_M(SECOND);
1083  if (*cp == '.')
1084  {
1085  dterr = ParseFractionalSecond(cp, fsec);
1086  if (dterr)
1087  return dterr;
1088  tmask = DTK_ALL_SECS_M;
1089  }
1090  break;
1091 
1092  case DTK_TZ:
1093  tmask = DTK_M(TZ);
1094  dterr = DecodeTimezone(field[i], tzp);
1095  if (dterr)
1096  return dterr;
1097  break;
1098 
1099  case DTK_JULIAN:
1100  /* previous field was a label for "julian date" */
1101  if (val < 0)
1102  return DTERR_FIELD_OVERFLOW;
1103  tmask = DTK_DATE_M;
1104  j2date(val, &tm->tm_year, &tm->tm_mon, &tm->tm_mday);
1105  isjulian = true;
1106 
1107  /* fractional Julian Day? */
1108  if (*cp == '.')
1109  {
1110  double time;
1111 
1112  errno = 0;
1113  time = strtod(cp, &cp);
1114  if (*cp != '\0' || errno != 0)
1115  return DTERR_BAD_FORMAT;
1116  time *= USECS_PER_DAY;
1117  dt2time(time,
1118  &tm->tm_hour, &tm->tm_min,
1119  &tm->tm_sec, fsec);
1120  tmask |= DTK_TIME_M;
1121  }
1122  break;
1123 
1124  case DTK_TIME:
1125  /* previous field was "t" for ISO time */
1126  dterr = DecodeNumberField(strlen(field[i]), field[i],
1127  (fmask | DTK_DATE_M),
1128  &tmask, tm,
1129  fsec, &is2digits);
1130  if (dterr < 0)
1131  return dterr;
1132  if (tmask != DTK_TIME_M)
1133  return DTERR_BAD_FORMAT;
1134  break;
1135 
1136  default:
1137  return DTERR_BAD_FORMAT;
1138  break;
1139  }
1140 
1141  ptype = 0;
1142  *dtype = DTK_DATE;
1143  }
1144  else
1145  {
1146  char *cp;
1147  int flen;
1148 
1149  flen = strlen(field[i]);
1150  cp = strchr(field[i], '.');
1151 
1152  /* Embedded decimal and no date yet? */
1153  if (cp != NULL && !(fmask & DTK_DATE_M))
1154  {
1155  dterr = DecodeDate(field[i], fmask,
1156  &tmask, &is2digits, tm);
1157  if (dterr)
1158  return dterr;
1159  }
1160  /* embedded decimal and several digits before? */
1161  else if (cp != NULL && flen - strlen(cp) > 2)
1162  {
1163  /*
1164  * Interpret as a concatenated date or time Set the
1165  * type field to allow decoding other fields later.
1166  * Example: 20011223 or 040506
1167  */
1168  dterr = DecodeNumberField(flen, field[i], fmask,
1169  &tmask, tm,
1170  fsec, &is2digits);
1171  if (dterr < 0)
1172  return dterr;
1173  }
1174 
1175  /*
1176  * Is this a YMD or HMS specification, or a year number?
1177  * YMD and HMS are required to be six digits or more, so
1178  * if it is 5 digits, it is a year. If it is six or more
1179  * digits, we assume it is YMD or HMS unless no date and
1180  * no time values have been specified. This forces 6+
1181  * digit years to be at the end of the string, or to use
1182  * the ISO date specification.
1183  */
1184  else if (flen >= 6 && (!(fmask & DTK_DATE_M) ||
1185  !(fmask & DTK_TIME_M)))
1186  {
1187  dterr = DecodeNumberField(flen, field[i], fmask,
1188  &tmask, tm,
1189  fsec, &is2digits);
1190  if (dterr < 0)
1191  return dterr;
1192  }
1193  /* otherwise it is a single date/time field... */
1194  else
1195  {
1196  dterr = DecodeNumber(flen, field[i],
1197  haveTextMonth, fmask,
1198  &tmask, tm,
1199  fsec, &is2digits);
1200  if (dterr)
1201  return dterr;
1202  }
1203  }
1204  break;
1205 
1206  case DTK_STRING:
1207  case DTK_SPECIAL:
1208  /* timezone abbrevs take precedence over built-in tokens */
1209  type = DecodeTimezoneAbbrev(i, field[i], &val, &valtz);
1210  if (type == UNKNOWN_FIELD)
1211  type = DecodeSpecial(i, field[i], &val);
1212  if (type == IGNORE_DTF)
1213  continue;
1214 
1215  tmask = DTK_M(type);
1216  switch (type)
1217  {
1218  case RESERV:
1219  switch (val)
1220  {
1221  case DTK_NOW:
1222  tmask = (DTK_DATE_M | DTK_TIME_M | DTK_M(TZ));
1223  *dtype = DTK_DATE;
1224  GetCurrentTimeUsec(tm, fsec, tzp);
1225  break;
1226 
1227  case DTK_YESTERDAY:
1228  tmask = DTK_DATE_M;
1229  *dtype = DTK_DATE;
1230  GetCurrentDateTime(&cur_tm);
1231  j2date(date2j(cur_tm.tm_year, cur_tm.tm_mon, cur_tm.tm_mday) - 1,
1232  &tm->tm_year, &tm->tm_mon, &tm->tm_mday);
1233  break;
1234 
1235  case DTK_TODAY:
1236  tmask = DTK_DATE_M;
1237  *dtype = DTK_DATE;
1238  GetCurrentDateTime(&cur_tm);
1239  tm->tm_year = cur_tm.tm_year;
1240  tm->tm_mon = cur_tm.tm_mon;
1241  tm->tm_mday = cur_tm.tm_mday;
1242  break;
1243 
1244  case DTK_TOMORROW:
1245  tmask = DTK_DATE_M;
1246  *dtype = DTK_DATE;
1247  GetCurrentDateTime(&cur_tm);
1248  j2date(date2j(cur_tm.tm_year, cur_tm.tm_mon, cur_tm.tm_mday) + 1,
1249  &tm->tm_year, &tm->tm_mon, &tm->tm_mday);
1250  break;
1251 
1252  case DTK_ZULU:
1253  tmask = (DTK_TIME_M | DTK_M(TZ));
1254  *dtype = DTK_DATE;
1255  tm->tm_hour = 0;
1256  tm->tm_min = 0;
1257  tm->tm_sec = 0;
1258  if (tzp != NULL)
1259  *tzp = 0;
1260  break;
1261 
1262  default:
1263  *dtype = val;
1264  }
1265 
1266  break;
1267 
1268  case MONTH:
1269 
1270  /*
1271  * already have a (numeric) month? then see if we can
1272  * substitute...
1273  */
1274  if ((fmask & DTK_M(MONTH)) && !haveTextMonth &&
1275  !(fmask & DTK_M(DAY)) && tm->tm_mon >= 1 &&
1276  tm->tm_mon <= 31)
1277  {
1278  tm->tm_mday = tm->tm_mon;
1279  tmask = DTK_M(DAY);
1280  }
1281  haveTextMonth = true;
1282  tm->tm_mon = val;
1283  break;
1284 
1285  case DTZMOD:
1286 
1287  /*
1288  * daylight savings time modifier (solves "MET DST"
1289  * syntax)
1290  */
1291  tmask |= DTK_M(DTZ);
1292  tm->tm_isdst = 1;
1293  if (tzp == NULL)
1294  return DTERR_BAD_FORMAT;
1295  *tzp -= val;
1296  break;
1297 
1298  case DTZ:
1299 
1300  /*
1301  * set mask for TZ here _or_ check for DTZ later when
1302  * getting default timezone
1303  */
1304  tmask |= DTK_M(TZ);
1305  tm->tm_isdst = 1;
1306  if (tzp == NULL)
1307  return DTERR_BAD_FORMAT;
1308  *tzp = -val;
1309  break;
1310 
1311  case TZ:
1312  tm->tm_isdst = 0;
1313  if (tzp == NULL)
1314  return DTERR_BAD_FORMAT;
1315  *tzp = -val;
1316  break;
1317 
1318  case DYNTZ:
1319  tmask |= DTK_M(TZ);
1320  if (tzp == NULL)
1321  return DTERR_BAD_FORMAT;
1322  /* we'll determine the actual offset later */
1323  abbrevTz = valtz;
1324  abbrev = field[i];
1325  break;
1326 
1327  case AMPM:
1328  mer = val;
1329  break;
1330 
1331  case ADBC:
1332  bc = (val == BC);
1333  break;
1334 
1335  case DOW:
1336  tm->tm_wday = val;
1337  break;
1338 
1339  case UNITS:
1340  tmask = 0;
1341  ptype = val;
1342  break;
1343 
1344  case ISOTIME:
1345 
1346  /*
1347  * This is a filler field "t" indicating that the next
1348  * field is time. Try to verify that this is sensible.
1349  */
1350  tmask = 0;
1351 
1352  /* No preceding date? Then quit... */
1353  if ((fmask & DTK_DATE_M) != DTK_DATE_M)
1354  return DTERR_BAD_FORMAT;
1355 
1356  /***
1357  * We will need one of the following fields:
1358  * DTK_NUMBER should be hhmmss.fff
1359  * DTK_TIME should be hh:mm:ss.fff
1360  * DTK_DATE should be hhmmss-zz
1361  ***/
1362  if (i >= nf - 1 ||
1363  (ftype[i + 1] != DTK_NUMBER &&
1364  ftype[i + 1] != DTK_TIME &&
1365  ftype[i + 1] != DTK_DATE))
1366  return DTERR_BAD_FORMAT;
1367 
1368  ptype = val;
1369  break;
1370 
1371  case UNKNOWN_FIELD:
1372 
1373  /*
1374  * Before giving up and declaring error, check to see
1375  * if it is an all-alpha timezone name.
1376  */
1377  namedTz = pg_tzset(field[i]);
1378  if (!namedTz)
1379  return DTERR_BAD_FORMAT;
1380  /* we'll apply the zone setting below */
1381  tmask = DTK_M(TZ);
1382  break;
1383 
1384  default:
1385  return DTERR_BAD_FORMAT;
1386  }
1387  break;
1388 
1389  default:
1390  return DTERR_BAD_FORMAT;
1391  }
1392 
1393  if (tmask & fmask)
1394  return DTERR_BAD_FORMAT;
1395  fmask |= tmask;
1396  } /* end loop over fields */
1397 
1398  /* do final checking/adjustment of Y/M/D fields */
1399  dterr = ValidateDate(fmask, isjulian, is2digits, bc, tm);
1400  if (dterr)
1401  return dterr;
1402 
1403  /* handle AM/PM */
1404  if (mer != HR24 && tm->tm_hour > HOURS_PER_DAY / 2)
1405  return DTERR_FIELD_OVERFLOW;
1406  if (mer == AM && tm->tm_hour == HOURS_PER_DAY / 2)
1407  tm->tm_hour = 0;
1408  else if (mer == PM && tm->tm_hour != HOURS_PER_DAY / 2)
1409  tm->tm_hour += HOURS_PER_DAY / 2;
1410 
1411  /* do additional checking for full date specs... */
1412  if (*dtype == DTK_DATE)
1413  {
1414  if ((fmask & DTK_DATE_M) != DTK_DATE_M)
1415  {
1416  if ((fmask & DTK_TIME_M) == DTK_TIME_M)
1417  return 1;
1418  return DTERR_BAD_FORMAT;
1419  }
1420 
1421  /*
1422  * If we had a full timezone spec, compute the offset (we could not do
1423  * it before, because we need the date to resolve DST status).
1424  */
1425  if (namedTz != NULL)
1426  {
1427  /* daylight savings time modifier disallowed with full TZ */
1428  if (fmask & DTK_M(DTZMOD))
1429  return DTERR_BAD_FORMAT;
1430 
1431  *tzp = DetermineTimeZoneOffset(tm, namedTz);
1432  }
1433 
1434  /*
1435  * Likewise, if we had a dynamic timezone abbreviation, resolve it
1436  * now.
1437  */
1438  if (abbrevTz != NULL)
1439  {
1440  /* daylight savings time modifier disallowed with dynamic TZ */
1441  if (fmask & DTK_M(DTZMOD))
1442  return DTERR_BAD_FORMAT;
1443 
1444  *tzp = DetermineTimeZoneAbbrevOffset(tm, abbrev, abbrevTz);
1445  }
1446 
1447  /* timezone not specified? then use session timezone */
1448  if (tzp != NULL && !(fmask & DTK_M(TZ)))
1449  {
1450  /*
1451  * daylight savings time modifier but no standard timezone? then
1452  * error
1453  */
1454  if (fmask & DTK_M(DTZMOD))
1455  return DTERR_BAD_FORMAT;
1456 
1458  }
1459  }
1460 
1461  return 0;
1462 }
#define DTERR_BAD_FORMAT
Definition: datetime.h:280
void GetCurrentDateTime(struct pg_tm *tm)
Definition: datetime.c:349
#define PM
Definition: datetime.h:73
#define DAY
Definition: datetime.h:94
#define UNITS
Definition: datetime.h:108
#define IGNORE_DTF
Definition: datetime.h:99
int tm_wday
Definition: pgtime.h:40
PGDLLIMPORT pg_tz * session_timezone
Definition: pgtz.c:28
#define DTK_JULIAN
Definition: datetime.h:174
#define DTK_YEAR
Definition: datetime.h:168
int tm_isdst
Definition: pgtime.h:42
#define YEAR
Definition: datetime.h:93
int tm_hour
Definition: pgtime.h:36
#define DTK_TIME_M
Definition: datetime.h:193
int errcode(int sqlerrcode)
Definition: elog.c:698
#define TZ
Definition: datetime.h:96
#define UNKNOWN_FIELD
Definition: datetime.h:125
#define SECOND
Definition: datetime.h:103
static int DecodeNumberField(int len, char *str, int fmask, int *tmask, struct pg_tm *tm, fsec_t *fsec, bool *is2digits)
Definition: datetime.c:2820
#define DTK_TODAY
Definition: datetime.h:156
#define DTK_TOMORROW
Definition: datetime.h:157
#define ADBC
Definition: datetime.h:109
Definition: pgtime.h:32
#define INTERVAL_FULL_RANGE
Definition: timestamp.h:48
#define DTK_DATE_M
Definition: datetime.h:192
pg_tz * pg_tzset(const char *tzname)
Definition: pgtz.c:234
#define DTK_MONTH
Definition: datetime.h:166
#define DTK_TZ
Definition: datetime.h:147
#define DTK_HOUR
Definition: datetime.h:163
#define ERROR
Definition: elog.h:46
#define DTERR_FIELD_OVERFLOW
Definition: datetime.h:281
#define DOW
Definition: datetime.h:107
#define AM
Definition: datetime.h:72
int DecodeTimezoneAbbrev(int field, char *lowtoken, int *offset, pg_tz **tz)
Definition: datetime.c:2985
#define DTK_SECOND
Definition: datetime.h:161
int DecodeTimezone(char *str, int *tzp)
Definition: datetime.c:2907
int tm_mday
Definition: pgtime.h:37
#define HOURS_PER_DAY
Definition: timestamp.h:78
int tm_mon
Definition: pgtime.h:38
int DetermineTimeZoneOffset(struct pg_tm *tm, pg_tz *tzp)
Definition: datetime.c:1478
bool time_overflows(int hour, int min, int sec, fsec_t fsec)
Definition: date.c:1426
int strtoint(const char *pg_restrict str, char **pg_restrict endptr, int base)
Definition: string.c:50
static int DecodeDate(char *str, int fmask, int *tmask, bool *is2digits, struct pg_tm *tm)
Definition: datetime.c:2370
void GetCurrentTimeUsec(struct pg_tm *tm, fsec_t *fsec, int *tzp)
Definition: datetime.c:370
#define DTK_YESTERDAY
Definition: datetime.h:155
#define DTK_NUMBER
Definition: datetime.h:142
#define MINUTE
Definition: datetime.h:102
#define USECS_PER_DAY
Definition: timestamp.h:91
void j2date(int jd, int *year, int *month, int *day)
Definition: datetime.c:294
#define MONTH
Definition: datetime.h:92
#define DTK_MINUTE
Definition: datetime.h:162
static int ParseFractionalSecond(char *cp, fsec_t *fsec)
Definition: datetime.c:534
#define BC
Definition: datetime.h:77
char * abbrev
Definition: tzparser.h:26
Definition: pgtz.h:65
#define ISOTIME
Definition: datetime.h:116
#define DTK_TIME
Definition: datetime.h:146
int date2j(int y, int m, int d)
Definition: datetime.c:269
#define ereport(elevel,...)
Definition: elog.h:157
static int DecodeTime(char *str, int fmask, int range, int *tmask, struct pg_tm *tm, fsec_t *fsec)
Definition: datetime.c:2559
void dt2time(Timestamp jd, int *hour, int *min, int *sec, fsec_t *fsec)
Definition: timestamp.c:1795
#define AMPM
Definition: datetime.h:100
#define DTK_STRING
Definition: datetime.h:143
#define DTK_DAY
Definition: datetime.h:164
static int DecodeNumber(int flen, char *field, bool haveTextMonth, int fmask, int *tmask, struct pg_tm *tm, fsec_t *fsec, bool *is2digits)
Definition: datetime.c:2635
#define RESERV
Definition: datetime.h:91
int DecodeSpecial(int field, char *lowtoken, int *val)
Definition: datetime.c:3040
#define DTK_ALL_SECS_M
Definition: datetime.h:191
#define DTZ
Definition: datetime.h:97
int tm_year
Definition: pgtime.h:39
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define DYNTZ
Definition: datetime.h:98
#define DTK_NOW
Definition: datetime.h:154
int i
#define DTK_M(t)
Definition: datetime.h:188
#define HOUR
Definition: datetime.h:101
#define DTK_ZULU
Definition: datetime.h:158
int tm_sec
Definition: pgtime.h:34
#define DTZMOD
Definition: datetime.h:123
int tm_min
Definition: pgtime.h:35
int DetermineTimeZoneAbbrevOffset(struct pg_tm *tm, const char *abbr, pg_tz *tzp)
Definition: datetime.c:1639
long val
Definition: informix.c:664
#define DTK_DATE
Definition: datetime.h:145
#define HR24
Definition: datetime.h:74
#define DTK_SPECIAL
Definition: datetime.h:150
int ValidateDate(int fmask, bool isjulian, bool is2digits, bool bc, struct pg_tm *tm)
Definition: datetime.c:2480

◆ DecodeInterval()

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

Definition at line 3096 of file datetime.c.

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

Referenced by interval_in().

3098 {
3099  bool is_before = false;
3100  char *cp;
3101  int fmask = 0,
3102  tmask,
3103  type;
3104  int i;
3105  int dterr;
3106  int val;
3107  double fval;
3108 
3109  *dtype = DTK_DELTA;
3110  type = IGNORE_DTF;
3111  ClearPgTm(tm, fsec);
3112 
3113  /* read through list backwards to pick up units before values */
3114  for (i = nf - 1; i >= 0; i--)
3115  {
3116  switch (ftype[i])
3117  {
3118  case DTK_TIME:
3119  dterr = DecodeTime(field[i], fmask, range,
3120  &tmask, tm, fsec);
3121  if (dterr)
3122  return dterr;
3123  type = DTK_DAY;
3124  break;
3125 
3126  case DTK_TZ:
3127 
3128  /*
3129  * Timezone means a token with a leading sign character and at
3130  * least one digit; there could be ':', '.', '-' embedded in
3131  * it as well.
3132  */
3133  Assert(*field[i] == '-' || *field[i] == '+');
3134 
3135  /*
3136  * Check for signed hh:mm or hh:mm:ss. If so, process exactly
3137  * like DTK_TIME case above, plus handling the sign.
3138  */
3139  if (strchr(field[i] + 1, ':') != NULL &&
3140  DecodeTime(field[i] + 1, fmask, range,
3141  &tmask, tm, fsec) == 0)
3142  {
3143  if (*field[i] == '-')
3144  {
3145  /* flip the sign on all fields */
3146  tm->tm_hour = -tm->tm_hour;
3147  tm->tm_min = -tm->tm_min;
3148  tm->tm_sec = -tm->tm_sec;
3149  *fsec = -(*fsec);
3150  }
3151 
3152  /*
3153  * Set the next type to be a day, if units are not
3154  * specified. This handles the case of '1 +02:03' since we
3155  * are reading right to left.
3156  */
3157  type = DTK_DAY;
3158  break;
3159  }
3160 
3161  /*
3162  * Otherwise, fall through to DTK_NUMBER case, which can
3163  * handle signed float numbers and signed year-month values.
3164  */
3165 
3166  /* FALLTHROUGH */
3167 
3168  case DTK_DATE:
3169  case DTK_NUMBER:
3170  if (type == IGNORE_DTF)
3171  {
3172  /* use typmod to decide what rightmost field is */
3173  switch (range)
3174  {
3175  case INTERVAL_MASK(YEAR):
3176  type = DTK_YEAR;
3177  break;
3178  case INTERVAL_MASK(MONTH):
3180  type = DTK_MONTH;
3181  break;
3182  case INTERVAL_MASK(DAY):
3183  type = DTK_DAY;
3184  break;
3185  case INTERVAL_MASK(HOUR):
3187  type = DTK_HOUR;
3188  break;
3189  case INTERVAL_MASK(MINUTE):
3192  type = DTK_MINUTE;
3193  break;
3194  case INTERVAL_MASK(SECOND):
3198  type = DTK_SECOND;
3199  break;
3200  default:
3201  type = DTK_SECOND;
3202  break;
3203  }
3204  }
3205 
3206  errno = 0;
3207  val = strtoint(field[i], &cp, 10);
3208  if (errno == ERANGE)
3209  return DTERR_FIELD_OVERFLOW;
3210 
3211  if (*cp == '-')
3212  {
3213  /* SQL "years-months" syntax */
3214  int val2;
3215 
3216  val2 = strtoint(cp + 1, &cp, 10);
3217  if (errno == ERANGE || val2 < 0 || val2 >= MONTHS_PER_YEAR)
3218  return DTERR_FIELD_OVERFLOW;
3219  if (*cp != '\0')
3220  return DTERR_BAD_FORMAT;
3221  type = DTK_MONTH;
3222  if (*field[i] == '-')
3223  val2 = -val2;
3224  if (((double) val * MONTHS_PER_YEAR + val2) > INT_MAX ||
3225  ((double) val * MONTHS_PER_YEAR + val2) < INT_MIN)
3226  return DTERR_FIELD_OVERFLOW;
3227  val = val * MONTHS_PER_YEAR + val2;
3228  fval = 0;
3229  }
3230  else if (*cp == '.')
3231  {
3232  errno = 0;
3233  fval = strtod(cp, &cp);
3234  if (*cp != '\0' || errno != 0)
3235  return DTERR_BAD_FORMAT;
3236 
3237  if (*field[i] == '-')
3238  fval = -fval;
3239  }
3240  else if (*cp == '\0')
3241  fval = 0;
3242  else
3243  return DTERR_BAD_FORMAT;
3244 
3245  tmask = 0; /* DTK_M(type); */
3246 
3247  switch (type)
3248  {
3249  case DTK_MICROSEC:
3250  *fsec += rint(val + fval);
3251  tmask = DTK_M(MICROSECOND);
3252  break;
3253 
3254  case DTK_MILLISEC:
3255  /* avoid overflowing the fsec field */
3256  tm->tm_sec += val / 1000;
3257  val -= (val / 1000) * 1000;
3258  *fsec += rint((val + fval) * 1000);
3259  tmask = DTK_M(MILLISECOND);
3260  break;
3261 
3262  case DTK_SECOND:
3263  tm->tm_sec += val;
3264  *fsec += rint(fval * 1000000);
3265 
3266  /*
3267  * If any subseconds were specified, consider this
3268  * microsecond and millisecond input as well.
3269  */
3270  if (fval == 0)
3271  tmask = DTK_M(SECOND);
3272  else
3273  tmask = DTK_ALL_SECS_M;
3274  break;
3275 
3276  case DTK_MINUTE:
3277  tm->tm_min += val;
3278  AdjustFractSeconds(fval, tm, fsec, SECS_PER_MINUTE);
3279  tmask = DTK_M(MINUTE);
3280  break;
3281 
3282  case DTK_HOUR:
3283  tm->tm_hour += val;
3284  AdjustFractSeconds(fval, tm, fsec, SECS_PER_HOUR);
3285  tmask = DTK_M(HOUR);
3286  type = DTK_DAY; /* set for next field */
3287  break;
3288 
3289  case DTK_DAY:
3290  tm->tm_mday += val;
3291  AdjustFractSeconds(fval, tm, fsec, SECS_PER_DAY);
3292  tmask = DTK_M(DAY);
3293  break;
3294 
3295  case DTK_WEEK:
3296  tm->tm_mday += val * 7;
3297  AdjustFractDays(fval, tm, fsec, 7);
3298  tmask = DTK_M(WEEK);
3299  break;
3300 
3301  case DTK_MONTH:
3302  tm->tm_mon += val;
3303  AdjustFractDays(fval, tm, fsec, DAYS_PER_MONTH);
3304  tmask = DTK_M(MONTH);
3305  break;
3306 
3307  case DTK_YEAR:
3308  tm->tm_year += val;
3309  tm->tm_mon += rint(fval * MONTHS_PER_YEAR);
3310  tmask = DTK_M(YEAR);
3311  break;
3312 
3313  case DTK_DECADE:
3314  tm->tm_year += val * 10;
3315  tm->tm_mon += rint(fval * MONTHS_PER_YEAR * 10);
3316  tmask = DTK_M(DECADE);
3317  break;
3318 
3319  case DTK_CENTURY:
3320  tm->tm_year += val * 100;
3321  tm->tm_mon += rint(fval * MONTHS_PER_YEAR * 100);
3322  tmask = DTK_M(CENTURY);
3323  break;
3324 
3325  case DTK_MILLENNIUM:
3326  tm->tm_year += val * 1000;
3327  tm->tm_mon += rint(fval * MONTHS_PER_YEAR * 1000);
3328  tmask = DTK_M(MILLENNIUM);
3329  break;
3330 
3331  default:
3332  return DTERR_BAD_FORMAT;
3333  }
3334  break;
3335 
3336  case DTK_STRING:
3337  case DTK_SPECIAL:
3338  type = DecodeUnits(i, field[i], &val);
3339  if (type == IGNORE_DTF)
3340  continue;
3341 
3342  tmask = 0; /* DTK_M(type); */
3343  switch (type)
3344  {
3345  case UNITS:
3346  type = val;
3347  break;
3348 
3349  case AGO:
3350  is_before = true;
3351  type = val;
3352  break;
3353 
3354  case RESERV:
3355  tmask = (DTK_DATE_M | DTK_TIME_M);
3356  *dtype = val;
3357  break;
3358 
3359  default:
3360  return DTERR_BAD_FORMAT;
3361  }
3362  break;
3363 
3364  default:
3365  return DTERR_BAD_FORMAT;
3366  }
3367 
3368  if (tmask & fmask)
3369  return DTERR_BAD_FORMAT;
3370  fmask |= tmask;
3371  }
3372 
3373  /* ensure that at least one time field has been found */
3374  if (fmask == 0)
3375  return DTERR_BAD_FORMAT;
3376 
3377  /* ensure fractional seconds are fractional */
3378  if (*fsec != 0)
3379  {
3380  int sec;
3381 
3382  sec = *fsec / USECS_PER_SEC;
3383  *fsec -= sec * USECS_PER_SEC;
3384  tm->tm_sec += sec;
3385  }
3386 
3387  /*----------
3388  * The SQL standard defines the interval literal
3389  * '-1 1:00:00'
3390  * to mean "negative 1 days and negative 1 hours", while Postgres
3391  * traditionally treats this as meaning "negative 1 days and positive
3392  * 1 hours". In SQL_STANDARD intervalstyle, we apply the leading sign
3393  * to all fields if there are no other explicit signs.
3394  *
3395  * We leave the signs alone if there are additional explicit signs.
3396  * This protects us against misinterpreting postgres-style dump output,
3397  * since the postgres-style output code has always put an explicit sign on
3398  * all fields following a negative field. But note that SQL-spec output
3399  * is ambiguous and can be misinterpreted on load! (So it's best practice
3400  * to dump in postgres style, not SQL style.)
3401  *----------
3402  */
3403  if (IntervalStyle == INTSTYLE_SQL_STANDARD && *field[0] == '-')
3404  {
3405  /* Check for additional explicit signs */
3406  bool more_signs = false;
3407 
3408  for (i = 1; i < nf; i++)
3409  {
3410  if (*field[i] == '-' || *field[i] == '+')
3411  {
3412  more_signs = true;
3413  break;
3414  }
3415  }
3416 
3417  if (!more_signs)
3418  {
3419  /*
3420  * Rather than re-determining which field was field[0], just force
3421  * 'em all negative.
3422  */
3423  if (*fsec > 0)
3424  *fsec = -(*fsec);
3425  if (tm->tm_sec > 0)
3426  tm->tm_sec = -tm->tm_sec;
3427  if (tm->tm_min > 0)
3428  tm->tm_min = -tm->tm_min;
3429  if (tm->tm_hour > 0)
3430  tm->tm_hour = -tm->tm_hour;
3431  if (tm->tm_mday > 0)
3432  tm->tm_mday = -tm->tm_mday;
3433  if (tm->tm_mon > 0)
3434  tm->tm_mon = -tm->tm_mon;
3435  if (tm->tm_year > 0)
3436  tm->tm_year = -tm->tm_year;
3437  }
3438  }
3439 
3440  /* finally, AGO negates everything */
3441  if (is_before)
3442  {
3443  *fsec = -(*fsec);
3444  tm->tm_sec = -tm->tm_sec;
3445  tm->tm_min = -tm->tm_min;
3446  tm->tm_hour = -tm->tm_hour;
3447  tm->tm_mday = -tm->tm_mday;
3448  tm->tm_mon = -tm->tm_mon;
3449  tm->tm_year = -tm->tm_year;
3450  }
3451 
3452  return 0;
3453 }
#define DTERR_BAD_FORMAT
Definition: datetime.h:280
#define DTK_CENTURY
Definition: datetime.h:170
#define DAY
Definition: datetime.h:94
#define UNITS
Definition: datetime.h:108
#define IGNORE_DTF
Definition: datetime.h:99
#define DTK_WEEK
Definition: datetime.h:165
#define DTK_YEAR
Definition: datetime.h:168
#define MILLENNIUM
Definition: datetime.h:121
#define USECS_PER_SEC
Definition: timestamp.h:94
#define YEAR
Definition: datetime.h:93
#define CENTURY
Definition: datetime.h:120
#define DTK_DELTA
Definition: datetime.h:160
int tm_hour
Definition: pgtime.h:36
#define DECADE
Definition: datetime.h:119
#define AGO
Definition: datetime.h:111
#define DTK_TIME_M
Definition: datetime.h:193
#define DTK_MILLENNIUM
Definition: datetime.h:171
int IntervalStyle
Definition: globals.c:120
#define SECOND
Definition: datetime.h:103
int DecodeUnits(int field, char *lowtoken, int *val)
Definition: datetime.c:3727
static void AdjustFractSeconds(double frac, struct pg_tm *tm, fsec_t *fsec, int scale)
Definition: datetime.c:504
#define DTK_DATE_M
Definition: datetime.h:192
#define DTK_MONTH
Definition: datetime.h:166
#define DTK_MILLISEC
Definition: datetime.h:172
#define MONTHS_PER_YEAR
Definition: timestamp.h:69
#define DTK_DECADE
Definition: datetime.h:169
#define DTK_TZ
Definition: datetime.h:147
#define DTK_HOUR
Definition: datetime.h:163
#define DTERR_FIELD_OVERFLOW
Definition: datetime.h:281
#define MILLISECOND
Definition: datetime.h:104
#define SECS_PER_DAY
Definition: timestamp.h:86
#define DTK_SECOND
Definition: datetime.h:161
int tm_mday
Definition: pgtime.h:37
int tm_mon
Definition: pgtime.h:38
#define SECS_PER_MINUTE
Definition: timestamp.h:88
int strtoint(const char *pg_restrict str, char **pg_restrict endptr, int base)
Definition: string.c:50
#define DTK_NUMBER
Definition: datetime.h:142
#define MINUTE
Definition: datetime.h:102
static struct cvec * range(struct vars *v, chr a, chr b, int cases)
Definition: regc_locale.c:412
#define SECS_PER_HOUR
Definition: timestamp.h:87
#define MONTH
Definition: datetime.h:92
#define DTK_MINUTE
Definition: datetime.h:162
#define DTK_MICROSEC
Definition: datetime.h:173
#define DAYS_PER_MONTH
Definition: timestamp.h:77
static void AdjustFractDays(double frac, struct pg_tm *tm, fsec_t *fsec, int scale)
Definition: datetime.c:519
#define WEEK
Definition: datetime.h:118
#define DTK_TIME
Definition: datetime.h:146
static int DecodeTime(char *str, int fmask, int range, int *tmask, struct pg_tm *tm, fsec_t *fsec)
Definition: datetime.c:2559
static void ClearPgTm(struct pg_tm *tm, fsec_t *fsec)
Definition: datetime.c:3072
#define Assert(condition)
Definition: c.h:804
#define DTK_STRING
Definition: datetime.h:143
#define DTK_DAY
Definition: datetime.h:164
#define RESERV
Definition: datetime.h:91
#define DTK_ALL_SECS_M
Definition: datetime.h:191
#define INTERVAL_MASK(b)
Definition: timestamp.h:45
#define INTSTYLE_SQL_STANDARD
Definition: miscadmin.h:251
int tm_year
Definition: pgtime.h:39
int i
#define DTK_M(t)
Definition: datetime.h:188
#define HOUR
Definition: datetime.h:101
int tm_sec
Definition: pgtime.h:34
int tm_min
Definition: pgtime.h:35
long val
Definition: informix.c:664
#define DTK_DATE
Definition: datetime.h:145
#define DTK_SPECIAL
Definition: datetime.h:150
#define MICROSECOND
Definition: datetime.h:105

◆ DecodeISO8601Interval()

int DecodeISO8601Interval ( char *  str,
int *  dtype,
struct pg_tm tm,
fsec_t fsec 
)

Definition at line 3518 of file datetime.c.

References AdjustFractDays(), AdjustFractSeconds(), ClearPgTm(), DAYS_PER_MONTH, DTERR_BAD_FORMAT, DTK_DELTA, ISO8601IntegerWidth(), MONTHS_PER_YEAR, ParseISO8601Number(), SECS_PER_DAY, SECS_PER_HOUR, SECS_PER_MINUTE, generate_unaccent_rules::str, pg_tm::tm_hour, pg_tm::tm_mday, pg_tm::tm_min, pg_tm::tm_mon, pg_tm::tm_sec, pg_tm::tm_year, and val.

Referenced by interval_in().

3520 {
3521  bool datepart = true;
3522  bool havefield = false;
3523 
3524  *dtype = DTK_DELTA;
3525  ClearPgTm(tm, fsec);
3526 
3527  if (strlen(str) < 2 || str[0] != 'P')
3528  return DTERR_BAD_FORMAT;
3529 
3530  str++;
3531  while (*str)
3532  {
3533  char *fieldstart;
3534  int val;
3535  double fval;
3536  char unit;
3537  int dterr;
3538 
3539  if (*str == 'T') /* T indicates the beginning of the time part */
3540  {
3541  datepart = false;
3542  havefield = false;
3543  str++;
3544  continue;
3545  }
3546 
3547  fieldstart = str;
3548  dterr = ParseISO8601Number(str, &str, &val, &fval);
3549  if (dterr)
3550  return dterr;
3551 
3552  /*
3553  * Note: we could step off the end of the string here. Code below
3554  * *must* exit the loop if unit == '\0'.
3555  */
3556  unit = *str++;
3557 
3558  if (datepart)
3559  {
3560  switch (unit) /* before T: Y M W D */
3561  {
3562  case 'Y':
3563  tm->tm_year += val;
3564  tm->tm_mon += rint(fval * MONTHS_PER_YEAR);
3565  break;
3566  case 'M':
3567  tm->tm_mon += val;
3568  AdjustFractDays(fval, tm, fsec, DAYS_PER_MONTH);
3569  break;
3570  case 'W':
3571  tm->tm_mday += val * 7;
3572  AdjustFractDays(fval, tm, fsec, 7);
3573  break;
3574  case 'D':
3575  tm->tm_mday += val;
3576  AdjustFractSeconds(fval, tm, fsec, SECS_PER_DAY);
3577  break;
3578  case 'T': /* ISO 8601 4.4.3.3 Alternative Format / Basic */
3579  case '\0':
3580  if (ISO8601IntegerWidth(fieldstart) == 8 && !havefield)
3581  {
3582  tm->tm_year += val / 10000;
3583  tm->tm_mon += (val / 100) % 100;
3584  tm->tm_mday += val % 100;
3585  AdjustFractSeconds(fval, tm, fsec, SECS_PER_DAY);
3586  if (unit == '\0')
3587  return 0;
3588  datepart = false;
3589  havefield = false;
3590  continue;
3591  }
3592  /* Else fall through to extended alternative format */
3593  /* FALLTHROUGH */
3594  case '-': /* ISO 8601 4.4.3.3 Alternative Format,
3595  * Extended */
3596  if (havefield)
3597  return DTERR_BAD_FORMAT;
3598 
3599  tm->tm_year += val;
3600  tm->tm_mon += rint(fval * MONTHS_PER_YEAR);
3601  if (unit == '\0')
3602  return 0;
3603  if (unit == 'T')
3604  {
3605  datepart = false;
3606  havefield = false;
3607  continue;
3608  }
3609 
3610  dterr = ParseISO8601Number(str, &str, &val, &fval);
3611  if (dterr)
3612  return dterr;
3613  tm->tm_mon += val;
3614  AdjustFractDays(fval, tm, fsec, DAYS_PER_MONTH);
3615  if (*str == '\0')
3616  return 0;
3617  if (*str == 'T')
3618  {
3619  datepart = false;
3620  havefield = false;
3621  continue;
3622  }
3623  if (*str != '-')
3624  return DTERR_BAD_FORMAT;
3625  str++;
3626 
3627  dterr = ParseISO8601Number(str, &str, &val, &fval);
3628  if (dterr)
3629  return dterr;
3630  tm->tm_mday += val;
3631  AdjustFractSeconds(fval, tm, fsec, SECS_PER_DAY);
3632  if (*str == '\0')
3633  return 0;
3634  if (*str == 'T')
3635  {
3636  datepart = false;
3637  havefield = false;
3638  continue;
3639  }
3640  return DTERR_BAD_FORMAT;
3641  default:
3642  /* not a valid date unit suffix */
3643  return DTERR_BAD_FORMAT;
3644  }
3645  }
3646  else
3647  {
3648  switch (unit) /* after T: H M S */
3649  {
3650  case 'H':
3651  tm->tm_hour += val;
3652  AdjustFractSeconds(fval, tm, fsec, SECS_PER_HOUR);
3653  break;
3654  case 'M':
3655  tm->tm_min += val;
3656  AdjustFractSeconds(fval, tm, fsec, SECS_PER_MINUTE);
3657  break;
3658  case 'S':
3659  tm->tm_sec += val;
3660  AdjustFractSeconds(fval, tm, fsec, 1);
3661  break;
3662  case '\0': /* ISO 8601 4.4.3.3 Alternative Format */
3663  if (ISO8601IntegerWidth(fieldstart) == 6 && !havefield)
3664  {
3665  tm->tm_hour += val / 10000;
3666  tm->tm_min += (val / 100) % 100;
3667  tm->tm_sec += val % 100;
3668  AdjustFractSeconds(fval, tm, fsec, 1);
3669  return 0;
3670  }
3671  /* Else fall through to extended alternative format */
3672  /* FALLTHROUGH */
3673  case ':': /* ISO 8601 4.4.3.3 Alternative Format,
3674  * Extended */
3675  if (havefield)
3676  return DTERR_BAD_FORMAT;
3677 
3678  tm->tm_hour += val;
3679  AdjustFractSeconds(fval, tm, fsec, SECS_PER_HOUR);
3680  if (unit == '\0')
3681  return 0;
3682 
3683  dterr = ParseISO8601Number(str, &str, &val, &fval);
3684  if (dterr)
3685  return dterr;
3686  tm->tm_min += val;
3687  AdjustFractSeconds(fval, tm, fsec, SECS_PER_MINUTE);
3688  if (*str == '\0')
3689  return 0;
3690  if (*str != ':')
3691  return DTERR_BAD_FORMAT;
3692  str++;
3693 
3694  dterr = ParseISO8601Number(str, &str, &val, &fval);
3695  if (dterr)
3696  return dterr;
3697  tm->tm_sec += val;
3698  AdjustFractSeconds(fval, tm, fsec, 1);
3699  if (*str == '\0')
3700  return 0;
3701  return DTERR_BAD_FORMAT;
3702 
3703  default:
3704  /* not a valid time unit suffix */
3705  return DTERR_BAD_FORMAT;
3706  }
3707  }
3708 
3709  havefield = true;
3710  }
3711 
3712  return 0;
3713 }
#define DTERR_BAD_FORMAT
Definition: datetime.h:280
#define DTK_DELTA
Definition: datetime.h:160
int tm_hour
Definition: pgtime.h:36
static void AdjustFractSeconds(double frac, struct pg_tm *tm, fsec_t *fsec, int scale)
Definition: datetime.c:504
#define MONTHS_PER_YEAR
Definition: timestamp.h:69
#define SECS_PER_DAY
Definition: timestamp.h:86
int tm_mday
Definition: pgtime.h:37
int tm_mon
Definition: pgtime.h:38
#define SECS_PER_MINUTE
Definition: timestamp.h:88
#define SECS_PER_HOUR
Definition: timestamp.h:87
#define DAYS_PER_MONTH
Definition: timestamp.h:77
static void AdjustFractDays(double frac, struct pg_tm *tm, fsec_t *fsec, int scale)
Definition: datetime.c:519
static void ClearPgTm(struct pg_tm *tm, fsec_t *fsec)
Definition: datetime.c:3072
static int ISO8601IntegerWidth(char *fieldstart)
Definition: datetime.c:3491
int tm_year
Definition: pgtime.h:39
static int ParseISO8601Number(char *str, char **endptr, int *ipart, double *fpart)
Definition: datetime.c:3463
int tm_sec
Definition: pgtime.h:34
int tm_min
Definition: pgtime.h:35
long val
Definition: informix.c:664

◆ DecodeSpecial()

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

Definition at line 3040 of file datetime.c.

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

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

3041 {
3042  int type;
3043  const datetkn *tp;
3044 
3045  tp = datecache[field];
3046  /* use strncmp so that we match truncated tokens */
3047  if (tp == NULL || strncmp(lowtoken, tp->token, TOKMAXLEN) != 0)
3048  {
3049  tp = datebsearch(lowtoken, datetktbl, szdatetktbl);
3050  }
3051  if (tp == NULL)
3052  {
3053  type = UNKNOWN_FIELD;
3054  *val = 0;
3055  }
3056  else
3057  {
3058  datecache[field] = tp;
3059  type = tp->type;
3060  *val = tp->value;
3061  }
3062 
3063  return type;
3064 }
static const datetkn datetktbl[]
Definition: datetime.c:89
#define UNKNOWN_FIELD
Definition: datetime.h:125
int32 value
Definition: datetime.h:212
char token[TOKMAXLEN+1]
Definition: datetime.h:210
static const datetkn * datebsearch(const char *key, const datetkn *base, int nel)
Definition: datetime.c:3809
char type
Definition: datetime.h:211
static const datetkn * datecache[MAXDATEFIELDS]
Definition: datetime.c:241
static const int szdatetktbl
Definition: datetime.c:164
#define TOKMAXLEN
Definition: datetime.h:205
long val
Definition: informix.c:664

◆ DecodeTimeOnly()

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

Definition at line 1754 of file datetime.c.

References ADBC, AM, AMPM, BC, DAY, DecodeDate(), DecodeNumber(), DecodeNumberField(), DecodeSpecial(), DecodeTime(), DecodeTimezone(), DecodeTimezoneAbbrev(), DetermineTimeZoneAbbrevOffset(), DetermineTimeZoneOffset(), dt2time(), DTERR_BAD_FORMAT, DTERR_FIELD_OVERFLOW, DTK_ALL_SECS_M, DTK_DATE, DTK_DATE_M, DTK_DAY, DTK_HOUR, DTK_JULIAN, DTK_M, DTK_MINUTE, DTK_MONTH, DTK_NOW, DTK_NUMBER, DTK_SECOND, DTK_SPECIAL, DTK_STRING, DTK_TIME, DTK_TIME_M, DTK_TZ, DTK_YEAR, DTK_ZULU, DTZ, DTZMOD, DYNTZ, ereport, errcode(), errmsg(), ERROR, GetCurrentDateTime(), GetCurrentTimeUsec(), HOUR, HOURS_PER_DAY, HR24, i, IGNORE_DTF, INTERVAL_FULL_RANGE, ISOTIME, j2date(), MINUTE, MONTH, ParseFractionalSecond(), pg_get_timezone_offset(), pg_tzset(), PM, RESERV, SECOND, session_timezone, strtoint(), time_overflows(), 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, generate_unaccent_rules::type, TZ, UNITS, UNKNOWN_FIELD, USECS_PER_DAY, val, ValidateDate(), and YEAR.

Referenced by time_in(), and timetz_in().

1756 {
1757  int fmask = 0,
1758  tmask,
1759  type;
1760  int ptype = 0; /* "prefix type" for ISO h04mm05s06 format */
1761  int i;
1762  int val;
1763  int dterr;
1764  bool isjulian = false;
1765  bool is2digits = false;
1766  bool bc = false;
1767  int mer = HR24;
1768  pg_tz *namedTz = NULL;
1769  pg_tz *abbrevTz = NULL;
1770  char *abbrev = NULL;
1771  pg_tz *valtz;
1772 
1773  *dtype = DTK_TIME;
1774  tm->tm_hour = 0;
1775  tm->tm_min = 0;
1776  tm->tm_sec = 0;
1777  *fsec = 0;
1778  /* don't know daylight savings time status apriori */
1779  tm->tm_isdst = -1;
1780 
1781  if (tzp != NULL)
1782  *tzp = 0;
1783 
1784  for (i = 0; i < nf; i++)
1785  {
1786  switch (ftype[i])
1787  {
1788  case DTK_DATE:
1789 
1790  /*
1791  * Time zone not allowed? Then should not accept dates or time
1792  * zones no matter what else!
1793  */
1794  if (tzp == NULL)
1795  return DTERR_BAD_FORMAT;
1796 
1797  /* Under limited circumstances, we will accept a date... */
1798  if (i == 0 && nf >= 2 &&
1799  (ftype[nf - 1] == DTK_DATE || ftype[1] == DTK_TIME))
1800  {
1801  dterr = DecodeDate(field[i], fmask,
1802  &tmask, &is2digits, tm);
1803  if (dterr)
1804  return dterr;
1805  }
1806  /* otherwise, this is a time and/or time zone */
1807  else
1808  {
1809  if (isdigit((unsigned char) *field[i]))
1810  {
1811  char *cp;
1812 
1813  /*
1814  * Starts with a digit but we already have a time
1815  * field? Then we are in trouble with time already...
1816  */
1817  if ((fmask & DTK_TIME_M) == DTK_TIME_M)
1818  return DTERR_BAD_FORMAT;
1819 
1820  /*
1821  * Should not get here and fail. Sanity check only...
1822  */
1823  if ((cp = strchr(field[i], '-')) == NULL)
1824  return DTERR_BAD_FORMAT;
1825 
1826  /* Get the time zone from the end of the string */
1827  dterr = DecodeTimezone(cp, tzp);
1828  if (dterr)
1829  return dterr;
1830  *cp = '\0';
1831 
1832  /*
1833  * Then read the rest of the field as a concatenated
1834  * time
1835  */
1836  dterr = DecodeNumberField(strlen(field[i]), field[i],
1837  (fmask | DTK_DATE_M),
1838  &tmask, tm,
1839  fsec, &is2digits);
1840  if (dterr < 0)
1841  return dterr;
1842  ftype[i] = dterr;
1843 
1844  tmask |= DTK_M(TZ);
1845  }
1846  else
1847  {
1848  namedTz = pg_tzset(field[i]);
1849  if (!namedTz)
1850  {
1851  /*
1852  * We should return an error code instead of
1853  * ereport'ing directly, but then there is no way
1854  * to report the bad time zone name.
1855  */
1856  ereport(ERROR,
1857  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1858  errmsg("time zone \"%s\" not recognized",
1859  field[i])));
1860  }
1861  /* we'll apply the zone setting below */
1862  ftype[i] = DTK_TZ;
1863  tmask = DTK_M(TZ);
1864  }
1865  }
1866  break;
1867 
1868  case DTK_TIME:
1869  dterr = DecodeTime(field[i], (fmask | DTK_DATE_M),
1871  &tmask, tm, fsec);
1872  if (dterr)
1873  return dterr;
1874  break;
1875 
1876  case DTK_TZ:
1877  {
1878  int tz;
1879 
1880  if (tzp == NULL)
1881  return DTERR_BAD_FORMAT;
1882 
1883  dterr = DecodeTimezone(field[i], &tz);
1884  if (dterr)
1885  return dterr;
1886  *tzp = tz;
1887  tmask = DTK_M(TZ);
1888  }
1889  break;
1890 
1891  case DTK_NUMBER:
1892 
1893  /*
1894  * Was this an "ISO time" with embedded field labels? An
1895  * example is "h04mm05s06" - thomas 2001-02-04
1896  */
1897  if (ptype != 0)
1898  {
1899  char *cp;
1900  int val;
1901 
1902  /* Only accept a date under limited circumstances */
1903  switch (ptype)
1904  {
1905  case DTK_JULIAN:
1906  case DTK_YEAR:
1907  case DTK_MONTH:
1908  case DTK_DAY:
1909  if (tzp == NULL)
1910  return DTERR_BAD_FORMAT;
1911  default:
1912  break;
1913  }
1914 
1915  errno = 0;
1916  val = strtoint(field[i], &cp, 10);
1917  if (errno == ERANGE)
1918  return DTERR_FIELD_OVERFLOW;
1919 
1920  /*
1921  * only a few kinds are allowed to have an embedded
1922  * decimal
1923  */
1924  if (*cp == '.')
1925  switch (ptype)
1926  {
1927  case DTK_JULIAN:
1928  case DTK_TIME:
1929  case DTK_SECOND:
1930  break;
1931  default:
1932  return DTERR_BAD_FORMAT;
1933  break;
1934  }
1935  else if (*cp != '\0')
1936  return DTERR_BAD_FORMAT;
1937 
1938  switch (ptype)
1939  {
1940  case DTK_YEAR:
1941  tm->tm_year = val;
1942  tmask = DTK_M(YEAR);
1943  break;
1944 
1945  case DTK_MONTH:
1946 
1947  /*
1948  * already have a month and hour? then assume
1949  * minutes
1950  */
1951  if ((fmask & DTK_M(MONTH)) != 0 &&
1952  (fmask & DTK_M(HOUR)) != 0)
1953  {
1954  tm->tm_min = val;
1955  tmask = DTK_M(MINUTE);
1956  }
1957  else
1958  {
1959  tm->tm_mon = val;
1960  tmask = DTK_M(MONTH);
1961  }
1962  break;
1963 
1964  case DTK_DAY:
1965  tm->tm_mday = val;
1966  tmask = DTK_M(DAY);
1967  break;
1968 
1969  case DTK_HOUR:
1970  tm->tm_hour = val;
1971  tmask = DTK_M(HOUR);
1972  break;
1973 
1974  case DTK_MINUTE:
1975  tm->tm_min = val;
1976  tmask = DTK_M(MINUTE);
1977  break;
1978 
1979  case DTK_SECOND:
1980  tm->tm_sec = val;
1981  tmask = DTK_M(SECOND);
1982  if (*cp == '.')
1983  {
1984  dterr = ParseFractionalSecond(cp, fsec);
1985  if (dterr)
1986  return dterr;
1987  tmask = DTK_ALL_SECS_M;
1988  }
1989  break;
1990 
1991  case DTK_TZ:
1992  tmask = DTK_M(TZ);
1993  dterr = DecodeTimezone(field[i], tzp);
1994  if (dterr)
1995  return dterr;
1996  break;
1997 
1998  case DTK_JULIAN:
1999  /* previous field was a label for "julian date" */
2000  if (val < 0)
2001  return DTERR_FIELD_OVERFLOW;
2002  tmask = DTK_DATE_M;
2003  j2date(val, &tm->tm_year, &tm->tm_mon, &tm->tm_mday);
2004  isjulian = true;
2005 
2006  if (*cp == '.')
2007  {
2008  double time;
2009 
2010  errno = 0;
2011  time = strtod(cp, &cp);
2012  if (*cp != '\0' || errno != 0)
2013  return DTERR_BAD_FORMAT;
2014  time *= USECS_PER_DAY;
2015  dt2time(time,
2016  &tm->tm_hour, &tm->tm_min,
2017  &tm->tm_sec, fsec);
2018  tmask |= DTK_TIME_M;
2019  }
2020  break;
2021 
2022  case DTK_TIME:
2023  /* previous field was "t" for ISO time */
2024  dterr = DecodeNumberField(strlen(field[i]), field[i],
2025  (fmask | DTK_DATE_M),
2026  &tmask, tm,
2027  fsec, &is2digits);
2028  if (dterr < 0)
2029  return dterr;
2030  ftype[i] = dterr;
2031 
2032  if (tmask != DTK_TIME_M)
2033  return DTERR_BAD_FORMAT;
2034  break;
2035 
2036  default:
2037  return DTERR_BAD_FORMAT;
2038  break;
2039  }
2040 
2041  ptype = 0;
2042  *dtype = DTK_DATE;
2043  }
2044  else
2045  {
2046  char *cp;
2047  int flen;
2048 
2049  flen = strlen(field[i]);
2050  cp = strchr(field[i], '.');
2051 
2052  /* Embedded decimal? */
2053  if (cp != NULL)
2054  {
2055  /*
2056  * Under limited circumstances, we will accept a
2057  * date...
2058  */
2059  if (i == 0 && nf >= 2 && ftype[nf - 1] == DTK_DATE)
2060  {
2061  dterr = DecodeDate(field[i], fmask,
2062  &tmask, &is2digits, tm);
2063  if (dterr)
2064  return dterr;
2065  }
2066  /* embedded decimal and several digits before? */
2067  else if (flen - strlen(cp) > 2)
2068  {
2069  /*
2070  * Interpret as a concatenated date or time Set
2071  * the type field to allow decoding other fields
2072  * later. Example: 20011223 or 040506
2073  */
2074  dterr = DecodeNumberField(flen, field[i],
2075  (fmask | DTK_DATE_M),
2076  &tmask, tm,
2077  fsec, &is2digits);
2078  if (dterr < 0)
2079  return dterr;
2080  ftype[i] = dterr;
2081  }
2082  else
2083  return DTERR_BAD_FORMAT;
2084  }
2085  else if (flen > 4)
2086  {
2087  dterr = DecodeNumberField(flen, field[i],
2088  (fmask | DTK_DATE_M),
2089  &tmask, tm,
2090  fsec, &is2digits);
2091  if (dterr < 0)
2092  return dterr;
2093  ftype[i] = dterr;
2094  }
2095  /* otherwise it is a single date/time field... */
2096  else
2097  {
2098  dterr = DecodeNumber(flen, field[i],
2099  false,
2100  (fmask | DTK_DATE_M),
2101  &tmask, tm,
2102  fsec, &is2digits);
2103  if (dterr)
2104  return dterr;
2105  }
2106  }
2107  break;
2108 
2109  case DTK_STRING:
2110  case DTK_SPECIAL:
2111  /* timezone abbrevs take precedence over built-in tokens */
2112  type = DecodeTimezoneAbbrev(i, field[i], &val, &valtz);
2113  if (type == UNKNOWN_FIELD)
2114  type = DecodeSpecial(i, field[i], &val);
2115  if (type == IGNORE_DTF)
2116  continue;
2117 
2118  tmask = DTK_M(type);
2119  switch (type)
2120  {
2121  case RESERV:
2122  switch (val)
2123  {
2124  case DTK_NOW:
2125  tmask = DTK_TIME_M;
2126  *dtype = DTK_TIME;
2127  GetCurrentTimeUsec(tm, fsec, NULL);
2128  break;
2129 
2130  case DTK_ZULU:
2131  tmask = (DTK_TIME_M | DTK_M(TZ));
2132  *dtype = DTK_TIME;
2133  tm->tm_hour = 0;
2134  tm->tm_min = 0;
2135  tm->tm_sec = 0;
2136  tm->tm_isdst = 0;
2137  break;
2138 
2139  default:
2140  return DTERR_BAD_FORMAT;
2141  }
2142 
2143  break;
2144 
2145  case DTZMOD:
2146 
2147  /*
2148  * daylight savings time modifier (solves "MET DST"
2149  * syntax)
2150  */
2151  tmask |= DTK_M(DTZ);
2152  tm->tm_isdst = 1;
2153  if (tzp == NULL)
2154  return DTERR_BAD_FORMAT;
2155  *tzp -= val;
2156  break;
2157 
2158  case DTZ:
2159 
2160  /*
2161  * set mask for TZ here _or_ check for DTZ later when
2162  * getting default timezone
2163  */
2164  tmask |= DTK_M(TZ);
2165  tm->tm_isdst = 1;
2166  if (tzp == NULL)
2167  return DTERR_BAD_FORMAT;
2168  *tzp = -val;
2169  ftype[i] = DTK_TZ;
2170  break;
2171 
2172  case TZ:
2173  tm->tm_isdst = 0;
2174  if (tzp == NULL)
2175  return DTERR_BAD_FORMAT;
2176  *tzp = -val;
2177  ftype[i] = DTK_TZ;
2178  break;
2179 
2180  case DYNTZ:
2181  tmask |= DTK_M(TZ);
2182  if (tzp == NULL)
2183  return DTERR_BAD_FORMAT;
2184  /* we'll determine the actual offset later */
2185  abbrevTz = valtz;
2186  abbrev = field[i];
2187  ftype[i] = DTK_TZ;
2188  break;
2189 
2190  case AMPM:
2191  mer = val;
2192  break;
2193 
2194  case ADBC:
2195  bc = (val == BC);
2196  break;
2197 
2198  case UNITS:
2199  tmask = 0;
2200  ptype = val;
2201  break;
2202 
2203  case ISOTIME:
2204  tmask = 0;
2205 
2206  /***
2207  * We will need one of the following fields:
2208  * DTK_NUMBER should be hhmmss.fff
2209  * DTK_TIME should be hh:mm:ss.fff
2210  * DTK_DATE should be hhmmss-zz
2211  ***/
2212  if (i >= nf - 1 ||
2213  (ftype[i + 1] != DTK_NUMBER &&
2214  ftype[i + 1] != DTK_TIME &&
2215  ftype[i + 1] != DTK_DATE))
2216  return DTERR_BAD_FORMAT;
2217 
2218  ptype = val;
2219  break;
2220 
2221  case UNKNOWN_FIELD:
2222 
2223  /*
2224  * Before giving up and declaring error, check to see
2225  * if it is an all-alpha timezone name.
2226  */
2227  namedTz = pg_tzset(field[i]);
2228  if (!namedTz)
2229  return DTERR_BAD_FORMAT;
2230  /* we'll apply the zone setting below */
2231  tmask = DTK_M(TZ);
2232  break;
2233 
2234  default:
2235  return DTERR_BAD_FORMAT;
2236  }
2237  break;
2238 
2239  default:
2240  return DTERR_BAD_FORMAT;
2241  }
2242 
2243  if (tmask & fmask)
2244  return DTERR_BAD_FORMAT;
2245  fmask |= tmask;
2246  } /* end loop over fields */
2247 
2248  /* do final checking/adjustment of Y/M/D fields */
2249  dterr = ValidateDate(fmask, isjulian, is2digits, bc, tm);
2250  if (dterr)
2251  return dterr;
2252 
2253  /* handle AM/PM */
2254  if (mer != HR24 && tm->tm_hour > HOURS_PER_DAY / 2)
2255  return DTERR_FIELD_OVERFLOW;
2256  if (mer == AM && tm->tm_hour == HOURS_PER_DAY / 2)
2257  tm->tm_hour = 0;
2258  else if (mer == PM && tm->tm_hour != HOURS_PER_DAY / 2)
2259  tm->tm_hour += HOURS_PER_DAY / 2;
2260 
2261  /* check for time overflow */
2262  if (time_overflows(tm->tm_hour, tm->tm_min, tm->tm_sec, *fsec))
2263  return DTERR_FIELD_OVERFLOW;
2264 
2265  if ((fmask & DTK_TIME_M) != DTK_TIME_M)
2266  return DTERR_BAD_FORMAT;
2267 
2268  /*
2269  * If we had a full timezone spec, compute the offset (we could not do it
2270  * before, because we may need the date to resolve DST status).
2271  */
2272  if (namedTz != NULL)
2273  {
2274  long int gmtoff;
2275 
2276  /* daylight savings time modifier disallowed with full TZ */
2277  if (fmask & DTK_M(DTZMOD))
2278  return DTERR_BAD_FORMAT;
2279 
2280  /* if non-DST zone, we do not need to know the date */
2281  if (pg_get_timezone_offset(namedTz, &gmtoff))
2282  {
2283  *tzp = -(int) gmtoff;
2284  }
2285  else
2286  {
2287  /* a date has to be specified */
2288  if ((fmask & DTK_DATE_M) != DTK_DATE_M)
2289  return DTERR_BAD_FORMAT;
2290  *tzp = DetermineTimeZoneOffset(tm, namedTz);
2291  }
2292  }
2293 
2294  /*
2295  * Likewise, if we had a dynamic timezone abbreviation, resolve it now.
2296  */
2297  if (abbrevTz != NULL)
2298  {
2299  struct pg_tm tt,
2300  *tmp = &tt;
2301 
2302  /*
2303  * daylight savings time modifier but no standard timezone? then error
2304  */
2305  if (fmask & DTK_M(DTZMOD))
2306  return DTERR_BAD_FORMAT;
2307 
2308  if ((fmask & DTK_DATE_M) == 0)
2309  GetCurrentDateTime(tmp);
2310  else
2311  {
2312  /* a date has to be specified */
2313  if ((fmask & DTK_DATE_M) != DTK_DATE_M)
2314  return DTERR_BAD_FORMAT;
2315  tmp->tm_year = tm->tm_year;
2316  tmp->tm_mon = tm->tm_mon;
2317  tmp->tm_mday = tm->tm_mday;
2318  }
2319  tmp->tm_hour = tm->tm_hour;
2320  tmp->tm_min = tm->tm_min;
2321  tmp->tm_sec = tm->tm_sec;
2322  *tzp = DetermineTimeZoneAbbrevOffset(tmp, abbrev, abbrevTz);
2323  tm->tm_isdst = tmp->tm_isdst;
2324  }
2325 
2326  /* timezone not specified? then use session timezone */
2327  if (tzp != NULL && !(fmask & DTK_M(TZ)))
2328  {
2329  struct pg_tm tt,
2330  *tmp = &tt;
2331 
2332  /*
2333  * daylight savings time modifier but no standard timezone? then error
2334  */
2335  if (fmask & DTK_M(DTZMOD))
2336  return DTERR_BAD_FORMAT;
2337 
2338  if ((fmask & DTK_DATE_M) == 0)
2339  GetCurrentDateTime(tmp);
2340  else
2341  {
2342  /* a date has to be specified */
2343  if ((fmask & DTK_DATE_M) != DTK_DATE_M)
2344  return DTERR_BAD_FORMAT;
2345  tmp->tm_year = tm->tm_year;
2346  tmp->tm_mon = tm->tm_mon;
2347  tmp->tm_mday = tm->tm_mday;
2348  }
2349  tmp->tm_hour = tm->tm_hour;
2350  tmp->tm_min = tm->tm_min;
2351  tmp->tm_sec = tm->tm_sec;
2353  tm->tm_isdst = tmp->tm_isdst;
2354  }
2355 
2356  return 0;
2357 }
#define DTERR_BAD_FORMAT
Definition: datetime.h:280
void GetCurrentDateTime(struct pg_tm *tm)
Definition: datetime.c:349
#define PM
Definition: datetime.h:73
#define DAY
Definition: datetime.h:94
#define UNITS
Definition: datetime.h:108
#define IGNORE_DTF
Definition: datetime.h:99
PGDLLIMPORT pg_tz * session_timezone
Definition: pgtz.c:28
#define DTK_JULIAN
Definition: datetime.h:174
#define DTK_YEAR
Definition: datetime.h:168
int tm_isdst
Definition: pgtime.h:42
#define YEAR
Definition: datetime.h:93
int tm_hour
Definition: pgtime.h:36
#define DTK_TIME_M
Definition: datetime.h:193
int errcode(int sqlerrcode)
Definition: elog.c:698
#define TZ
Definition: datetime.h:96
#define UNKNOWN_FIELD
Definition: datetime.h:125
#define SECOND
Definition: datetime.h:103
static int DecodeNumberField(int len, char *str, int fmask, int *tmask, struct pg_tm *tm, fsec_t *fsec, bool *is2digits)
Definition: datetime.c:2820
#define ADBC
Definition: datetime.h:109
Definition: pgtime.h:32
#define INTERVAL_FULL_RANGE
Definition: timestamp.h:48
#define DTK_DATE_M
Definition: datetime.h:192
pg_tz * pg_tzset(const char *tzname)
Definition: pgtz.c:234
#define DTK_MONTH
Definition: datetime.h:166
#define DTK_TZ
Definition: datetime.h:147
#define DTK_HOUR
Definition: datetime.h:163
#define ERROR
Definition: elog.h:46
#define DTERR_FIELD_OVERFLOW
Definition: datetime.h:281
#define AM
Definition: datetime.h:72
int DecodeTimezoneAbbrev(int field, char *lowtoken, int *offset, pg_tz **tz)
Definition: datetime.c:2985
#define DTK_SECOND
Definition: datetime.h:161
int DecodeTimezone(char *str, int *tzp)
Definition: datetime.c:2907
int tm_mday
Definition: pgtime.h:37
#define HOURS_PER_DAY
Definition: timestamp.h:78
int tm_mon
Definition: pgtime.h:38
int DetermineTimeZoneOffset(struct pg_tm *tm, pg_tz *tzp)
Definition: datetime.c:1478
bool time_overflows(int hour, int min, int sec, fsec_t fsec)
Definition: date.c:1426
int strtoint(const char *pg_restrict str, char **pg_restrict endptr, int base)
Definition: string.c:50
static int DecodeDate(char *str, int fmask, int *tmask, bool *is2digits, struct pg_tm *tm)
Definition: datetime.c:2370
void GetCurrentTimeUsec(struct pg_tm *tm, fsec_t *fsec, int *tzp)
Definition: datetime.c:370
#define DTK_NUMBER
Definition: datetime.h:142
#define MINUTE
Definition: datetime.h:102
#define USECS_PER_DAY
Definition: timestamp.h:91
void j2date(int jd, int *year, int *month, int *day)
Definition: datetime.c:294
#define MONTH
Definition: datetime.h:92
#define DTK_MINUTE
Definition: datetime.h:162
static int ParseFractionalSecond(char *cp, fsec_t *fsec)
Definition: datetime.c:534
#define BC
Definition: datetime.h:77
Definition: pgtz.h:65
#define ISOTIME
Definition: datetime.h:116
#define DTK_TIME
Definition: datetime.h:146
#define ereport(elevel,...)
Definition: elog.h:157
static int DecodeTime(char *str, int fmask, int range, int *tmask, struct pg_tm *tm, fsec_t *fsec)
Definition: datetime.c:2559
void dt2time(Timestamp jd, int *hour, int *min, int *sec, fsec_t *fsec)
Definition: timestamp.c:1795
#define AMPM
Definition: datetime.h:100
#define DTK_STRING
Definition: datetime.h:143
#define DTK_DAY
Definition: datetime.h:164
static int DecodeNumber(int flen, char *field, bool haveTextMonth, int fmask, int *tmask, struct pg_tm *tm, fsec_t *fsec, bool *is2digits)
Definition: datetime.c:2635
#define RESERV
Definition: datetime.h:91
int DecodeSpecial(int field, char *lowtoken, int *val)
Definition: datetime.c:3040
#define DTK_ALL_SECS_M
Definition: datetime.h:191
#define DTZ
Definition: datetime.h:97
int tm_year
Definition: pgtime.h:39
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define DYNTZ
Definition: datetime.h:98
#define DTK_NOW
Definition: datetime.h:154
int i
#define DTK_M(t)
Definition: datetime.h:188
#define HOUR
Definition: datetime.h:101
bool pg_get_timezone_offset(const pg_tz *tz, long int *gmtoff)
Definition: localtime.c:1849
#define DTK_ZULU
Definition: datetime.h:158
int tm_sec
Definition: pgtime.h:34
#define DTZMOD
Definition: datetime.h:123
int tm_min
Definition: pgtime.h:35
int DetermineTimeZoneAbbrevOffset(struct pg_tm *tm, const char *abbr, pg_tz *tzp)
Definition: datetime.c:1639
long val
Definition: informix.c:664
#define DTK_DATE
Definition: datetime.h:145
#define HR24
Definition: datetime.h:74
#define DTK_SPECIAL
Definition: datetime.h:150
int ValidateDate(int fmask, bool isjulian, bool is2digits, bool bc, struct pg_tm *tm)
Definition: datetime.c:2480

◆ DecodeTimezone()

int DecodeTimezone ( char *  str,
int *  tzp 
)

Definition at line 2907 of file datetime.c.

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

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

2908 {
2909  int tz;
2910  int hr,
2911  min,
2912  sec = 0;
2913  char *cp;
2914 
2915  /* leading character must be "+" or "-" */
2916  if (*str != '+' && *str != '-')
2917  return DTERR_BAD_FORMAT;
2918 
2919  errno = 0;
2920  hr = strtoint(str + 1, &cp, 10);
2921  if (errno == ERANGE)
2922  return DTERR_TZDISP_OVERFLOW;
2923 
2924  /* explicit delimiter? */
2925  if (*cp == ':')
2926  {
2927  errno = 0;
2928  min = strtoint(cp + 1, &cp, 10);
2929  if (errno == ERANGE)
2930  return DTERR_TZDISP_OVERFLOW;
2931  if (*cp == ':')
2932  {
2933  errno = 0;
2934  sec = strtoint(cp + 1, &cp, 10);
2935  if (errno == ERANGE)
2936  return DTERR_TZDISP_OVERFLOW;
2937  }
2938  }
2939  /* otherwise, might have run things together... */
2940  else if (*cp == '\0' && strlen(str) > 3)
2941  {
2942  min = hr % 100;
2943  hr = hr / 100;
2944  /* we could, but don't, support a run-together hhmmss format */
2945  }
2946  else
2947  min = 0;
2948 
2949  /* Range-check the values; see notes in datatype/timestamp.h */
2950  if (hr < 0 || hr > MAX_TZDISP_HOUR)
2951  return DTERR_TZDISP_OVERFLOW;
2952  if (min < 0 || min >= MINS_PER_HOUR)
2953  return DTERR_TZDISP_OVERFLOW;
2954  if (sec < 0 || sec >= SECS_PER_MINUTE)
2955  return DTERR_TZDISP_OVERFLOW;
2956 
2957  tz = (hr * MINS_PER_HOUR + min) * SECS_PER_MINUTE + sec;
2958  if (*str == '-')
2959  tz = -tz;
2960 
2961  *tzp = -tz;
2962 
2963  if (*cp != '\0')
2964  return DTERR_BAD_FORMAT;
2965 
2966  return 0;
2967 }
#define DTERR_BAD_FORMAT
Definition: datetime.h:280
#define MINS_PER_HOUR
Definition: timestamp.h:89
#define MAX_TZDISP_HOUR
Definition: timestamp.h:103
#define SECS_PER_MINUTE
Definition: timestamp.h:88
int strtoint(const char *pg_restrict str, char **pg_restrict endptr, int base)
Definition: string.c:50
#define DTERR_TZDISP_OVERFLOW
Definition: datetime.h:284

◆ DecodeTimezoneAbbrev()

int DecodeTimezoneAbbrev ( int  field,
char *  lowtoken,
int *  offset,
pg_tz **  tz 
)

Definition at line 2985 of file datetime.c.

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

Referenced by DecodeDateTime(), DecodeTimeOnly(), parse_sane_timezone(), timestamp_zone(), timestamptz_trunc_zone(), timestamptz_zone(), and timetz_zone().

2987 {
2988  int type;
2989  const datetkn *tp;
2990 
2991  tp = abbrevcache[field];
2992  /* use strncmp so that we match truncated tokens */
2993  if (tp == NULL || strncmp(lowtoken, tp->token, TOKMAXLEN) != 0)
2994  {
2995  if (zoneabbrevtbl)
2996  tp = datebsearch(lowtoken, zoneabbrevtbl->abbrevs,
2998  else
2999  tp = NULL;
3000  }
3001  if (tp == NULL)
3002  {
3003  type = UNKNOWN_FIELD;
3004  *offset = 0;
3005  *tz = NULL;
3006  }
3007  else
3008  {
3009  abbrevcache[field] = tp;
3010  type = tp->type;
3011  if (type == DYNTZ)
3012  {
3013  *offset = 0;
3015  }
3016  else
3017  {
3018  *offset = tp->value;
3019  *tz = NULL;
3020  }
3021  }
3022 
3023  return type;
3024 }
static pg_tz * FetchDynamicTimeZone(TimeZoneAbbrevTable *tbl, const datetkn *tp)
Definition: datetime.c:4622
#define UNKNOWN_FIELD
Definition: datetime.h:125
int32 value
Definition: datetime.h:212
char token[TOKMAXLEN+1]
Definition: datetime.h:210
datetkn abbrevs[FLEXIBLE_ARRAY_MEMBER]
Definition: datetime.h:220
static const datetkn * datebsearch(const char *key, const datetkn *base, int nel)
Definition: datetime.c:3809
char type
Definition: datetime.h:211
static TimeZoneAbbrevTable * zoneabbrevtbl
Definition: datetime.c:237
static const datetkn * abbrevcache[MAXDATEFIELDS]
Definition: datetime.c:245
#define DYNTZ
Definition: datetime.h:98
#define TOKMAXLEN
Definition: datetime.h:205

◆ DecodeUnits()

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

Definition at line 3727 of file datetime.c.

3728 {
3729  int type;
3730  const datetkn *tp;
3731 
3732  tp = deltacache[field];
3733  /* use strncmp so that we match truncated tokens */
3734  if (tp == NULL || strncmp(lowtoken, tp->token, TOKMAXLEN) != 0)
3735  {
3736  tp = datebsearch(lowtoken, deltatktbl, szdeltatktbl);
3737  }
3738  if (tp == NULL)
3739  {
3740  type = UNKNOWN_FIELD;
3741  *val = 0;
3742  }
3743  else
3744  {
3745  deltacache[field] = tp;
3746  type = tp->type;
3747  *val = tp->value;
3748  }
3749 
3750  return type;
3751 } /* DecodeUnits() */
static const datetkn * deltacache[MAXDATEFIELDS]
Definition: datetime.c:243
#define UNKNOWN_FIELD
Definition: datetime.h:125
int32 value
Definition: datetime.h:212
static const int szdeltatktbl
Definition: datetime.c:235
char token[TOKMAXLEN+1]
Definition: datetime.h:210
static const datetkn * datebsearch(const char *key, const datetkn *base, int nel)
Definition: datetime.c:3809
static const datetkn deltatktbl[]
Definition: datetime.c:170
char type
Definition: datetime.h:211
#define TOKMAXLEN
Definition: datetime.h:205
long val
Definition: informix.c:664

◆ DetermineTimeZoneAbbrevOffset()

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

Definition at line 1639 of file datetime.c.

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

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

1640 {
1641  pg_time_t t;
1642  int zone_offset;
1643  int abbr_offset;
1644  int abbr_isdst;
1645 
1646  /*
1647  * Compute the UTC time we want to probe at. (In event of overflow, we'll
1648  * probe at the epoch, which is a bit random but probably doesn't matter.)
1649  */
1650  zone_offset = DetermineTimeZoneOffsetInternal(tm, tzp, &t);
1651 
1652  /*
1653  * Try to match the abbreviation to something in the zone definition.
1654  */
1655  if (DetermineTimeZoneAbbrevOffsetInternal(t, abbr, tzp,
1656  &abbr_offset, &abbr_isdst))
1657  {
1658  /* Success, so use the abbrev-specific answers. */
1659  tm->tm_isdst = abbr_isdst;
1660  return abbr_offset;
1661  }
1662 
1663  /*
1664  * No match, so use the answers we already got from
1665  * DetermineTimeZoneOffsetInternal.
1666  */
1667  return zone_offset;
1668 }
int64 pg_time_t
Definition: pgtime.h:23
int tm_isdst
Definition: pgtime.h:42
static int DetermineTimeZoneOffsetInternal(struct pg_tm *tm, pg_tz *tzp, pg_time_t *tp)
Definition: datetime.c:1500
static bool DetermineTimeZoneAbbrevOffsetInternal(pg_time_t t, const char *abbr, pg_tz *tzp, int *offset, int *isdst)
Definition: datetime.c:1714

◆ DetermineTimeZoneAbbrevOffsetTS()

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

Definition at line 1677 of file datetime.c.

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

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

1679 {
1681  int zone_offset;
1682  int abbr_offset;
1683  int tz;
1684  struct pg_tm tm;
1685  fsec_t fsec;
1686 
1687  /*
1688  * If the abbrev matches anything in the zone data, this is pretty easy.
1689  */
1690  if (DetermineTimeZoneAbbrevOffsetInternal(t, abbr, tzp,
1691  &abbr_offset, isdst))
1692  return abbr_offset;
1693 
1694  /*
1695  * Else, break down the timestamp so we can use DetermineTimeZoneOffset.
1696  */
1697  if (timestamp2tm(ts, &tz, &tm, &fsec, NULL, tzp) != 0)
1698  ereport(ERROR,
1699  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1700  errmsg("timestamp out of range")));
1701 
1702  zone_offset = DetermineTimeZoneOffset(&tm, tzp);
1703  *isdst = tm.tm_isdst;
1704  return zone_offset;
1705 }
int64 pg_time_t
Definition: pgtime.h:23
int tm_isdst
Definition: pgtime.h:42
int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm *tm, fsec_t *fsec, const char **tzn, pg_tz *attimezone)
Definition: timestamp.c:1822
int errcode(int sqlerrcode)
Definition: elog.c:698
Definition: pgtime.h:32
static struct pg_tm tm
Definition: localtime.c:102
#define ERROR
Definition: elog.h:46
int DetermineTimeZoneOffset(struct pg_tm *tm, pg_tz *tzp)
Definition: datetime.c:1478
pg_time_t timestamptz_to_time_t(TimestampTz t)
Definition: timestamp.c:1754
int32 fsec_t
Definition: timestamp.h:41
#define ereport(elevel,...)
Definition: elog.h:157
int errmsg(const char *fmt,...)
Definition: elog.c:909
static bool DetermineTimeZoneAbbrevOffsetInternal(pg_time_t t, const char *abbr, pg_tz *tzp, int *offset, int *isdst)
Definition: datetime.c:1714

◆ DetermineTimeZoneOffset()

int DetermineTimeZoneOffset ( struct pg_tm tm,
pg_tz tzp 
)

Definition at line 1478 of file datetime.c.

References DetermineTimeZoneOffsetInternal().

Referenced by date2timestamptz_opt_overflow(), DecodeDateTime(), DecodeTimeOnly(), DetermineTimeZoneAbbrevOffsetTS(), parse_sane_timezone(), time_timetz(), timestamp2timestamptz_opt_overflow(), timestamp_zone(), timestamptz_pl_interval(), timestamptz_trunc_internal(), and to_timestamp().

1479 {
1480  pg_time_t t;
1481 
1482  return DetermineTimeZoneOffsetInternal(tm, tzp, &t);
1483 }
int64 pg_time_t
Definition: pgtime.h:23
static int DetermineTimeZoneOffsetInternal(struct pg_tm *tm, pg_tz *tzp, pg_time_t *tp)
Definition: datetime.c:1500

◆ EncodeDateOnly()

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

Definition at line 3883 of file datetime.c.

References Assert, DateOrder, DATEORDER_DMY, MONTHS_PER_YEAR, pg_ultostr_zeropad(), 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().

3884 {
3885  Assert(tm->tm_mon >= 1 && tm->tm_mon <= MONTHS_PER_YEAR);
3886 
3887  switch (style)
3888  {
3889  case USE_ISO_DATES:
3890  case USE_XSD_DATES:
3891  /* compatible with ISO date formats */
3893  (tm->tm_year > 0) ? tm->tm_year : -(tm->tm_year - 1), 4);
3894  *str++ = '-';
3895  str = pg_ultostr_zeropad(str, tm->tm_mon, 2);
3896  *str++ = '-';
3897  str = pg_ultostr_zeropad(str, tm->tm_mday, 2);
3898  break;
3899 
3900  case USE_SQL_DATES:
3901  /* compatible with Oracle/Ingres date formats */
3902  if (DateOrder == DATEORDER_DMY)
3903  {
3904  str = pg_ultostr_zeropad(str, tm->tm_mday, 2);
3905  *str++ = '/';
3906  str = pg_ultostr_zeropad(str, tm->tm_mon, 2);
3907  }
3908  else
3909  {
3910  str = pg_ultostr_zeropad(str, tm->tm_mon, 2);
3911  *str++ = '/';
3912  str = pg_ultostr_zeropad(str, tm->tm_mday, 2);
3913  }
3914  *str++ = '/';
3916  (tm->tm_year > 0) ? tm->tm_year : -(tm->tm_year - 1), 4);
3917  break;
3918 
3919  case USE_GERMAN_DATES:
3920  /* German-style date format */
3921  str = pg_ultostr_zeropad(str, tm->tm_mday, 2);
3922  *str++ = '.';
3923  str = pg_ultostr_zeropad(str, tm->tm_mon, 2);
3924  *str++ = '.';
3926  (tm->tm_year > 0) ? tm->tm_year : -(tm->tm_year - 1), 4);
3927  break;
3928 
3929  case USE_POSTGRES_DATES:
3930  default:
3931  /* traditional date-only style for Postgres */
3932  if (DateOrder == DATEORDER_DMY)
3933  {
3934  str = pg_ultostr_zeropad(str, tm->tm_mday, 2);
3935  *str++ = '-';
3936  str = pg_ultostr_zeropad(str, tm->tm_mon, 2);
3937  }
3938  else
3939  {
3940  str = pg_ultostr_zeropad(str, tm->tm_mon, 2);
3941  *str++ = '-';
3942  str = pg_ultostr_zeropad(str, tm->tm_mday, 2);
3943  }
3944  *str++ = '-';
3946  (tm->tm_year > 0) ? tm->tm_year : -(tm->tm_year - 1), 4);
3947  break;
3948  }
3949 
3950  if (tm->tm_year <= 0)
3951  {
3952  memcpy(str, " BC", 3); /* Don't copy NUL */
3953  str += 3;
3954  }
3955  *str = '\0';
3956 }
char * pg_ultostr_zeropad(char *str, uint32 value, int32 minwidth)
Definition: numutils.c:563
#define USE_SQL_DATES
Definition: miscadmin.h:230
#define USE_ISO_DATES
Definition: miscadmin.h:229
#define MONTHS_PER_YEAR
Definition: timestamp.h:69
int tm_mday
Definition: pgtime.h:37
int tm_mon
Definition: pgtime.h:38
#define USE_POSTGRES_DATES
Definition: miscadmin.h:228
int DateOrder
Definition: globals.c:119
#define DATEORDER_DMY
Definition: miscadmin.h:236
#define Assert(condition)
Definition: c.h:804
#define USE_XSD_DATES
Definition: miscadmin.h:232
int tm_year
Definition: pgtime.h:39
#define USE_GERMAN_DATES
Definition: miscadmin.h:231

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

References AppendTimestampSeconds(), Assert, date2j(), DateOrder, DATEORDER_DMY, days, EncodeTimezone(), j2day(), MAXTZLEN, months, MONTHS_PER_YEAR, pg_ultostr_zeropad(), sprintf, 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().

3999 {
4000  int day;
4001 
4002  Assert(tm->tm_mon >= 1 && tm->tm_mon <= MONTHS_PER_YEAR);
4003 
4004  /*
4005  * Negative tm_isdst means we have no valid time zone translation.
4006  */
4007  if (tm->tm_isdst < 0)
4008  print_tz = false;
4009 
4010  switch (style)
4011  {
4012  case USE_ISO_DATES:
4013  case USE_XSD_DATES:
4014  /* Compatible with ISO-8601 date formats */
4016  (tm->tm_year > 0) ? tm->tm_year : -(tm->tm_year - 1), 4);
4017  *str++ = '-';
4018  str = pg_ultostr_zeropad(str, tm->tm_mon, 2);
4019  *str++ = '-';
4020  str = pg_ultostr_zeropad(str, tm->tm_mday, 2);
4021  *str++ = (style == USE_ISO_DATES) ? ' ' : 'T';
4022  str = pg_ultostr_zeropad(str, tm->tm_hour, 2);
4023  *str++ = ':';
4024  str = pg_ultostr_zeropad(str, tm->tm_min, 2);
4025  *str++ = ':';
4026  str = AppendTimestampSeconds(str, tm, fsec);
4027  if (print_tz)
4028  str = EncodeTimezone(str, tz, style);
4029  break;
4030 
4031  case USE_SQL_DATES:
4032  /* Compatible with Oracle/Ingres date formats */
4033  if (DateOrder == DATEORDER_DMY)
4034  {
4035  str = pg_ultostr_zeropad(str, tm->tm_mday, 2);
4036  *str++ = '/';
4037  str = pg_ultostr_zeropad(str, tm->tm_mon, 2);
4038  }
4039  else
4040  {
4041  str = pg_ultostr_zeropad(str, tm->tm_mon, 2);
4042  *str++ = '/';
4043  str = pg_ultostr_zeropad(str, tm->tm_mday, 2);
4044  }
4045  *str++ = '/';
4047  (tm->tm_year > 0) ? tm->tm_year : -(tm->tm_year - 1), 4);
4048  *str++ = ' ';
4049  str = pg_ultostr_zeropad(str, tm->tm_hour, 2);
4050  *str++ = ':';
4051  str = pg_ultostr_zeropad(str, tm->tm_min, 2);
4052  *str++ = ':';
4053  str = AppendTimestampSeconds(str, tm, fsec);
4054 
4055  /*
4056  * Note: the uses of %.*s in this function would be risky if the
4057  * timezone names ever contain non-ASCII characters, since we are
4058  * not being careful to do encoding-aware clipping. However, all
4059  * TZ abbreviations in the IANA database are plain ASCII.
4060  */
4061  if (print_tz)
4062  {
4063  if (tzn)
4064  {
4065  sprintf(str, " %.*s", MAXTZLEN, tzn);
4066  str += strlen(str);
4067  }
4068  else
4069  str = EncodeTimezone(str, tz, style);
4070  }
4071  break;
4072 
4073  case USE_GERMAN_DATES:
4074  /* German variant on European style */
4075  str = pg_ultostr_zeropad(str, tm->tm_mday, 2);
4076  *str++ = '.';
4077  str = pg_ultostr_zeropad(str, tm->tm_mon, 2);
4078  *str++ = '.';
4080  (tm->tm_year > 0) ? tm->tm_year : -(tm->tm_year - 1), 4);
4081  *str++ = ' ';
4082  str = pg_ultostr_zeropad(str, tm->tm_hour, 2);
4083  *str++ = ':';
4084  str = pg_ultostr_zeropad(str, tm->tm_min, 2);
4085  *str++ = ':';
4086  str = AppendTimestampSeconds(str, tm, fsec);
4087 
4088  if (print_tz)
4089  {
4090  if (tzn)
4091  {
4092  sprintf(str, " %.*s", MAXTZLEN, tzn);
4093  str += strlen(str);
4094  }
4095  else
4096  str = EncodeTimezone(str, tz, style);
4097  }
4098  break;
4099 
4100  case USE_POSTGRES_DATES:
4101  default:
4102  /* Backward-compatible with traditional Postgres abstime dates */
4103  day = date2j(tm->tm_year, tm->tm_mon, tm->tm_mday);
4104  tm->tm_wday = j2day(day);
4105  memcpy(str, days[tm->tm_wday], 3);
4106  str += 3;
4107  *str++ = ' ';
4108  if (DateOrder == DATEORDER_DMY)
4109  {
4110  str = pg_ultostr_zeropad(str, tm->tm_mday, 2);
4111  *str++ = ' ';
4112  memcpy(str, months[tm->tm_mon - 1], 3);
4113  str += 3;
4114  }
4115  else
4116  {
4117  memcpy(str, months[tm->tm_mon - 1], 3);
4118  str += 3;
4119  *str++ = ' ';
4120  str = pg_ultostr_zeropad(str, tm->tm_mday, 2);
4121  }
4122  *str++ = ' ';
4123  str = pg_ultostr_zeropad(str, tm->tm_hour, 2);
4124  *str++ = ':';
4125  str = pg_ultostr_zeropad(str, tm->tm_min, 2);
4126  *str++ = ':';
4127  str = AppendTimestampSeconds(str, tm, fsec);
4128  *str++ = ' ';
4130  (tm->tm_year > 0) ? tm->tm_year : -(tm->tm_year - 1), 4);
4131 
4132  if (print_tz)
4133  {
4134  if (tzn)
4135  {
4136  sprintf(str, " %.*s", MAXTZLEN, tzn);
4137  str += strlen(str);
4138  }
4139  else
4140  {
4141  /*
4142  * We have a time zone, but no string version. Use the
4143  * numeric form, but be sure to include a leading space to
4144  * avoid formatting something which would be rejected by
4145  * the date/time parser later. - thomas 2001-10-19
4146  */
4147  *str++ = ' ';
4148  str = EncodeTimezone(str, tz, style);
4149  }
4150  }
4151  break;
4152  }
4153 
4154  if (tm->tm_year <= 0)
4155  {
4156  memcpy(str, " BC", 3); /* Don't copy NUL */
4157  str += 3;
4158  }
4159  *str = '\0';
4160 }
int tm_wday
Definition: pgtime.h:40
int tm_isdst
Definition: pgtime.h:42
int tm_hour
Definition: pgtime.h:36
static char * EncodeTimezone(char *str, int tz, int style)
Definition: datetime.c:3845
char * pg_ultostr_zeropad(char *str, uint32 value, int32 minwidth)
Definition: numutils.c:563
#define USE_SQL_DATES
Definition: miscadmin.h:230
#define MAXTZLEN
Definition: miscadmin.h:256
#define USE_ISO_DATES
Definition: miscadmin.h:229
#define MONTHS_PER_YEAR
Definition: timestamp.h:69
#define sprintf
Definition: port.h:219
const char *const months[]
Definition: datetime.c:65
int j2day(int date)
Definition: datetime.c:327
int tm_mday
Definition: pgtime.h:37
int tm_mon
Definition: pgtime.h:38
#define USE_POSTGRES_DATES
Definition: miscadmin.h:228
const char *const days[]
Definition: datetime.c:68
int DateOrder
Definition: globals.c:119
static char * AppendTimestampSeconds(char *cp, struct pg_tm *tm, fsec_t fsec)
Definition: datetime.c:494
#define DATEORDER_DMY
Definition: miscadmin.h:236
int date2j(int y, int m, int d)
Definition: datetime.c:269
#define Assert(condition)
Definition: c.h:804
#define USE_XSD_DATES
Definition: miscadmin.h:232
int tm_year
Definition: pgtime.h:39
#define USE_GERMAN_DATES
Definition: miscadmin.h:231
int tm_min
Definition: pgtime.h:35

◆ EncodeInterval()

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

Definition at line 4241 of file datetime.c.

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

Referenced by interval_out().

4242 {
4243  char *cp = str;
4244  int year = tm->tm_year;
4245  int mon = tm->tm_mon;
4246  int mday = tm->tm_mday;
4247  int hour = tm->tm_hour;
4248  int min = tm->tm_min;
4249  int sec = tm->tm_sec;
4250  bool is_before = false;
4251  bool is_zero = true;
4252 
4253  /*
4254  * The sign of year and month are guaranteed to match, since they are
4255  * stored internally as "month". But we'll need to check for is_before and
4256  * is_zero when determining the signs of day and hour/minute/seconds
4257  * fields.
4258  */
4259  switch (style)
4260  {
4261  /* SQL Standard interval format */
4262  case INTSTYLE_SQL_STANDARD:
4263  {
4264  bool has_negative = year < 0 || mon < 0 ||
4265  mday < 0 || hour < 0 ||
4266  min < 0 || sec < 0 || fsec < 0;
4267  bool has_positive = year > 0 || mon > 0 ||
4268  mday > 0 || hour > 0 ||
4269  min > 0 || sec > 0 || fsec > 0;
4270  bool has_year_month = year != 0 || mon != 0;
4271  bool has_day_time = mday != 0 || hour != 0 ||
4272  min != 0 || sec != 0 || fsec != 0;
4273  bool has_day = mday != 0;
4274  bool sql_standard_value = !(has_negative && has_positive) &&
4275  !(has_year_month && has_day_time);
4276 
4277  /*
4278  * SQL Standard wants only 1 "<sign>" preceding the whole
4279  * interval ... but can't do that if mixed signs.
4280  */
4281  if (has_negative && sql_standard_value)
4282  {
4283  *cp++ = '-';
4284  year = -year;
4285  mon = -mon;
4286  mday = -mday;
4287  hour = -hour;
4288  min = -min;
4289  sec = -sec;
4290  fsec = -fsec;
4291  }
4292 
4293  if (!has_negative && !has_positive)
4294  {
4295  sprintf(cp, "0");
4296  }
4297  else if (!sql_standard_value)
4298  {
4299  /*
4300  * For non sql-standard interval values, force outputting
4301  * the signs to avoid ambiguities with intervals with
4302  * mixed sign components.
4303  */
4304  char year_sign = (year < 0 || mon < 0) ? '-' : '+';
4305  char day_sign = (mday < 0) ? '-' : '+';
4306  char sec_sign = (hour < 0 || min < 0 ||
4307  sec < 0 || fsec < 0) ? '-' : '+';
4308 
4309  sprintf(cp, "%c%d-%d %c%d %c%d:%02d:",
4310  year_sign, abs(year), abs(mon),
4311  day_sign, abs(mday),
4312  sec_sign, abs(hour), abs(min));
4313  cp += strlen(cp);
4314  cp = AppendSeconds(cp, sec, fsec, MAX_INTERVAL_PRECISION, true);
4315  *cp = '\0';
4316  }
4317  else if (has_year_month)
4318  {
4319  sprintf(cp, "%d-%d", year, mon);
4320  }
4321  else if (has_day)
4322  {
4323  sprintf(cp, "%d %d:%02d:", mday, hour, min);
4324  cp += strlen(cp);
4325  cp = AppendSeconds(cp, sec, fsec, MAX_INTERVAL_PRECISION, true);
4326  *cp = '\0';
4327  }
4328  else
4329  {
4330  sprintf(cp, "%d:%02d:", hour, min);
4331  cp += strlen(cp);
4332  cp = AppendSeconds(cp, sec, fsec, MAX_INTERVAL_PRECISION, true);
4333  *cp = '\0';
4334  }
4335  }
4336  break;
4337 
4338  /* ISO 8601 "time-intervals by duration only" */
4339  case INTSTYLE_ISO_8601:
4340  /* special-case zero to avoid printing nothing */
4341  if (year == 0 && mon == 0 && mday == 0 &&
4342  hour == 0 && min == 0 && sec == 0 && fsec == 0)
4343  {
4344  sprintf(cp, "PT0S");
4345  break;
4346  }
4347  *cp++ = 'P';
4348  cp = AddISO8601IntPart(cp, year, 'Y');
4349  cp = AddISO8601IntPart(cp, mon, 'M');
4350  cp = AddISO8601IntPart(cp, mday, 'D');
4351  if (hour != 0 || min != 0 || sec != 0 || fsec != 0)
4352  *cp++ = 'T';
4353  cp = AddISO8601IntPart(cp, hour, 'H');
4354  cp = AddISO8601IntPart(cp, min, 'M');
4355  if (sec != 0 || fsec != 0)
4356  {
4357  if (sec < 0 || fsec < 0)
4358  *cp++ = '-';
4359  cp = AppendSeconds(cp, sec, fsec, MAX_INTERVAL_PRECISION, false);
4360  *cp++ = 'S';
4361  *cp++ = '\0';
4362  }
4363  break;
4364 
4365  /* Compatible with postgresql < 8.4 when DateStyle = 'iso' */
4366  case INTSTYLE_POSTGRES:
4367  cp = AddPostgresIntPart(cp, year, "year", &is_zero, &is_before);
4368 
4369  /*
4370  * Ideally we should spell out "month" like we do for "year" and
4371  * "day". However, for backward compatibility, we can't easily
4372  * fix this. bjm 2011-05-24
4373  */
4374  cp = AddPostgresIntPart(cp, mon, "mon", &is_zero, &is_before);
4375  cp = AddPostgresIntPart(cp, mday, "day", &is_zero, &is_before);
4376  if (is_zero || hour != 0 || min != 0 || sec != 0 || fsec != 0)
4377  {
4378  bool minus = (hour < 0 || min < 0 || sec < 0 || fsec < 0);
4379 
4380  sprintf(cp, "%s%s%02d:%02d:",
4381  is_zero ? "" : " ",
4382  (minus ? "-" : (is_before ? "+" : "")),
4383  abs(hour), abs(min));
4384  cp += strlen(cp);
4385  cp = AppendSeconds(cp, sec, fsec, MAX_INTERVAL_PRECISION, true);
4386  *cp = '\0';
4387  }
4388  break;
4389 
4390  /* Compatible with postgresql < 8.4 when DateStyle != 'iso' */
4392  default:
4393  strcpy(cp, "@");
4394  cp++;
4395  cp = AddVerboseIntPart(cp, year, "year", &is_zero, &is_before);
4396  cp = AddVerboseIntPart(cp, mon, "mon", &is_zero, &is_before);
4397  cp = AddVerboseIntPart(cp, mday, "day", &is_zero, &is_before);
4398  cp = AddVerboseIntPart(cp, hour, "hour", &is_zero, &is_before);
4399  cp = AddVerboseIntPart(cp, min, "min", &is_zero, &is_before);
4400  if (sec != 0 || fsec != 0)
4401  {
4402  *cp++ = ' ';
4403  if (sec < 0 || (sec == 0 && fsec < 0))
4404  {
4405  if (is_zero)
4406  is_before = true;
4407  else if (!is_before)
4408  *cp++ = '-';
4409  }
4410  else if (is_before)
4411  *cp++ = '-';
4412  cp = AppendSeconds(cp, sec, fsec, MAX_INTERVAL_PRECISION, false);
4413  /* We output "ago", not negatives, so use abs(). */
4414  sprintf(cp, " sec%s",
4415  (abs(sec) != 1 || fsec != 0) ? "s" : "");
4416  is_zero = false;
4417  }
4418  /* identically zero? then put in a unitless zero... */
4419  if (is_zero)
4420  strcat(cp, " 0");
4421  if (is_before)
4422  strcat(cp, " ago");
4423  break;
4424  }
4425 }
#define INTSTYLE_POSTGRES_VERBOSE
Definition: miscadmin.h:250
int tm_hour
Definition: pgtime.h:36
#define sprintf
Definition: port.h:219
#define MAX_INTERVAL_PRECISION
Definition: timestamp.h:54
static char * AddISO8601IntPart(char *cp, int value, char units)
Definition: datetime.c:4169
int tm_mday
Definition: pgtime.h:37
int tm_mon
Definition: pgtime.h:38
static char * AddPostgresIntPart(char *cp, int value, const char *units, bool *is_zero, bool *is_before)
Definition: datetime.c:4179
#define INTSTYLE_ISO_8601
Definition: miscadmin.h:252
static char * AddVerboseIntPart(char *cp, int value, const char *units, bool *is_zero, bool *is_before)
Definition: datetime.c:4202
static char * AppendSeconds(char *cp, int sec, fsec_t fsec, int precision, bool fillzeros)
Definition: datetime.c:431
#define INTSTYLE_SQL_STANDARD
Definition: miscadmin.h:251
int tm_year
Definition: pgtime.h:39
int tm_sec
Definition: pgtime.h:34
int tm_min
Definition: pgtime.h:35
#define INTSTYLE_POSTGRES
Definition: miscadmin.h:249

◆ EncodeSpecialTimestamp()

void EncodeSpecialTimestamp ( Timestamp  dt,
char *  str 
)

Definition at line 1533 of file timestamp.c.

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

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

1534 {
1535  if (TIMESTAMP_IS_NOBEGIN(dt))
1536  strcpy(str, EARLY);
1537  else if (TIMESTAMP_IS_NOEND(dt))
1538  strcpy(str, LATE);
1539  else /* shouldn't happen */
1540  elog(ERROR, "invalid argument for EncodeSpecialTimestamp");
1541 }
#define LATE
Definition: datetime.h:41
#define ERROR
Definition: elog.h:46
#define TIMESTAMP_IS_NOEND(j)
Definition: timestamp.h:120
#define TIMESTAMP_IS_NOBEGIN(j)
Definition: timestamp.h:115
#define elog(elevel,...)
Definition: elog.h:232
#define EARLY
Definition: datetime.h:40

◆ EncodeTimeOnly()

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

Definition at line 3968 of file datetime.c.

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

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

3969 {
3970  str = pg_ultostr_zeropad(str, tm->tm_hour, 2);
3971  *str++ = ':';
3972  str = pg_ultostr_zeropad(str, tm->tm_min, 2);
3973  *str++ = ':';
3974  str = AppendSeconds(str, tm->tm_sec, fsec, MAX_TIME_PRECISION, true);
3975  if (print_tz)
3976  str = EncodeTimezone(str, tz, style);
3977  *str = '\0';
3978 }
int tm_hour
Definition: pgtime.h:36
static char * EncodeTimezone(char *str, int tz, int style)
Definition: datetime.c:3845
char * pg_ultostr_zeropad(char *str, uint32 value, int32 minwidth)
Definition: numutils.c:563
static char * AppendSeconds(char *cp, int sec, fsec_t fsec, int precision, bool fillzeros)
Definition: datetime.c:431
int tm_sec
Definition: pgtime.h:34
int tm_min
Definition: pgtime.h:35
#define MAX_TIME_PRECISION
Definition: date.h:51

◆ GetCurrentDateTime()

void GetCurrentDateTime ( struct pg_tm tm)

Definition at line 349 of file datetime.c.

References GetCurrentTimeUsec().

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

350 {
351  fsec_t fsec;
352 
353  GetCurrentTimeUsec(tm, &fsec, NULL);
354 }
void GetCurrentTimeUsec(struct pg_tm *tm, fsec_t *fsec, int *tzp)
Definition: datetime.c:370
int32 fsec_t
Definition: timestamp.h:41

◆ GetCurrentTimeUsec()

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

Definition at line 370 of file datetime.c.

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

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

371 {
373 
374  /*
375  * The cache key must include both current time and current timezone. By
376  * representing the timezone by just a pointer, we're assuming that
377  * distinct timezone settings could never have the same pointer value.
378  * This is true by virtue of the hashtable used inside pg_tzset();
379  * however, it might need another look if we ever allow entries in that
380  * hash to be recycled.
381  */
382  static TimestampTz cache_ts = 0;
383  static pg_tz *cache_timezone = NULL;
384  static struct pg_tm cache_tm;
385  static fsec_t cache_fsec;
386  static int cache_tz;
387 
388  if (cur_ts != cache_ts || session_timezone != cache_timezone)
389  {
390  /*
391  * Make sure cache is marked invalid in case of error after partial
392  * update within timestamp2tm.
393  */
394  cache_timezone = NULL;
395 
396  /*
397  * Perform the computation, storing results into cache. We do not
398  * really expect any error here, since current time surely ought to be
399  * within range, but check just for sanity's sake.
400  */
401  if (timestamp2tm(cur_ts, &cache_tz, &cache_tm, &cache_fsec,
402  NULL, session_timezone) != 0)
403  ereport(ERROR,
404  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
405  errmsg("timestamp out of range")));
406 
407  /* OK, so mark the cache valid. */
408  cache_ts = cur_ts;
409  cache_timezone = session_timezone;
410  }
411 
412  *tm = cache_tm;
413  *fsec = cache_fsec;
414  if (tzp != NULL)
415  *tzp = cache_tz;
416 }
PGDLLIMPORT pg_tz * session_timezone
Definition: pgtz.c:28
int64 TimestampTz
Definition: timestamp.h:39
int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm *tm, fsec_t *fsec, const char **tzn, pg_tz *attimezone)
Definition: timestamp.c:1822
int errcode(int sqlerrcode)
Definition: elog.c:698
Definition: pgtime.h:32
#define ERROR
Definition: elog.h:46
int32 fsec_t
Definition: timestamp.h:41
TimestampTz GetCurrentTransactionStartTimestamp(void)
Definition: xact.c:798
Definition: pgtz.h:65
#define ereport(elevel,...)
Definition: elog.h:157
int errmsg(const char *fmt,...)
Definition: elog.c:909

◆ InstallTimeZoneAbbrevs()

void InstallTimeZoneAbbrevs ( TimeZoneAbbrevTable tbl)

Definition at line 4611 of file datetime.c.

Referenced by assign_timezone_abbreviations().

4612 {
4613  zoneabbrevtbl = tbl;
4614  /* reset abbrevcache, which may contain pointers into old table */
4615  memset(abbrevcache, 0, sizeof(abbrevcache));
4616 }
static TimeZoneAbbrevTable * zoneabbrevtbl
Definition: datetime.c:237
static const datetkn * abbrevcache[MAXDATEFIELDS]
Definition: datetime.c:245

◆ j2date()

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

Definition at line 294 of file datetime.c.

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

◆ j2day()

int j2day ( int  jd)

Definition at line 327 of file datetime.c.

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

328 {
329  date += 1;
330  date %= 7;
331  /* Cope if division truncates towards zero, as it probably does */
332  if (date < 0)
333  date += 7;
334 
335  return date;
336 } /* j2day() */
long date
Definition: pgtypes_date.h:9

◆ ParseDateTime()

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

Definition at line 582 of file datetime.c.

References APPEND_CHAR, datebsearch(), 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(), pg_logdir_ls_internal(), PGTYPESdate_from_asc(), PGTYPESinterval_from_asc(), PGTYPEStimestamp_from_asc(), time_in(), timestamp_in(), timestamptz_in(), and timetz_in().

584 {
585  int nf = 0;
586  const char *cp = timestr;
587  char *bufp = workbuf;
588  const char *bufend = workbuf + buflen;
589 
590  /*
591  * Set the character pointed-to by "bufptr" to "newchar", and increment
592  * "bufptr". "end" gives the end of the buffer -- we return an error if
593  * there is no space left to append a character to the buffer. Note that
594  * "bufptr" is evaluated twice.
595  */
596 #define APPEND_CHAR(bufptr, end, newchar) \
597  do \
598  { \
599  if (((bufptr) + 1) >= (end)) \
600  return DTERR_BAD_FORMAT; \
601  *(bufptr)++ = newchar; \
602  } while (0)
603 
604  /* outer loop through fields */
605  while (*cp != '\0')
606  {
607  /* Ignore spaces between fields */
608  if (isspace((unsigned char) *cp))
609  {
610  cp++;
611  continue;
612  }
613 
614  /* Record start of current field */
615  if (nf >= maxfields)
616  return DTERR_BAD_FORMAT;
617  field[nf] = bufp;
618 
619  /* leading digit? then date or time */
620  if (isdigit((unsigned char) *cp))
621  {
622  APPEND_CHAR(bufp, bufend, *cp++);
623  while (isdigit((unsigned char) *cp))
624  APPEND_CHAR(bufp, bufend, *cp++);
625 
626  /* time field? */
627  if (*cp == ':')
628  {
629  ftype[nf] = DTK_TIME;
630  APPEND_CHAR(bufp, bufend, *cp++);
631  while (isdigit((unsigned char) *cp) ||
632  (*cp == ':') || (*cp == '.'))
633  APPEND_CHAR(bufp, bufend, *cp++);
634  }
635  /* date field? allow embedded text month */
636  else if (*cp == '-' || *cp == '/' || *cp == '.')
637  {
638  /* save delimiting character to use later */
639  char delim = *cp;
640 
641  APPEND_CHAR(bufp, bufend, *cp++);
642  /* second field is all digits? then no embedded text month */
643  if (isdigit((unsigned char) *cp))
644  {
645  ftype[nf] = ((delim == '.') ? DTK_NUMBER : DTK_DATE);
646  while (isdigit((unsigned char) *cp))
647  APPEND_CHAR(bufp, bufend, *cp++);
648 
649  /*
650  * insist that the delimiters match to get a three-field
651  * date.
652  */
653  if (*cp == delim)
654  {
655  ftype[nf] = DTK_DATE;
656  APPEND_CHAR(bufp, bufend, *cp++);
657  while (isdigit((unsigned char) *cp) || *cp == delim)
658  APPEND_CHAR(bufp, bufend, *cp++);
659  }
660  }
661  else
662  {
663  ftype[nf] = DTK_DATE;
664  while (isalnum((unsigned char) *cp) || *cp == delim)
665  APPEND_CHAR(bufp, bufend, pg_tolower((unsigned char) *cp++));
666  }
667  }
668 
669  /*
670  * otherwise, number only and will determine year, month, day, or
671  * concatenated fields later...
672  */
673  else
674  ftype[nf] = DTK_NUMBER;
675  }
676  /* Leading decimal point? Then fractional seconds... */
677  else if (*cp == '.')
678  {
679  APPEND_CHAR(bufp, bufend, *cp++);
680  while (isdigit((unsigned char) *cp))
681  APPEND_CHAR(bufp, bufend, *cp++);
682 
683  ftype[nf] = DTK_NUMBER;
684  }
685 
686  /*
687  * text? then date string, month, day of week, special, or timezone
688  */
689  else if (isalpha((unsigned char) *cp))
690  {
691  bool is_date;
692 
693  ftype[nf] = DTK_STRING;
694  APPEND_CHAR(bufp, bufend, pg_tolower((unsigned char) *cp++));
695  while (isalpha((unsigned char) *cp))
696  APPEND_CHAR(bufp, bufend, pg_tolower((unsigned char) *cp++));
697 
698  /*
699  * Dates can have embedded '-', '/', or '.' separators. It could
700  * also be a timezone name containing embedded '/', '+', '-', '_',
701  * or ':' (but '_' or ':' can't be the first punctuation). If the
702  * next character is a digit or '+', we need to check whether what
703  * we have so far is a recognized non-timezone keyword --- if so,
704  * don't believe that this is the start of a timezone.
705  */
706  is_date = false;
707  if (*cp == '-' || *cp == '/' || *cp == '.')
708  is_date = true;
709  else if (*cp == '+' || isdigit((unsigned char) *cp))
710  {
711  *bufp = '\0'; /* null-terminate current field value */
712  /* we need search only the core token table, not TZ names */
713  if (datebsearch(field[nf], datetktbl, szdatetktbl) == NULL)
714  is_date = true;
715  }
716  if (is_date)
717  {
718  ftype[nf] = DTK_DATE;
719  do
720  {
721  APPEND_CHAR(bufp, bufend, pg_tolower((unsigned char) *cp++));
722  } while (*cp == '+' || *cp == '-' ||
723  *cp == '/' || *cp == '_' ||
724  *cp == '.' || *cp == ':' ||
725  isalnum((unsigned char) *cp));
726  }
727  }
728  /* sign? then special or numeric timezone */
729  else if (*cp == '+' || *cp == '-')
730  {
731  APPEND_CHAR(bufp, bufend, *cp++);
732  /* soak up leading whitespace */
733  while (isspace((unsigned char) *cp))
734  cp++;
735  /* numeric timezone? */
736  /* note that "DTK_TZ" could also be a signed float or yyyy-mm */
737  if (isdigit((unsigned char) *cp))
738  {
739  ftype[nf] = DTK_TZ;
740  APPEND_CHAR(bufp, bufend, *cp++);
741  while (isdigit((unsigned char) *cp) ||
742  *cp == ':' || *cp == '.' || *cp == '-')
743  APPEND_CHAR(bufp, bufend, *cp++);
744  }
745  /* special? */
746  else if (isalpha((unsigned char) *cp))
747  {
748  ftype[nf] = DTK_SPECIAL;
749  APPEND_CHAR(bufp, bufend, pg_tolower((unsigned char) *cp++));
750  while (isalpha((unsigned char) *cp))
751  APPEND_CHAR(bufp, bufend, pg_tolower((unsigned char) *cp++));
752  }
753  /* otherwise something wrong... */
754  else
755  return DTERR_BAD_FORMAT;
756  }
757  /* ignore other punctuation but use as delimiter */
758  else if (ispunct((unsigned char) *cp))
759  {
760  cp++;
761  continue;
762  }
763  /* otherwise, something is not right... */
764  else
765  return DTERR_BAD_FORMAT;
766 
767  /* force in a delimiter after each field */
768  *bufp++ = '\0';
769  nf++;
770  }
771 
772  *numfields = nf;
773 
774  return 0;
775 }
#define DTERR_BAD_FORMAT
Definition: datetime.h:280
#define APPEND_CHAR(bufptr, end, newchar)
static const datetkn datetktbl[]
Definition: datetime.c:89
unsigned char pg_tolower(unsigned char ch)
Definition: pgstrcasecmp.c:122
#define DTK_TZ
Definition: datetime.h:147
static const datetkn * datebsearch(const char *key, const datetkn *base, int nel)
Definition: datetime.c:3809
#define DTK_NUMBER
Definition: datetime.h:142
#define DTK_TIME
Definition: datetime.h:146
#define DTK_STRING
Definition: datetime.h:143
static const int szdatetktbl
Definition: datetime.c:164
#define DTK_DATE
Definition: datetime.h:145
#define DTK_SPECIAL
Definition: datetime.h:150

◆ TemporalSimplify()

Node* TemporalSimplify ( int32  max_precis,
Node node 
)

Definition at line 4494 of file datetime.c.

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

Referenced by time_support(), and timestamp_support().

4495 {
4496  FuncExpr *expr = castNode(FuncExpr, node);
4497  Node *ret = NULL;
4498  Node *typmod;
4499 
4500  Assert(list_length(expr->args) >= 2);
4501 
4502  typmod = (Node *) lsecond(expr->args);
4503 
4504  if (IsA(typmod, Const) && !((Const *) typmod)->constisnull)
4505  {
4506  Node *source = (Node *) linitial(expr->args);
4507  int32 old_precis = exprTypmod(source);
4508  int32 new_precis = DatumGetInt32(((Const *) typmod)->constvalue);
4509 
4510  if (new_precis < 0 || new_precis == max_precis ||
4511  (old_precis >= 0 && new_precis >= old_precis))
4512  ret = relabel_to_typmod(source, new_precis);
4513  }
4514 
4515  return ret;
4516 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:587
List * args
Definition: primnodes.h:503
#define DatumGetInt32(X)
Definition: postgres.h:516
#define castNode(_type_, nodeptr)
Definition: nodes.h:605
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:267
Definition: nodes.h:536
#define lsecond(l)
Definition: pg_list.h:179
signed int int32
Definition: c.h:429
#define linitial(l)
Definition: pg_list.h:174
#define Assert(condition)
Definition: c.h:804
static rewind_source * source
Definition: pg_rewind.c:79
static int list_length(const List *l)
Definition: pg_list.h:149
Node * relabel_to_typmod(Node *expr, int32 typmod)
Definition: nodeFuncs.c:635

◆ ValidateDate()

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

Definition at line 2480 of file datetime.c.

References date2j(), DAY, day_tab, DOY, DTERR_FIELD_OVERFLOW, DTERR_MD_FIELD_OVERFLOW, DTK_DATE_M, DTK_M, isleap, j2date(), MONTH, MONTHS_PER_YEAR, 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().

2482 {
2483  if (fmask & DTK_M(YEAR))
2484  {
2485  if (isjulian)
2486  {
2487  /* tm_year is correct and should not be touched */
2488  }
2489  else if (bc)
2490  {
2491  /* there is no year zero in AD/BC notation */
2492  if (tm->tm_year <= 0)
2493  return DTERR_FIELD_OVERFLOW;
2494  /* internally, we represent 1 BC as year zero, 2 BC as -1, etc */
2495  tm->tm_year = -(tm->tm_year - 1);
2496  }
2497  else if (is2digits)
2498  {
2499  /* process 1 or 2-digit input as 1970-2069 AD, allow '0' and '00' */
2500  if (tm->tm_year < 0) /* just paranoia */
2501  return DTERR_FIELD_OVERFLOW;
2502  if (tm->tm_year < 70)
2503  tm->tm_year += 2000;
2504  else if (tm->tm_year < 100)
2505  tm->tm_year += 1900;
2506  }
2507  else
2508  {
2509  /* there is no year zero in AD/BC notation */
2510  if (tm->tm_year <= 0)
2511  return DTERR_FIELD_OVERFLOW;
2512  }
2513  }
2514 
2515  /* now that we have correct year, decode DOY */
2516  if (fmask & DTK_M(DOY))
2517  {
2518  j2date(date2j(tm->tm_year, 1, 1) + tm->tm_yday - 1,
2519  &tm->tm_year, &tm->tm_mon, &tm->tm_mday);
2520  }
2521 
2522  /* check for valid month */
2523  if (fmask & DTK_M(MONTH))
2524  {
2525  if (tm->tm_mon < 1 || tm->tm_mon > MONTHS_PER_YEAR)
2526  return DTERR_MD_FIELD_OVERFLOW;
2527  }
2528 
2529  /* minimal check for valid day */
2530  if (fmask & DTK_M(DAY))
2531  {
2532  if (tm->tm_mday < 1 || tm->tm_mday > 31)
2533  return DTERR_MD_FIELD_OVERFLOW;
2534  }
2535 
2536  if ((fmask & DTK_DATE_M) == DTK_DATE_M)
2537  {
2538  /*
2539  * Check for valid day of month, now that we know for sure the month
2540  * and year. Note we don't use MD_FIELD_OVERFLOW here, since it seems
2541  * unlikely that "Feb 29" is a YMD-order error.
2542  */
2543  if (tm->tm_mday > day_tab[isleap(tm->tm_year)][tm->tm_mon - 1])
2544  return DTERR_FIELD_OVERFLOW;
2545  }
2546 
2547  return 0;
2548 }
#define DAY
Definition: datetime.h:94
#define YEAR
Definition: datetime.h:93
#define isleap(y)
Definition: datetime.h:271
#define DTK_DATE_M
Definition: datetime.h:192
#define MONTHS_PER_YEAR
Definition: timestamp.h:69
#define DTERR_FIELD_OVERFLOW
Definition: datetime.h:281
int tm_mday
Definition: pgtime.h:37
int tm_mon
Definition: pgtime.h:38
void j2date(int jd, int *year, int *month, int *day)
Definition: datetime.c:294
#define MONTH
Definition: datetime.h:92
const int day_tab[2][13]
Definition: datetime.c:59
int date2j(int y, int m, int d)
Definition: datetime.c:269
int tm_year
Definition: pgtime.h:39
#define DTK_M(t)
Definition: datetime.h:188
int tm_yday
Definition: pgtime.h:41
#define DTERR_MD_FIELD_OVERFLOW
Definition: datetime.h:282
#define DOY
Definition: datetime.h:106

Variable Documentation

◆ day_tab

const int day_tab[2][13]

Definition at line 59 of file datetime.c.

◆ days

const char* const days[]

Definition at line 68 of file datetime.c.

◆ months

const char* const months[]

Definition at line 65 of file datetime.c.