PostgreSQL Source Code  git master
date.h File Reference
#include <math.h>
#include "datatype/timestamp.h"
#include "fmgr.h"
#include "pgtime.h"
Include dependency graph for date.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  TimeTzADT
 

Macros

#define DATEVAL_NOBEGIN   ((DateADT) PG_INT32_MIN)
 
#define DATEVAL_NOEND   ((DateADT) PG_INT32_MAX)
 
#define DATE_NOBEGIN(j)   ((j) = DATEVAL_NOBEGIN)
 
#define DATE_IS_NOBEGIN(j)   ((j) == DATEVAL_NOBEGIN)
 
#define DATE_NOEND(j)   ((j) = DATEVAL_NOEND)
 
#define DATE_IS_NOEND(j)   ((j) == DATEVAL_NOEND)
 
#define DATE_NOT_FINITE(j)   (DATE_IS_NOBEGIN(j) || DATE_IS_NOEND(j))
 
#define MAX_TIME_PRECISION   6
 
#define DatumGetDateADT(X)   ((DateADT) DatumGetInt32(X))
 
#define DatumGetTimeADT(X)   ((TimeADT) DatumGetInt64(X))
 
#define DatumGetTimeTzADTP(X)   ((TimeTzADT *) DatumGetPointer(X))
 
#define DateADTGetDatum(X)   Int32GetDatum(X)
 
#define TimeADTGetDatum(X)   Int64GetDatum(X)
 
#define TimeTzADTPGetDatum(X)   PointerGetDatum(X)
 
#define PG_GETARG_DATEADT(n)   DatumGetDateADT(PG_GETARG_DATUM(n))
 
#define PG_GETARG_TIMEADT(n)   DatumGetTimeADT(PG_GETARG_DATUM(n))
 
#define PG_GETARG_TIMETZADT_P(n)   DatumGetTimeTzADTP(PG_GETARG_DATUM(n))
 
#define PG_RETURN_DATEADT(x)   return DateADTGetDatum(x)
 
#define PG_RETURN_TIMEADT(x)   return TimeADTGetDatum(x)
 
#define PG_RETURN_TIMETZADT_P(x)   return TimeTzADTPGetDatum(x)
 

Typedefs

typedef int32 DateADT
 
typedef int64 TimeADT
 

Functions

int32 anytime_typmod_check (bool istz, int32 typmod)
 
double date2timestamp_no_overflow (DateADT dateVal)
 
Timestamp date2timestamp_opt_overflow (DateADT dateVal, int *overflow)
 
TimestampTz date2timestamptz_opt_overflow (DateADT dateVal, int *overflow)
 
void EncodeSpecialDate (DateADT dt, char *str)
 
DateADT GetSQLCurrentDate (void)
 
TimeTzADTGetSQLCurrentTime (int32 typmod)
 
TimeADT GetSQLLocalTime (int32 typmod)
 
int time2tm (TimeADT time, struct pg_tm *tm, fsec_t *fsec)
 
int timetz2tm (TimeTzADT *time, struct pg_tm *tm, fsec_t *fsec, int *tzp)
 
int tm2time (struct pg_tm *tm, fsec_t fsec, TimeADT *result)
 
int tm2timetz (struct pg_tm *tm, fsec_t fsec, int tz, TimeTzADT *result)
 
bool time_overflows (int hour, int min, int sec, fsec_t fsec)
 
bool float_time_overflows (int hour, int min, double sec)
 
void AdjustTimeForTypmod (TimeADT *time, int32 typmod)
 

Macro Definition Documentation

◆ DATE_IS_NOBEGIN

#define DATE_IS_NOBEGIN (   j)    ((j) == DATEVAL_NOBEGIN)

◆ DATE_IS_NOEND

#define DATE_IS_NOEND (   j)    ((j) == DATEVAL_NOEND)

◆ DATE_NOBEGIN

#define DATE_NOBEGIN (   j)    ((j) = DATEVAL_NOBEGIN)

Definition at line 39 of file date.h.

