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

Go to the source code of this file.

Macros

#define DatumGetTimestamp(X)   ((Timestamp) DatumGetInt64(X))
 
#define DatumGetTimestampTz(X)   ((TimestampTz) DatumGetInt64(X))
 
#define DatumGetIntervalP(X)   ((Interval *) DatumGetPointer(X))
 
#define TimestampGetDatum(X)   Int64GetDatum(X)
 
#define TimestampTzGetDatum(X)   Int64GetDatum(X)
 
#define IntervalPGetDatum(X)   PointerGetDatum(X)
 
#define PG_GETARG_TIMESTAMP(n)   DatumGetTimestamp(PG_GETARG_DATUM(n))
 
#define PG_GETARG_TIMESTAMPTZ(n)   DatumGetTimestampTz(PG_GETARG_DATUM(n))
 
#define PG_GETARG_INTERVAL_P(n)   DatumGetIntervalP(PG_GETARG_DATUM(n))
 
#define PG_RETURN_TIMESTAMP(x)   return TimestampGetDatum(x)
 
#define PG_RETURN_TIMESTAMPTZ(x)   return TimestampTzGetDatum(x)
 
#define PG_RETURN_INTERVAL_P(x)   return IntervalPGetDatum(x)
 
#define TIMESTAMP_MASK(b)   (1 << (b))
 
#define INTERVAL_MASK(b)   (1 << (b))
 
#define INTERVAL_FULL_RANGE   (0x7FFF)
 
#define INTERVAL_RANGE_MASK   (0x7FFF)
 
#define INTERVAL_FULL_PRECISION   (0xFFFF)
 
#define INTERVAL_PRECISION_MASK   (0xFFFF)
 
#define INTERVAL_TYPMOD(p, r)   ((((r) & INTERVAL_RANGE_MASK) << 16) | ((p) & INTERVAL_PRECISION_MASK))
 
#define INTERVAL_PRECISION(t)   ((t) & INTERVAL_PRECISION_MASK)
 
#define INTERVAL_RANGE(t)   (((t) >> 16) & INTERVAL_RANGE_MASK)
 
#define TimestampTzPlusMilliseconds(tz, ms)   ((tz) + ((ms) * (int64) 1000))
 
#define timestamptz_cmp_internal(dt1, dt2)   timestamp_cmp_internal(dt1, dt2)
 

Functions

int32 anytimestamp_typmod_check (bool istz, int32 typmod)
 
TimestampTz GetCurrentTimestamp (void)
 
TimestampTz GetSQLCurrentTimestamp (int32 typmod)
 
Timestamp GetSQLLocalTimestamp (int32 typmod)
 
void TimestampDifference (TimestampTz start_time, TimestampTz stop_time, long *secs, int *microsecs)
 
long TimestampDifferenceMilliseconds (TimestampTz start_time, TimestampTz stop_time)
 
bool TimestampDifferenceExceeds (TimestampTz start_time, TimestampTz stop_time, int msec)
 
TimestampTz time_t_to_timestamptz (pg_time_t tm)
 
pg_time_t timestamptz_to_time_t (TimestampTz t)
 
const char * timestamptz_to_str (TimestampTz t)
 
int tm2timestamp (struct pg_tm *tm, fsec_t fsec, int *tzp, Timestamp *dt)
 
int timestamp2tm (Timestamp dt, int *tzp, struct pg_tm *tm, fsec_t *fsec, const char **tzn, pg_tz *attimezone)
 
void dt2time (Timestamp dt, int *hour, int *min, int *sec, fsec_t *fsec)
 
int interval2tm (Interval span, struct pg_tm *tm, fsec_t *fsec)
 
int tm2interval (struct pg_tm *tm, fsec_t fsec, Interval *span)
 
Timestamp SetEpochTimestamp (void)
 
void GetEpochTime (struct pg_tm *tm)
 
int timestamp_cmp_internal (Timestamp dt1, Timestamp dt2)
 
TimestampTz timestamp2timestamptz_opt_overflow (Timestamp timestamp, int *overflow)
 
int32 timestamp_cmp_timestamptz_internal (Timestamp timestampVal, TimestampTz dt2)
 
int isoweek2j (int year, int week)
 
void isoweek2date (int woy, int *year, int *mon, int *mday)
 
void isoweekdate2date (int isoweek, int wday, int *year, int *mon, int *mday)
 
int date2isoweek (int year, int mon, int mday)
 
int date2isoyear (int year, int mon, int mday)
 
int date2isoyearday (int year, int mon, int mday)
 
bool TimestampTimestampTzRequiresRewrite (void)
 

Variables

TimestampTz PgStartTime
 
TimestampTz PgReloadTime
 

Macro Definition Documentation

◆ DatumGetIntervalP

◆ DatumGetTimestamp

◆ DatumGetTimestampTz

◆ INTERVAL_FULL_PRECISION

#define INTERVAL_FULL_PRECISION   (0xFFFF)

◆ INTERVAL_FULL_RANGE

◆ INTERVAL_MASK

#define INTERVAL_MASK (   b)    (1 << (b))

◆ INTERVAL_PRECISION

#define INTERVAL_PRECISION (   t)    ((t) & INTERVAL_PRECISION_MASK)

Definition at line 53 of file timestamp.h.

Referenced by AdjustIntervalForTypmod(), interval_support(), and intervaltypmodout().

◆ INTERVAL_PRECISION_MASK

#define INTERVAL_PRECISION_MASK   (0xFFFF)

Definition at line 51 of file timestamp.h.

◆ INTERVAL_RANGE

#define INTERVAL_RANGE (   t)    (((t) >> 16) & INTERVAL_RANGE_MASK)

◆ INTERVAL_RANGE_MASK

#define INTERVAL_RANGE_MASK   (0x7FFF)

Definition at line 49 of file timestamp.h.

◆ INTERVAL_TYPMOD

#define INTERVAL_TYPMOD (   p,
 
)    ((((r) & INTERVAL_RANGE_MASK) << 16) | ((p) & INTERVAL_PRECISION_MASK))

Definition at line 52 of file timestamp.h.

Referenced by intervaltypmodin().

◆ IntervalPGetDatum

◆ PG_GETARG_INTERVAL_P

◆ PG_GETARG_TIMESTAMP

#define PG_GETARG_TIMESTAMP (   n)    DatumGetTimestamp(PG_GETARG_DATUM(n))

Definition at line 35 of file timestamp.h.

