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

◆ DateADTGetDatum

#define DateADTGetDatum (   X)    Int32GetDatum(X)

Definition at line 57 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.

◆ DatumGetDateADT

#define DatumGetDateADT (   X)    ((DateADT) DatumGetInt32(X))

Definition at line 53 of file date.h.

◆ DatumGetTimeADT

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

Definition at line 54 of file date.h.

◆ DatumGetTimeTzADTP

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

Definition at line 55 of file date.h.

◆ MAX_TIME_PRECISION

#define MAX_TIME_PRECISION   6

Definition at line 51 of file date.h.

◆ PG_GETARG_DATEADT

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

Definition at line 61 of file date.h.

◆ PG_GETARG_TIMEADT

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

Definition at line 62 of file date.h.

◆ PG_GETARG_TIMETZADT_P

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

Definition at line 63 of file date.h.

◆ PG_RETURN_DATEADT

#define PG_RETURN_DATEADT (   x)    return DateADTGetDatum(x)

Definition at line 65 of file date.h.

◆ PG_RETURN_TIMEADT

#define PG_RETURN_TIMEADT (   x)    return TimeADTGetDatum(x)

Definition at line 66 of file date.h.

◆ PG_RETURN_TIMETZADT_P

#define PG_RETURN_TIMETZADT_P (   x)    return TimeTzADTPGetDatum(x)

Definition at line 67 of file date.h.

◆ TimeADTGetDatum

#define TimeADTGetDatum (   X)    Int64GetDatum(X)

Definition at line 58 of file date.h.

◆ TimeTzADTPGetDatum

#define TimeTzADTPGetDatum (   X)    PointerGetDatum(X)

Definition at line 59 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 1633 of file date.c.

1634 {
1635  static const int64 TimeScales[MAX_TIME_PRECISION + 1] = {
1636  INT64CONST(1000000),
1637  INT64CONST(100000),
1638  INT64CONST(10000),
1639  INT64CONST(1000),
1640  INT64CONST(100),
1641  INT64CONST(10),
1642  INT64CONST(1)
1643  };
1644 
1645  static const int64 TimeOffsets[MAX_TIME_PRECISION + 1] = {
1646  INT64CONST(500000),
1647  INT64CONST(50000),
1648  INT64CONST(5000),
1649  INT64CONST(500),
1650  INT64CONST(50),
1651  INT64CONST(5),
1652  INT64CONST(0)
1653  };
1654 
1655  if (typmod >= 0 && typmod <= MAX_TIME_PRECISION)
1656  {
1657  if (*time >= INT64CONST(0))
1658  *time = ((*time + TimeOffsets[typmod]) / TimeScales[typmod]) *
1659  TimeScales[typmod];
1660  else
1661  *time = -((((-*time) + TimeOffsets[typmod]) / TimeScales[typmod]) *
1662  TimeScales[typmod]);
1663  }
1664 }
#define MAX_TIME_PRECISION
Definition: date.h:51

References MAX_TIME_PRECISION.

Referenced by 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 70 of file date.c.

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

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

Referenced by anytime_typmodin(), and transformSQLValueFunction().

◆ date2timestamp_no_overflow()

double date2timestamp_no_overflow ( DateADT  dateVal)

Definition at line 713 of file date.c.

714 {
715  double result;
716 
717  if (DATE_IS_NOBEGIN(dateVal))
718  result = -DBL_MAX;
719  else if (DATE_IS_NOEND(dateVal))
720  result = DBL_MAX;
721  else
722  {
723  /* date is days since 2000, timestamp is microseconds since same... */
724  result = dateVal * (double) USECS_PER_DAY;
725  }
726 
727  return result;
728 }
#define USECS_PER_DAY
Definition: timestamp.h:130
#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 557 of file date.c.