Referenced by date_in(), timestamp_date(), and timestamptz_date().

◆ DATE_NOEND

#define DATE_NOEND (   j)    ((j) = DATEVAL_NOEND)

Definition at line 41 of file date.h.

Referenced by date_in(), timestamp_date(), and timestamptz_date().

◆ DATE_NOT_FINITE

#define DATE_NOT_FINITE (   j)    (DATE_IS_NOBEGIN(j) || DATE_IS_NOEND(j))

◆ DateADTGetDatum

◆ DATEVAL_NOBEGIN

#define DATEVAL_NOBEGIN   ((DateADT) PG_INT32_MIN)

Definition at line 36 of file date.h.

Referenced by leftmostvalue_date().

◆ DATEVAL_NOEND

#define DATEVAL_NOEND   ((DateADT) PG_INT32_MAX)

Definition at line 37 of file date.h.

◆ DatumGetDateADT

◆ DatumGetTimeADT

#define DatumGetTimeADT (   X)    ((TimeADT) DatumGetInt64(X))

Definition at line 54 of file date.h.

Referenced by convert_timevalue_to_scalar(), gbt_num_compress(), and JsonEncodeDateTime().

◆ DatumGetTimeTzADTP

#define DatumGetTimeTzADTP (   X)    ((TimeTzADT *) DatumGetPointer(X))

Definition at line 55 of file date.h.

Referenced by convert_timevalue_to_scalar(), gbt_timetz_compress(), and JsonEncodeDateTime().

◆ MAX_TIME_PRECISION

#define MAX_TIME_PRECISION   6

Definition at line 51 of file date.h.

Referenced by AdjustTimeForTypmod(), anytime_typmod_check(), EncodeTimeOnly(), and time_support().

◆ PG_GETARG_DATEADT

◆ PG_GETARG_TIMEADT

◆ PG_GETARG_TIMETZADT_P

◆ PG_RETURN_DATEADT

#define PG_RETURN_DATEADT (   x)    return DateADTGetDatum(x)

◆ PG_RETURN_TIMEADT

◆ PG_RETURN_TIMETZADT_P

◆ TimeADTGetDatum

#define TimeADTGetDatum (   X)    Int64GetDatum(X)

Definition at line 58 of file date.h.

Referenced by ExecEvalSQLValueFunction(), gbt_num_fetch(), leftmostvalue_time(), and parse_datetime().

◆ TimeTzADTPGetDatum

#define TimeTzADTPGetDatum (   X)    PointerGetDatum(X)

Definition at line 59 of file date.h.

Referenced by ExecEvalSQLValueFunction(), leftmostvalue_timetz(), and parse_datetime().

Typedef Documentation

◆ DateADT

typedef int32 DateADT

Definition at line 23 of file date.h.

◆ TimeADT

typedef int64 TimeADT

Definition at line 25 of file date.h.

Function Documentation

◆ AdjustTimeForTypmod()

void AdjustTimeForTypmod ( TimeADT time,
int32  typmod 
)

Definition at line 1498 of file date.c.

References MAX_TIME_PRECISION.

Referenced by GetSQLCurrentTime(), GetSQLLocalTime(), parse_datetime(), time_in(), time_recv(), time_scale(), timetz_in(), timetz_recv(), and timetz_scale().

1499 {
1500  static const int64 TimeScales[MAX_TIME_PRECISION + 1] = {
1501  INT64CONST(1000000),
1502  INT64CONST(100000),
1503  INT64CONST(10000),
1504  INT64CONST(1000),
1505  INT64CONST(100),
1506  INT64CONST(10),
1507  INT64CONST(1)
1508  };
1509 
1510  static const int64 TimeOffsets[MAX_TIME_PRECISION + 1] = {
1511  INT64CONST(500000),
1512  INT64CONST(50000),
1513  INT64CONST(5000),
1514  INT64CONST(500),
1515  INT64CONST(50),
1516  INT64CONST(5),
1517  INT64CONST(0)
1518  };
1519 
1520  if (typmod >= 0 && typmod <= MAX_TIME_PRECISION)
1521  {
1522  if (*time >= INT64CONST(0))
1523  *time = ((*time + TimeOffsets[typmod]) / TimeScales[typmod]) *
1524  TimeScales[typmod];
1525  else
1526  *time = -((((-*time) + TimeOffsets[typmod]) / TimeScales[typmod]) *
1527  TimeScales[typmod]);
1528  }
1529 }
#define MAX_TIME_PRECISION
Definition: date.h:51

