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

#define DatumGetIntervalP (   X)    ((Interval *) DatumGetPointer(X))

Definition at line 29 of file timestamp.h.

◆ DatumGetTimestamp

#define DatumGetTimestamp (   X)    ((Timestamp) DatumGetInt64(X))

Definition at line 27 of file timestamp.h.

◆ DatumGetTimestampTz

#define DatumGetTimestampTz (   X)    ((TimestampTz) DatumGetInt64(X))

Definition at line 28 of file timestamp.h.

◆ INTERVAL_FULL_PRECISION

#define INTERVAL_FULL_PRECISION   (0xFFFF)

Definition at line 50 of file timestamp.h.

◆ INTERVAL_FULL_RANGE

#define INTERVAL_FULL_RANGE   (0x7FFF)

Definition at line 48 of file timestamp.h.

◆ INTERVAL_MASK

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

Definition at line 45 of file timestamp.h.

◆ INTERVAL_PRECISION

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

Definition at line 53 of file timestamp.h.

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

Definition at line 54 of file timestamp.h.

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

◆ IntervalPGetDatum

#define IntervalPGetDatum (   X)    PointerGetDatum(X)

Definition at line 33 of file timestamp.h.

◆ PG_GETARG_INTERVAL_P

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

Definition at line 37 of file timestamp.h.

◆ PG_GETARG_TIMESTAMP

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

Definition at line 35 of file timestamp.h.

◆ PG_GETARG_TIMESTAMPTZ

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

Definition at line 36 of file timestamp.h.

◆ PG_RETURN_INTERVAL_P

#define PG_RETURN_INTERVAL_P (   x)    return IntervalPGetDatum(x)

Definition at line 41 of file timestamp.h.

◆ PG_RETURN_TIMESTAMP

#define PG_RETURN_TIMESTAMP (   x)    return TimestampGetDatum(x)

Definition at line 39 of file timestamp.h.

◆ PG_RETURN_TIMESTAMPTZ

#define PG_RETURN_TIMESTAMPTZ (   x)    return TimestampTzGetDatum(x)

Definition at line 40 of file timestamp.h.

◆ TIMESTAMP_MASK

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

Definition at line 44 of file timestamp.h.

◆ TimestampGetDatum

#define TimestampGetDatum (   X)    Int64GetDatum(X)

Definition at line 31 of file timestamp.h.

◆ timestamptz_cmp_internal

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

Definition at line 100 of file timestamp.h.

◆ TimestampTzGetDatum

#define TimestampTzGetDatum (   X)    Int64GetDatum(X)

Definition at line 32 of file timestamp.h.

◆ TimestampTzPlusMilliseconds

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

Definition at line 56 of file timestamp.h.

Function Documentation

◆ anytimestamp_typmod_check()

int32 anytimestamp_typmod_check ( bool  istz,
int32  typmod 
)

Definition at line 104 of file timestamp.c.

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 }
#define MAX_TIMESTAMP_PRECISION
Definition: timestamp.h:53
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_TIMESTAMP_PRECISION, and WARNING.

Referenced by anytimestamp_typmodin(), and transformSQLValueFunction().

◆ date2isoweek()

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

Definition at line 4423 of file timestamp.c.

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

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

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

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

4536 {
4537  return date2j(year, mon, mday) - isoweek2j(date2isoyear(year, mon, mday), 1) + 1;
4538 }
int isoweek2j(int year, int week)
Definition: timestamp.c:4372
int date2isoyear(int year, int mon, int mday)
Definition: timestamp.c:4478

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

Referenced by DCH_to_char().

◆ dt2time()

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

Definition at line 1795 of file timestamp.c.

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_HOUR
Definition: timestamp.h:92
#define USECS_PER_SEC
Definition: timestamp.h:94
#define USECS_PER_MINUTE
Definition: timestamp.h:93
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 1580 of file timestamp.c.

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 }
int64 TimestampTz
Definition: timestamp.h:39
#define UNIX_EPOCH_JDATE
Definition: timestamp.h:162
#define SECS_PER_DAY
Definition: timestamp.h:86
#define POSTGRES_EPOCH_JDATE
Definition: timestamp.h:163
int gettimeofday(struct timeval *tp, struct timezone *tzp)
Definition: gettimeofday.c:104

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(), 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(), launcher_determine_sleep(), LockBufferForCleanup(), log_disconnections(), LogCheckpointEnd(), logicalrep_worker_launch(), LogicalRepApplyLoop(), maybe_start_bgworkers(), 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_subworker_error(), pgstat_report_vacuum(), pgstat_send_archiver(), pgstat_send_wal(), pgstat_write_statsfiles(), PostgresSingleUserMain(), 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(), XLogRestorePoint(), XLogSendPhysical(), XLogWalRcvSendHSFeedback(), and XLogWalRcvSendReply().