558 {
559  Timestamp result;
560 
561  if (overflow)
562  *overflow = 0;
563 
564  if (DATE_IS_NOBEGIN(dateVal))
565  TIMESTAMP_NOBEGIN(result);
566  else if (DATE_IS_NOEND(dateVal))
567  TIMESTAMP_NOEND(result);
568  else
569  {
570  /*
571  * Since dates have the same minimum values as timestamps, only upper
572  * boundary need be checked for overflow.
573  */
574  if (dateVal >= (TIMESTAMP_END_JULIAN - POSTGRES_EPOCH_JDATE))
575  {
576  if (overflow)
577  {
578  *overflow = 1;
579  TIMESTAMP_NOEND(result);
580  return result;
581  }
582  else
583  {
584  ereport(ERROR,
585  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
586  errmsg("date out of range for timestamp")));
587  }
588  }
589 
590  /* date is days since 2000, timestamp is microseconds since same... */
591  result = dateVal * USECS_PER_DAY;
592  }
593 
594  return result;
595 }
int64 Timestamp
Definition: timestamp.h:38
#define TIMESTAMP_NOBEGIN(j)
Definition: timestamp.h:151
#define TIMESTAMP_END_JULIAN
Definition: timestamp.h:220
#define POSTGRES_EPOCH_JDATE
Definition: timestamp.h:202
#define TIMESTAMP_NOEND(j)
Definition: timestamp.h:156

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

618 {
619  TimestampTz result;
620  struct pg_tm tt,
621  *tm = &tt;
622  int tz;
623 
624  if (overflow)
625  *overflow = 0;
626 
627  if (DATE_IS_NOBEGIN(dateVal))
628  TIMESTAMP_NOBEGIN(result);
629  else if (DATE_IS_NOEND(dateVal))
630  TIMESTAMP_NOEND(result);
631  else
632  {
633  /*
634  * Since dates have the same minimum values as timestamps, only upper
635  * boundary need be checked for overflow.
636  */
637  if (dateVal >= (TIMESTAMP_END_JULIAN - POSTGRES_EPOCH_JDATE))
638  {
639  if (overflow)
640  {
641  *overflow = 1;
642  TIMESTAMP_NOEND(result);
643  return result;
644  }
645  else
646  {
647  ereport(ERROR,
648  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
649  errmsg("date out of range for timestamp")));
650  }
651  }
652 
653  j2date(dateVal + POSTGRES_EPOCH_JDATE,
654  &(tm->tm_year), &(tm->tm_mon), &(tm->tm_mday));
655  tm->tm_hour = 0;
656  tm->tm_min = 0;
657  tm->tm_sec = 0;
659 
660  result = dateVal * USECS_PER_DAY + tz * USECS_PER_SEC;
661 
662  /*
663  * Since it is possible to go beyond allowed timestamptz range because
664  * of time zone, check for allowed timestamp range after adding tz.
665  */
666  if (!IS_VALID_TIMESTAMP(result))
667  {
668  if (overflow)
669  {
670  if (result < MIN_TIMESTAMP)
671  {
672  *overflow = -1;
673  TIMESTAMP_NOBEGIN(result);
674  }
675  else
676  {
677  *overflow = 1;
678  TIMESTAMP_NOEND(result);
679  }
680  }
681  else
682  {
683  ereport(ERROR,
684  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
685  errmsg("date out of range for timestamp")));
686  }
687  }
688  }
689 
690  return result;
691 }
int DetermineTimeZoneOffset(struct pg_tm *tm, pg_tz *tzp)
Definition: datetime.c:1647
void j2date(int jd, int *year, int *month, int *day)
Definition: datetime.c:309
int64 TimestampTz
Definition: timestamp.h:39
#define IS_VALID_TIMESTAMP(t)
Definition: timestamp.h:234
#define USECS_PER_SEC
Definition: timestamp.h:133
#define MIN_TIMESTAMP
Definition: timestamp.h:223
static struct pg_tm tm
Definition: localtime.c:102
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 736 of file date.c.

737 {
738  Timestamp dt1;
739  int overflow;
740 
741  dt1 = date2timestamp_opt_overflow(dateVal, &overflow);
742  if (overflow > 0)
743  {
744  /* dt1 is larger than any finite timestamp, but less than infinity */
745  return TIMESTAMP_IS_NOEND(dt2) ? -1 : +1;
746  }
747  Assert(overflow == 0); /* -1 case cannot occur */
748 
749  return timestamp_cmp_internal(dt1, dt2);
750 }
int timestamp_cmp_internal(Timestamp dt1, Timestamp dt2)
Definition: timestamp.c:2111
#define TIMESTAMP_IS_NOEND(j)
Definition: timestamp.h:159
Timestamp date2timestamp_opt_overflow(DateADT dateVal, int *overflow)
Definition: date.c:557
Assert(fmt[strlen(fmt) - 1] !='\n')

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