◆ anytime_typmod_check()

int32 anytime_typmod_check ( bool  istz,
int32  typmod 
)

Definition at line 68 of file date.c.

References ereport, errcode(), errmsg(), ERROR, MAX_TIME_PRECISION, and WARNING.

Referenced by anytime_typmodin(), and transformSQLValueFunction().

69 {
70  if (typmod < 0)
71  ereport(ERROR,
72  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
73  errmsg("TIME(%d)%s precision must not be negative",
74  typmod, (istz ? " WITH TIME ZONE" : ""))));
75  if (typmod > MAX_TIME_PRECISION)
76  {
78  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
79  errmsg("TIME(%d)%s precision reduced to maximum allowed, %d",
80  typmod, (istz ? " WITH TIME ZONE" : ""),
82  typmod = MAX_TIME_PRECISION;
83  }
84 
85  return typmod;
86 }
int errcode(int sqlerrcode)
Definition: elog.c:610
#define ERROR
Definition: elog.h:43
#define WARNING
Definition: elog.h:40
#define ereport(elevel,...)
Definition: elog.h:144
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define MAX_TIME_PRECISION
Definition: date.h:51

◆ date2timestamp_no_overflow()

double date2timestamp_no_overflow ( DateADT  dateVal)

Definition at line 708 of file date.c.

References DATE_IS_NOBEGIN, DATE_IS_NOEND, and USECS_PER_DAY.

Referenced by convert_timevalue_to_scalar().

709 {
710  double result;
711 
712  if (DATE_IS_NOBEGIN(dateVal))
713  result = -DBL_MAX;
714  else if (DATE_IS_NOEND(dateVal))
715  result = DBL_MAX;
716  else
717  {
718  /* date is days since 2000, timestamp is microseconds since same... */
719  result = dateVal * (double) USECS_PER_DAY;
720  }
721 
722  return result;
723 }
#define DATE_IS_NOEND(j)
Definition: date.h:42
#define USECS_PER_DAY
Definition: timestamp.h:91
#define DATE_IS_NOBEGIN(j)
Definition: date.h:40

◆ date2timestamp_opt_overflow()

Timestamp date2timestamp_opt_overflow ( DateADT  dateVal,
int *  overflow 
)

Definition at line 563 of file date.c.

References DATE_IS_NOBEGIN, DATE_IS_NOEND, ereport, errcode(), errmsg(), ERROR, POSTGRES_EPOCH_JDATE, TIMESTAMP_END_JULIAN, TIMESTAMP_NOBEGIN, TIMESTAMP_NOEND, and USECS_PER_DAY.

Referenced by cmpDateToTimestamp(), and date2timestamp().

564 {
565  Timestamp result;
566 
567  if (DATE_IS_NOBEGIN(dateVal))
568  TIMESTAMP_NOBEGIN(result);
569  else if (DATE_IS_NOEND(dateVal))
570  TIMESTAMP_NOEND(result);
571  else
572  {
573  /*
574  * Date's range is wider than timestamp's, so check for boundaries.
575  * Since dates have the same minimum values as timestamps, only upper
576  * boundary need be checked for overflow.
577  */
578  if (dateVal >= (TIMESTAMP_END_JULIAN - POSTGRES_EPOCH_JDATE))
579  {
580  if (overflow)
581  {
582  *overflow = 1;
583  return (Timestamp) 0;
584  }
585  else
586  {
587  ereport(ERROR,
588  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
589  errmsg("date out of range for timestamp")));
590  }
591  }
592 
593  /* date is days since 2000, timestamp is microseconds since same... */
594  result = dateVal * USECS_PER_DAY;
595  }
596 
597  return result;
598 }
#define TIMESTAMP_NOEND(j)
Definition: timestamp.h:117
#define DATE_IS_NOEND(j)
Definition: date.h:42
#define TIMESTAMP_END_JULIAN
Definition: timestamp.h:181
int errcode(int sqlerrcode)
Definition: elog.c:610
#define ERROR
Definition: elog.h:43
#define TIMESTAMP_NOBEGIN(j)
Definition: timestamp.h:112
#define USECS_PER_DAY
Definition: timestamp.h:91
int64 Timestamp
Definition: timestamp.h:38
#define ereport(elevel,...)
Definition: elog.h:144
#define DATE_IS_NOBEGIN(j)
Definition: date.h:40
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define POSTGRES_EPOCH_JDATE
Definition: timestamp.h:163