Referenced by brin_minmax_multi_distance_timestamp(), date_cmp_timestamp(), date_eq_timestamp(), date_ge_timestamp(), date_gt_timestamp(), date_le_timestamp(), date_lt_timestamp(), date_ne_timestamp(), gbt_ts_consistent(), gbt_ts_distance(), generate_series_timestamp(), in_range_timestamp_interval(), timestamp_age(), timestamp_bin(), timestamp_cmp(), timestamp_cmp_date(), timestamp_cmp_timestamptz(), timestamp_date(), timestamp_eq(), timestamp_eq_date(), timestamp_eq_timestamptz(), timestamp_finite(), timestamp_ge(), timestamp_ge_date(), timestamp_ge_timestamptz(), timestamp_gt(), timestamp_gt_date(), timestamp_gt_timestamptz(), timestamp_izone(), timestamp_larger(), timestamp_le(), timestamp_le_date(), timestamp_le_timestamptz(), timestamp_lt(), timestamp_lt_date(), timestamp_lt_timestamptz(), timestamp_mi(), timestamp_mi_interval(), timestamp_ne(), timestamp_ne_date(), timestamp_ne_timestamptz(), timestamp_out(), timestamp_part_common(), timestamp_pl_interval(), timestamp_scale(), timestamp_send(), timestamp_smaller(), timestamp_time(), timestamp_timestamptz(), timestamp_to_char(), timestamp_trunc(), timestamp_zone(), timestamptz_cmp_timestamp(), timestamptz_date(), timestamptz_eq_timestamp(), timestamptz_ge_timestamp(), timestamptz_gt_timestamp(), timestamptz_le_timestamp(), timestamptz_lt_timestamp(), timestamptz_ne_timestamp(), timestamptz_time(), timestamptz_timetz(), timestamptz_to_char(), ts_dist(), tsrange_subdiff(), and tstzrange_subdiff().

◆ PG_GETARG_TIMESTAMPTZ

◆ PG_RETURN_INTERVAL_P

◆ PG_RETURN_TIMESTAMP

◆ PG_RETURN_TIMESTAMPTZ

◆ TIMESTAMP_MASK

#define TIMESTAMP_MASK (   b)    (1 << (b))

Definition at line 44 of file timestamp.h.

◆ TimestampGetDatum

◆ timestamptz_cmp_internal

#define timestamptz_cmp_internal (   dt1,
  dt2 
)    timestamp_cmp_internal(dt1, dt2)

◆ TimestampTzGetDatum

◆ TimestampTzPlusMilliseconds

Function Documentation

◆ anytimestamp_typmod_check()

int32 anytimestamp_typmod_check ( bool  istz,
int32  typmod 
)

Definition at line 104 of file timestamp.c.

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

Referenced by anytimestamp_typmodin(), and transformSQLValueFunction().

105 {
106  if (typmod < 0)
107  ereport(ERROR,
108  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
109  errmsg("TIMESTAMP(%d)%s precision must not be negative",
110  typmod, (istz ? " WITH TIME ZONE" : ""))));
111  if (typmod > MAX_TIMESTAMP_PRECISION)
112  {
114  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
115  errmsg("TIMESTAMP(%d)%s precision reduced to maximum allowed, %d",
116  typmod, (istz ? " WITH TIME ZONE" : ""),
118  typmod = MAX_TIMESTAMP_PRECISION;
119  }
120 
121  return typmod;
122 }
int errcode(int sqlerrcode)
Definition: elog.c:698
#define ERROR
Definition: elog.h:46
#define MAX_TIMESTAMP_PRECISION
Definition: timestamp.h:53
#define WARNING
Definition: elog.h:40
#define ereport(elevel,...)
Definition: elog.h:157
int errmsg(const char *fmt,...)
Definition: elog.c:909

◆ date2isoweek()

int date2isoweek ( int  year,
int  mon,
int  mday 
)

Definition at line 4422 of file timestamp.c.

References date2j(), and j2day().

Referenced by DCH_to_char(), extract_date(), timestamp_part_common(), timestamp_trunc(), timestamptz_part_common(), and timestamptz_trunc_internal().

4423 {
4424  float8 result;
4425  int day0,
4426  day4,
4427  dayn;
4428 
4429  /* current day */
4430  dayn = date2j(year, mon, mday);
4431 
4432  /* fourth day of current year */
4433  day4 = date2j(year, 1, 4);
4434 
4435  /* day0 == offset to first day of week (Monday) */
4436  day0 = j2day(day4 - 1);
4437 
4438  /*
4439  * We need the first week containing a Thursday, otherwise this day falls
4440  * into the previous year for purposes of counting weeks
4441  */
4442  if (dayn < day4 - day0)
4443  {
4444  day4 = date2j(year - 1, 1, 4);
4445 
4446  /* day0 == offset to first day of week (Monday) */
4447  day0 = j2day(day4 - 1);
4448  }
4449 
4450  result = (dayn - (day4 - day0)) / 7 + 1;
4451 
4452  /*
4453  * Sometimes the last few days in a year will fall into the first week of
4454  * the next year, so check for this.
4455  */
4456  if (result >= 52)
4457  {
4458  day4 = date2j(year + 1, 1, 4);
4459 
4460  /* day0 == offset to first day of week (Monday) */
4461  day0 = j2day(day4 - 1);
4462 
4463  if (dayn >= day4 - day0)
4464  result = (dayn - (day4 - day0)) / 7 + 1;
4465  }
4466 
4467  return (int) result;
4468 }
double float8
Definition: c.h:565
int j2day(int date)
Definition: datetime.c:327
int date2j(int y, int m, int d)
Definition: datetime.c:269

◆ date2isoyear()

int date2isoyear ( int  year,
int  mon,
int  mday 
)

Definition at line 4477 of file timestamp.c.

References date2j(), and j2day().

Referenced by date2isoyearday(), DCH_to_char(), extract_date(), timestamp_part_common(), and timestamptz_part_common().

4478 {
4479  float8 result;
4480  int day0,
4481  day4,
4482  dayn;
4483 
4484  /* current day */
4485  dayn = date2j(year, mon, mday);
4486 
4487  /* fourth day of current year */
4488  day4 = date2j(year, 1, 4);
4489 
4490  /* day0 == offset to first day of week (Monday) */
4491  day0 = j2day(day4 - 1);
4492 
4493  /*
4494  * We need the first week containing a Thursday, otherwise this day falls
4495  * into the previous year for purposes of counting weeks
4496  */
4497  if (dayn < day4 - day0)
4498  {
4499  day4 = date2j(year - 1, 1, 4);
4500 
4501  /* day0 == offset to first day of week (Monday) */
4502  day0 = j2day(day4 - 1);
4503 
4504  year--;
4505  }
4506 
4507  result = (dayn - (day4 - day0)) / 7 + 1;
4508 
4509  /*
4510  * Sometimes the last few days in a year will fall into the first week of
4511  * the next year, so check for this.
4512  */
4513  if (result >= 52)
4514  {
4515  day4 = date2j(year + 1, 1, 4);
4516 
4517  /* day0 == offset to first day of week (Monday) */
4518  day0 = j2day(day4 - 1);
4519 
4520  if (dayn >= day4 - day0)
4521  year++;
4522  }
4523 
4524  return year;
4525 }
double float8
Definition: c.h:565
int j2day(int date)
Definition: datetime.c:327
int date2j(int y, int m, int d)
Definition: datetime.c:269