817 {
818  TimestampTz dt1;
819  int overflow;
820 
821  dt1 = date2timestamptz_opt_overflow(dateVal, &overflow);
822  if (overflow > 0)
823  {
824  /* dt1 is larger than any finite timestamp, but less than infinity */
825  return TIMESTAMP_IS_NOEND(dt2) ? -1 : +1;
826  }
827  if (overflow < 0)
828  {
829  /* dt1 is less than any finite timestamp, but more than -infinity */
830  return TIMESTAMP_IS_NOBEGIN(dt2) ? +1 : -1;
831  }
832 
833  return timestamptz_cmp_internal(dt1, dt2);
834 }
#define TIMESTAMP_IS_NOBEGIN(j)
Definition: timestamp.h:154
TimestampTz date2timestamptz_opt_overflow(DateADT dateVal, int *overflow)
Definition: date.c:617
#define timestamptz_cmp_internal(dt1, dt2)
Definition: timestamp.h:101

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

◆ EncodeSpecialDate()

void EncodeSpecialDate ( DateADT  dt,
char *  str 
)

Definition at line 287 of file date.c.

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

References DATE_IS_NOBEGIN, DATE_IS_NOEND, EARLY, elog(), ERROR, LATE, and generate_unaccent_rules::str.

Referenced by date_out(), and JsonEncodeDateTime().

◆ float_time_overflows()

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

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  return true;
1445 
1446  /*
1447  * "sec", being double, requires extra care. Cope with NaN, and round off
1448  * before applying the range check to avoid unexpected errors due to
1449  * imprecise input. (We assume rint() behaves sanely with infinities.)
1450  */
1451  if (isnan(sec))
1452  return true;
1453  sec = rint(sec * USECS_PER_SEC);
1454  if (sec < 0 || sec > SECS_PER_MINUTE * USECS_PER_SEC)
1455  return true;
1456 
1457  /*
1458  * Because we allow, eg, hour = 24 or sec = 60, we must check separately
1459  * that the total time value doesn't exceed 24:00:00. This must match the
1460  * way that callers will convert the fields to a time.
1461  */
1462  if (((((hour * MINS_PER_HOUR + min) * SECS_PER_MINUTE)
1463  * USECS_PER_SEC) + (int64) sec) > USECS_PER_DAY)
1464  return true;
1465 
1466  return false;
1467 }
#define MINS_PER_HOUR
Definition: timestamp.h:128
#define SECS_PER_MINUTE
Definition: timestamp.h:127
#define HOURS_PER_DAY
Definition: timestamp.h:117

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

303 {
304  struct pg_tm tm;
305 
306  static int cache_year = 0;
307  static int cache_mon = 0;
308  static int cache_mday = 0;
309  static DateADT cache_date;
310 
312 
313  /*
314  * date2j involves several integer divisions; moreover, unless our session
315  * lives across local midnight, we don't really have to do it more than
316  * once. So it seems worth having a separate cache here.
317  */
318  if (tm.tm_year != cache_year ||
319  tm.tm_mon != cache_mon ||
320  tm.tm_mday != cache_mday)
321  {
323  cache_year = tm.tm_year;
324  cache_mon = tm.tm_mon;
325  cache_mday = tm.tm_mday;
326  }
327 
328  return cache_date;
329 }
int date2j(int y, int m, int d)
Definition: datetime.c:284
void GetCurrentDateTime(struct pg_tm *tm)
Definition: datetime.c:364
int32 DateADT
Definition: date.h:23

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