◆ GetEpochTime()

void GetEpochTime ( struct pg_tm tm)

Definition at line 2048 of file timestamp.c.

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 }
#define elog(elevel,...)
Definition: elog.h:218
static const unsigned __int64 epoch
Definition: gettimeofday.c:34
static struct pg_tm tm
Definition: localtime.c:102
struct pg_tm * pg_gmtime(const pg_time_t *timep)
Definition: localtime.c:1387
int64 pg_time_t
Definition: pgtime.h:23
Definition: pgtime.h:33
int tm_hour
Definition: pgtime.h:36
int tm_mday
Definition: pgtime.h:37
int tm_mon
Definition: pgtime.h:38
int tm_min
Definition: pgtime.h:35
int tm_sec
Definition: pgtime.h:34
int tm_year
Definition: pgtime.h:39

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

◆ GetSQLCurrentTimestamp()

TimestampTz GetSQLCurrentTimestamp ( int32  typmod)

Definition at line 1598 of file timestamp.c.

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

References AdjustTimestampForTypmod(), and GetCurrentTransactionStartTimestamp().

Referenced by ExecEvalSQLValueFunction().

◆ GetSQLLocalTimestamp()

Timestamp GetSQLLocalTimestamp ( int32  typmod)

Definition at line 1612 of file timestamp.c.

1613 {
1614  Timestamp ts;
1615 
1617  if (typmod >= 0)
1618  AdjustTimestampForTypmod(&ts, typmod);
1619  return ts;
1620 }
static Timestamp timestamptz2timestamp(TimestampTz timestamp)
Definition: timestamp.c:5570
int64 Timestamp
Definition: timestamp.h:38

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

Referenced by ExecEvalSQLValueFunction().

◆ interval2tm()

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

Definition at line 1966 of file timestamp.c.

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 SAMESIGN(a, b)
Definition: timestamp.c:49
#define MONTHS_PER_YEAR
Definition: timestamp.h:69
int32 day
Definition: timestamp.h:47
int32 month
Definition: timestamp.h:48
TimeOffset time
Definition: timestamp.h:45

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

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

◆ isoweek2date()

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

Definition at line 4392 of file timestamp.c.

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

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

4373 {
4374  int day0,
4375  day4;
4376 
4377  /* fourth day of current year */
4378  day4 = date2j(year, 1, 4);
4379 
4380  /* day0 == offset to first day of week (Monday) */
4381  day0 = j2day(day4 - 1);
4382 
4383  return ((week - 1) * 7) + (day4 - day0);
4384 }

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

4406 {
4407  int jday;
4408 
4409  jday = isoweek2j(*year, isoweek);
4410  /* convert Gregorian week start (Sunday=1) to ISO week start (Monday=1) */
4411  if (wday > 1)
4412  jday += wday - 2;
4413  else
4414  jday += 6;
4415  j2date(jday, year, mon, mday);
4416 }

References isoweek2j(), and j2date().

Referenced by do_to_timestamp().

◆ SetEpochTimestamp()

Timestamp SetEpochTimestamp ( void  )

Definition at line 2070 of file timestamp.c.

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() */
void GetEpochTime(struct pg_tm *tm)
Definition: timestamp.c:2048
int tm2timestamp(struct pg_tm *tm, fsec_t fsec, int *tzp, Timestamp *result)
Definition: timestamp.c:1918

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

1733 {
1734  TimestampTz result;
1735 
1736  result = (TimestampTz) tm -
1738  result *= USECS_PER_SEC;
1739 
1740  return result;
1741 }

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