◆ date2isoyearday()

int date2isoyearday ( int  year,
int  mon,
int  mday 
)

Definition at line 4534 of file timestamp.c.

References date2isoyear(), date2j(), and isoweek2j().

Referenced by DCH_to_char().

4535 {
4536  return date2j(year, mon, mday) - isoweek2j(date2isoyear(year, mon, mday), 1) + 1;
4537 }
int date2isoyear(int year, int mon, int mday)
Definition: timestamp.c:4477
int isoweek2j(int year, int week)
Definition: timestamp.c:4371
int date2j(int y, int m, int d)
Definition: datetime.c:269

◆ dt2time()

void dt2time ( Timestamp  dt,
int *  hour,
int *  min,
int *  sec,
fsec_t fsec 
)

Definition at line 1795 of file timestamp.c.

References USECS_PER_HOUR, USECS_PER_MINUTE, and USECS_PER_SEC.

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

1796 {
1797  TimeOffset time;
1798 
1799  time = jd;
1800 
1801  *hour = time / USECS_PER_HOUR;
1802  time -= (*hour) * USECS_PER_HOUR;
1803  *min = time / USECS_PER_MINUTE;
1804  time -= (*min) * USECS_PER_MINUTE;
1805  *sec = time / USECS_PER_SEC;
1806  *fsec = time - (*sec * USECS_PER_SEC);
1807 } /* dt2time() */
#define USECS_PER_SEC
Definition: timestamp.h:94
#define USECS_PER_MINUTE
Definition: timestamp.h:93
#define USECS_PER_HOUR
Definition: timestamp.h:92
int64 TimeOffset
Definition: timestamp.h:40

◆ GetCurrentTimestamp()

TimestampTz GetCurrentTimestamp ( void  )

Definition at line 1580 of file timestamp.c.

References gettimeofday(), POSTGRES_EPOCH_JDATE, SECS_PER_DAY, UNIX_EPOCH_JDATE, and USECS_PER_SEC.

Referenced by ApplyLauncherMain(), ApplyWorkerMain(), asyncQueueFillWarning(), autoprewarm_main(), autovac_refresh_stats(), AutoVacLauncherMain(), backend_read_statsfile(), BackgroundWriterMain(), check_log_duration(), CheckPointGuts(), CleanupBackgroundWorker(), clock_timestamp(), CreateCheckPoint(), CreateEndOfRecoveryRecord(), CreateRestartPoint(), DetermineSleepTime(), disable_timeout(), disable_timeouts(), do_analyze_rel(), do_start_bgworker(), do_start_worker(), drandom(), enable_timeout_after(), enable_timeout_at(), enable_timeouts(), entry_reset(), get_role_password(), GetCurrentTransactionStopTimestamp(), GetReplicationApplyDelay(), GetSnapshotCurrentTimestamp(), handle_sig_alarm(), heap_vacuum_rel(), InitProcessGlobals(), launcher_determine_sleep(), LockBufferForCleanup(), log_disconnections(), LogCheckpointEnd(), logicalrep_worker_launch(), LogicalRepApplyLoop(), maybe_start_bgworkers(), perform_base_backup(), pgfdw_cancel_query(), pgfdw_exec_cleanup_query(), pgfdw_get_cleanup_result(), pgss_shmem_startup(), pgstat_read_statsfiles(), pgstat_recv_inquiry(), pgstat_recv_resetreplslotcounter(), pgstat_recv_resetsharedcounter(), pgstat_recv_resetsinglecounter(), pgstat_recv_resetslrucounter(), pgstat_report_activity(), pgstat_report_analyze(), pgstat_report_autovac(), pgstat_report_checksum_failures_in_db(), pgstat_report_vacuum(), pgstat_send_archiver(), pgstat_send_connstats(), pgstat_send_wal(), pgstat_write_statsfiles(), PostgresMain(), PostmasterMain(), PrepareTransaction(), process_syncing_tables_for_apply(), ProcessRepliesIfAny(), ProcessStandbyReplyMessage(), ProcessWalSndrMessage(), ProcSleep(), rebuild_database_list(), RecordTransactionAbort(), RecordTransactionAbortPrepared(), RecordTransactionCommitPrepared(), recoveryApplyDelay(), reschedule_timeouts(), reset_dbentry_counters(), ResolveRecoveryConflictWithBufferPin(), ResolveRecoveryConflictWithLock(), ResolveRecoveryConflictWithVirtualXIDs(), send_feedback(), SetCurrentStatementStartTimestamp(), SetCurrentTransactionStopTimestamp(), StartTransaction(), StartupXLOG(), test_pattern(), throttle(), UpdateWorkerStats(), WaitExceedsMaxStandbyDelay(), WaitForWALToBecomeAvailable(), WalReceiverMain(), WalSndKeepalive(), WalSndLoop(), WalSndUpdateProgress(), WalSndWaitForWal(), WalSndWriteData(), XLogBackgroundFlush(), XLogFileRead(), XLogPrefetchResetStats(), XLogPrefetchShmemInit(), XLogRestorePoint(), XLogSendPhysical(), XLogWalRcvSendHSFeedback(), and XLogWalRcvSendReply().

1581 {
1582  TimestampTz result;
1583  struct timeval tp;
1584 
1585  gettimeofday(&tp, NULL);
1586 
1587  result = (TimestampTz) tp.tv_sec -
1589  result = (result * USECS_PER_SEC) + tp.tv_usec;
1590 
1591  return result;
1592 }
int gettimeofday(struct timeval *tp, struct timezone *tzp)
Definition: gettimeofday.c:104
#define USECS_PER_SEC
Definition: timestamp.h:94
int64 TimestampTz
Definition: timestamp.h:39
#define SECS_PER_DAY
Definition: timestamp.h:86
#define UNIX_EPOCH_JDATE
Definition: timestamp.h:162
#define POSTGRES_EPOCH_JDATE
Definition: timestamp.h:163