◆ date2timestamptz_opt_overflow()

TimestampTz date2timestamptz_opt_overflow ( DateADT  dateVal,
int *  overflow 
)

Definition at line 617 of file date.c.

References Assert, DATE_IS_NOBEGIN, DATE_IS_NOEND, DetermineTimeZoneOffset(), END_TIMESTAMP, ereport, errcode(), errmsg(), ERROR, IS_VALID_TIMESTAMP, j2date(), MIN_TIMESTAMP, POSTGRES_EPOCH_JDATE, session_timezone, TIMESTAMP_END_JULIAN, TIMESTAMP_NOBEGIN, TIMESTAMP_NOEND, tm, pg_tm::tm_hour, pg_tm::tm_mday, pg_tm::tm_min, pg_tm::tm_mon, pg_tm::tm_sec, pg_tm::tm_year, USECS_PER_DAY, and USECS_PER_SEC.

Referenced by cmpDateToTimestampTz(), and date2timestamptz().

618 {
619  TimestampTz result;
620  struct pg_tm tt,
621  *tm = &tt;
622  int tz;
623 
624  if (DATE_IS_NOBEGIN(dateVal))
625  TIMESTAMP_NOBEGIN(result);
626  else if (DATE_IS_NOEND(dateVal))
627  TIMESTAMP_NOEND(result);
628  else
629  {
630  /*
631  * Date's range is wider than timestamp's, so check for boundaries.
632  * Since dates have the same minimum values as timestamps, only upper
633  * boundary need be checked for overflow.
634  */
635  if (dateVal >= (TIMESTAMP_END_JULIAN - POSTGRES_EPOCH_JDATE))
636  {
637  if (overflow)
638  {
639  *overflow = 1;
640  return (TimestampTz) 0;
641  }
642  else
643  {
644  ereport(ERROR,
645  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
646  errmsg("date out of range for timestamp")));
647  }
648  }
649 
650  j2date(dateVal + POSTGRES_EPOCH_JDATE,
651  &(tm->tm_year), &(tm->tm_mon), &(tm->tm_mday));
652  tm->tm_hour = 0;
653  tm->tm_min = 0;
654  tm->tm_sec = 0;
656 
657  result = dateVal * USECS_PER_DAY + tz * USECS_PER_SEC;
658 
659  /*
660  * Since it is possible to go beyond allowed timestamptz range because
661  * of time zone, check for allowed timestamp range after adding tz.
662  */
663  if (!IS_VALID_TIMESTAMP(result))
664  {
665  if (overflow)
666  {
667  if (result < MIN_TIMESTAMP)
668  *overflow = -1;
669  else
670  {
671  Assert(result >= END_TIMESTAMP);
672  *overflow = 1;
673  }
674  return (TimestampTz) 0;
675  }
676  else
677  {
678  ereport(ERROR,
679  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
680  errmsg("date out of range for timestamp")));
681  }
682  }
683  }
684 
685  return result;
686 }
#define TIMESTAMP_NOEND(j)
Definition: timestamp.h:117
#define DATE_IS_NOEND(j)
Definition: date.h:42
#define TIMESTAMP_END_JULIAN
Definition: timestamp.h:181
PGDLLIMPORT pg_tz * session_timezone
Definition: pgtz.c:28
#define USECS_PER_SEC
Definition: timestamp.h:94
int64 TimestampTz
Definition: timestamp.h:39
int tm_hour
Definition: pgtime.h:29
int errcode(int sqlerrcode)
Definition: elog.c:610
Definition: pgtime.h:25
static struct pg_tm tm
Definition: localtime.c:102
#define ERROR
Definition: elog.h:43
int tm_mday
Definition: pgtime.h:30
int tm_mon
Definition: pgtime.h:31
int DetermineTimeZoneOffset(struct pg_tm *tm, pg_tz *tzp)
Definition: datetime.c:1433
#define TIMESTAMP_NOBEGIN(j)
Definition: timestamp.h:112
#define MIN_TIMESTAMP
Definition: timestamp.h:184
#define USECS_PER_DAY
Definition: timestamp.h:91
void j2date(int jd, int *year, int *month, int *day)
Definition: datetime.c:294
#define END_TIMESTAMP
Definition: timestamp.h:186
#define ereport(elevel,...)
Definition: elog.h:144
#define DATE_IS_NOBEGIN(j)
Definition: date.h:40
#define Assert(condition)
Definition: c.h:745
#define IS_VALID_TIMESTAMP(t)
Definition: timestamp.h:195
int tm_year
Definition: pgtime.h:32
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define POSTGRES_EPOCH_JDATE
Definition: timestamp.h:163
int tm_sec
Definition: pgtime.h:27
int tm_min
Definition: pgtime.h:28

