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 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

static DateADT DatumGetDateADT (Datum X)
 
static TimeADT DatumGetTimeADT (Datum X)
 
static TimeTzADTDatumGetTimeTzADTP (Datum X)
 
static Datum DateADTGetDatum (DateADT X)
 
static Datum TimeADTGetDatum (TimeADT X)
 
static Datum TimeTzADTPGetDatum (const TimeTzADT *X)
 
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)
 
int32 date_cmp_timestamp_internal (DateADT dateVal, Timestamp dt2)
 
int32 date_cmp_timestamptz_internal (DateADT dateVal, TimestampTz dt2)
 
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)

Definition at line 40 of file date.h.

◆ DATE_IS_NOEND

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

Definition at line 42 of file date.h.

◆ DATE_NOBEGIN

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

Definition at line 39 of file date.h.

◆ DATE_NOEND

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

Definition at line 41 of file date.h.

◆ DATE_NOT_FINITE

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

Definition at line 43 of file date.h.

◆ DATEVAL_NOBEGIN

#define DATEVAL_NOBEGIN   ((DateADT) PG_INT32_MIN)

Definition at line 36 of file date.h.

◆ DATEVAL_NOEND

#define DATEVAL_NOEND   ((DateADT) PG_INT32_MAX)

Definition at line 37 of file date.h.

◆ MAX_TIME_PRECISION

#define MAX_TIME_PRECISION   6

Definition at line 45 of file date.h.

◆ PG_GETARG_DATEADT

#define PG_GETARG_DATEADT (   n)    DatumGetDateADT(PG_GETARG_DATUM(n))

Definition at line 89 of file date.h.

◆ PG_GETARG_TIMEADT

#define PG_GETARG_TIMEADT (   n)    DatumGetTimeADT(PG_GETARG_DATUM(n))

Definition at line 90 of file date.h.

◆ PG_GETARG_TIMETZADT_P

#define PG_GETARG_TIMETZADT_P (   n)    DatumGetTimeTzADTP(PG_GETARG_DATUM(n))

Definition at line 91 of file date.h.

◆ PG_RETURN_DATEADT

#define PG_RETURN_DATEADT (   x)    return DateADTGetDatum(x)

Definition at line 93 of file date.h.

◆ PG_RETURN_TIMEADT

#define PG_RETURN_TIMEADT (   x)    return TimeADTGetDatum(x)

Definition at line 94 of file date.h.

◆ PG_RETURN_TIMETZADT_P

#define PG_RETURN_TIMETZADT_P (   x)    return TimeTzADTPGetDatum(x)

Definition at line 95 of file date.h.

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 1657 of file date.c.

1658 {
1659  static const int64 TimeScales[MAX_TIME_PRECISION + 1] = {
1660  INT64CONST(1000000),
1661  INT64CONST(100000),
1662  INT64CONST(10000),
1663  INT64CONST(1000),
1664  INT64CONST(100),
1665  INT64CONST(10),
1666  INT64CONST(1)
1667  };
1668 
1669  static const int64 TimeOffsets[MAX_TIME_PRECISION + 1] = {
1670  INT64CONST(500000),
1671  INT64CONST(50000),
1672  INT64CONST(5000),
1673  INT64CONST(500),
1674  INT64CONST(50),
1675  INT64CONST(5),
1676  INT64CONST(0)
1677  };
1678 
1679  if (typmod >= 0 && typmod <= MAX_TIME_PRECISION)
1680  {
1681  if (*time >= INT64CONST(0))
1682  *time = ((*time + TimeOffsets[typmod]) / TimeScales[typmod]) *
1683  TimeScales[typmod];
1684  else
1685  *time = -((((-*time) + TimeOffsets[typmod]) / TimeScales[typmod]) *
1686  TimeScales[typmod]);
1687  }
1688 }
#define MAX_TIME_PRECISION
Definition: date.h:45

References MAX_TIME_PRECISION.

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

◆ anytime_typmod_check()

int32 anytime_typmod_check ( bool  istz,
int32  typmod 
)

Definition at line 71 of file date.c.