◆ GetEpochTime()

void GetEpochTime ( struct pg_tm tm)

Definition at line 2048 of file timestamp.c.

References elog, epoch, ERROR, pg_gmtime(), 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 date_in(), PGTYPESdate_from_asc(), and SetEpochTimestamp().

2049 {
2050  struct pg_tm *t0;
2051  pg_time_t epoch = 0;
2052 
2053  t0 = pg_gmtime(&epoch);
2054 
2055  if (t0 == NULL)
2056  elog(ERROR, "could not convert epoch to timestamp: %m");
2057 
2058  tm->tm_year = t0->tm_year;
2059  tm->tm_mon = t0->tm_mon;
2060  tm->tm_mday = t0->tm_mday;
2061  tm->tm_hour = t0->tm_hour;
2062  tm->tm_min = t0->tm_min;
2063  tm->tm_sec = t0->tm_sec;
2064 
2065  tm->tm_year += 1900;
2066  tm->tm_mon++;
2067 }
int64 pg_time_t
Definition: pgtime.h:23
struct pg_tm * pg_gmtime(const pg_time_t *timep)
Definition: localtime.c:1387
int tm_hour
Definition: pgtime.h:29
Definition: pgtime.h:25
#define ERROR
Definition: elog.h:46
int tm_mday
Definition: pgtime.h:30
int tm_mon
Definition: pgtime.h:31
int tm_year
Definition: pgtime.h:32
#define elog(elevel,...)
Definition: elog.h:232
static const unsigned __int64 epoch
Definition: gettimeofday.c:34
int tm_sec
Definition: pgtime.h:27
int tm_min
Definition: pgtime.h:28

◆ GetSQLCurrentTimestamp()

TimestampTz GetSQLCurrentTimestamp ( int32  typmod)

Definition at line 1598 of file timestamp.c.

References AdjustTimestampForTypmod(), and GetCurrentTransactionStartTimestamp().

Referenced by ExecEvalSQLValueFunction().

1599 {
1600  TimestampTz ts;
1601 
1603  if (typmod >= 0)
1604  AdjustTimestampForTypmod(&ts, typmod);
1605  return ts;
1606 }
int64 TimestampTz
Definition: timestamp.h:39
void AdjustTimestampForTypmod(Timestamp *time, int32 typmod)
Definition: timestamp.c:394
TimestampTz GetCurrentTransactionStartTimestamp(void)
Definition: xact.c:798

◆ GetSQLLocalTimestamp()

Timestamp GetSQLLocalTimestamp ( int32  typmod)

Definition at line 1612 of file timestamp.c.

References AdjustTimestampForTypmod(), GetCurrentTransactionStartTimestamp(), and timestamptz2timestamp().

Referenced by ExecEvalSQLValueFunction().

1613 {
1614  Timestamp ts;
1615 
1617  if (typmod >= 0)
1618  AdjustTimestampForTypmod(&ts, typmod);
1619  return ts;
1620 }
void AdjustTimestampForTypmod(Timestamp *time, int32 typmod)
Definition: timestamp.c:394
static Timestamp timestamptz2timestamp(TimestampTz timestamp)
Definition: timestamp.c:5580
int64 Timestamp
Definition: timestamp.h:38
TimestampTz GetCurrentTransactionStartTimestamp(void)
Definition: xact.c:798

◆ interval2tm()

int interval2tm ( Interval  span,
struct pg_tm tm,
fsec_t fsec 
)

Definition at line 1966 of file timestamp.c.

References Interval::day, ereport, errcode(), errmsg(), ERROR, Interval::month, MONTHS_PER_YEAR, SAMESIGN, Interval::time, 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_HOUR, USECS_PER_MINUTE, and USECS_PER_SEC.

Referenced by interval_out(), interval_part_common(), interval_to_char(), and interval_trunc().

1967 {
1968  TimeOffset time;
1969  TimeOffset tfrac;
1970 
1971  tm->tm_year = span.month / MONTHS_PER_YEAR;
1972  tm->tm_mon = span.month % MONTHS_PER_YEAR;
1973  tm->tm_mday = span.day;
1974  time = span.time;
1975 
1976  tfrac = time / USECS_PER_HOUR;
1977  time -= tfrac * USECS_PER_HOUR;
1978  tm->tm_hour = tfrac;
1979  if (!SAMESIGN(tm->tm_hour, tfrac))
1980  ereport(ERROR,
1981  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1982  errmsg("interval out of range")));
1983  tfrac = time / USECS_PER_MINUTE;
1984  time -= tfrac * USECS_PER_MINUTE;
1985  tm->tm_min = tfrac;
1986  tfrac = time / USECS_PER_SEC;
1987  *fsec = time - (tfrac * USECS_PER_SEC);
1988  tm->tm_sec = tfrac;
1989 
1990  return 0;
1991 }
#define USECS_PER_SEC
Definition: timestamp.h:94
int tm_hour
Definition: pgtime.h:29
#define USECS_PER_MINUTE
Definition: timestamp.h:93
int errcode(int sqlerrcode)
Definition: elog.c:698
int32 day
Definition: timestamp.h:47
#define MONTHS_PER_YEAR
Definition: timestamp.h:69
#define ERROR
Definition: elog.h:46
int tm_mday
Definition: pgtime.h:30
int tm_mon
Definition: pgtime.h:31
#define USECS_PER_HOUR
Definition: timestamp.h:92
TimeOffset time
Definition: timestamp.h:45
#define SAMESIGN(a, b)
Definition: timestamp.c:49
int32 month
Definition: timestamp.h:48
int64 TimeOffset
Definition: timestamp.h:40
#define ereport(elevel,...)
Definition: elog.h:157
int tm_year
Definition: pgtime.h:32
int errmsg(const char *fmt,...)
Definition: elog.c:909
int tm_sec
Definition: pgtime.h:27
int tm_min
Definition: pgtime.h:28

◆ isoweek2date()

void isoweek2date ( int  woy,
int *  year,
int *  mon,
int *  mday 
)

Definition at line 4391 of file timestamp.c.

References isoweek2j(), and j2date().

Referenced by do_to_timestamp(), timestamp_trunc(), and timestamptz_trunc_internal().

4392 {
4393  j2date(isoweek2j(*year, woy), year, mon, mday);
4394 }
void j2date(int jd, int *year, int *month, int *day)
Definition: datetime.c:294
int isoweek2j(int year, int week)
Definition: timestamp.c:4371

◆ isoweek2j()