◆ EncodeSpecialDate()

void EncodeSpecialDate ( DateADT  dt,
char *  str 
)

Definition at line 285 of file date.c.

References DATE_IS_NOBEGIN, DATE_IS_NOEND, EARLY, elog, ERROR, and LATE.

Referenced by date_out(), and JsonEncodeDateTime().

286 {
287  if (DATE_IS_NOBEGIN(dt))
288  strcpy(str, EARLY);
289  else if (DATE_IS_NOEND(dt))
290  strcpy(str, LATE);
291  else /* shouldn't happen */
292  elog(ERROR, "invalid argument for EncodeSpecialDate");
293 }
#define DATE_IS_NOEND(j)
Definition: date.h:42
#define LATE
Definition: datetime.h:41
#define ERROR
Definition: elog.h:43
#define DATE_IS_NOBEGIN(j)
Definition: date.h:40
#define elog(elevel,...)
Definition: elog.h:214
#define EARLY
Definition: datetime.h:40

◆ float_time_overflows()

bool float_time_overflows ( int  hour,
int  min,
double  sec 
)

Definition at line 1302 of file date.c.

References HOURS_PER_DAY, MINS_PER_HOUR, SECS_PER_MINUTE, USECS_PER_DAY, and USECS_PER_SEC.

Referenced by make_time(), and make_timestamp_internal().

1303 {
1304  /* Range-check the fields individually. */
1305  if (hour < 0 || hour > HOURS_PER_DAY ||
1306  min < 0 || min >= MINS_PER_HOUR)
1307  return true;
1308 
1309  /*
1310  * "sec", being double, requires extra care. Cope with NaN, and round off
1311  * before applying the range check to avoid unexpected errors due to
1312  * imprecise input. (We assume rint() behaves sanely with infinities.)
1313  */
1314  if (isnan(sec))
1315  return true;
1316  sec = rint(sec * USECS_PER_SEC);
1317  if (sec < 0 || sec > SECS_PER_MINUTE * USECS_PER_SEC)
1318  return true;
1319 
1320  /*
1321  * Because we allow, eg, hour = 24 or sec = 60, we must check separately
1322  * that the total time value doesn't exceed 24:00:00. This must match the
1323  * way that callers will convert the fields to a time.
1324  */
1325  if (((((hour * MINS_PER_HOUR + min) * SECS_PER_MINUTE)
1326  * USECS_PER_SEC) + (int64) sec) > USECS_PER_DAY)
1327  return true;
1328 
1329  return false;
1330 }
#define USECS_PER_SEC
Definition: timestamp.h:94
#define MINS_PER_HOUR
Definition: timestamp.h:89
#define HOURS_PER_DAY
Definition: timestamp.h:78
#define SECS_PER_MINUTE
Definition: timestamp.h:88
#define USECS_PER_DAY
Definition: timestamp.h:91

