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)
 
void interval2itm (Interval span, struct pg_itm *itm)
 
int itm2interval (struct pg_itm *itm, Interval *span)
 
int itmin2interval (struct pg_itm_in *itm_in, Interval *span)
 
Timestamp SetEpochTimestamp (void)
 
void GetEpochTime (struct pg_tm *tm)
 
int timestamp_cmp_internal (Timestamp dt1, Timestamp dt2)
 
TimestampTz timestamp2timestamptz_opt_overflow (Timestamp timestamp, int *overflow)
 
int32 timestamp_cmp_timestamptz_internal (Timestamp timestampVal, TimestampTz dt2)
 
int isoweek2j (int year, int week)
 
void isoweek2date (int woy, int *year, int *mon, int *mday)
 
void isoweekdate2date (int isoweek, int wday, int *year, int *mon, int *mday)
 
int date2isoweek (int year, int mon, int mday)
 
int date2isoyear (int year, int mon, int mday)
 
int date2isoyearday (int year, int mon, int mday)
 
bool TimestampTimestampTzRequiresRewrite (void)
 

Variables

PGDLLIMPORT TimestampTz PgStartTime
 
PGDLLIMPORT TimestampTz PgReloadTime
 

Macro Definition Documentation

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

106 {
107  if (typmod < 0)
108  ereport(ERROR,
109  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
110  errmsg("TIMESTAMP(%d)%s precision must not be negative",
111  typmod, (istz ? " WITH TIME ZONE" : ""))));
112  if (typmod > MAX_TIMESTAMP_PRECISION)
113  {
115  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
116  errmsg("TIMESTAMP(%d)%s precision reduced to maximum allowed, %d",
117  typmod, (istz ? " WITH TIME ZONE" : ""),
119  typmod = MAX_TIMESTAMP_PRECISION;
120  }
121 
122  return typmod;
123 }
#define MAX_TIMESTAMP_PRECISION
Definition: timestamp.h:92
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 4475 of file timestamp.c.

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

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

4531 {
4532  float8 result;
4533  int day0,
4534  day4,
4535  dayn;
4536 
4537  /* current day */
4538  dayn = date2j(year, mon, mday);
4539 
4540  /* fourth day of current year */
4541  day4 = date2j(year, 1, 4);
4542 
4543  /* day0 == offset to first day of week (Monday) */
4544  day0 = j2day(day4 - 1);
4545 
4546  /*
4547  * We need the first week containing a Thursday, otherwise this day falls
4548  * into the previous year for purposes of counting weeks
4549  */
4550  if (dayn < day4 - day0)
4551  {
4552  day4 = date2j(year - 1, 1, 4);
4553 
4554  /* day0 == offset to first day of week (Monday) */
4555  day0 = j2day(day4 - 1);
4556 
4557  year--;
4558  }
4559 
4560  result = (dayn - (day4 - day0)) / 7 + 1;
4561 
4562  /*
4563  * Sometimes the last few days in a year will fall into the first week of
4564  * the next year, so check for this.
4565  */
4566  if (result >= 52)
4567  {
4568  day4 = date2j(year + 1, 1, 4);
4569 
4570  /* day0 == offset to first day of week (Monday) */
4571  day0 = j2day(day4 - 1);
4572 
4573  if (dayn >= day4 - day0)
4574  year++;
4575  }
4576 
4577  return year;
4578 }

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

4588 {
4589  return date2j(year, mon, mday) - isoweek2j(date2isoyear(year, mon, mday), 1) + 1;
4590 }
int isoweek2j(int year, int week)
Definition: timestamp.c:4424
int date2isoyear(int year, int mon, int mday)
Definition: timestamp.c:4530

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

1790 {
1791  TimeOffset time;
1792 
1793  time = jd;
1794 
1795  *hour = time / USECS_PER_HOUR;
1796  time -= (*hour) * USECS_PER_HOUR;
1797  *min = time / USECS_PER_MINUTE;
1798  time -= (*min) * USECS_PER_MINUTE;
1799  *sec = time / USECS_PER_SEC;
1800  *fsec = time - (*sec * USECS_PER_SEC);
1801 } /* dt2time() */
#define USECS_PER_HOUR
Definition: timestamp.h:131
#define USECS_PER_SEC
Definition: timestamp.h:133
#define USECS_PER_MINUTE
Definition: timestamp.h:132
int64 TimeOffset
Definition: timestamp.h:40