int isoweek2j ( int  year,
int  week 
)

Definition at line 4371 of file timestamp.c.

References date2j(), and j2day().

Referenced by date2isoyearday(), do_to_timestamp(), isoweek2date(), and isoweekdate2date().

4372 {
4373  int day0,
4374  day4;
4375 
4376  /* fourth day of current year */
4377  day4 = date2j(year, 1, 4);
4378 
4379  /* day0 == offset to first day of week (Monday) */
4380  day0 = j2day(day4 - 1);
4381 
4382  return ((week - 1) * 7) + (day4 - day0);
4383 }
int j2day(int date)
Definition: datetime.c:327
int date2j(int y, int m, int d)
Definition: datetime.c:269

◆ isoweekdate2date()

void isoweekdate2date ( int  isoweek,
int  wday,
int *  year,
int *  mon,
int *  mday 
)

Definition at line 4404 of file timestamp.c.

References isoweek2j(), and j2date().

Referenced by do_to_timestamp().

4405 {
4406  int jday;
4407 
4408  jday = isoweek2j(*year, isoweek);
4409  /* convert Gregorian week start (Sunday=1) to ISO week start (Monday=1) */
4410  if (wday > 1)
4411  jday += wday - 2;
4412  else
4413  jday += 6;
4414  j2date(jday, year, mon, mday);
4415 }
void j2date(int jd, int *year, int *month, int *day)
Definition: datetime.c:294
int isoweek2j(int year, int week)
Definition: timestamp.c:4371

◆ SetEpochTimestamp()

Timestamp SetEpochTimestamp ( void  )

Definition at line 2070 of file timestamp.c.

References GetEpochTime(), tm, and tm2timestamp().

Referenced by dttofmtasc_replace(), PGTYPEStimestamp_from_asc(), timestamp_in(), timestamp_part_common(), timestamptz_in(), and timestamptz_part_common().

2071 {
2072  Timestamp dt;
2073  struct pg_tm tt,
2074  *tm = &tt;
2075 
2076  GetEpochTime(tm);
2077  /* we don't bother to test for failure ... */
2078  tm2timestamp(tm, 0, NULL, &dt);
2079 
2080  return dt;
2081 } /* SetEpochTimestamp() */
Definition: pgtime.h:25
static struct pg_tm tm
Definition: localtime.c:102
int64 Timestamp
Definition: timestamp.h:38
int tm2timestamp(struct pg_tm *tm, fsec_t fsec, int *tzp, Timestamp *result)
Definition: timestamp.c:1918
void GetEpochTime(struct pg_tm *tm)
Definition: timestamp.c:2048

◆ time_t_to_timestamptz()

TimestampTz time_t_to_timestamptz ( pg_time_t  tm)

Definition at line 1732 of file timestamp.c.

References POSTGRES_EPOCH_JDATE, SECS_PER_DAY, UNIX_EPOCH_JDATE, and USECS_PER_SEC.

Referenced by pg_control_checkpoint(), pg_control_system(), pg_ls_dir_files(), and pg_stat_file().

1733 {
1734  TimestampTz result;
1735 
1736  result = (TimestampTz) tm -
1738  result *= USECS_PER_SEC;
1739 
1740  return result;
1741 }
#define USECS_PER_SEC
Definition: timestamp.h:94
int64 TimestampTz
Definition: timestamp.h:39
static struct pg_tm tm
Definition: localtime.c:102
#define SECS_PER_DAY
Definition: timestamp.h:86
#define UNIX_EPOCH_JDATE
Definition: timestamp.h:162
#define POSTGRES_EPOCH_JDATE
Definition: timestamp.h:163

◆ timestamp2timestamptz_opt_overflow()

TimestampTz timestamp2timestamptz_opt_overflow ( Timestamp  timestamp,
int *  overflow 
)

Definition at line 5511 of file timestamp.c.

References DetermineTimeZoneOffset(), dt2local(), ereport, errcode(), errmsg(), ERROR, IS_VALID_TIMESTAMP, MIN_TIMESTAMP, session_timezone, timestamp2tm(), TIMESTAMP_NOBEGIN, TIMESTAMP_NOEND, TIMESTAMP_NOT_FINITE, and tm.

Referenced by timestamp2timestamptz(), and timestamp_cmp_timestamptz_internal().

5512 {
5513  TimestampTz result;
5514  struct pg_tm tt,
5515  *tm = &tt;
5516  fsec_t fsec;
5517  int tz;
5518 
5519  if (overflow)
5520  *overflow = 0;
5521 
5523  return timestamp;
5524 
5525  /* We don't expect this to fail, but check it pro forma */
5526  if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) == 0)
5527  {
5529 
5530  result = dt2local(timestamp, -tz);
5531 
5532  if (IS_VALID_TIMESTAMP(result))
5533  {
5534  return result;
5535  }
5536  else if (overflow)
5537  {
5538  if (result < MIN_TIMESTAMP)
5539  {
5540  *overflow = -1;
5541  TIMESTAMP_NOBEGIN(result);
5542  }
5543  else
5544  {
5545  *overflow = 1;
5546  TIMESTAMP_NOEND(result);
5547  }
5548  return result;
5549  }
5550  }
5551 
5552  ereport(ERROR,
5553  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
5554  errmsg("timestamp out of range")));
5555 
5556  return 0;
5557 }
#define TIMESTAMP_NOEND(j)
Definition: timestamp.h:117
PGDLLIMPORT pg_tz * session_timezone
Definition: pgtz.c:28
int64 timestamp
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:25
static struct pg_tm tm
Definition: localtime.c:102
#define TIMESTAMP_NOT_FINITE(j)
Definition: timestamp.h:122
#define ERROR
Definition: elog.h:46
int DetermineTimeZoneOffset(struct pg_tm *tm, pg_tz *tzp)
Definition: datetime.c:1478
#define TIMESTAMP_NOBEGIN(j)
Definition: timestamp.h:112
int32 fsec_t
Definition: timestamp.h:41
#define MIN_TIMESTAMP
Definition: timestamp.h:184
#define ereport(elevel,...)
Definition: elog.h:157
#define IS_VALID_TIMESTAMP(t)
Definition: timestamp.h:195
int errmsg(const char *fmt,...)
Definition: elog.c:909
static Timestamp dt2local(Timestamp dt, int timezone)
Definition: timestamp.c:2016

◆ timestamp2tm()

int timestamp2tm ( Timestamp  dt,
int *  tzp,
struct pg_tm tm,
fsec_t fsec,
const char **  tzn,
pg_tz attimezone 
)