◆ GetSQLCurrentDate()

DateADT GetSQLCurrentDate ( void  )

Definition at line 300 of file date.c.

References date2j(), ereport, errcode(), errmsg(), ERROR, GetCurrentTransactionStartTimestamp(), POSTGRES_EPOCH_JDATE, timestamp2tm(), tm, pg_tm::tm_mday, pg_tm::tm_mon, and pg_tm::tm_year.

Referenced by ExecEvalSQLValueFunction().

301 {
302  TimestampTz ts;
303  struct pg_tm tt,
304  *tm = &tt;
305  fsec_t fsec;
306  int tz;
307 
309 
310  if (timestamp2tm(ts, &tz, tm, &fsec, NULL, NULL) != 0)
311  ereport(ERROR,
312  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
313  errmsg("timestamp out of range")));
314 
315  return date2j(tm->tm_year, tm->tm_mon, tm->tm_mday) - POSTGRES_EPOCH_JDATE;
316 }
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:1784
int errcode(int sqlerrcode)
Definition: elog.c:610
Definition: pgtime.h:25
static struct pg_tm tm
Definition: localtime.c:102
#define ERROR
Definition: elog.h:43
int tm_mday
Definition: pgtime.h:30
int tm_mon
Definition: pgtime.h:31
int32 fsec_t
Definition: timestamp.h:41
TimestampTz GetCurrentTransactionStartTimestamp(void)
Definition: xact.c:798
int date2j(int y, int m, int d)
Definition: datetime.c:269
#define ereport(elevel,...)
Definition: elog.h:144
int tm_year
Definition: pgtime.h:32
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define POSTGRES_EPOCH_JDATE
Definition: timestamp.h:163

◆ GetSQLCurrentTime()

TimeTzADT* GetSQLCurrentTime ( int32  typmod)

Definition at line 322 of file date.c.

References AdjustTimeForTypmod(), ereport, errcode(), errmsg(), ERROR, GetCurrentTransactionStartTimestamp(), palloc(), TimeTzADT::time, timestamp2tm(), tm, and tm2timetz().

Referenced by ExecEvalSQLValueFunction().

323 {
324  TimeTzADT *result;
325  TimestampTz ts;
326  struct pg_tm tt,
327  *tm = &tt;
328  fsec_t fsec;
329  int tz;
330 
332 
333  if (timestamp2tm(ts, &tz, tm, &fsec, NULL, NULL) != 0)
334  ereport(ERROR,
335  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
336  errmsg("timestamp out of range")));
337 
338  result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
339  tm2timetz(tm, fsec, tz, result);
340  AdjustTimeForTypmod(&(result->time), typmod);
341  return result;
342 }
TimeADT time
Definition: date.h:29
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:1784
int errcode(int sqlerrcode)
Definition: elog.c:610
Definition: pgtime.h:25
static struct pg_tm tm
Definition: localtime.c:102
int tm2timetz(struct pg_tm *tm, fsec_t fsec, int tz, TimeTzADT *result)
Definition: date.c:2076
#define ERROR
Definition: elog.h:43
int32 fsec_t
Definition: timestamp.h:41
TimestampTz GetCurrentTransactionStartTimestamp(void)
Definition: xact.c:798
#define ereport(elevel,...)
Definition: elog.h:144
void * palloc(Size size)
Definition: mcxt.c:949
int errmsg(const char *fmt,...)
Definition: elog.c:824
Definition: date.h:27
void AdjustTimeForTypmod(TimeADT *time, int32 typmod)
Definition: date.c:1498

◆ GetSQLLocalTime()

TimeADT GetSQLLocalTime ( int32  typmod)

