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

◆ 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

◆ TimestampTzGetDatum

◆ TimestampTzPlusMilliseconds

Function Documentation

◆ anytimestamp_typmod_check()

int32 anytimestamp_typmod_check ( bool  istz,
int32  typmod 
)

Definition at line 101 of file timestamp.c.

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

Referenced by anytimestamp_typmodin(), and transformSQLValueFunction().

102 {
103  if (typmod < 0)
104  ereport(ERROR,
105  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
106  errmsg("TIMESTAMP(%d)%s precision must not be negative",
107  typmod, (istz ? " WITH TIME ZONE" : ""))));
108  if (typmod > MAX_TIMESTAMP_PRECISION)
109  {
111  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
112  errmsg("TIMESTAMP(%d)%s precision reduced to maximum allowed, %d",
113  typmod, (istz ? " WITH TIME ZONE" : ""),
115  typmod = MAX_TIMESTAMP_PRECISION;
116  }
117 
118  return typmod;
119 }
int errcode(int sqlerrcode)
Definition: elog.c:608
#define ERROR
Definition: elog.h:43
#define MAX_TIMESTAMP_PRECISION
Definition: timestamp.h:53
#define ereport(elevel, rest)
Definition: elog.h:141
#define WARNING
Definition: elog.h:40
int errmsg(const char *fmt,...)
Definition: elog.c:822

◆ date2isoweek()

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

Definition at line 4324 of file timestamp.c.

References date2j(), and j2day().

Referenced by DCH_to_char(), timestamp_part(), timestamp_trunc(), timestamptz_part(), and timestamptz_trunc_internal().

4325 {
4326  float8 result;
4327  int day0,
4328  day4,
4329  dayn;
4330 
4331  /* current day */
4332  dayn = date2j(year, mon, mday);
4333 
4334  /* fourth day of current year */
4335  day4 = date2j(year, 1, 4);
4336 
4337  /* day0 == offset to first day of week (Monday) */
4338  day0 = j2day(day4 - 1);
4339 
4340  /*
4341  * We need the first week containing a Thursday, otherwise this day falls
4342  * into the previous year for purposes of counting weeks
4343  */
4344  if (dayn < day4 - day0)
4345  {
4346  day4 = date2j(year - 1, 1, 4);
4347 
4348  /* day0 == offset to first day of week (Monday) */
4349  day0 = j2day(day4 - 1);
4350  }
4351 
4352  result = (dayn - (day4 - day0)) / 7 + 1;
4353 
4354  /*
4355  * Sometimes the last few days in a year will fall into the first week of
4356  * the next year, so check for this.
4357  */
4358  if (result >= 52)
4359  {
4360  day4 = date2j(year + 1, 1, 4);
4361 
4362  /* day0 == offset to first day of week (Monday) */
4363  day0 = j2day(day4 - 1);
4364 
4365  if (dayn >= day4 - day0)
4366  result = (dayn - (day4 - day0)) / 7 + 1;
4367  }
4368 
4369  return (int) result;
4370 }
double float8
Definition: c.h:492
int j2day(int date)
Definition: datetime.c:329
int date2j(int y, int m, int d)
Definition: datetime.c:269

◆ date2isoyear()

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

Definition at line 4378 of file timestamp.c.

References date2j(), and j2day().

Referenced by date2isoyearday(), DCH_to_char(), timestamp_part(), and timestamptz_part().