References USECS_PER_HOUR, USECS_PER_MINUTE, and USECS_PER_SEC.

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

◆ GetCurrentTimestamp()

TimestampTz GetCurrentTimestamp ( void  )

Definition at line 1574 of file timestamp.c.

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 }
int64 TimestampTz
Definition: timestamp.h:39
#define UNIX_EPOCH_JDATE
Definition: timestamp.h:201
#define SECS_PER_DAY
Definition: timestamp.h:125
#define POSTGRES_EPOCH_JDATE
Definition: timestamp.h:202
int gettimeofday(struct timeval *tp, 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(), AutoVacLauncherMain(), BackgroundWriterMain(), bbsink_copystream_archive_contents(), bbsink_copystream_end_archive(), bbsink_copystream_new(), bbsink_throttle_begin_backup(), begin_startup_progress_phase(), check_log_duration(), CheckPointGuts(), CleanupBackgroundWorker(), clock_timestamp(), CreateCheckPoint(), CreateEndOfRecoveryRecord(), CreateOverwriteContrecordRecord(), CreateRestartPoint(), DetermineSleepTime(), disable_timeout(), disable_timeouts(), do_analyze_rel(), do_start_bgworker(), do_start_worker(), drandom(), enable_timeout_after(), enable_timeout_at(), enable_timeout_every(), enable_timeouts(), entry_reset(), get_role_password(), GetCurrentTransactionStopTimestamp(), GetReplicationApplyDelay(), GetSnapshotCurrentTimestamp(), handle_sig_alarm(), has_startup_progress_timeout_expired(), heap_vacuum_rel(), InitProcessGlobals(), launcher_determine_sleep(), LockBufferForCleanup(), log_disconnections(), LogCheckpointEnd(), logicalrep_worker_launch(), LogicalRepApplyLoop(), maybe_start_bgworkers(), PerformWalRecovery(), pgfdw_cancel_query(), pgfdw_exec_cleanup_query(), pgfdw_get_cleanup_result(), pgss_shmem_startup(), pgstat_build_snapshot(), pgstat_report_activity(), pgstat_report_analyze(), pgstat_report_archiver(), pgstat_report_autovac(), pgstat_report_checksum_failures_in_db(), pgstat_report_vacuum(), pgstat_reset(), pgstat_reset_after_failure(), pgstat_reset_counters(), pgstat_reset_of_kind(), pgstat_reset_slru(), PostgresSingleUserMain(), PostmasterMain(), PrepareTransaction(), process_syncing_tables_for_apply(), ProcessPendingWrites(), ProcessRepliesIfAny(), ProcessStandbyReplyMessage(), ProcessWalSndrMessage(), ProcSleep(), rebuild_database_list(), RecordTransactionAbort(), RecordTransactionAbortPrepared(), RecordTransactionCommitPrepared(), recoveryApplyDelay(), reschedule_timeouts(), ResolveRecoveryConflictWithBufferPin(), ResolveRecoveryConflictWithLock(), ResolveRecoveryConflictWithVirtualXIDs(), send_feedback(), SetCurrentStatementStartTimestamp(), SetCurrentTransactionStopTimestamp(), StartTransaction(), test_pattern(), throttle(), UpdateWorkerStats(), WaitExceedsMaxStandbyDelay(), WaitForWALToBecomeAvailable(), WalReceiverMain(), WalSndKeepalive(), WalSndLoop(), WalSndUpdateProgress(), WalSndWaitForWal(), WalSndWriteData(), XLogBackgroundFlush(), XLogFileRead(), XLogPrefetchResetStats(), XLogPrefetchShmemInit(), XLogRestorePoint(), XLogSendPhysical(), XLogWalRcvSendHSFeedback(), and XLogWalRcvSendReply().

◆ GetEpochTime()

void GetEpochTime ( struct pg_tm tm)

Definition at line 2069 of file timestamp.c.

2070 {
2071  struct pg_tm *t0;
2072  pg_time_t epoch = 0;
2073 
2074  t0 = pg_gmtime(&epoch);
2075 
2076  if (t0 == NULL)
2077  elog(ERROR, "could not convert epoch to timestamp: %m");
2078 
2079  tm->tm_year = t0->tm_year;
2080  tm->tm_mon = t0->tm_mon;
2081  tm->tm_mday = t0->tm_mday;
2082  tm->tm_hour = t0->tm_hour;
2083  tm->tm_min = t0->tm_min;
2084  tm->tm_sec = t0->tm_sec;
2085 
2086  tm->tm_year += 1900;
2087  tm->tm_mon++;
2088 }
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:35
int tm_hour
Definition: pgtime.h:38
int tm_mday
Definition: pgtime.h:39
int tm_mon
Definition: pgtime.h:40
int tm_min
Definition: pgtime.h:37
int tm_sec
Definition: pgtime.h:36
int tm_year
Definition: pgtime.h:41

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

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

References AdjustTimestampForTypmod(), and GetCurrentTransactionStartTimestamp().

Referenced by ExecEvalSQLValueFunction().

◆ GetSQLLocalTimestamp()

Timestamp GetSQLLocalTimestamp ( int32  typmod)

Definition at line 1606 of file timestamp.c.

1607 {
1608  Timestamp ts;
1609 
1611  if (typmod >= 0)
1612  AdjustTimestampForTypmod(&ts, typmod);
1613  return ts;
1614 }
static Timestamp timestamptz2timestamp(TimestampTz timestamp)
Definition: timestamp.c:5618
int64 Timestamp
Definition: timestamp.h:38

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

Referenced by ExecEvalSQLValueFunction().

◆ interval2itm()

void interval2itm ( Interval  span,
struct pg_itm itm 
)

Definition at line 1962 of file timestamp.c.

1963 {
1964  TimeOffset time;
1965  TimeOffset tfrac;
1966 
1967  itm->tm_year = span.month / MONTHS_PER_YEAR;
1968  itm->tm_mon = span.month % MONTHS_PER_YEAR;
1969  itm->tm_mday = span.day;
1970  time = span.time;
1971 
1972  tfrac = time / USECS_PER_HOUR;
1973  time -= tfrac * USECS_PER_HOUR;
1974  itm->tm_hour = tfrac;
1975  tfrac = time / USECS_PER_MINUTE;
1976  time -= tfrac * USECS_PER_MINUTE;
1977  itm->tm_min = (int) tfrac;
1978  tfrac = time / USECS_PER_SEC;
1979  time -= tfrac * USECS_PER_SEC;
1980  itm->tm_sec = (int) tfrac;
1981  itm->tm_usec = (int) time;
1982 }
#define MONTHS_PER_YEAR
Definition: timestamp.h:108
int32 day
Definition: timestamp.h:51
int32 month
Definition: timestamp.h:52
TimeOffset time
Definition: timestamp.h:49
int64 tm_hour
Definition: timestamp.h:70
int tm_year
Definition: timestamp.h:73
int tm_mon
Definition: timestamp.h:72
int tm_mday
Definition: timestamp.h:71
int tm_sec
Definition: timestamp.h:68
int tm_min
Definition: timestamp.h:69
int tm_usec
Definition: timestamp.h:67

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

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

◆ isoweek2date()

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

Definition at line 4444 of file timestamp.c.

4445 {
4446  j2date(isoweek2j(*year, woy), year, mon, mday);
4447 }
void j2date(int jd, int *year, int *month, int *day)
Definition: datetime.c:309

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

4425 {
4426  int day0,
4427  day4;
4428 
4429  /* fourth day of current year */
4430  day4 = date2j(year, 1, 4);
4431 
4432  /* day0 == offset to first day of week (Monday) */
4433  day0 = j2day(day4 - 1);
4434 
4435  return ((week - 1) * 7) + (day4 - day0);
4436 }

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

4458 {
4459  int jday;
4460 
4461  jday = isoweek2j(*year, isoweek);
4462  /* convert Gregorian week start (Sunday=1) to ISO week start (Monday=1) */
4463  if (wday > 1)
4464  jday += wday - 2;
4465  else
4466  jday += 6;
4467  j2date(jday, year, mon, mday);
4468 }

References isoweek2j(), and j2date().

Referenced by do_to_timestamp().

◆ itm2interval()

int itm2interval ( struct pg_itm itm,
Interval span 
)

Definition at line 1989 of file timestamp.c.

1990 {
1991  int64 total_months = (int64) itm->tm_year * MONTHS_PER_YEAR + itm->tm_mon;
1992 
1993  if (total_months > INT_MAX || total_months < INT_MIN)
1994  return -1;
1995  span->month = (int32) total_months;
1996  span->day = itm->tm_mday;
1998  &span->time))
1999  return -1;
2000  /* tm_min, tm_sec are 32 bits, so intermediate products can't overflow */
2001  if (pg_add_s64_overflow(span->time, itm->tm_min * USECS_PER_MINUTE,
2002  &span->time))
2003  return -1;
2004  if (pg_add_s64_overflow(span->time, itm->tm_sec * USECS_PER_SEC,
2005  &span->time))
2006  return -1;
2007  if (pg_add_s64_overflow(span->time, itm->tm_usec,
2008  &span->time))
2009  return -1;
2010  return 0;
2011 }
signed int int32
Definition: c.h:440
static bool pg_mul_s64_overflow(int64 a, int64 b, int64 *result)
Definition: int.h:215
static bool pg_add_s64_overflow(int64 a, int64 b, int64 *result)
Definition: int.h:161
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:77

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

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