Definition at line 348 of file date.c.

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

Referenced by ExecEvalSQLValueFunction().

349 {
350  TimeADT result;
351  TimestampTz ts;
352  struct pg_tm tt,
353  *tm = &tt;
354  fsec_t fsec;
355  int tz;
356 
358 
359  if (timestamp2tm(ts, &tz, tm, &fsec, NULL, NULL) != 0)
360  ereport(ERROR,
361  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
362  errmsg("timestamp out of range")));
363 
364  tm2time(tm, fsec, &result);
365  AdjustTimeForTypmod(&result, typmod);
366  return result;
367 }
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:1784
int errcode(int sqlerrcode)
Definition: elog.c:610
Definition: pgtime.h:25
static struct pg_tm tm
Definition: localtime.c:102
#define ERROR
Definition: elog.h:43
int32 fsec_t
Definition: timestamp.h:41
int64 TimeADT
Definition: date.h:25
TimestampTz GetCurrentTransactionStartTimestamp(void)
Definition: xact.c:798
#define ereport(elevel,...)
Definition: elog.h:144
int tm2time(struct pg_tm *tm, fsec_t fsec, TimeADT *result)
Definition: date.c:1267
int errmsg(const char *fmt,...)
Definition: elog.c:824
void AdjustTimeForTypmod(TimeADT *time, int32 typmod)
Definition: date.c:1498

◆ time2tm()

int time2tm ( TimeADT  time,
struct pg_tm tm,
fsec_t fsec 
)

Definition at line 1341 of file date.c.

References pg_tm::tm_hour, pg_tm::tm_min, pg_tm::tm_sec, USECS_PER_HOUR, USECS_PER_MINUTE, and USECS_PER_SEC.

Referenced by JsonEncodeDateTime(), time_out(), time_part(), and time_timetz().

1342 {
1343  tm->tm_hour = time / USECS_PER_HOUR;
1344  time -= tm->tm_hour * USECS_PER_HOUR;
1345  tm->tm_min = time / USECS_PER_MINUTE;
1346  time -= tm->tm_min * USECS_PER_MINUTE;
1347  tm->tm_sec = time / USECS_PER_SEC;
1348  time -= tm->tm_sec * USECS_PER_SEC;
1349  *fsec = time;
1350  return 0;
1351 }
#define USECS_PER_SEC
Definition: timestamp.h:94
int tm_hour
Definition: pgtime.h:29
#define USECS_PER_MINUTE
Definition: timestamp.h:93
#define USECS_PER_HOUR
Definition: timestamp.h:92
int tm_sec
Definition: pgtime.h:27
int tm_min
Definition: pgtime.h:28

◆ time_overflows()

bool time_overflows ( int  hour,
int  min,
int  sec,
fsec_t  fsec 
)

Definition at line 1278 of file date.c.

References HOURS_PER_DAY, MINS_PER_HOUR, SECS_PER_MINUTE, USECS_PER_DAY, and USECS_PER_SEC.

Referenced by DecodeDateTime(), and DecodeTimeOnly().

1279 {
1280  /* Range-check the fields individually. */
1281  if (hour < 0 || hour > HOURS_PER_DAY ||
1282  min < 0 || min >= MINS_PER_HOUR ||
1283  sec < 0 || sec > SECS_PER_MINUTE ||
1284  fsec < 0 || fsec > USECS_PER_SEC)
1285  return true;
1286 
1287  /*
1288  * Because we allow, eg, hour = 24 or sec = 60, we must check separately
1289  * that the total time value doesn't exceed 24:00:00.
1290  */
1291  if ((((((hour * MINS_PER_HOUR + min) * SECS_PER_MINUTE)
1292  + sec) * USECS_PER_SEC) + fsec) > USECS_PER_DAY)
1293  return true;
1294 
1295  return false;
1296 }
#define USECS_PER_SEC
Definition: timestamp.h:94
#define MINS_PER_HOUR
Definition: timestamp.h:89
#define HOURS_PER_DAY
Definition: timestamp.h:78
#define SECS_PER_MINUTE
Definition: timestamp.h:88
#define USECS_PER_DAY
Definition: timestamp.h:91