4379 {
4380  float8 result;
4381  int day0,
4382  day4,
4383  dayn;
4384 
4385  /* current day */
4386  dayn = date2j(year, mon, mday);
4387 
4388  /* fourth day of current year */
4389  day4 = date2j(year, 1, 4);
4390 
4391  /* day0 == offset to first day of week (Monday) */
4392  day0 = j2day(day4 - 1);
4393 
4394  /*
4395  * We need the first week containing a Thursday, otherwise this day falls
4396  * into the previous year for purposes of counting weeks
4397  */
4398  if (dayn < day4 - day0)
4399  {
4400  day4 = date2j(year - 1, 1, 4);
4401 
4402  /* day0 == offset to first day of week (Monday) */
4403  day0 = j2day(day4 - 1);
4404 
4405  year--;
4406  }
4407 
4408  result = (dayn - (day4 - day0)) / 7 + 1;
4409 
4410  /*
4411  * Sometimes the last few days in a year will fall into the first week of
4412  * the next year, so check for this.
4413  */
4414  if (result >= 52)
4415  {
4416  day4 = date2j(year + 1, 1, 4);
4417 
4418  /* day0 == offset to first day of week (Monday) */
4419  day0 = j2day(day4 - 1);
4420 
4421  if (dayn >= day4 - day0)
4422  year++;
4423  }
4424 
4425  return year;
4426 }
double float8
Definition: c.h:492
int j2day(int date)
Definition: datetime.c:329
int date2j(int y, int m, int d)
Definition: datetime.c:269

◆ date2isoyearday()

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

Definition at line 4435 of file timestamp.c.

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

Referenced by DCH_to_char().

4436 {
4437  return date2j(year, mon, mday) - isoweek2j(date2isoyear(year, mon, mday), 1) + 1;
4438 }
int date2isoyear(int year, int mon, int mday)
Definition: timestamp.c:4378
int isoweek2j(int year, int week)
Definition: timestamp.c:4273
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 1764 of file timestamp.c.

References USECS_PER_HOUR, USECS_PER_MINUTE, and USECS_PER_SEC.

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