◆ itmin2interval()

int itmin2interval ( struct pg_itm_in itm_in,
Interval span 
)

Definition at line 2018 of file timestamp.c.

2019 {
2020  int64 total_months = (int64) itm_in->tm_year * MONTHS_PER_YEAR + itm_in->tm_mon;
2021 
2022  if (total_months > INT_MAX || total_months < INT_MIN)
2023  return -1;
2024  span->month = (int32) total_months;
2025  span->day = itm_in->tm_mday;
2026  span->time = itm_in->tm_usec;
2027  return 0;
2028 }
int tm_mon
Definition: timestamp.h:86
int tm_year
Definition: timestamp.h:87
int tm_mday
Definition: timestamp.h:85
int64 tm_usec
Definition: timestamp.h:84

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

Referenced by interval_in(), pg_timezone_abbrevs(), and pg_timezone_names().

◆ SetEpochTimestamp()

Timestamp SetEpochTimestamp ( void  )

Definition at line 2091 of file timestamp.c.

2092 {
2093  Timestamp dt;
2094  struct pg_tm tt,
2095  *tm = &tt;
2096 
2097  GetEpochTime(tm);
2098  /* we don't bother to test for failure ... */
2099  tm2timestamp(tm, 0, NULL, &dt);
2100 
2101  return dt;
2102 } /* SetEpochTimestamp() */
void GetEpochTime(struct pg_tm *tm)
Definition: timestamp.c:2069
int tm2timestamp(struct pg_tm *tm, fsec_t fsec, int *tzp, Timestamp *result)
Definition: timestamp.c:1912

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