Definition at line 1822 of file timestamp.c.

References dt2time(), j2date(), pg_localtime(), POSTGRES_EPOCH_JDATE, SECS_PER_DAY, session_timezone, pg_tm::tm_gmtoff, 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, pg_tm::tm_zone, TMODULO, UNIX_EPOCH_JDATE, USECS_PER_DAY, and USECS_PER_SEC.

Referenced by DetermineTimeZoneAbbrevOffsetTS(), GetCurrentTimeUsec(), JsonEncodeDateTime(), make_timestamptz_at_timezone(), map_sql_value_to_xml_value(), pg_timezone_names(), PGTYPEStimestamp_add_interval(), PGTYPEStimestamp_fmt_asc(), PGTYPEStimestamp_to_asc(), timestamp2timestamptz_opt_overflow(), timestamp_age(), timestamp_date(), timestamp_out(), timestamp_part_common(), timestamp_pl_interval(), timestamp_recv(), timestamp_time(), timestamp_to_char(), timestamp_trunc(), timestamp_zone(), timestamptz2timestamp(), timestamptz_age(), timestamptz_date(), timestamptz_out(), timestamptz_part_common(), timestamptz_pl_interval(), timestamptz_recv(), timestamptz_time(), timestamptz_timetz(), timestamptz_to_char(), timestamptz_to_str(), timestamptz_trunc_internal(), and timestamptz_zone().

1823 {
1824  Timestamp date;
1825  Timestamp time;
1826  pg_time_t utime;
1827 
1828  /* Use session timezone if caller asks for default */
1829  if (attimezone == NULL)
1830  attimezone = session_timezone;
1831 
1832  time = dt;
1833  TMODULO(time, date, USECS_PER_DAY);
1834 
1835  if (time < INT64CONST(0))
1836  {
1837  time += USECS_PER_DAY;
1838  date -= 1;
1839  }
1840 
1841  /* add offset to go from J2000 back to standard Julian date */
1842  date += POSTGRES_EPOCH_JDATE;
1843 
1844  /* Julian day routine does not work for negative Julian days */
1845  if (date < 0 || date > (Timestamp) INT_MAX)
1846  return -1;
1847 
1848  j2date((int) date, &tm->tm_year, &tm->tm_mon, &tm->tm_mday);
1849  dt2time(time, &tm->tm_hour, &tm->tm_min, &tm->tm_sec, fsec);
1850 
1851  /* Done if no TZ conversion wanted */
1852  if (tzp == NULL)
1853  {
1854  tm->tm_isdst = -1;
1855  tm->tm_gmtoff = 0;
1856  tm->tm_zone = NULL;
1857  if (tzn != NULL)
1858  *tzn = NULL;
1859  return 0;
1860  }
1861 
1862  /*
1863  * If the time falls within the range of pg_time_t, use pg_localtime() to
1864  * rotate to the local time zone.
1865  *
1866  * First, convert to an integral timestamp, avoiding possibly
1867  * platform-specific roundoff-in-wrong-direction errors, and adjust to
1868  * Unix epoch. Then see if we can convert to pg_time_t without loss. This
1869  * coding avoids hardwiring any assumptions about the width of pg_time_t,
1870  * so it should behave sanely on machines without int64.
1871  */
1872  dt = (dt - *fsec) / USECS_PER_SEC +
1874  utime = (pg_time_t) dt;
1875  if ((Timestamp) utime == dt)
1876  {
1877  struct pg_tm *tx = pg_localtime(&utime, attimezone);
1878 
1879  tm->tm_year = tx->tm_year + 1900;
1880  tm->tm_mon = tx->tm_mon + 1;
1881  tm->tm_mday = tx->tm_mday;
1882  tm->tm_hour = tx->tm_hour;
1883  tm->tm_min = tx->tm_min;
1884  tm->tm_sec = tx->tm_sec;
1885  tm->tm_isdst = tx->tm_isdst;
1886  tm->tm_gmtoff = tx->tm_gmtoff;
1887  tm->tm_zone = tx->tm_zone;
1888  *tzp = -tm->tm_gmtoff;
1889  if (tzn != NULL)
1890  *tzn = tm->tm_zone;
1891  }
1892  else
1893  {
1894  /*
1895  * When out of range of pg_time_t, treat as GMT
1896  */
1897  *tzp = 0;
1898  /* Mark this as *no* time zone available */
1899  tm->tm_isdst = -1;
1900  tm->tm_gmtoff = 0;
1901  tm->tm_zone = NULL;
1902  if (tzn != NULL)
1903  *tzn = NULL;
1904  }
1905 
1906  return 0;
1907 }
int64 pg_time_t
Definition: pgtime.h:23
PGDLLIMPORT pg_tz * session_timezone
Definition: pgtz.c:28
int tm_isdst
Definition: pgtime.h:35
#define USECS_PER_SEC
Definition: timestamp.h:94
int tm_hour
Definition: pgtime.h:29
long date
Definition: pgtypes_date.h:9
long int tm_gmtoff
Definition: pgtime.h:36
Definition: pgtime.h:25
#define SECS_PER_DAY
Definition: timestamp.h:86
int tm_mday
Definition: pgtime.h:30
int tm_mon
Definition: pgtime.h:31
#define USECS_PER_DAY
Definition: timestamp.h:91
const char * tm_zone
Definition: pgtime.h:37
void j2date(int jd, int *year, int *month, int *day)
Definition: datetime.c:294
int64 Timestamp
Definition: timestamp.h:38
void dt2time(Timestamp jd, int *hour, int *min, int *sec, fsec_t *fsec)
Definition: timestamp.c:1795
struct pg_tm * pg_localtime(const pg_time_t *timep, const pg_tz *tz)
Definition: localtime.c:1342
int tm_year
Definition: pgtime.h:32
#define UNIX_EPOCH_JDATE
Definition: timestamp.h:162
#define POSTGRES_EPOCH_JDATE
Definition: timestamp.h:163
int tm_sec
Definition: pgtime.h:27
int tm_min
Definition: pgtime.h:28
#define TMODULO(t, q, u)
Definition: datetime.h:249

◆ timestamp_cmp_internal()

int timestamp_cmp_internal ( Timestamp  dt1,
Timestamp  dt2 
)

◆ timestamp_cmp_timestamptz_internal()

int32 timestamp_cmp_timestamptz_internal ( Timestamp  timestampVal,
TimestampTz  dt2 
)

Definition at line 2194 of file timestamp.c.

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