1765 {
1766  TimeOffset time;
1767 
1768  time = jd;
1769 
1770  *hour = time / USECS_PER_HOUR;
1771  time -= (*hour) * USECS_PER_HOUR;
1772  *min = time / USECS_PER_MINUTE;
1773  time -= (*min) * USECS_PER_MINUTE;
1774  *sec = time / USECS_PER_SEC;
1775  *fsec = time - (*sec * USECS_PER_SEC);
1776 } /* 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 1583 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(), CheckPointBuffers(), 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(), get_role_password(), GetCurrentTransactionStopTimestamp(), GetReplicationApplyDelay(), GetSnapshotCurrentTimestamp(), handle_sig_alarm(), heap_vacuum_rel(), InitProcessGlobals(), launcher_determine_sleep(), log_disconnections(), LogCheckpointEnd(), logicalrep_worker_launch(), LogicalRepApplyLoop(), maybe_start_bgworkers(), perform_base_backup(), pgfdw_cancel_query(), pgfdw_exec_cleanup_query(), pgfdw_get_cleanup_result(), pgstat_read_statsfiles(), pgstat_recv_inquiry(), pgstat_recv_resetsharedcounter(), pgstat_recv_resetsinglecounter(), pgstat_report_activity(), pgstat_report_analyze(), pgstat_report_autovac(), pgstat_report_checksum_failures_in_db(), pgstat_report_vacuum(), pgstat_send_archiver(), 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(), XLogRestorePoint(), XLogSendPhysical(), XLogWalRcvSendHSFeedback(), and XLogWalRcvSendReply().

1584 {
1585  TimestampTz result;
1586  struct timeval tp;
1587 
1588  gettimeofday(&tp, NULL);
1589 
1590  result = (TimestampTz) tp.tv_sec -
1592  result = (result * USECS_PER_SEC) + tp.tv_usec;
1593 
1594  return result;
1595 }
int gettimeofday(struct timeval *tp, struct timezone *tzp)
Definition: gettimeofday.c:105
#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 2017 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().

2018 {
2019  struct pg_tm *t0;
2020  pg_time_t epoch = 0;
2021 
2022  t0 = pg_gmtime(&epoch);
2023 
2024  if (t0 == NULL)
2025  elog(ERROR, "could not convert epoch to timestamp: %m");
2026 
2027  tm->tm_year = t0->tm_year;
2028  tm->tm_mon = t0->tm_mon;
2029  tm->tm_mday = t0->tm_mday;
2030  tm->tm_hour = t0->tm_hour;
2031  tm->tm_min = t0->tm_min;
2032  tm->tm_sec = t0->tm_sec;
2033 
2034  tm->tm_year += 1900;
2035  tm->tm_mon++;
2036 }
int64 pg_time_t
Definition: pgtime.h:23
struct pg_tm * pg_gmtime(const pg_time_t *timep)
Definition: localtime.c:1420
int tm_hour
Definition: pgtime.h:29
Definition: pgtime.h:25
#define ERROR
Definition: elog.h:43
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:228
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 1601 of file timestamp.c.

References AdjustTimestampForTypmod(), and GetCurrentTransactionStartTimestamp().

Referenced by ExecEvalSQLValueFunction().

1602 {
1603  TimestampTz ts;
1604 
1606  if (typmod >= 0)
1607  AdjustTimestampForTypmod(&ts, typmod);
1608  return ts;
1609 }
int64 TimestampTz
Definition: timestamp.h:39
void AdjustTimestampForTypmod(Timestamp *time, int32 typmod)
Definition: timestamp.c:391
TimestampTz GetCurrentTransactionStartTimestamp(void)
Definition: xact.c:782

◆ GetSQLLocalTimestamp()

Timestamp GetSQLLocalTimestamp ( int32  typmod)

Definition at line 1615 of file timestamp.c.

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

Referenced by ExecEvalSQLValueFunction().

1616 {
1617  Timestamp ts;
1618 
1620  if (typmod >= 0)
1621  AdjustTimestampForTypmod(&ts, typmod);
1622  return ts;
1623 }
void AdjustTimestampForTypmod(Timestamp *time, int32 typmod)
Definition: timestamp.c:391
static Timestamp timestamptz2timestamp(TimestampTz timestamp)
Definition: timestamp.c:5260
int64 Timestamp
Definition: timestamp.h:38
TimestampTz GetCurrentTransactionStartTimestamp(void)
Definition: xact.c:782

◆ interval2tm()

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

Definition at line 1935 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(), interval_to_char(), and interval_trunc().

1936 {
1937  TimeOffset time;
1938  TimeOffset tfrac;
1939 
1940  tm->tm_year = span.month / MONTHS_PER_YEAR;
1941  tm->tm_mon = span.month % MONTHS_PER_YEAR;
1942  tm->tm_mday = span.day;
1943  time = span.time;
1944 
1945  tfrac = time / USECS_PER_HOUR;
1946  time -= tfrac * USECS_PER_HOUR;
1947  tm->tm_hour = tfrac;
1948  if (!SAMESIGN(tm->tm_hour, tfrac))
1949  ereport(ERROR,
1950  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1951  errmsg("interval out of range")));
1952  tfrac = time / USECS_PER_MINUTE;
1953  time -= tfrac * USECS_PER_MINUTE;
1954  tm->tm_min = tfrac;
1955  tfrac = time / USECS_PER_SEC;
1956  *fsec = time - (tfrac * USECS_PER_SEC);
1957  tm->tm_sec = tfrac;
1958 
1959  return 0;
1960 }
#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:608
int32 day
Definition: timestamp.h:47
#define MONTHS_PER_YEAR
Definition: timestamp.h:69
#define ERROR
Definition: elog.h:43
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 ereport(elevel, rest)
Definition: elog.h:141
#define SAMESIGN(a, b)
Definition: timestamp.c:46
int32 month
Definition: timestamp.h:48
int64 TimeOffset
Definition: timestamp.h:40
int tm_year
Definition: pgtime.h:32
int errmsg(const char *fmt,...)
Definition: elog.c:822
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 4293 of file timestamp.c.

References isoweek2j(), and j2date().

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

4294 {
4295  j2date(isoweek2j(*year, woy), year, mon, mday);
4296 }
void j2date(int jd, int *year, int *month, int *day)
Definition: datetime.c:294
int isoweek2j(int year, int week)
Definition: timestamp.c:4273

◆ isoweek2j()

int isoweek2j ( int  year,
int  week 
)

Definition at line 4273 of file timestamp.c.

References date2j(), and j2day().

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

4274 {
4275  int day0,
4276  day4;
4277 
4278  /* fourth day of current year */
4279  day4 = date2j(year, 1, 4);
4280 
4281  /* day0 == offset to first day of week (Monday) */
4282  day0 = j2day(day4 - 1);
4283 
4284  return ((week - 1) * 7) + (day4 - day0);
4285 }
int j2day(int date)
Definition: datetime.c:329
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 4306 of file timestamp.c.