1727 {
1728  TimestampTz result;
1729 
1730  result = (TimestampTz) tm -
1732  result *= USECS_PER_SEC;
1733 
1734  return result;
1735 }

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

5550 {
5551  TimestampTz result;
5552  struct pg_tm tt,
5553  *tm = &tt;
5554  fsec_t fsec;
5555  int tz;
5556 
5557  if (overflow)
5558  *overflow = 0;
5559 
5561  return timestamp;
5562 
5563  /* We don't expect this to fail, but check it pro forma */
5564  if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) == 0)
5565  {
5567 
5568  result = dt2local(timestamp, -tz);
5569 
5570  if (IS_VALID_TIMESTAMP(result))
5571  {
5572  return result;
5573  }
5574  else if (overflow)
5575  {
5576  if (result < MIN_TIMESTAMP)
5577  {
5578  *overflow = -1;
5579  TIMESTAMP_NOBEGIN(result);
5580  }
5581  else
5582  {
5583  *overflow = 1;
5584  TIMESTAMP_NOEND(result);
5585  }
5586  return result;
5587  }
5588  }
5589 
5590  ereport(ERROR,
5591  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
5592  errmsg("timestamp out of range")));
5593 
5594  return 0;
5595 }
int DetermineTimeZoneOffset(struct pg_tm *tm, pg_tz *tzp)
Definition: datetime.c:1647
static Timestamp dt2local(Timestamp dt, int timezone)
Definition: timestamp.c:2037
int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm *tm, fsec_t *fsec, const char **tzn, pg_tz *attimezone)
Definition: timestamp.c:1816
int32 fsec_t
Definition: timestamp.h:41
#define TIMESTAMP_NOBEGIN(j)
Definition: timestamp.h:151
#define IS_VALID_TIMESTAMP(t)
Definition: timestamp.h:234
#define TIMESTAMP_NOT_FINITE(j)
Definition: timestamp.h:161
#define TIMESTAMP_NOEND(j)
Definition: timestamp.h:156
#define MIN_TIMESTAMP
Definition: timestamp.h:223
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 1816 of file timestamp.c.

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