72 {
73  if (typmod < 0)
74  ereport(ERROR,
75  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
76  errmsg("TIME(%d)%s precision must not be negative",
77  typmod, (istz ? " WITH TIME ZONE" : ""))));
78  if (typmod > MAX_TIME_PRECISION)
79  {
81  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
82  errmsg("TIME(%d)%s precision reduced to maximum allowed, %d",
83  typmod, (istz ? " WITH TIME ZONE" : ""),
85  typmod = MAX_TIME_PRECISION;
86  }
87 
88  return typmod;
89 }
int errcode(int sqlerrcode)
Definition: elog.c:853
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define WARNING
Definition: elog.h:36
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149

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

Referenced by anytime_typmodin(), executeDateTimeMethod(), and transformSQLValueFunction().

◆ date2timestamp_no_overflow()

double date2timestamp_no_overflow ( DateADT  dateVal)

Definition at line 732 of file date.c.

733 {
734  double result;
735 
736  if (DATE_IS_NOBEGIN(dateVal))
737  result = -DBL_MAX;
738  else if (DATE_IS_NOEND(dateVal))
739  result = DBL_MAX;
740  else
741  {
742  /* date is days since 2000, timestamp is microseconds since same... */
743  result = dateVal * (double) USECS_PER_DAY;
744  }
745 
746  return result;
747 }
#define USECS_PER_DAY
Definition: timestamp.h:131
#define DATE_IS_NOEND(j)
Definition: date.h:42
#define DATE_IS_NOBEGIN(j)
Definition: date.h:40

References DATE_IS_NOBEGIN, DATE_IS_NOEND, and USECS_PER_DAY.

Referenced by convert_timevalue_to_scalar().

◆ date2timestamp_opt_overflow()

Timestamp date2timestamp_opt_overflow ( DateADT  dateVal,
int *  overflow 
)

Definition at line 576 of file date.c.

577 {
578  Timestamp result;
579 
580  if (overflow)
581  *overflow = 0;
582 
583  if (DATE_IS_NOBEGIN(dateVal))
584  TIMESTAMP_NOBEGIN(result);
585  else if (DATE_IS_NOEND(dateVal))
586  TIMESTAMP_NOEND(result);
587  else
588  {
589  /*
590  * Since dates have the same minimum values as timestamps, only upper
591  * boundary need be checked for overflow.
592  */
593  if (dateVal >= (TIMESTAMP_END_JULIAN - POSTGRES_EPOCH_JDATE))
594  {
595  if (overflow)
596  {
597  *overflow = 1;
598  TIMESTAMP_NOEND(result);
599  return result;
600  }
601  else
602  {
603  ereport(ERROR,
604  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
605  errmsg("date out of range for timestamp")));
606  }
607  }
608 
609  /* date is days since 2000, timestamp is microseconds since same... */
610  result = dateVal * USECS_PER_DAY;
611  }
612 
613  return result;
614 }
int64 Timestamp
Definition: timestamp.h:38
#define TIMESTAMP_NOBEGIN(j)
Definition: timestamp.h:159
#define TIMESTAMP_END_JULIAN
Definition: timestamp.h:253
#define POSTGRES_EPOCH_JDATE
Definition: timestamp.h:235
#define TIMESTAMP_NOEND(j)
Definition: timestamp.h:164

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 date2timestamp(), and date_cmp_timestamp_internal().

◆ date2timestamptz_opt_overflow()

TimestampTz date2timestamptz_opt_overflow ( DateADT  dateVal,
int *  overflow 
)

Definition at line 636 of file date.c.