References isoweek2j(), and j2date().

Referenced by do_to_timestamp().

4307 {
4308  int jday;
4309 
4310  jday = isoweek2j(*year, isoweek);
4311  /* convert Gregorian week start (Sunday=1) to ISO week start (Monday=1) */
4312  if (wday > 1)
4313  jday += wday - 2;
4314  else
4315  jday += 6;
4316  j2date(jday, year, mon, mday);
4317 }
void j2date(int jd, int *year, int *month, int *day)
Definition: datetime.c:294
int isoweek2j(int year, int week)
Definition: timestamp.c:4273

◆ SetEpochTimestamp()

Timestamp SetEpochTimestamp ( void  )

Definition at line 2039 of file timestamp.c.

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

Referenced by dttofmtasc_replace(), PGTYPEStimestamp_from_asc(), timestamp_in(), timestamp_part(), timestamptz_in(), and timestamptz_part().

2040 {
2041  Timestamp dt;
2042  struct pg_tm tt,
2043  *tm = &tt;
2044 
2045  GetEpochTime(tm);
2046  /* we don't bother to test for failure ... */
2047  tm2timestamp(tm, 0, NULL, &dt);
2048 
2049  return dt;
2050 } /* SetEpochTimestamp() */
Definition: pgtime.h:25
static struct pg_tm tm
Definition: localtime.c:108
int64 Timestamp
Definition: timestamp.h:38
int tm2timestamp(struct pg_tm *tm, fsec_t fsec, int *tzp, Timestamp *result)
Definition: timestamp.c:1887
void GetEpochTime(struct pg_tm *tm)
Definition: timestamp.c:2017

◆ time_t_to_timestamptz()

TimestampTz time_t_to_timestamptz ( pg_time_t  tm)

Definition at line 1703 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().

1704 {
1705  TimestampTz result;
1706 
1707  result = (TimestampTz) tm -
1709  result *= USECS_PER_SEC;
1710 
1711  return result;
1712 }
#define USECS_PER_SEC
Definition: timestamp.h:94
int64 TimestampTz
Definition: timestamp.h:39
static struct pg_tm tm
Definition: localtime.c:108
#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 5198 of file timestamp.c.

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

Referenced by cmpTimestampToTimestampTz(), and timestamp2timestamptz().