References dt2time(), j2date(), pg_localtime(), POSTGRES_EPOCH_JDATE, SECS_PER_DAY, session_timezone, tm, pg_tm::tm_gmtoff, pg_tm::tm_hour, pg_tm::tm_isdst, pg_tm::tm_mday, pg_tm::tm_min, pg_tm::tm_mon, pg_tm::tm_sec, pg_tm::tm_year, pg_tm::tm_zone, TMODULO, UNIX_EPOCH_JDATE, USECS_PER_DAY, and USECS_PER_SEC.

Referenced by DetermineTimeZoneAbbrevOffsetTS(), GetCurrentTimeUsec(), JsonEncodeDateTime(), make_timestamptz_at_timezone(), map_sql_value_to_xml_value(), 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 2226 of file timestamp.c.

2227 {
2228  TimestampTz dt1;
2229  int overflow;
2230 
2231  dt1 = timestamp2timestamptz_opt_overflow(timestampVal, &overflow);
2232  if (overflow > 0)
2233  {
2234  /* dt1 is larger than any finite timestamp, but less than infinity */
2235  return TIMESTAMP_IS_NOEND(dt2) ? -1 : +1;
2236  }
2237  if (overflow < 0)
2238  {
2239  /* dt1 is less than any finite timestamp, but more than -infinity */
2240  return TIMESTAMP_IS_NOBEGIN(dt2) ? +1 : -1;
2241  }
2242 
2243  return timestamptz_cmp_internal(dt1, dt2);
2244 }
TimestampTz timestamp2timestamptz_opt_overflow(Timestamp timestamp, int *overflow)
Definition: timestamp.c:5549
#define TIMESTAMP_IS_NOEND(j)
Definition: timestamp.h:159
#define TIMESTAMP_IS_NOBEGIN(j)
Definition: timestamp.h:154
#define timestamptz_cmp_internal(dt1, dt2)
Definition: timestamp.h:101

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

1652 {
1653  TimestampTz diff = stop_time - start_time;
1654 
1655  if (diff <= 0)
1656  {
1657  *secs = 0;
1658  *microsecs = 0;
1659  }
1660  else
1661  {
1662  *secs = (long) (diff / USECS_PER_SEC);
1663  *microsecs = (int) (diff % USECS_PER_SEC);
1664  }
1665 }
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(), pgstat_update_dbstats(), ProcSleep(), and schedule_alarm().

◆ TimestampDifferenceExceeds()

◆ TimestampDifferenceMilliseconds()

long TimestampDifferenceMilliseconds ( TimestampTz  start_time,
TimestampTz  stop_time 
)

Definition at line 1687 of file timestamp.c.

1688 {
1689  TimestampTz diff = stop_time - start_time;
1690 
1691  if (diff <= 0)
1692  return 0;
1693  else
1694  return (long) ((diff + 999) / 1000);
1695 }

References start_time.

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

◆ TimestampTimestampTzRequiresRewrite()

bool TimestampTimestampTzRequiresRewrite ( void  )

Definition at line 5518 of file timestamp.c.

5519 {
5520  long offset;
5521 
5522  if (pg_get_timezone_offset(session_timezone, &offset) && offset == 0)
5523  return false;
5524  return true;
5525 }
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 1768 of file timestamp.c.