5502 {
5503  TimestampTz result;
5504  struct pg_tm tt,
5505  *tm = &tt;
5506  fsec_t fsec;
5507  int tz;
5508 
5509  if (overflow)
5510  *overflow = 0;
5511 
5513  return timestamp;
5514 
5515  /* We don't expect this to fail, but check it pro forma */
5516  if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) == 0)
5517  {
5519 
5520  result = dt2local(timestamp, -tz);
5521 
5522  if (IS_VALID_TIMESTAMP(result))
5523  {
5524  return result;
5525  }
5526  else if (overflow)
5527  {
5528  if (result < MIN_TIMESTAMP)
5529  {
5530  *overflow = -1;
5531  TIMESTAMP_NOBEGIN(result);
5532  }
5533  else
5534  {
5535  *overflow = 1;
5536  TIMESTAMP_NOEND(result);
5537  }
5538  return result;
5539  }
5540  }
5541 
5542  ereport(ERROR,
5543  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
5544  errmsg("timestamp out of range")));
5545 
5546  return 0;
5547 }
int DetermineTimeZoneOffset(struct pg_tm *tm, pg_tz *tzp)
Definition: datetime.c:1478
static Timestamp dt2local(Timestamp dt, int timezone)
Definition: timestamp.c:2016
int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm *tm, fsec_t *fsec, const char **tzn, pg_tz *attimezone)
Definition: timestamp.c:1822
int32 fsec_t
Definition: timestamp.h:41
#define TIMESTAMP_NOBEGIN(j)
Definition: timestamp.h:112
#define IS_VALID_TIMESTAMP(t)
Definition: timestamp.h:195
#define TIMESTAMP_NOT_FINITE(j)
Definition: timestamp.h:122
#define TIMESTAMP_NOEND(j)
Definition: timestamp.h:117
#define MIN_TIMESTAMP
Definition: timestamp.h:184
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 1822 of file timestamp.c.

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 */
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 }
void dt2time(Timestamp jd, int *hour, int *min, int *sec, fsec_t *fsec)
Definition: timestamp.c:1795
#define USECS_PER_DAY
Definition: timestamp.h:91
#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:1342
long date
Definition: pgtypes_date.h:9
const char * tm_zone
Definition: pgtime.h:44
int tm_isdst
Definition: pgtime.h:42
long int tm_gmtoff
Definition: pgtime.h:43

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(), 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(), 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 2194 of file timestamp.c.

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 }
TimestampTz timestamp2timestamptz_opt_overflow(Timestamp timestamp, int *overflow)
Definition: timestamp.c:5501
#define TIMESTAMP_IS_NOEND(j)
Definition: timestamp.h:120
#define TIMESTAMP_IS_NOBEGIN(j)
Definition: timestamp.h:115
#define timestamptz_cmp_internal(dt1, dt2)
Definition: timestamp.h:100

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

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 }
static time_t start_time
Definition: pg_ctl.c:99

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(), ProcSleep(), and schedule_alarm().

◆ TimestampDifferenceExceeds()

◆ TimestampDifferenceMilliseconds()

long TimestampDifferenceMilliseconds ( TimestampTz  start_time,
TimestampTz  stop_time 
)

Definition at line 1693 of file timestamp.c.

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 }

References start_time.

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

◆ TimestampTimestampTzRequiresRewrite()

bool TimestampTimestampTzRequiresRewrite ( void  )

Definition at line 5470 of file timestamp.c.

5471 {
5472  long offset;
5473 
5474  if (pg_get_timezone_offset(session_timezone, &offset) && offset == 0)
5475  return false;
5476  return true;
5477 }
bool pg_get_timezone_offset(const pg_tz *tz, long int *gmtoff)
Definition: localtime.c:1849

References pg_get_timezone_offset(), and session_timezone.

Referenced by ATColumnChangeRequiresRewrite().

◆ timestamptz_to_str()

const char* timestamptz_to_str ( TimestampTz  t)

Definition at line 1774 of file timestamp.c.

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))
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 }
void EncodeDateTime(struct pg_tm *tm, fsec_t fsec, bool print_tz, int tz, const char *tzn, int style, char *str)
Definition: datetime.c:3998
void EncodeSpecialTimestamp(Timestamp dt, char *str)
Definition: timestamp.c:1533
#define MAXDATELEN
Definition: datetime.h:201
#define USE_ISO_DATES
Definition: miscadmin.h:229
static char * buf
Definition: pg_test_fsync.c:70
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 apply_error_callback(), backend_read_statsfile(), CreateRestartPoint(), getRecoveryStopReason(), 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(), VerifyOverwriteContrecord(), xact_desc_abort(), xact_desc_commit(), xact_desc_prepare(), and xlog_desc().

◆ timestamptz_to_time_t()

pg_time_t timestamptz_to_time_t ( TimestampTz  t)

Definition at line 1754 of file timestamp.c.

1755 {
1756  pg_time_t result;
1757 
1758  result = (pg_time_t) (t / USECS_PER_SEC +
1760 
1761  return result;
1762 }

References POSTGRES_EPOCH_JDATE, SECS_PER_DAY, UNIX_EPOCH_JDATE, and USECS_PER_SEC.

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

◆ tm2interval()

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

Definition at line 1994 of file timestamp.c.

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 }

References Interval::day, if(), Interval::month, MONTHS_PER_YEAR, Interval::time, 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_SEC.

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

◆ tm2timestamp()

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

Definition at line 1918 of file timestamp.c.

1919 {
1920  TimeOffset date;
1921  TimeOffset time;
1922 
1923  /* Prevent overflow in Julian-day routines */
1925  {
1926  *result = 0; /* keep compiler quiet */
1927  return -1;
1928  }
1929 
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
#define IS_VALID_JULIAN(y, m, d)
Definition: timestamp.h:155

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

TimestampTz PgReloadTime
extern

Definition at line 55 of file timestamp.c.

Referenced by pg_conf_load_time().

◆ PgStartTime

TimestampTz PgStartTime
extern

Definition at line 52 of file timestamp.c.

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