5199 {
5200  TimestampTz result;
5201  struct pg_tm tt,
5202  *tm = &tt;
5203  fsec_t fsec;
5204  int tz;
5205 
5207  return timestamp;
5208 
5209  if (!timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL))
5210  {
5212 
5213  result = dt2local(timestamp, -tz);
5214 
5215  if (IS_VALID_TIMESTAMP(result))
5216  {
5217  return result;
5218  }
5219  else if (overflow)
5220  {
5221  if (result < MIN_TIMESTAMP)
5222  *overflow = -1;
5223  else
5224  {
5225  Assert(result >= END_TIMESTAMP);
5226  *overflow = 1;
5227  }
5228  return (TimestampTz) 0;
5229  }
5230  }
5231 
5232  ereport(ERROR,
5233  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
5234  errmsg("timestamp out of range")));
5235 
5236  return 0;
5237 }
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:1791
int errcode(int sqlerrcode)
Definition: elog.c:608
Definition: pgtime.h:25
static struct pg_tm tm
Definition: localtime.c:108
#define TIMESTAMP_NOT_FINITE(j)
Definition: timestamp.h:122
#define ERROR
Definition: elog.h:43
int DetermineTimeZoneOffset(struct pg_tm *tm, pg_tz *tzp)
Definition: datetime.c:1440
int32 fsec_t
Definition: timestamp.h:41
#define MIN_TIMESTAMP
Definition: timestamp.h:184
#define ereport(elevel, rest)
Definition: elog.h:141
#define END_TIMESTAMP
Definition: timestamp.h:186
#define Assert(condition)
Definition: c.h:733
#define IS_VALID_TIMESTAMP(t)
Definition: timestamp.h:195
int errmsg(const char *fmt,...)
Definition: elog.c:822
static Timestamp dt2local(Timestamp dt, int timezone)
Definition: timestamp.c:1985

◆ timestamp2tm()

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

Definition at line 1791 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(), GetCurrentDateTime(), GetCurrentTimeUsec(), GetSQLCurrentDate(), GetSQLCurrentTime(), GetSQLLocalTime(), 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(), timestamp_pl_interval(), timestamp_recv(), timestamp_time(), timestamp_to_char(), timestamp_trunc(), timestamp_zone(), timestamptz2timestamp(), timestamptz_age(), timestamptz_date(), timestamptz_out(), timestamptz_part(), timestamptz_pl_interval(), timestamptz_recv(), timestamptz_time(), timestamptz_timetz(), timestamptz_to_char(), timestamptz_to_str(), timestamptz_trunc_internal(), and timestamptz_zone().

1792 {
1793  Timestamp date;
1794  Timestamp time;
1795  pg_time_t utime;
1796 
1797  /* Use session timezone if caller asks for default */
1798  if (attimezone == NULL)
1799  attimezone = session_timezone;
1800 
1801  time = dt;
1802  TMODULO(time, date, USECS_PER_DAY);
1803 
1804  if (time < INT64CONST(0))
1805  {
1806  time += USECS_PER_DAY;
1807  date -= 1;
1808  }
1809 
1810  /* add offset to go from J2000 back to standard Julian date */
1811  date += POSTGRES_EPOCH_JDATE;
1812 
1813  /* Julian day routine does not work for negative Julian days */
1814  if (date < 0 || date > (Timestamp) INT_MAX)
1815  return -1;
1816 
1817  j2date((int) date, &tm->tm_year, &tm->tm_mon, &tm->tm_mday);
1818  dt2time(time, &tm->tm_hour, &tm->tm_min, &tm->tm_sec, fsec);
1819 
1820  /* Done if no TZ conversion wanted */
1821  if (tzp == NULL)
1822  {
1823  tm->tm_isdst = -1;
1824  tm->tm_gmtoff = 0;
1825  tm->tm_zone = NULL;
1826  if (tzn != NULL)
1827  *tzn = NULL;
1828  return 0;
1829  }
1830 
1831  /*
1832  * If the time falls within the range of pg_time_t, use pg_localtime() to
1833  * rotate to the local time zone.
1834  *
1835  * First, convert to an integral timestamp, avoiding possibly
1836  * platform-specific roundoff-in-wrong-direction errors, and adjust to
1837  * Unix epoch. Then see if we can convert to pg_time_t without loss. This
1838  * coding avoids hardwiring any assumptions about the width of pg_time_t,
1839  * so it should behave sanely on machines without int64.
1840  */
1841  dt = (dt - *fsec) / USECS_PER_SEC +
1843  utime = (pg_time_t) dt;
1844  if ((Timestamp) utime == dt)
1845  {
1846  struct pg_tm *tx = pg_localtime(&utime, attimezone);
1847 
1848  tm->tm_year = tx->tm_year + 1900;
1849  tm->tm_mon = tx->tm_mon + 1;
1850  tm->tm_mday = tx->tm_mday;
1851  tm->tm_hour = tx->tm_hour;
1852  tm->tm_min = tx->tm_min;
1853  tm->tm_sec = tx->tm_sec;
1854  tm->tm_isdst = tx->tm_isdst;
1855  tm->tm_gmtoff = tx->tm_gmtoff;
1856  tm->tm_zone = tx->tm_zone;
1857  *tzp = -tm->tm_gmtoff;
1858  if (tzn != NULL)
1859  *tzn = tm->tm_zone;
1860  }
1861  else
1862  {
1863  /*
1864  * When out of range of pg_time_t, treat as GMT
1865  */
1866  *tzp = 0;
1867  /* Mark this as *no* time zone available */
1868  tm->tm_isdst = -1;
1869  tm->tm_gmtoff = 0;
1870  tm->tm_zone = NULL;
1871  if (tzn != NULL)
1872  *tzn = NULL;
1873  }
1874 
1875  return 0;
1876 }
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:1764
struct pg_tm * pg_localtime(const pg_time_t *timep, const pg_tz *tz)
Definition: localtime.c:1375
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()