637 {
638  TimestampTz result;
639  struct pg_tm tt,
640  *tm = &tt;
641  int tz;
642 
643  if (overflow)
644  *overflow = 0;
645 
646  if (DATE_IS_NOBEGIN(dateVal))
647  TIMESTAMP_NOBEGIN(result);
648  else if (DATE_IS_NOEND(dateVal))
649  TIMESTAMP_NOEND(result);
650  else
651  {
652  /*
653  * Since dates have the same minimum values as timestamps, only upper
654  * boundary need be checked for overflow.
655  */
656  if (dateVal >= (TIMESTAMP_END_JULIAN - POSTGRES_EPOCH_JDATE))
657  {
658  if (overflow)
659  {
660  *overflow = 1;
661  TIMESTAMP_NOEND(result);
662  return result;
663  }
664  else
665  {
666  ereport(ERROR,
667  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
668  errmsg("date out of range for timestamp")));
669  }
670  }
671 
672  j2date(dateVal + POSTGRES_EPOCH_JDATE,
673  &(tm->tm_year), &(tm->tm_mon), &(tm->tm_mday));
674  tm->tm_hour = 0;
675  tm->tm_min = 0;
676  tm->tm_sec = 0;
678 
679  result = dateVal * USECS_PER_DAY + tz * USECS_PER_SEC;
680 
681  /*
682  * Since it is possible to go beyond allowed timestamptz range because
683  * of time zone, check for allowed timestamp range after adding tz.
684  */
685  if (!IS_VALID_TIMESTAMP(result))
686  {
687  if (overflow)
688  {
689  if (result < MIN_TIMESTAMP)
690  {
691  *overflow = -1;
692  TIMESTAMP_NOBEGIN(result);
693  }
694  else
695  {
696  *overflow = 1;
697  TIMESTAMP_NOEND(result);
698  }
699  }
700  else
701  {
702  ereport(ERROR,
703  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
704  errmsg("date out of range for timestamp")));
705  }
706  }
707  }
708 
709  return result;
710 }
int DetermineTimeZoneOffset(struct pg_tm *tm, pg_tz *tzp)
Definition: datetime.c:1585
void j2date(int jd, int *year, int *month, int *day)
Definition: datetime.c:311
int64 TimestampTz
Definition: timestamp.h:39
#define IS_VALID_TIMESTAMP(t)
Definition: timestamp.h:267
#define USECS_PER_SEC
Definition: timestamp.h:134
#define MIN_TIMESTAMP
Definition: timestamp.h:256
static struct pg_tm tm
Definition: localtime.c:104
PGDLLIMPORT pg_tz * session_timezone
Definition: pgtz.c:28
Definition: pgtime.h:35
int tm_hour
Definition: pgtime.h:38
int tm_mday
Definition: pgtime.h:39
int tm_mon
Definition: pgtime.h:40
int tm_min
Definition: pgtime.h:37
int tm_sec
Definition: pgtime.h:36
int tm_year
Definition: pgtime.h:41

References DATE_IS_NOBEGIN, DATE_IS_NOEND, DetermineTimeZoneOffset(), 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 date2timestamptz(), and date_cmp_timestamptz_internal().

◆ date_cmp_timestamp_internal()

int32 date_cmp_timestamp_internal ( DateADT  dateVal,
Timestamp  dt2 
)

Definition at line 755 of file date.c.

756 {
757  Timestamp dt1;
758  int overflow;
759 
760  dt1 = date2timestamp_opt_overflow(dateVal, &overflow);
761  if (overflow > 0)
762  {
763  /* dt1 is larger than any finite timestamp, but less than infinity */
764  return TIMESTAMP_IS_NOEND(dt2) ? -1 : +1;
765  }
766  Assert(overflow == 0); /* -1 case cannot occur */
767 
768  return timestamp_cmp_internal(dt1, dt2);
769 }
int timestamp_cmp_internal(Timestamp dt1, Timestamp dt2)
Definition: timestamp.c:2191
#define Assert(condition)
Definition: c.h:849
#define TIMESTAMP_IS_NOEND(j)
Definition: timestamp.h:167
Timestamp date2timestamp_opt_overflow(DateADT dateVal, int *overflow)
Definition: date.c:576

References Assert, date2timestamp_opt_overflow(), timestamp_cmp_internal(), and TIMESTAMP_IS_NOEND.

Referenced by cmpDateToTimestamp(), date_cmp_timestamp(), date_eq_timestamp(), date_ge_timestamp(), date_gt_timestamp(), date_le_timestamp(), date_lt_timestamp(), date_ne_timestamp(), timestamp_cmp_date(), timestamp_eq_date(), timestamp_ge_date(), timestamp_gt_date(), timestamp_le_date(), timestamp_lt_date(), and timestamp_ne_date().

◆ date_cmp_timestamptz_internal()

int32 date_cmp_timestamptz_internal ( DateADT  dateVal,
TimestampTz  dt2 
)

