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

static Timestamp DatumGetTimestamp (Datum X)
 
static TimestampTz DatumGetTimestampTz (Datum X)
 
static IntervalDatumGetIntervalP (Datum X)
 
static Datum TimestampGetDatum (Timestamp X)
 
static Datum TimestampTzGetDatum (TimestampTz X)
 
static Datum IntervalPGetDatum (const Interval *X)
 
TimestampTz GetCurrentTimestamp (void)
 
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 *result)
 
int timestamp2tm (Timestamp dt, int *tzp, struct pg_tm *tm, fsec_t *fsec, const char **tzn, pg_tz *attimezone)
 
void dt2time (Timestamp jd, int *hour, int *min, int *sec, fsec_t *fsec)
 
void interval2itm (Interval span, struct pg_itm *itm)
 
int itm2interval (struct pg_itm *itm, Interval *span)
 
int itmin2interval (struct pg_itm_in *itm_in, 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

PGDLLIMPORT TimestampTz PgStartTime
 
PGDLLIMPORT TimestampTz PgReloadTime
 

Macro Definition Documentation

◆ INTERVAL_FULL_PRECISION

#define INTERVAL_FULL_PRECISION   (0xFFFF)

Definition at line 78 of file timestamp.h.

◆ INTERVAL_FULL_RANGE

#define INTERVAL_FULL_RANGE   (0x7FFF)

Definition at line 76 of file timestamp.h.

◆ INTERVAL_MASK

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

Definition at line 73 of file timestamp.h.

◆ INTERVAL_PRECISION

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

Definition at line 81 of file timestamp.h.

◆ INTERVAL_PRECISION_MASK

#define INTERVAL_PRECISION_MASK   (0xFFFF)

Definition at line 79 of file timestamp.h.

◆ INTERVAL_RANGE

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

Definition at line 82 of file timestamp.h.

◆ INTERVAL_RANGE_MASK

#define INTERVAL_RANGE_MASK   (0x7FFF)

Definition at line 77 of file timestamp.h.

◆ INTERVAL_TYPMOD

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

Definition at line 80 of file timestamp.h.

◆ PG_GETARG_INTERVAL_P

#define PG_GETARG_INTERVAL_P (   n)    DatumGetIntervalP(PG_GETARG_DATUM(n))

Definition at line 65 of file timestamp.h.

◆ PG_GETARG_TIMESTAMP

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

Definition at line 63 of file timestamp.h.

◆ PG_GETARG_TIMESTAMPTZ

#define PG_GETARG_TIMESTAMPTZ (   n)    DatumGetTimestampTz(PG_GETARG_DATUM(n))

Definition at line 64 of file timestamp.h.

◆ PG_RETURN_INTERVAL_P

#define PG_RETURN_INTERVAL_P (   x)    return IntervalPGetDatum(x)

Definition at line 69 of file timestamp.h.

◆ PG_RETURN_TIMESTAMP

#define PG_RETURN_TIMESTAMP (   x)    return TimestampGetDatum(x)

Definition at line 67 of file timestamp.h.

◆ PG_RETURN_TIMESTAMPTZ

#define PG_RETURN_TIMESTAMPTZ (   x)    return TimestampTzGetDatum(x)

Definition at line 68 of file timestamp.h.

◆ TIMESTAMP_MASK

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

Definition at line 72 of file timestamp.h.

◆ timestamptz_cmp_internal

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

Definition at line 125 of file timestamp.h.

◆ TimestampTzPlusMilliseconds

#define TimestampTzPlusMilliseconds (   tz,
  ms 
)    ((tz) + ((ms) * (int64) 1000))

Definition at line 84 of file timestamp.h.

Function Documentation

◆ date2isoweek()

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

Definition at line 4489 of file timestamp.c.

