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

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

Referenced by anytimestamp_typmodin(), and transformSQLValueFunction().

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

◆ date2isoweek()

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

Definition at line 4317 of file timestamp.c.

References date2j(), and j2day().

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

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

◆ date2isoyear()

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

Definition at line 4372 of file timestamp.c.

References date2j(), and j2day().

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

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

◆ date2isoyearday()

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

Definition at line 4429 of file timestamp.c.

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

Referenced by DCH_to_char().

4430 {
4431  return date2j(year, mon, mday) - isoweek2j(date2isoyear(year, mon, mday), 1) + 1;
4432 }
int date2isoyear(int year, int mon, int mday)
Definition: timestamp.c:4372
int isoweek2j(int year, int week)
Definition: timestamp.c:4266
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 1757 of file timestamp.c.

References USECS_PER_HOUR, USECS_PER_MINUTE, and USECS_PER_SEC.

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

1758 {
1759  TimeOffset time;
1760 
1761  time = jd;
1762 
1763  *hour = time / USECS_PER_HOUR;
1764  time -= (*hour) * USECS_PER_HOUR;
1765  *min = time / USECS_PER_MINUTE;
1766  time -= (*min) * USECS_PER_MINUTE;
1767  *sec = time / USECS_PER_SEC;
1768  *fsec = time - (*sec * USECS_PER_SEC);
1769 } /* 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 1574 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_recv_resetslrucounter(), 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().

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

◆ GetEpochTime()

void GetEpochTime ( struct pg_tm tm)

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

2011 {
2012  struct pg_tm *t0;
2013  pg_time_t epoch = 0;
2014 
2015  t0 = pg_gmtime(&epoch);
2016 
2017  if (t0 == NULL)
2018  elog(ERROR, "could not convert epoch to timestamp: %m");
2019 
2020  tm->tm_year = t0->tm_year;
2021  tm->tm_mon = t0->tm_mon;
2022  tm->tm_mday = t0->tm_mday;
2023  tm->tm_hour = t0->tm_hour;
2024  tm->tm_min = t0->tm_min;
2025  tm->tm_sec = t0->tm_sec;
2026 
2027  tm->tm_year += 1900;
2028  tm->tm_mon++;
2029 }
int64 pg_time_t
Definition: pgtime.h:23
struct pg_tm * pg_gmtime(const pg_time_t *timep)
Definition: localtime.c:1387
int tm_hour
Definition: pgtime.h:29
Definition: pgtime.h:25
#define ERROR
Definition: elog.h: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:214
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 1592 of file timestamp.c.

References AdjustTimestampForTypmod(), and GetCurrentTransactionStartTimestamp().

Referenced by ExecEvalSQLValueFunction().

1593 {
1594  TimestampTz ts;
1595 
1597  if (typmod >= 0)
1598  AdjustTimestampForTypmod(&ts, typmod);
1599  return ts;
1600 }
int64 TimestampTz
Definition: timestamp.h:39
void AdjustTimestampForTypmod(Timestamp *time, int32 typmod)
Definition: timestamp.c:392
TimestampTz GetCurrentTransactionStartTimestamp(void)
Definition: xact.c:783

◆ GetSQLLocalTimestamp()

Timestamp GetSQLLocalTimestamp ( int32  typmod)

Definition at line 1606 of file timestamp.c.

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

Referenced by ExecEvalSQLValueFunction().

1607 {
1608  Timestamp ts;
1609 
1611  if (typmod >= 0)
1612  AdjustTimestampForTypmod(&ts, typmod);
1613  return ts;
1614 }
void AdjustTimestampForTypmod(Timestamp *time, int32 typmod)
Definition: timestamp.c:392
static Timestamp timestamptz2timestamp(TimestampTz timestamp)
Definition: timestamp.c:5244
int64 Timestamp
Definition: timestamp.h:38
TimestampTz GetCurrentTransactionStartTimestamp(void)
Definition: xact.c:783

◆ interval2tm()

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

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

1929 {
1930  TimeOffset time;
1931  TimeOffset tfrac;
1932 
1933  tm->tm_year = span.month / MONTHS_PER_YEAR;
1934  tm->tm_mon = span.month % MONTHS_PER_YEAR;
1935  tm->tm_mday = span.day;
1936  time = span.time;
1937 
1938  tfrac = time / USECS_PER_HOUR;
1939  time -= tfrac * USECS_PER_HOUR;
1940  tm->tm_hour = tfrac;
1941  if (!SAMESIGN(tm->tm_hour, tfrac))
1942  ereport(ERROR,
1943  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1944  errmsg("interval out of range")));
1945  tfrac = time / USECS_PER_MINUTE;
1946  time -= tfrac * USECS_PER_MINUTE;
1947  tm->tm_min = tfrac;
1948  tfrac = time / USECS_PER_SEC;
1949  *fsec = time - (tfrac * USECS_PER_SEC);
1950  tm->tm_sec = tfrac;
1951 
1952  return 0;
1953 }
#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:610
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 SAMESIGN(a, b)
Definition: timestamp.c:47
int32 month
Definition: timestamp.h:48
int64 TimeOffset
Definition: timestamp.h:40
#define ereport(elevel,...)
Definition: elog.h:144
int tm_year
Definition: pgtime.h:32
int errmsg(const char *fmt,...)
Definition: elog.c:824
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 4286 of file timestamp.c.

References isoweek2j(), and j2date().

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

4287 {
4288  j2date(isoweek2j(*year, woy), year, mon, mday);
4289 }
void j2date(int jd, int *year, int *month, int *day)
Definition: datetime.c:294
int isoweek2j(int year, int week)
Definition: timestamp.c:4266

◆ isoweek2j()

int isoweek2j ( int  year,
int  week 
)

Definition at line 4266 of file timestamp.c.

References date2j(), and j2day().

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

4267 {
4268  int day0,
4269  day4;
4270 
4271  /* fourth day of current year */
4272  day4 = date2j(year, 1, 4);
4273 
4274  /* day0 == offset to first day of week (Monday) */
4275  day0 = j2day(day4 - 1);
4276 
4277  return ((week - 1) * 7) + (day4 - day0);
4278 }
int j2day(int date)
Definition: datetime.c:327
int date2j(int y, int m, int d)
Definition: datetime.c:269