Definition at line 835 of file date.c.

836 {
837  TimestampTz dt1;
838  int overflow;
839 
840  dt1 = date2timestamptz_opt_overflow(dateVal, &overflow);
841  if (overflow > 0)
842  {
843  /* dt1 is larger than any finite timestamp, but less than infinity */
844  return TIMESTAMP_IS_NOEND(dt2) ? -1 : +1;
845  }
846  if (overflow < 0)
847  {
848  /* dt1 is less than any finite timestamp, but more than -infinity */
849  return TIMESTAMP_IS_NOBEGIN(dt2) ? +1 : -1;
850  }
851 
852  return timestamptz_cmp_internal(dt1, dt2);
853 }
#define TIMESTAMP_IS_NOBEGIN(j)
Definition: timestamp.h:162
TimestampTz date2timestamptz_opt_overflow(DateADT dateVal, int *overflow)
Definition: date.c:636
#define timestamptz_cmp_internal(dt1, dt2)
Definition: timestamp.h:131

References date2timestamptz_opt_overflow(), TIMESTAMP_IS_NOBEGIN, TIMESTAMP_IS_NOEND, and timestamptz_cmp_internal.

Referenced by cmpDateToTimestampTz(), date_cmp_timestamptz(), date_eq_timestamptz(), date_ge_timestamptz(), date_gt_timestamptz(), date_le_timestamptz(), date_lt_timestamptz(), date_ne_timestamptz(), timestamptz_cmp_date(), timestamptz_eq_date(), timestamptz_ge_date(), timestamptz_gt_date(), timestamptz_le_date(), timestamptz_lt_date(), and timestamptz_ne_date().

◆ DateADTGetDatum()

static Datum DateADTGetDatum ( DateADT  X)
inlinestatic

◆ DatumGetDateADT()

static DateADT DatumGetDateADT ( Datum  X)
inlinestatic

Definition at line 54 of file date.h.

55 {
56  return (DateADT) DatumGetInt32(X);
57 }
int32 DateADT
Definition: date.h:23
static int32 DatumGetInt32(Datum X)
Definition: postgres.h:202

References DatumGetInt32().

Referenced by compareDatetime(), convert_timevalue_to_scalar(), daterange_canonical(), executeDateTimeMethod(), gbt_num_compress(), JsonEncodeDateTime(), and map_sql_value_to_xml_value().

◆ DatumGetTimeADT()

static TimeADT DatumGetTimeADT ( Datum  X)
inlinestatic

Definition at line 60 of file date.h.

61 {
62  return (TimeADT) DatumGetInt64(X);
63 }
int64 TimeADT
Definition: date.h:25
static int64 DatumGetInt64(Datum X)
Definition: postgres.h:385

References DatumGetInt64().

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

◆ DatumGetTimeTzADTP()

static TimeTzADT* DatumGetTimeTzADTP ( Datum  X)
inlinestatic

Definition at line 66 of file date.h.

67 {
68  return (TimeTzADT *) DatumGetPointer(X);
69 }
static Pointer DatumGetPointer(Datum X)
Definition: postgres.h:312
Definition: date.h:28

References DatumGetPointer().

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

◆ EncodeSpecialDate()

void EncodeSpecialDate ( DateADT  dt,
char *  str 
)

Definition at line 294 of file date.c.

295 {
296  if (DATE_IS_NOBEGIN(dt))
297  strcpy(str, EARLY);
298  else if (DATE_IS_NOEND(dt))
299  strcpy(str, LATE);
300  else /* shouldn't happen */
301  elog(ERROR, "invalid argument for EncodeSpecialDate");
302 }
#define elog(elevel,...)
Definition: elog.h:225
const char * str
#define EARLY
Definition: datetime.h:39
#define LATE
Definition: datetime.h:40

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

Referenced by date_out(), and JsonEncodeDateTime().

◆ float_time_overflows()

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

Definition at line 1463 of file date.c.