Referenced by cmpTimestampToTimestampTz(), timestamp_cmp_timestamptz(), timestamp_eq_timestamptz(), timestamp_ge_timestamptz(), timestamp_gt_timestamptz(), timestamp_le_timestamptz(), timestamp_lt_timestamptz(), timestamp_ne_timestamptz(), timestamptz_cmp_timestamp(), timestamptz_eq_timestamp(), timestamptz_ge_timestamp(), timestamptz_gt_timestamp(), timestamptz_le_timestamp(), timestamptz_lt_timestamp(), and timestamptz_ne_timestamp().

2195 {
2196  TimestampTz dt1;
2197  int overflow;
2198 
2199  dt1 = timestamp2timestamptz_opt_overflow(timestampVal, &overflow);
2200  if (overflow > 0)
2201  {
2202  /* dt1 is larger than any finite timestamp, but less than infinity */
2203  return TIMESTAMP_IS_NOEND(dt2) ? -1 : +1;
2204  }
2205  if (overflow < 0)
2206  {
2207  /* dt1 is less than any finite timestamp, but more than -infinity */
2208  return TIMESTAMP_IS_NOBEGIN(dt2) ? +1 : -1;
2209  }
2210 
2211  return timestamptz_cmp_internal(dt1, dt2);
2212 }
int64 TimestampTz
Definition: timestamp.h:39
TimestampTz timestamp2timestamptz_opt_overflow(Timestamp timestamp, int *overflow)
Definition: timestamp.c:5511
#define timestamptz_cmp_internal(dt1, dt2)
Definition: timestamp.h:100
#define TIMESTAMP_IS_NOEND(j)
Definition: timestamp.h:120
#define TIMESTAMP_IS_NOBEGIN(j)
Definition: timestamp.h:115

◆ TimestampDifference()

void TimestampDifference ( TimestampTz  start_time,
TimestampTz  stop_time,
long *  secs,
int *  microsecs 
)

Definition at line 1656 of file timestamp.c.

References start_time, and USECS_PER_SEC.

Referenced by check_log_duration(), DetermineSleepTime(), heap_vacuum_rel(), launcher_determine_sleep(), log_disconnections(), LogRecoveryConflict(), pgstat_report_activity(), pgstat_send_connstats(), ProcSleep(), and schedule_alarm().

1658 {
1659  TimestampTz diff = stop_time - start_time;
1660 
1661  if (diff <= 0)
1662  {
1663  *secs = 0;
1664  *microsecs = 0;
1665  }
1666  else
1667  {
1668  *secs = (long) (diff / USECS_PER_SEC);
1669  *microsecs = (int) (diff % USECS_PER_SEC);
1670  }
1671 }
#define USECS_PER_SEC
Definition: timestamp.h:94
int64 TimestampTz
Definition: timestamp.h:39
static time_t start_time
Definition: pg_ctl.c:99

◆ TimestampDifferenceExceeds()

◆ TimestampDifferenceMilliseconds()

long TimestampDifferenceMilliseconds ( TimestampTz  start_time,
TimestampTz  stop_time 
)

Definition at line 1693 of file timestamp.c.

References start_time.

Referenced by autoprewarm_main(), do_analyze_rel(), GetReplicationApplyDelay(), GetReplicationTransferLatency(), LogCheckpointEnd(), pgfdw_get_cleanup_result(), recoveryApplyDelay(), WaitForWALToBecomeAvailable(), and WalSndComputeSleeptime().

1694 {
1695  TimestampTz diff = stop_time - start_time;
1696 
1697  if (diff <= 0)
1698  return 0;
1699  else
1700  return (long) ((diff + 999) / 1000);
1701 }
int64 TimestampTz
Definition: timestamp.h:39
static time_t start_time
Definition: pg_ctl.c:99

◆ TimestampTimestampTzRequiresRewrite()

bool TimestampTimestampTzRequiresRewrite ( void  )

Definition at line 5480 of file timestamp.c.

References pg_get_timezone_offset(), and session_timezone.

Referenced by ATColumnChangeRequiresRewrite().

5481 {
5482  long offset;
5483 
5484  if (pg_get_timezone_offset(session_timezone, &offset) && offset == 0)
5485  return false;
5486  return true;
5487 }
PGDLLIMPORT pg_tz * session_timezone
Definition: pgtz.c:28
bool pg_get_timezone_offset(const pg_tz *tz, long int *gmtoff)
Definition: localtime.c:1849

◆ timestamptz_to_str()

const char* timestamptz_to_str ( TimestampTz  t)

Definition at line 1774 of file timestamp.c.

References buf, EncodeDateTime(), EncodeSpecialTimestamp(), MAXDATELEN, snprintf, strlcpy(), timestamp2tm(), TIMESTAMP_NOT_FINITE, timestamptz_to_time_t(), tm, USE_ISO_DATES, and USECS_PER_SEC.

Referenced by backend_read_statsfile(), CreateRestartPoint(), pg_decode_commit_prepared_txn(), pg_decode_commit_txn(), pg_decode_prepare_txn(), pg_decode_rollback_prepared_txn(), pg_decode_stream_commit(), pg_decode_stream_prepare(), pgstat_recv_inquiry(), ProcessStandbyHSFeedbackMessage(), ProcessStandbyReplyMessage(), ProcessWalSndrMessage(), recoveryStopsAfter(), recoveryStopsBefore(), StartupXLOG(), xact_desc_abort(), xact_desc_commit(), xact_desc_prepare(), and xlog_desc().

1775 {
1776  static char buf[MAXDATELEN + 1];
1777  int tz;
1778  struct pg_tm tt,
1779  *tm = &tt;
1780  fsec_t fsec;
1781  const char *tzn;
1782 
1783  if (TIMESTAMP_NOT_FINITE(t))
1784  EncodeSpecialTimestamp(t, buf);
1785  else if (timestamp2tm(t, &tz, tm, &fsec, &tzn, NULL) == 0)
1786  EncodeDateTime(tm, fsec, true, tz, tzn, USE_ISO_DATES, buf);
1787  else
1788  strlcpy(buf, "(timestamp out of range)", sizeof(buf));
1789 
1790  return buf;
1791 }
#define MAXDATELEN
Definition: datetime.h:201
int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm *tm, fsec_t *fsec, const char **tzn, pg_tz *attimezone)
Definition: timestamp.c:1822
void EncodeSpecialTimestamp(Timestamp dt, char *str)
Definition: timestamp.c:1533
Definition: pgtime.h:25
#define USE_ISO_DATES
Definition: miscadmin.h:217
static struct pg_tm tm
Definition: localtime.c:102
#define TIMESTAMP_NOT_FINITE(j)
Definition: timestamp.h:122
static char * buf
Definition: pg_test_fsync.c:68
void EncodeDateTime(struct pg_tm *tm, fsec_t fsec, bool print_tz, int tz, const char *tzn, int style, char *str)
Definition: datetime.c:4002
int32 fsec_t
Definition: timestamp.h:41
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45