4490 {
4491  float8 result;
4492  int day0,
4493  day4,
4494  dayn;
4495 
4496  /* current day */
4497  dayn = date2j(year, mon, mday);
4498 
4499  /* fourth day of current year */
4500  day4 = date2j(year, 1, 4);
4501 
4502  /* day0 == offset to first day of week (Monday) */
4503  day0 = j2day(day4 - 1);
4504 
4505  /*
4506  * We need the first week containing a Thursday, otherwise this day falls
4507  * into the previous year for purposes of counting weeks
4508  */
4509  if (dayn < day4 - day0)
4510  {
4511  day4 = date2j(year - 1, 1, 4);
4512 
4513  /* day0 == offset to first day of week (Monday) */
4514  day0 = j2day(day4 - 1);
4515  }
4516 
4517  result = (dayn - (day4 - day0)) / 7 + 1;
4518 
4519  /*
4520  * Sometimes the last few days in a year will fall into the first week of
4521  * the next year, so check for this.
4522  */
4523  if (result >= 52)
4524  {
4525  day4 = date2j(year + 1, 1, 4);
4526 
4527  /* day0 == offset to first day of week (Monday) */
4528  day0 = j2day(day4 - 1);
4529 
4530  if (dayn >= day4 - day0)
4531  result = (dayn - (day4 - day0)) / 7 + 1;
4532  }
4533 
4534  return (int) result;
4535 }
int j2day(int date)
Definition: datetime.c:343
int date2j(int year, int month, int day)
Definition: datetime.c:285
double float8
Definition: c.h:566

References date2j(), and j2day().

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

◆ date2isoyear()

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

Definition at line 4544 of file timestamp.c.

4545 {
4546  float8 result;
4547  int day0,
4548  day4,
4549  dayn;
4550 
4551  /* current day */
4552  dayn = date2j(year, mon, mday);
4553 
4554  /* fourth day of current year */
4555  day4 = date2j(year, 1, 4);
4556 
4557  /* day0 == offset to first day of week (Monday) */
4558  day0 = j2day(day4 - 1);
4559 
4560  /*
4561  * We need the first week containing a Thursday, otherwise this day falls
4562  * into the previous year for purposes of counting weeks
4563  */
4564  if (dayn < day4 - day0)
4565  {
4566  day4 = date2j(year - 1, 1, 4);
4567 
4568  /* day0 == offset to first day of week (Monday) */
4569  day0 = j2day(day4 - 1);
4570 
4571  year--;
4572  }
4573 
4574  result = (dayn - (day4 - day0)) / 7 + 1;
4575 
4576  /*
4577  * Sometimes the last few days in a year will fall into the first week of
4578  * the next year, so check for this.
4579  */
4580  if (result >= 52)
4581  {
4582  day4 = date2j(year + 1, 1, 4);
4583 
4584  /* day0 == offset to first day of week (Monday) */
4585  day0 = j2day(day4 - 1);
4586 
4587  if (dayn >= day4 - day0)
4588  year++;
4589  }
4590 
4591  return year;
4592 }

References date2j(), and j2day().

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

◆ date2isoyearday()

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

Definition at line 4601 of file timestamp.c.

4602 {
4603  return date2j(year, mon, mday) - isoweek2j(date2isoyear(year, mon, mday), 1) + 1;
4604 }
int isoweek2j(int year, int week)
Definition: timestamp.c:4438
int date2isoyear(int year, int mon, int mday)
Definition: timestamp.c:4544

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

Referenced by DCH_to_char().

◆ DatumGetIntervalP()

◆ DatumGetTimestamp()

static Timestamp DatumGetTimestamp ( Datum  X)
inlinestatic

◆ DatumGetTimestampTz()

◆ dt2time()

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

Definition at line 1803 of file timestamp.c.

1804 {
1805  TimeOffset time;
1806 
1807  time = jd;
1808 
1809  *hour = time / USECS_PER_HOUR;
1810  time -= (*hour) * USECS_PER_HOUR;
1811  *min = time / USECS_PER_MINUTE;
1812  time -= (*min) * USECS_PER_MINUTE;
1813  *sec = time / USECS_PER_SEC;
1814  *fsec = time - (*sec * USECS_PER_SEC);
1815 } /* dt2time() */
#define USECS_PER_HOUR
Definition: timestamp.h:131
#define USECS_PER_SEC
Definition: timestamp.h:133
#define USECS_PER_MINUTE
Definition: timestamp.h:132
int64 TimeOffset
Definition: timestamp.h:40

References USECS_PER_HOUR, USECS_PER_MINUTE, and USECS_PER_SEC.

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

◆ GetCurrentTimestamp()

TimestampTz GetCurrentTimestamp ( void  )

Definition at line 1573 of file timestamp.c.

1574 {
1575  TimestampTz result;
1576  struct timeval tp;
1577 
1578  gettimeofday(&tp, NULL);
1579 
1580  result = (TimestampTz) tp.tv_sec -
1582  result = (result * USECS_PER_SEC) + tp.tv_usec;
1583 
1584  return result;
1585 }
#define UNIX_EPOCH_JDATE
Definition: timestamp.h:201
#define SECS_PER_DAY
Definition: timestamp.h:125
#define POSTGRES_EPOCH_JDATE
Definition: timestamp.h:202
int gettimeofday(struct timeval *tp, void *tzp)

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