◆ isoweekdate2date()

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

Definition at line 4299 of file timestamp.c.

References isoweek2j(), and j2date().

Referenced by do_to_timestamp().

4300 {
4301  int jday;
4302 
4303  jday = isoweek2j(*year, isoweek);
4304  /* convert Gregorian week start (Sunday=1) to ISO week start (Monday=1) */
4305  if (wday > 1)
4306  jday += wday - 2;
4307  else
4308  jday += 6;
4309  j2date(jday, year, mon, mday);
4310 }
void j2date(int jd, int *year, int *month, int *day)
Definition: datetime.c:294
int isoweek2j(int year, int week)
Definition: timestamp.c:4266

◆ SetEpochTimestamp()

Timestamp SetEpochTimestamp ( void  )

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

2033 {
2034  Timestamp dt;
2035  struct pg_tm tt,
2036  *tm = &tt;
2037 
2038  GetEpochTime(tm);
2039  /* we don't bother to test for failure ... */
2040  tm2timestamp(tm, 0, NULL, &dt);
2041 
2042  return dt;
2043 } /* SetEpochTimestamp() */
Definition: pgtime.h:25
static struct pg_tm tm
Definition: localtime.c:102
int64 Timestamp
Definition: timestamp.h:38
int tm2timestamp(struct pg_tm *tm, fsec_t fsec, int *tzp, Timestamp *result)
Definition: timestamp.c:1880
void GetEpochTime(struct pg_tm *tm)
Definition: timestamp.c:2010

◆ time_t_to_timestamptz()

TimestampTz time_t_to_timestamptz ( pg_time_t  tm)

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

1695 {
1696  TimestampTz result;
1697 
1698  result = (TimestampTz) tm -
1700  result *= USECS_PER_SEC;
1701 
1702  return result;
1703 }
#define USECS_PER_SEC
Definition: timestamp.h:94
int64 TimestampTz
Definition: timestamp.h:39
static struct pg_tm tm
Definition: localtime.c:102
#define SECS_PER_DAY
Definition: timestamp.h:86
#define UNIX_EPOCH_JDATE
Definition: timestamp.h:162
#define POSTGRES_EPOCH_JDATE
Definition: timestamp.h:163

◆ timestamp2timestamptz_opt_overflow()

TimestampTz timestamp2timestamptz_opt_overflow ( Timestamp  timestamp,
int *  overflow 
)

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