336 {
337  TimeTzADT *result;
338  struct pg_tm tt,
339  *tm = &tt;
340  fsec_t fsec;
341  int tz;
342 
343  GetCurrentTimeUsec(tm, &fsec, &tz);
344 
345  result = (TimeTzADT *) palloc(sizeof(TimeTzADT));
346  tm2timetz(tm, fsec, tz, result);
347  AdjustTimeForTypmod(&(result->time), typmod);
348  return result;
349 }
void GetCurrentTimeUsec(struct pg_tm *tm, fsec_t *fsec, int *tzp)
Definition: datetime.c:385
int32 fsec_t
Definition: timestamp.h:41
int tm2timetz(struct pg_tm *tm, fsec_t fsec, int tz, TimeTzADT *result)
Definition: date.c:2243
void AdjustTimeForTypmod(TimeADT *time, int32 typmod)
Definition: date.c:1633
void * palloc(Size size)
Definition: mcxt.c:1068
Definition: date.h:28
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 355 of file date.c.

356 {
357  TimeADT result;
358  struct pg_tm tt,
359  *tm = &tt;
360  fsec_t fsec;
361  int tz;
362 
363  GetCurrentTimeUsec(tm, &fsec, &tz);
364 
365  tm2time(tm, fsec, &result);
366  AdjustTimeForTypmod(&result, typmod);
367  return result;
368 }
int tm2time(struct pg_tm *tm, fsec_t fsec, TimeADT *result)
Definition: date.c:1404
int64 TimeADT
Definition: date.h:25

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

Referenced by ExecEvalSQLValueFunction().

◆ time2tm()

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

Definition at line 1476 of file date.c.

1477 {
1478  tm->tm_hour = time / USECS_PER_HOUR;
1479  time -= tm->tm_hour * USECS_PER_HOUR;
1480  tm->tm_min = time / USECS_PER_MINUTE;
1481  time -= tm->tm_min * USECS_PER_MINUTE;
1482  tm->tm_sec = time / USECS_PER_SEC;
1483  time -= tm->tm_sec * USECS_PER_SEC;
1484  *fsec = time;
1485  return 0;
1486 }
#define USECS_PER_HOUR
Definition: timestamp.h:131
#define USECS_PER_MINUTE
Definition: timestamp.h:132

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

1416 {
1417  /* Range-check the fields individually. */
1418  if (hour < 0 || hour > HOURS_PER_DAY ||
1419  min < 0 || min >= MINS_PER_HOUR ||
1420  sec < 0 || sec > SECS_PER_MINUTE ||
1421  fsec < 0 || fsec > USECS_PER_SEC)
1422  return true;
1423 
1424  /*
1425  * Because we allow, eg, hour = 24 or sec = 60, we must check separately
1426  * that the total time value doesn't exceed 24:00:00.
1427  */
1428  if ((((((hour * MINS_PER_HOUR + min) * SECS_PER_MINUTE)
1429  + sec) * USECS_PER_SEC) + fsec) > USECS_PER_DAY)
1430  return true;
1431 
1432  return false;
1433 }

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

Referenced by DecodeDateTime(), and DecodeTimeOnly().

◆ timetz2tm()

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

Definition at line 2377 of file date.c.

2378 {
2379  TimeOffset trem = time->time;
2380 
2381  tm->tm_hour = trem / USECS_PER_HOUR;
2382  trem -= tm->tm_hour * USECS_PER_HOUR;
2383  tm->tm_min = trem / USECS_PER_MINUTE;
2384  trem -= tm->tm_min * USECS_PER_MINUTE;
2385  tm->tm_sec = trem / USECS_PER_SEC;
2386  *fsec = trem - tm->tm_sec * USECS_PER_SEC;
2387 
2388  if (tzp != NULL)
2389  *tzp = time->zone;
2390 
2391  return 0;
2392 }
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().

◆ tm2time()

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

Definition at line 1404 of file date.c.

1405 {
1406  *result = ((((tm->tm_hour * MINS_PER_HOUR + tm->tm_min) * SECS_PER_MINUTE) + tm->tm_sec)
1407  * USECS_PER_SEC) + fsec;
1408  return 0;
1409 }

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

2244 {
2245  result->time = ((((tm->tm_hour * MINS_PER_HOUR + tm->tm_min) * SECS_PER_MINUTE) + tm->tm_sec) *
2246  USECS_PER_SEC) + fsec;
2247  result->zone = tz;
2248 
2249  return 0;
2250 }

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