◆ timestamptz_to_time_t()

pg_time_t timestamptz_to_time_t ( TimestampTz  t)

Definition at line 1754 of file timestamp.c.

References POSTGRES_EPOCH_JDATE, SECS_PER_DAY, UNIX_EPOCH_JDATE, and USECS_PER_SEC.

Referenced by DetermineTimeZoneAbbrevOffsetTS(), InitProcessGlobals(), and timestamptz_to_str().

1755 {
1756  pg_time_t result;
1757 
1758  result = (pg_time_t) (t / USECS_PER_SEC +
1760 
1761  return result;
1762 }
int64 pg_time_t
Definition: pgtime.h:23
#define USECS_PER_SEC
Definition: timestamp.h:94
#define SECS_PER_DAY
Definition: timestamp.h:86
#define UNIX_EPOCH_JDATE
Definition: timestamp.h:162
#define POSTGRES_EPOCH_JDATE
Definition: timestamp.h:163

◆ tm2interval()

int tm2interval ( struct pg_tm tm,
fsec_t  fsec,
Interval span 
)

Definition at line 1994 of file timestamp.c.

References Interval::day, Interval::month, MONTHS_PER_YEAR, Interval::time, 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 USECS_PER_SEC.

Referenced by interval_in(), interval_trunc(), pg_timezone_abbrevs(), pg_timezone_names(), timestamp_age(), and timestamptz_age().

1995 {
1996  double total_months = (double) tm->tm_year * MONTHS_PER_YEAR + tm->tm_mon;
1997 
1998  if (total_months > INT_MAX || total_months < INT_MIN)
1999  return -1;
2000  span->month = total_months;
2001  span->day = tm->tm_mday;
2002  span->time = (((((tm->tm_hour * INT64CONST(60)) +
2003  tm->tm_min) * INT64CONST(60)) +
2004  tm->tm_sec) * USECS_PER_SEC) + fsec;
2005 
2006  return 0;
2007 }
#define USECS_PER_SEC
Definition: timestamp.h:94
int tm_hour
Definition: pgtime.h:29
int32 day
Definition: timestamp.h:47
#define MONTHS_PER_YEAR
Definition: timestamp.h:69
int tm_mday
Definition: pgtime.h:30
int tm_mon
Definition: pgtime.h:31
TimeOffset time
Definition: timestamp.h:45
int32 month
Definition: timestamp.h:48
int tm_year
Definition: pgtime.h:32
int tm_sec
Definition: pgtime.h:27
int tm_min
Definition: pgtime.h:28

◆ tm2timestamp()

int tm2timestamp ( struct pg_tm tm,
fsec_t  fsec,
int *  tzp,
Timestamp dt 
)

Definition at line 1918 of file timestamp.c.

References date2j(), dt2local(), IS_VALID_JULIAN, IS_VALID_TIMESTAMP, POSTGRES_EPOCH_JDATE, time2t(), 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 USECS_PER_DAY.

Referenced by check_recovery_target_time(), parse_datetime(), PGTYPEStimestamp_add_interval(), PGTYPEStimestamp_current(), PGTYPEStimestamp_defmt_scan(), PGTYPEStimestamp_from_asc(), SetEpochTimestamp(), timestamp_in(), timestamp_pl_interval(), timestamp_trunc(), timestamp_zone(), timestamptz2timestamp(), timestamptz_in(), timestamptz_pl_interval(), timestamptz_trunc_internal(), timestamptz_zone(), and to_timestamp().

1919 {
1920  TimeOffset date;
1921  TimeOffset time;
1922 
1923  /* Prevent overflow in Julian-day routines */
1924  if (!IS_VALID_JULIAN(tm->tm_year, tm->tm_mon, tm->tm_mday))
1925  {
1926  *result = 0; /* keep compiler quiet */
1927  return -1;
1928  }
1929 
1930  date = date2j(tm->tm_year, tm->tm_mon, tm->tm_mday) - POSTGRES_EPOCH_JDATE;
1931  time = time2t(tm->tm_hour, tm->tm_min, tm->tm_sec, fsec);
1932 
1933  *result = date * USECS_PER_DAY + time;
1934  /* check for major overflow */
1935  if ((*result - time) / USECS_PER_DAY != date)
1936  {
1937  *result = 0; /* keep compiler quiet */
1938  return -1;
1939  }
1940  /* check for just-barely overflow (okay except time-of-day wraps) */
1941  /* caution: we want to allow 1999-12-31 24:00:00 */
1942  if ((*result < 0 && date > 0) ||
1943  (*result > 0 && date < -1))
1944  {
1945  *result = 0; /* keep compiler quiet */
1946  return -1;
1947  }
1948  if (tzp != NULL)
1949  *result = dt2local(*result, -(*tzp));
1950 
1951  /* final range check catches just-out-of-range timestamps */
1952  if (!IS_VALID_TIMESTAMP(*result))
1953  {
1954  *result = 0; /* keep compiler quiet */
1955  return -1;
1956  }
1957 
1958  return 0;
1959 }
static TimeOffset time2t(const int hour, const int min, const int sec, const fsec_t fsec)
Definition: timestamp.c:2010
int tm_hour
Definition: pgtime.h:29
long date
Definition: pgtypes_date.h:9
int tm_mday
Definition: pgtime.h:30
int tm_mon
Definition: pgtime.h:31
#define IS_VALID_JULIAN(y, m, d)
Definition: timestamp.h:155
#define USECS_PER_DAY
Definition: timestamp.h:91
int64 TimeOffset
Definition: timestamp.h:40
int date2j(int y, int m, int d)
Definition: datetime.c:269
#define IS_VALID_TIMESTAMP(t)
Definition: timestamp.h:195
int tm_year
Definition: pgtime.h:32
#define POSTGRES_EPOCH_JDATE
Definition: timestamp.h:163
static Timestamp dt2local(Timestamp dt, int timezone)
Definition: timestamp.c:2016
int tm_sec
Definition: pgtime.h:27
int tm_min
Definition: pgtime.h:28

Variable Documentation

◆ PgReloadTime

TimestampTz PgReloadTime

Definition at line 55 of file timestamp.c.

Referenced by pg_conf_load_time(), and PostmasterMarkPIDForWorkerNotify().

◆ PgStartTime