1464 {
1465  /* Range-check the fields individually. */
1466  if (hour < 0 || hour > HOURS_PER_DAY ||
1467  min < 0 || min >= MINS_PER_HOUR)
1468  return true;
1469 
1470  /*
1471  * "sec", being double, requires extra care. Cope with NaN, and round off
1472  * before applying the range check to avoid unexpected errors due to
1473  * imprecise input. (We assume rint() behaves sanely with infinities.)
1474  */
1475  if (isnan(sec))
1476  return true;
1477  sec = rint(sec * USECS_PER_SEC);
1478  if (sec < 0 || sec > SECS_PER_MINUTE * USECS_PER_SEC)
1479  return true;
1480 
1481  /*
1482  * Because we allow, eg, hour = 24 or sec = 60, we must check separately
1483  * that the total time value doesn't exceed 24:00:00. This must match the
1484  * way that callers will convert the fields to a time.
1485  */
1486  if (((((hour * MINS_PER_HOUR + min) * SECS_PER_MINUTE)
1487  * USECS_PER_SEC) + (int64) sec) > USECS_PER_DAY)
1488  return true;
1489 
1490  return false;
1491 }
#define MINS_PER_HOUR
Definition: timestamp.h:129
#define SECS_PER_MINUTE
Definition: timestamp.h:128
#define HOURS_PER_DAY
Definition: timestamp.h:118

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().

◆ GetSQLCurrentDate()

DateADT GetSQLCurrentDate ( void  )

Definition at line 309 of file date.c.

310 {
311  struct pg_tm tm;
312 
313  static int cache_year = 0;
314  static int cache_mon = 0;
315  static int cache_mday = 0;
316  static DateADT cache_date;
317 
319 
320  /*
321  * date2j involves several integer divisions; moreover, unless our session
322  * lives across local midnight, we don't really have to do it more than
323  * once. So it seems worth having a separate cache here.
324  */
325  if (tm.tm_year != cache_year ||
326  tm.tm_mon != cache_mon ||
327  tm.tm_mday != cache_mday)
328  {
330  cache_year = tm.tm_year;
331  cache_mon = tm.tm_mon;
332  cache_mday = tm.tm_mday;
333  }
334 
335  return cache_date;
336 }
void GetCurrentDateTime(struct pg_tm *tm)
Definition: datetime.c:366
int date2j(int year, int month, int day)
Definition: datetime.c:286

References date2j(), GetCurrentDateTime(), POSTGRES_EPOCH_JDATE, tm, pg_tm::tm_mday, pg_tm::tm_mon, and pg_tm::tm_year.

Referenced by ExecEvalSQLValueFunction().

◆ GetSQLCurrentTime()

TimeTzADT* GetSQLCurrentTime ( int32  typmod)

Definition at line 342 of file date.c.

343 {
344  TimeTzADT *result;
345  struct pg_tm tt,
346  *tm = &tt;
347  fsec_t fsec;
348  int tz;
349 
350  GetCurrentTimeUsec(tm, &fsec, &tz);
351 
352  result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
353  tm2timetz(tm, fsec, tz, result);
354  AdjustTimeForTypmod(&(result->time), typmod);
355  return result;
356 }
void GetCurrentTimeUsec(struct pg_tm *tm, fsec_t *fsec, int *tzp)
Definition: datetime.c:387
int32 fsec_t
Definition: timestamp.h:41
int tm2timetz(struct pg_tm *tm, fsec_t fsec, int tz, TimeTzADT *result)
Definition: date.c:2275
void AdjustTimeForTypmod(TimeADT *time, int32 typmod)
Definition: date.c:1657
void * palloc(Size size)
Definition: mcxt.c:1317
TimeADT time
Definition: date.h:29

References AdjustTimeForTypmod(), GetCurrentTimeUsec(), palloc(), TimeTzADT::time, tm, and tm2timetz().

Referenced by ExecEvalSQLValueFunction().

◆ GetSQLLocalTime()

TimeADT GetSQLLocalTime ( int32  typmod)

Definition at line 362 of file date.c.

363 {
364  TimeADT result;
365  struct pg_tm tt,
366  *tm = &tt;
367  fsec_t fsec;
368  int tz;
369 
370  GetCurrentTimeUsec(tm, &fsec, &tz);
371 
372  tm2time(tm, fsec, &result);
373  AdjustTimeForTypmod(&result, typmod);
374  return result;
375 }
int tm2time(struct pg_tm *tm, fsec_t fsec, TimeADT *result)
Definition: date.c:1428