Referenced by ApplyLauncherMain(), ApplyWorkerMain(), asyncQueueFillWarning(), autoprewarm_main(), AutoVacLauncherMain(), BackgroundWriterMain(), bbsink_copystream_archive_contents(), bbsink_copystream_end_archive(), bbsink_copystream_new(), bbsink_throttle_begin_backup(), begin_startup_progress_phase(), check_log_duration(), CheckPointGuts(), CleanupBackgroundWorker(), clock_timestamp(), CreateCheckPoint(), CreateEndOfRecoveryRecord(), CreateOverwriteContrecordRecord(), CreateRestartPoint(), DetermineSleepTime(), disable_timeout(), disable_timeouts(), do_analyze_rel(), do_start_bgworker(), do_start_worker(), drandom(), enable_timeout_after(), enable_timeout_at(), enable_timeout_every(), enable_timeouts(), entry_reset(), get_role_password(), GetCurrentTransactionStopTimestamp(), GetReplicationApplyDelay(), GetSnapshotCurrentTimestamp(), handle_sig_alarm(), has_startup_progress_timeout_expired(), heap_vacuum_rel(), InitProcessGlobals(), KnownAssignedXidsCompress(), launcher_determine_sleep(), LockBufferForCleanup(), log_disconnections(), LogCheckpointEnd(), logicalrep_worker_launch(), LogicalRepApplyLoop(), maybe_start_bgworkers(), PerformWalRecovery(), pgfdw_cancel_query(), pgfdw_exec_cleanup_query(), pgfdw_get_cleanup_result(), pgss_shmem_startup(), pgstat_build_snapshot(), pgstat_report_activity(), pgstat_report_analyze(), pgstat_report_archiver(), pgstat_report_autovac(), pgstat_report_checksum_failures_in_db(), pgstat_report_vacuum(), pgstat_reset(), pgstat_reset_after_failure(), pgstat_reset_counters(), pgstat_reset_of_kind(), pgstat_reset_slru(), PostgresSingleUserMain(), PostmasterMain(), PrepareTransaction(), process_syncing_tables_for_apply(), ProcessConfigFileInternal(), ProcessPendingWrites(), ProcessRepliesIfAny(), ProcessStandbyReplyMessage(), ProcessWalSndrMessage(), ProcSleep(), rebuild_database_list(), RecordTransactionAbort(), RecordTransactionAbortPrepared(), RecordTransactionCommitPrepared(), recoveryApplyDelay(), reschedule_timeouts(), ResolveRecoveryConflictWithBufferPin(), ResolveRecoveryConflictWithLock(), ResolveRecoveryConflictWithVirtualXIDs(), send_feedback(), SetCurrentStatementStartTimestamp(), StartTransaction(), test_pattern(), throttle(), UpdateWorkerStats(), WaitExceedsMaxStandbyDelay(), WaitForWALToBecomeAvailable(), WalReceiverMain(), WalSndKeepalive(), WalSndLoop(), WalSndUpdateProgress(), WalSndWaitForWal(), WalSndWriteData(), XLogBackgroundFlush(), XLogFileRead(), XLogPrefetchResetStats(), XLogPrefetchShmemInit(), XLogRestorePoint(), XLogSendPhysical(), XLogWalRcvSendHSFeedback(), and XLogWalRcvSendReply().

◆ GetEpochTime()

void GetEpochTime ( struct pg_tm tm)

Definition at line 2083 of file timestamp.c.

2084 {
2085  struct pg_tm *t0;
2086  pg_time_t epoch = 0;
2087 
2088  t0 = pg_gmtime(&epoch);
2089 
2090  if (t0 == NULL)
2091  elog(ERROR, "could not convert epoch to timestamp: %m");
2092 
2093  tm->tm_year = t0->tm_year;
2094  tm->tm_mon = t0->tm_mon;
2095  tm->tm_mday = t0->tm_mday;
2096  tm->tm_hour = t0->tm_hour;
2097  tm->tm_min = t0->tm_min;
2098  tm->tm_sec = t0->tm_sec;
2099 
2100  tm->tm_year += 1900;
2101  tm->tm_mon++;
2102 }
#define ERROR
Definition: elog.h:35
static struct pg_tm tm
Definition: localtime.c:104
struct pg_tm * pg_gmtime(const pg_time_t *timep)
Definition: localtime.c:1389
int64 pg_time_t
Definition: pgtime.h:23
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
static const unsigned __int64 epoch