5183 {
5184  TimestampTz result;
5185  struct pg_tm tt,
5186  *tm = &tt;
5187  fsec_t fsec;
5188  int tz;
5189 
5191  return timestamp;
5192 
5193  if (!timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL))
5194  {
5196 
5197  result = dt2local(timestamp, -tz);
5198 
5199  if (IS_VALID_TIMESTAMP(result))
5200  {
5201  return result;
5202  }
5203  else if (overflow)
5204  {
5205  if (result < MIN_TIMESTAMP)
5206  *overflow = -1;
5207  else
5208  {
5209  Assert(result >= END_TIMESTAMP);
5210  *overflow = 1;
5211  }
5212  return (TimestampTz) 0;
5213  }
5214  }
5215 
5216  ereport(ERROR,
5217  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
5218  errmsg("timestamp out of range")));
5219 
5220  return 0;
5221 }
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:1784
int errcode(int sqlerrcode)
Definition: elog.c:610
Definition: pgtime.h:25
static struct pg_tm tm
Definition: localtime.c:102
#define TIMESTAMP_NOT_FINITE(j)
Definition: timestamp.h:122
#define ERROR
Definition: elog.h:43
int DetermineTimeZoneOffset(struct pg_tm *tm, pg_tz *tzp)
Definition: datetime.c:1433
int32 fsec_t
Definition: timestamp.h:41
#define MIN_TIMESTAMP
Definition: timestamp.h:184
#define END_TIMESTAMP
Definition: timestamp.h:186
#define ereport(elevel,...)
Definition: elog.h:144
#define Assert(condition)
Definition: c.h:745
#define IS_VALID_TIMESTAMP(t)
Definition: timestamp.h:195
int errmsg(const char *fmt,...)
Definition: elog.c:824
static Timestamp dt2local(Timestamp dt, int timezone)
Definition: timestamp.c:1978

◆ timestamp2tm()

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

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

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

◆ timestamp_cmp_internal()

◆ TimestampDifference()

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

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

1650 {
1651  TimestampTz diff = stop_time - start_time;
1652 
1653  if (diff <= 0)
1654  {
1655  *secs = 0;
1656  *microsecs = 0;
1657  }
1658  else
1659  {
1660  *secs = (long) (diff / USECS_PER_SEC);
1661  *microsecs = (int) (diff % USECS_PER_SEC);
1662  }
1663 }
#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 5154 of file timestamp.c.

References pg_get_timezone_offset(), and session_timezone.

Referenced by ATColumnChangeRequiresRewrite().

5155 {
5156  long offset;
5157 
5158  if (pg_get_timezone_offset(session_timezone, &offset) && offset == 0)
5159  return false;
5160  return true;
5161 }
PGDLLIMPORT pg_tz * session_timezone
Definition: pgtz.c:28
bool pg_get_timezone_offset(const pg_tz *tz, long int *gmtoff)
Definition: localtime.c:1849

◆ timestamptz_to_str()

const char* timestamptz_to_str ( TimestampTz  t)

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

1737 {
1738  static char buf[MAXDATELEN + 1];
1739  int tz;
1740  struct pg_tm tt,
1741  *tm = &tt;
1742  fsec_t fsec;
1743  const char *tzn;
1744 
1745  if (TIMESTAMP_NOT_FINITE(t))
1746  EncodeSpecialTimestamp(t, buf);
1747  else if (timestamp2tm(t, &tz, tm, &fsec, &tzn, NULL) == 0)
1748  EncodeDateTime(tm, fsec, true, tz, tzn, USE_ISO_DATES, buf);
1749  else
1750  strlcpy(buf, "(timestamp out of range)", sizeof(buf));
1751 
1752  return buf;
1753 }
#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:1784
void EncodeSpecialTimestamp(Timestamp dt, char *str)
Definition: timestamp.c:1527
Definition: pgtime.h:25
#define USE_ISO_DATES
Definition: miscadmin.h:214
static struct pg_tm tm
Definition: localtime.c:102
#define TIMESTAMP_NOT_FINITE(j)
Definition: timestamp.h:122
static char * buf
Definition: pg_test_fsync.c: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:3957
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 1716 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().

1717 {
1718  pg_time_t result;
1719 
1720  result = (pg_time_t) (t / USECS_PER_SEC +
1722 
1723  return result;
1724 }
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 1956 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().

1957 {
1958  double total_months = (double) tm->tm_year * MONTHS_PER_YEAR + tm->tm_mon;
1959 
1960  if (total_months > INT_MAX || total_months < INT_MIN)
1961  return -1;
1962  span->month = total_months;
1963  span->day = tm->tm_mday;
1964  span->time = (((((tm->tm_hour * INT64CONST(60)) +
1965  tm->tm_min) * INT64CONST(60)) +
1966  tm->tm_sec) * USECS_PER_SEC) + fsec;
1967 
1968  return 0;
1969 }
#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 1880 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().

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

Variable Documentation

◆ PgReloadTime

TimestampTz PgReloadTime

Definition at line 53 of file timestamp.c.

Referenced by pg_conf_load_time(), and PostmasterMarkPIDForWorkerNotify().

◆ PgStartTime