◆ TimestampDifference()

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

Definition at line 1657 of file timestamp.c.

References start_time, and USECS_PER_SEC.

Referenced by autoprewarm_main(), check_log_duration(), DetermineSleepTime(), GetReplicationApplyDelay(), GetReplicationTransferLatency(), heap_vacuum_rel(), launcher_determine_sleep(), log_disconnections(), LogCheckpointEnd(), pgfdw_get_cleanup_result(), ProcSleep(), recoveryApplyDelay(), schedule_alarm(), WaitForWALToBecomeAvailable(), and WalSndComputeSleeptime().

1659 {
1660  TimestampTz diff = stop_time - start_time;
1661 
1662  if (diff <= 0)
1663  {
1664  *secs = 0;
1665  *microsecs = 0;
1666  }
1667  else
1668  {
1669  *secs = (long) (diff / USECS_PER_SEC);
1670  *microsecs = (int) (diff % USECS_PER_SEC);
1671  }
1672 }
#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()

◆ TimestampTimestampTzRequiresRewrite()

bool TimestampTimestampTzRequiresRewrite ( void  )

Definition at line 5170 of file timestamp.c.

References pg_get_timezone_offset(), and session_timezone.

Referenced by ATColumnChangeRequiresRewrite().

5171 {
5172  long offset;
5173 
5174  if (pg_get_timezone_offset(session_timezone, &offset) && offset == 0)
5175  return false;
5176  return true;
5177 }
PGDLLIMPORT pg_tz * session_timezone
Definition: pgtz.c:28
bool pg_get_timezone_offset(const pg_tz *tz, long int *gmtoff)
Definition: localtime.c:1866

◆ timestamptz_to_str()

const char* timestamptz_to_str ( TimestampTz  t)

Definition at line 1743 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(), commit_ts_desc(), CreateRestartPoint(), pg_decode_commit_txn(), pgstat_recv_inquiry(), ProcessStandbyHSFeedbackMessage(), ProcessStandbyReplyMessage(), ProcessWalSndrMessage(), recoveryStopsAfter(), recoveryStopsBefore(), StartupXLOG(), xact_desc_abort(), xact_desc_commit(), xact_desc_prepare(), and xlog_desc().