References elog(), epoch, ERROR, pg_gmtime(), tm, 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().

◆ interval2itm()

void interval2itm ( Interval  span,
struct pg_itm itm 
)

Definition at line 1976 of file timestamp.c.

1977 {
1978  TimeOffset time;
1979  TimeOffset tfrac;
1980 
1981  itm->tm_year = span.month / MONTHS_PER_YEAR;
1982  itm->tm_mon = span.month % MONTHS_PER_YEAR;
1983  itm->tm_mday = span.day;
1984  time = span.time;
1985 
1986  tfrac = time / USECS_PER_HOUR;
1987  time -= tfrac * USECS_PER_HOUR;
1988  itm->tm_hour = tfrac;
1989  tfrac = time / USECS_PER_MINUTE;
1990  time -= tfrac * USECS_PER_MINUTE;
1991  itm->tm_min = (int) tfrac;
1992  tfrac = time / USECS_PER_SEC;
1993  time -= tfrac * USECS_PER_SEC;
1994  itm->tm_sec = (int) tfrac;
1995  itm->tm_usec = (int) time;
1996 }
#define MONTHS_PER_YEAR
Definition: timestamp.h:108
int32 day
Definition: timestamp.h:51
int32 month
Definition: timestamp.h:52
TimeOffset time
Definition: timestamp.h:49
int64 tm_hour
Definition: timestamp.h:70
int tm_year
Definition: timestamp.h:73
int tm_mon
Definition: timestamp.h:72
int tm_mday
Definition: timestamp.h:71
int tm_sec
Definition: timestamp.h:68
int tm_min
Definition: timestamp.h:69
int tm_usec
Definition: timestamp.h:67

References Interval::day, Interval::month, MONTHS_PER_YEAR, Interval::time, pg_itm::tm_hour, pg_itm::tm_mday, pg_itm::tm_min, pg_itm::tm_mon, pg_itm::tm_sec, pg_itm::tm_usec, pg_itm::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().

◆ IntervalPGetDatum()

◆ isoweek2date()

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

Definition at line 4458 of file timestamp.c.

4459 {
4460  j2date(isoweek2j(*year, woy), year, mon, mday);
4461 }
void j2date(int jd, int *year, int *month, int *day)
Definition: datetime.c:310

References isoweek2j(), and j2date().

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

◆ isoweek2j()

int isoweek2j ( int  year,
int  week 
)

Definition at line 4438 of file timestamp.c.

4439 {
4440  int day0,
4441  day4;
4442 
4443  /* fourth day of current year */
4444  day4 = date2j(year, 1, 4);
4445 
4446  /* day0 == offset to first day of week (Monday) */
4447  day0 = j2day(day4 - 1);
4448 
4449  return ((week - 1) * 7) + (day4 - day0);
4450 }

References date2j(), and j2day().

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

◆ isoweekdate2date()

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

Definition at line 4471 of file timestamp.c.

4472 {
4473  int jday;
4474 
4475  jday = isoweek2j(*year, isoweek);
4476  /* convert Gregorian week start (Sunday=1) to ISO week start (Monday=1) */
4477  if (wday > 1)
4478  jday += wday - 2;
4479  else
4480  jday += 6;
4481  j2date(jday, year, mon, mday);
4482 }

References isoweek2j(), and j2date().

Referenced by do_to_timestamp().

◆ itm2interval()

int itm2interval ( struct pg_itm itm,
Interval span 
)

Definition at line 2003 of file timestamp.c.

2004 {
2005  int64 total_months = (int64) itm->tm_year * MONTHS_PER_YEAR + itm->tm_mon;
2006 
2007  if (total_months > INT_MAX || total_months < INT_MIN)
2008  return -1;
2009  span->month = (int32) total_months;
2010  span->day = itm->tm_mday;
2012  &span->time))
2013  return -1;
2014  /* tm_min, tm_sec are 32 bits, so intermediate products can't overflow */
2015  if (pg_add_s64_overflow(span->time, itm->tm_min * USECS_PER_MINUTE,
2016  &span->time))
2017  return -1;
2018  if (pg_add_s64_overflow(span->time, itm->tm_sec * USECS_PER_SEC,
2019  &span->time))
2020  return -1;
2021  if (pg_add_s64_overflow(span->time, itm->tm_usec,
2022  &span->time))
2023  return -1;
2024  return 0;
2025 }
signed int int32
Definition: c.h:430
static bool pg_mul_s64_overflow(int64 a, int64 b, int64 *result)
Definition: int.h:215
static bool pg_add_s64_overflow(int64 a, int64 b, int64 *result)
Definition: int.h:161
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:77