References AdjustTimeForTypmod(), GetCurrentTimeUsec(), tm, and tm2time().

Referenced by ExecEvalSQLValueFunction().

◆ time2tm()

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

Definition at line 1500 of file date.c.

1501 {
1502  tm->tm_hour = time / USECS_PER_HOUR;
1503  time -= tm->tm_hour * USECS_PER_HOUR;
1504  tm->tm_min = time / USECS_PER_MINUTE;
1505  time -= tm->tm_min * USECS_PER_MINUTE;
1506  tm->tm_sec = time / USECS_PER_SEC;
1507  time -= tm->tm_sec * USECS_PER_SEC;
1508  *fsec = time;
1509  return 0;
1510 }
#define USECS_PER_HOUR
Definition: timestamp.h:132
#define USECS_PER_MINUTE
Definition: timestamp.h:133

References tm, 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_common(), and time_timetz().

◆ time_overflows()

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

Definition at line 1439 of file date.c.

1440 {
1441  /* Range-check the fields individually. */
1442  if (hour < 0 || hour > HOURS_PER_DAY ||
1443  min < 0 || min >= MINS_PER_HOUR ||
1444  sec < 0 || sec > SECS_PER_MINUTE ||
1445  fsec < 0 || fsec > USECS_PER_SEC)
1446  return true;
1447 
1448  /*
1449  * Because we allow, eg, hour = 24 or sec = 60, we must check separately
1450  * that the total time value doesn't exceed 24:00:00.
1451  */
1452  if ((((((hour * MINS_PER_HOUR + min) * SECS_PER_MINUTE)
1453  + sec) * USECS_PER_SEC) + fsec) > USECS_PER_DAY)
1454  return true;
1455 
1456  return false;
1457 }

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

Referenced by DecodeDateTime(), and DecodeTimeOnly().

◆ TimeADTGetDatum()

static Datum TimeADTGetDatum ( TimeADT  X)
inlinestatic

Definition at line 78 of file date.h.

79 {
80  return Int64GetDatum(X);
81 }
Datum Int64GetDatum(int64 X)
Definition: fmgr.c:1807

References Int64GetDatum().

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

◆ timetz2tm()

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

Definition at line 2415 of file date.c.

2416 {
2417  TimeOffset trem = time->time;
2418 
2419  tm->tm_hour = trem / USECS_PER_HOUR;
2420  trem -= tm->tm_hour * USECS_PER_HOUR;
2421  tm->tm_min = trem / USECS_PER_MINUTE;
2422  trem -= tm->tm_min * USECS_PER_MINUTE;
2423  tm->tm_sec = trem / USECS_PER_SEC;
2424  *fsec = trem - tm->tm_sec * USECS_PER_SEC;
2425 
2426  if (tzp != NULL)
2427  *tzp = time->zone;
2428 
2429  return 0;
2430 }
int64 TimeOffset
Definition: timestamp.h:40
int32 zone
Definition: date.h:30

References TimeTzADT::time, tm, 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_common().

◆ TimeTzADTPGetDatum()

static Datum TimeTzADTPGetDatum ( const TimeTzADT X)
inlinestatic

Definition at line 84 of file date.h.

85 {
86  return PointerGetDatum(X);
87 }
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:322

References PointerGetDatum().

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

◆ tm2time()

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

Definition at line 1428 of file date.c.

1429 {
1430  *result = ((((tm->tm_hour * MINS_PER_HOUR + tm->tm_min) * SECS_PER_MINUTE) + tm->tm_sec)
1431  * USECS_PER_SEC) + fsec;
1432  return 0;
1433 }

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

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

◆ tm2timetz()

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

Definition at line 2275 of file date.c.

2276 {
2277  result->time = ((((tm->tm_hour * MINS_PER_HOUR + tm->tm_min) * SECS_PER_MINUTE) + tm->tm_sec) *
2278  USECS_PER_SEC) + fsec;
2279  result->zone = tz;
2280 
2281  return 0;
2282 }

References MINS_PER_HOUR, SECS_PER_MINUTE, TimeTzADT::time, tm, 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().