1769 {
1770  static char buf[MAXDATELEN + 1];
1771  int tz;
1772  struct pg_tm tt,
1773  *tm = &tt;
1774  fsec_t fsec;
1775  const char *tzn;
1776 
1777  if (TIMESTAMP_NOT_FINITE(t))
1779  else if (timestamp2tm(t, &tz, tm, &fsec, &tzn, NULL) == 0)
1780  EncodeDateTime(tm, fsec, true, tz, tzn, USE_ISO_DATES, buf);
1781  else
1782  strlcpy(buf, "(timestamp out of range)", sizeof(buf));
1783 
1784  return buf;
1785 }
void EncodeDateTime(struct pg_tm *tm, fsec_t fsec, bool print_tz, int tz, const char *tzn, int style, char *str)
Definition: datetime.c:4258
void EncodeSpecialTimestamp(Timestamp dt, char *str)
Definition: timestamp.c:1527
#define MAXDATELEN
Definition: datetime.h:201
#define USE_ISO_DATES
Definition: miscadmin.h:230
static char * buf
Definition: pg_test_fsync.c:67
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45

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

Referenced by CreateRestartPoint(), getRecoveryStopReason(), InitWalRecovery(), PerformWalRecovery(), pg_decode_commit_prepared_txn(), pg_decode_commit_txn(), pg_decode_prepare_txn(), pg_decode_rollback_prepared_txn(), pg_decode_stream_commit(), pg_decode_stream_prepare(), ProcessStandbyHSFeedbackMessage(), ProcessStandbyReplyMessage(), ProcessWalSndrMessage(), recoveryStopsAfter(), recoveryStopsBefore(), xact_desc_abort(), xact_desc_commit(), xact_desc_prepare(), xlog_desc(), and xlogrecovery_redo().

◆ timestamptz_to_time_t()

pg_time_t timestamptz_to_time_t ( TimestampTz  t)

Definition at line 1748 of file timestamp.c.

1749 {
1750  pg_time_t result;
1751 
1752  result = (pg_time_t) (t / USECS_PER_SEC +
1754 
1755  return result;
1756 }

References POSTGRES_EPOCH_JDATE, SECS_PER_DAY, UNIX_EPOCH_JDATE, and USECS_PER_SEC.

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

◆ tm2timestamp()

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

Definition at line 1912 of file timestamp.c.

1913 {
1914  TimeOffset date;
1915  TimeOffset time;
1916 
1917  /* Prevent overflow in Julian-day routines */
1919  {
1920  *result = 0; /* keep compiler quiet */
1921  return -1;
1922  }
1923 
1925  time = time2t(tm->tm_hour, tm->tm_min, tm->tm_sec, fsec);
1926 
1927  *result = date * USECS_PER_DAY + time;
1928  /* check for major overflow */
1929  if ((*result - time) / USECS_PER_DAY != date)
1930  {
1931  *result = 0; /* keep compiler quiet */
1932  return -1;
1933  }
1934  /* check for just-barely overflow (okay except time-of-day wraps) */
1935  /* caution: we want to allow 1999-12-31 24:00:00 */
1936  if ((*result < 0 && date > 0) ||
1937  (*result > 0 && date < -1))
1938  {
1939  *result = 0; /* keep compiler quiet */
1940  return -1;
1941  }
1942  if (tzp != NULL)
1943  *result = dt2local(*result, -(*tzp));
1944 
1945  /* final range check catches just-out-of-range timestamps */
1946  if (!IS_VALID_TIMESTAMP(*result))
1947  {
1948  *result = 0; /* keep compiler quiet */
1949  return -1;
1950  }
1951 
1952  return 0;
1953 }
static TimeOffset time2t(const int hour, const int min, const int sec, const fsec_t fsec)
Definition: timestamp.c:2031
#define IS_VALID_JULIAN(y, m, d)
Definition: timestamp.h:194

References date2j(), dt2local(), IS_VALID_JULIAN, IS_VALID_TIMESTAMP, POSTGRES_EPOCH_JDATE, time2t(), tm, pg_tm::tm_hour, pg_tm::tm_mday, pg_tm::tm_min, pg_tm::tm_mon, pg_tm::tm_sec, pg_tm::tm_year, and USECS_PER_DAY.

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

Variable Documentation

◆ PgReloadTime

PGDLLIMPORT TimestampTz PgReloadTime
extern

Definition at line 56 of file timestamp.c.

Referenced by pg_conf_load_time().

◆ PgStartTime

PGDLLIMPORT TimestampTz PgStartTime
extern

Definition at line 53 of file timestamp.c.

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