References Interval::day, if(), Interval::month, MONTHS_PER_YEAR, pg_add_s64_overflow(), pg_mul_s64_overflow(), Interval::time, pg_itm::tm_hour, pg_itm::tm_mday, pg_itm::tm_min, pg_itm::tm_mon, pg_itm::tm_sec, pg_itm::tm_usec, pg_itm::tm_year, USECS_PER_HOUR, USECS_PER_MINUTE, and USECS_PER_SEC.

Referenced by interval_trunc(), timestamp_age(), and timestamptz_age().

◆ itmin2interval()

int itmin2interval ( struct pg_itm_in itm_in,
Interval span 
)

Definition at line 2032 of file timestamp.c.

2033 {
2034  int64 total_months = (int64) itm_in->tm_year * MONTHS_PER_YEAR + itm_in->tm_mon;
2035 
2036  if (total_months > INT_MAX || total_months < INT_MIN)
2037  return -1;
2038  span->month = (int32) total_months;
2039  span->day = itm_in->tm_mday;
2040  span->time = itm_in->tm_usec;
2041  return 0;
2042 }
int tm_mon
Definition: timestamp.h:86
int tm_year
Definition: timestamp.h:87
int tm_mday
Definition: timestamp.h:85
int64 tm_usec
Definition: timestamp.h:84

References Interval::day, if(), Interval::month, MONTHS_PER_YEAR, Interval::time, pg_itm_in::tm_mday, pg_itm_in::tm_mon, pg_itm_in::tm_usec, and pg_itm_in::tm_year.

Referenced by interval_in(), and pg_timezone_abbrevs().

◆ SetEpochTimestamp()

Timestamp SetEpochTimestamp ( void  )

Definition at line 2105 of file timestamp.c.

2106 {
2107  Timestamp dt;
2108  struct pg_tm tt,
2109  *tm = &tt;
2110 
2111  GetEpochTime(tm);
2112  /* we don't bother to test for failure ... */
2113  tm2timestamp(tm, 0, NULL, &dt);
2114 
2115  return dt;
2116 } /* SetEpochTimestamp() */
void GetEpochTime(struct pg_tm *tm)
Definition: timestamp.c:2083
int tm2timestamp(struct pg_tm *tm, fsec_t fsec, int *tzp, Timestamp *result)
Definition: timestamp.c:1926

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

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

◆ time_t_to_timestamptz()

TimestampTz time_t_to_timestamptz ( pg_time_t  tm)

Definition at line 1740 of file timestamp.c.

1741 {
1742  TimestampTz result;
1743 
1744  result = (TimestampTz) tm -
1746  result *= USECS_PER_SEC;
1747 
1748  return result;
1749 }

References POSTGRES_EPOCH_JDATE, SECS_PER_DAY, tm, UNIX_EPOCH_JDATE, and USECS_PER_SEC.

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

◆ timestamp2timestamptz_opt_overflow()

TimestampTz timestamp2timestamptz_opt_overflow ( Timestamp  timestamp,
int *  overflow 
)

Definition at line 5563 of file timestamp.c.