1744 {
1745  static char buf[MAXDATELEN + 1];
1746  int tz;
1747  struct pg_tm tt,
1748  *tm = &tt;
1749  fsec_t fsec;
1750  const char *tzn;
1751 
1752  if (TIMESTAMP_NOT_FINITE(t))
1753  EncodeSpecialTimestamp(t, buf);
1754  else if (timestamp2tm(t, &tz, tm, &fsec, &tzn, NULL) == 0)
1755  EncodeDateTime(tm, fsec, true, tz, tzn, USE_ISO_DATES, buf);
1756  else
1757  strlcpy(buf, "(timestamp out of range)", sizeof(buf));
1758 
1759  return buf;
1760 }
#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:1791
void EncodeSpecialTimestamp(Timestamp dt, char *str)
Definition: timestamp.c:1536
Definition: pgtime.h:25
#define USE_ISO_DATES
Definition: miscadmin.h:214
static struct pg_tm tm
Definition: localtime.c:108
#define TIMESTAMP_NOT_FINITE(j)
Definition: timestamp.h:122
static char * buf
Definition: pg_test_fsync.c:67
void EncodeDateTime(struct pg_tm *tm, fsec_t fsec, bool print_tz, int tz, const char *tzn, int style, char *str)
Definition: datetime.c:3972
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 1725 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().

1726 {
1727  pg_time_t result;
1728 
1729  result = (pg_time_t) (t / USECS_PER_SEC +
1731 
1732  return result;
1733 }
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 1963 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().

1964 {
1965  double total_months = (double) tm->tm_year * MONTHS_PER_YEAR + tm->tm_mon;
1966 
1967  if (total_months > INT_MAX || total_months < INT_MIN)
1968  return -1;
1969  span->month = total_months;
1970  span->day = tm->tm_mday;
1971  span->time = (((((tm->tm_hour * INT64CONST(60)) +
1972  tm->tm_min) * INT64CONST(60)) +
1973  tm->tm_sec) * USECS_PER_SEC) + fsec;
1974 
1975  return 0;
1976 }
#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 1887 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().

1888 {
1889  TimeOffset date;
1890  TimeOffset time;
1891 
1892  /* Prevent overflow in Julian-day routines */
1893  if (!IS_VALID_JULIAN(tm->tm_year, tm->tm_mon, tm->tm_mday))
1894  {
1895  *result = 0; /* keep compiler quiet */
1896  return -1;
1897  }
1898 
1899  date = date2j(tm->tm_year, tm->tm_mon, tm->tm_mday) - POSTGRES_EPOCH_JDATE;
1900  time = time2t(tm->tm_hour, tm->tm_min, tm->tm_sec, fsec);
1901 
1902  *result = date * USECS_PER_DAY + time;
1903  /* check for major overflow */
1904  if ((*result - time) / USECS_PER_DAY != date)
1905  {
1906  *result = 0; /* keep compiler quiet */
1907  return -1;
1908  }
1909  /* check for just-barely overflow (okay except time-of-day wraps) */
1910  /* caution: we want to allow 1999-12-31 24:00:00 */
1911  if ((*result < 0 && date > 0) ||
1912  (*result > 0 && date < -1))
1913  {
1914  *result = 0; /* keep compiler quiet */
1915  return -1;
1916  }
1917  if (tzp != NULL)
1918  *result = dt2local(*result, -(*tzp));
1919 
1920  /* final range check catches just-out-of-range timestamps */
1921  if (!IS_VALID_TIMESTAMP(*result))
1922  {
1923  *result = 0; /* keep compiler quiet */
1924  return -1;
1925  }
1926 
1927  return 0;
1928 }
static TimeOffset time2t(const int hour, const int min, const int sec, const fsec_t fsec)
Definition: timestamp.c:1979
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:1985
int tm_sec
Definition: pgtime.h:27
int tm_min
Definition: pgtime.h:28

Variable Documentation

◆ PgReloadTime

TimestampTz PgReloadTime

Definition at line 52 of file timestamp.c.

Referenced by pg_conf_load_time(), and PostmasterMarkPIDForWorkerNotify().

◆ PgStartTime