◆ timetz2tm()

int timetz2tm ( TimeTzADT time,
struct pg_tm tm,
fsec_t fsec,
int *  tzp 
)

Definition at line 2210 of file date.c.

References TimeTzADT::time, pg_tm::tm_hour, pg_tm::tm_min, pg_tm::tm_sec, USECS_PER_HOUR, USECS_PER_MINUTE, USECS_PER_SEC, and TimeTzADT::zone.

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

2211 {
2212  TimeOffset trem = time->time;
2213 
2214  tm->tm_hour = trem / USECS_PER_HOUR;
2215  trem -= tm->tm_hour * USECS_PER_HOUR;
2216  tm->tm_min = trem / USECS_PER_MINUTE;
2217  trem -= tm->tm_min * USECS_PER_MINUTE;
2218  tm->tm_sec = trem / USECS_PER_SEC;
2219  *fsec = trem - tm->tm_sec * USECS_PER_SEC;
2220 
2221  if (tzp != NULL)
2222  *tzp = time->zone;
2223 
2224  return 0;
2225 }
TimeADT time
Definition: date.h:29
#define USECS_PER_SEC
Definition: timestamp.h:94
int tm_hour
Definition: pgtime.h:29
#define USECS_PER_MINUTE
Definition: timestamp.h:93
int32 zone
Definition: date.h:30
#define USECS_PER_HOUR
Definition: timestamp.h:92
int64 TimeOffset
Definition: timestamp.h:40
int tm_sec
Definition: pgtime.h:27
int tm_min
Definition: pgtime.h:28

◆ tm2time()

int tm2time ( struct pg_tm tm,
fsec_t  fsec,
TimeADT result 
)

Definition at line 1267 of file date.c.

References MINS_PER_HOUR, SECS_PER_MINUTE, pg_tm::tm_hour, pg_tm::tm_min, pg_tm::tm_sec, and USECS_PER_SEC.

Referenced by GetSQLLocalTime(), parse_datetime(), and time_in().

1268 {
1269  *result = ((((tm->tm_hour * MINS_PER_HOUR + tm->tm_min) * SECS_PER_MINUTE) + tm->tm_sec)
1270  * USECS_PER_SEC) + fsec;
1271  return 0;
1272 }
#define USECS_PER_SEC
Definition: timestamp.h:94
int tm_hour
Definition: pgtime.h:29
#define MINS_PER_HOUR
Definition: timestamp.h:89
#define SECS_PER_MINUTE
Definition: timestamp.h:88
int tm_sec
Definition: pgtime.h:27
int tm_min
Definition: pgtime.h:28

◆ tm2timetz()

int tm2timetz ( struct pg_tm tm,
fsec_t  fsec,
int  tz,
TimeTzADT result 
)

Definition at line 2076 of file date.c.

References MINS_PER_HOUR, SECS_PER_MINUTE, TimeTzADT::time, pg_tm::tm_hour, pg_tm::tm_min, pg_tm::tm_sec, USECS_PER_SEC, and TimeTzADT::zone.

Referenced by GetSQLCurrentTime(), parse_datetime(), timestamptz_timetz(), and timetz_in().

2077 {
2078  result->time = ((((tm->tm_hour * MINS_PER_HOUR + tm->tm_min) * SECS_PER_MINUTE) + tm->tm_sec) *
2079  USECS_PER_SEC) + fsec;
2080  result->zone = tz;
2081 
2082  return 0;
2083 }
TimeADT time
Definition: date.h:29
#define USECS_PER_SEC
Definition: timestamp.h:94
int tm_hour
Definition: pgtime.h:29
#define MINS_PER_HOUR
Definition: timestamp.h:89
int32 zone
Definition: date.h:30
#define SECS_PER_MINUTE
Definition: timestamp.h:88
int tm_sec
Definition: pgtime.h:27
int tm_min
Definition: pgtime.h:28