5564 {
5565  TimestampTz result;
5566  struct pg_tm tt,
5567  *tm = &tt;
5568  fsec_t fsec;
5569  int tz;
5570 
5571  if (overflow)
5572  *overflow = 0;
5573 
5575  return timestamp;
5576 
5577  /* We don't expect this to fail, but check it pro forma */
5578  if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) == 0)
5579  {
5581 
5582  result = dt2local(timestamp, -tz);
5583 
5584  if (IS_VALID_TIMESTAMP(result))
5585  {
5586  return result;
5587  }
5588  else if (overflow)
5589  {
5590  if (result < MIN_TIMESTAMP)
5591  {
5592  *overflow = -1;
5593  TIMESTAMP_NOBEGIN(result);
5594  }
5595  else
5596  {
5597  *overflow = 1;
5598  TIMESTAMP_NOEND(result);
5599  }
5600  return result;
5601  }
5602  }
5603 
5604  ereport(ERROR,
5605  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
5606  errmsg("timestamp out of range")));
5607 
5608  return 0;
5609 }
int DetermineTimeZoneOffset(struct pg_tm *tm, pg_tz *tzp)
Definition: datetime.c:1648
static Timestamp dt2local(Timestamp dt, int timezone)
Definition: timestamp.c:2051
int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm *tm, fsec_t *fsec, const char **tzn, pg_tz *attimezone)
Definition: timestamp.c:1830
int32 fsec_t
Definition: timestamp.h:41
#define TIMESTAMP_NOBEGIN(j)
Definition: timestamp.h:151
#define IS_VALID_TIMESTAMP(t)
Definition: timestamp.h:234
#define TIMESTAMP_NOT_FINITE(j)
Definition: timestamp.h:161
#define TIMESTAMP_NOEND(j)
Definition: timestamp.h:156
#define MIN_TIMESTAMP
Definition: timestamp.h:223
int errcode(int sqlerrcode)
Definition: elog.c:695
int errmsg(const char *fmt,...)
Definition: elog.c:906
#define ereport(elevel,...)
Definition: elog.h:145
PGDLLIMPORT pg_tz * session_timezone
Definition: pgtz.c:28
int64 timestamp

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

◆ timestamp2tm()

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

Definition at line 1830 of file timestamp.c.

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

References dt2time(), j2date(), pg_localtime(), POSTGRES_EPOCH_JDATE, SECS_PER_DAY, session_timezone, tm, 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(), 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(), timestamptz_zone(), and timetz_zone().

◆ 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 2240 of file timestamp.c.

2241 {
2242  TimestampTz dt1;
2243  int overflow;
2244 
2245  dt1 = timestamp2timestamptz_opt_overflow(timestampVal, &overflow);
2246  if (overflow > 0)
2247  {
2248  /* dt1 is larger than any finite timestamp, but less than infinity */
2249  return TIMESTAMP_IS_NOEND(dt2) ? -1 : +1;
2250  }
2251  if (overflow < 0)
2252  {
2253  /* dt1 is less than any finite timestamp, but more than -infinity */
2254  return TIMESTAMP_IS_NOBEGIN(dt2) ? +1 : -1;
2255  }
2256 
2257  return timestamptz_cmp_internal(dt1, dt2);
2258 }
TimestampTz timestamp2timestamptz_opt_overflow(Timestamp timestamp, int *overflow)
Definition: timestamp.c:5563
#define TIMESTAMP_IS_NOEND(j)
Definition: timestamp.h:159
#define TIMESTAMP_IS_NOBEGIN(j)
Definition: timestamp.h:154
#define timestamptz_cmp_internal(dt1, dt2)
Definition: timestamp.h:125

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

◆ TimestampDifference()

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

Definition at line 1664 of file timestamp.c.

1666 {
1667  TimestampTz diff = stop_time - start_time;
1668 
1669  if (diff <= 0)
1670  {
1671  *secs = 0;
1672  *microsecs = 0;
1673  }
1674  else
1675  {
1676  *secs = (long) (diff / USECS_PER_SEC);
1677  *microsecs = (int) (diff % USECS_PER_SEC);
1678  }
1679 }
static time_t start_time
Definition: pg_ctl.c:94

References start_time, and USECS_PER_SEC.

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

◆ TimestampDifferenceExceeds()

◆ TimestampDifferenceMilliseconds()

long TimestampDifferenceMilliseconds ( TimestampTz  start_time,
TimestampTz  stop_time 
)

Definition at line 1701 of file timestamp.c.

1702 {
1703  TimestampTz diff = stop_time - start_time;
1704 
1705  if (diff <= 0)
1706  return 0;
1707  else
1708  return (long) ((diff + 999) / 1000);
1709 }

References start_time.

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

◆ TimestampGetDatum()

◆ TimestampTimestampTzRequiresRewrite()

bool TimestampTimestampTzRequiresRewrite ( void  )

Definition at line 5532 of file timestamp.c.

5533 {
5534  long offset;
5535 
5536  if (pg_get_timezone_offset(session_timezone, &offset) && offset == 0)
5537  return false;
5538  return true;
5539 }
bool pg_get_timezone_offset(const pg_tz *tz, long int *gmtoff)
Definition: localtime.c:1851

References pg_get_timezone_offset(), and session_timezone.

Referenced by ATColumnChangeRequiresRewrite().

◆ timestamptz_to_str()

const char* timestamptz_to_str ( TimestampTz  t)

Definition at line 1782 of file timestamp.c.

1783 {
1784  static char buf[MAXDATELEN + 1];
1785  int tz;
1786  struct pg_tm tt,
1787  *tm = &tt;
1788  fsec_t fsec;
1789  const char *tzn;
1790 
1791  if (TIMESTAMP_NOT_FINITE(t))
1793  else if (timestamp2tm(t, &tz, tm, &fsec, &tzn, NULL) == 0)
1794  EncodeDateTime(tm, fsec, true, tz, tzn, USE_ISO_DATES, buf);
1795  else
1796  strlcpy(buf, "(timestamp out of range)", sizeof(buf));
1797 
1798  return buf;
1799 }
void EncodeDateTime(struct pg_tm *tm, fsec_t fsec, bool print_tz, int tz, const char *tzn, int style, char *str)
Definition: datetime.c:4259
void EncodeSpecialTimestamp(Timestamp dt, char *str)
Definition: timestamp.c:1526
#define MAXDATELEN
Definition: datetime.h:201
#define USE_ISO_DATES
Definition: miscadmin.h:230
static char * buf
Definition: pg_test_fsync.c:67
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45

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 CreateRestartPoint(), getRecoveryStopReason(), InitWalRecovery(), PerformWalRecovery(), 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(), ProcessStandbyHSFeedbackMessage(), ProcessStandbyReplyMessage(), ProcessWalSndrMessage(), recoveryStopsAfter(), recoveryStopsBefore(), xact_desc_abort(), xact_desc_commit(), xact_desc_prepare(), xlog_desc(), and xlogrecovery_redo().

◆ timestamptz_to_time_t()

pg_time_t timestamptz_to_time_t ( TimestampTz  t)

Definition at line 1762 of file timestamp.c.

1763 {
1764  pg_time_t result;
1765 
1766  result = (pg_time_t) (t / USECS_PER_SEC +
1768 
1769  return result;
1770 }

References POSTGRES_EPOCH_JDATE, SECS_PER_DAY, UNIX_EPOCH_JDATE, and USECS_PER_SEC.

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

◆ TimestampTzGetDatum()

◆ tm2timestamp()

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

Definition at line 1926 of file timestamp.c.

1927 {
1928  TimeOffset date;
1929  TimeOffset time;
1930 
1931  /* Prevent overflow in Julian-day routines */
1933  {
1934  *result = 0; /* keep compiler quiet */
1935  return -1;
1936  }
1937 
1939  time = time2t(tm->tm_hour, tm->tm_min, tm->tm_sec, fsec);
1940 
1941  *result = date * USECS_PER_DAY + time;
1942  /* check for major overflow */
1943  if ((*result - time) / USECS_PER_DAY != date)
1944  {
1945  *result = 0; /* keep compiler quiet */
1946  return -1;
1947  }
1948  /* check for just-barely overflow (okay except time-of-day wraps) */
1949  /* caution: we want to allow 1999-12-31 24:00:00 */
1950  if ((*result < 0 && date > 0) ||
1951  (*result > 0 && date < -1))
1952  {
1953  *result = 0; /* keep compiler quiet */
1954  return -1;
1955  }
1956  if (tzp != NULL)
1957  *result = dt2local(*result, -(*tzp));
1958 
1959  /* final range check catches just-out-of-range timestamps */
1960  if (!IS_VALID_TIMESTAMP(*result))
1961  {
1962  *result = 0; /* keep compiler quiet */
1963  return -1;
1964  }
1965 
1966  return 0;
1967 }
static TimeOffset time2t(const int hour, const int min, const int sec, const fsec_t fsec)
Definition: timestamp.c:2045
#define IS_VALID_JULIAN(y, m, d)
Definition: timestamp.h:194

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

Variable Documentation

◆ PgReloadTime

PGDLLIMPORT TimestampTz PgReloadTime
extern

Definition at line 56 of file timestamp.c.

Referenced by pg_conf_load_time(), and ProcessConfigFileInternal().

◆ PgStartTime

PGDLLIMPORT TimestampTz PgStartTime
extern

Definition at line 53 of file timestamp.c.

Referenced by pg_postmaster_start_time(), PostgresSingleUserMain(), and PostmasterMain().