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)
 
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:570
#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:784

◆ date2isoweek()

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

Definition at line 4309 of file timestamp.c.

References date2j(), and j2day().

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

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

◆ date2isoyear()

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

Definition at line 4363 of file timestamp.c.

References date2j(), and j2day().

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

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

◆ date2isoyearday()

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

Definition at line 4420 of file timestamp.c.

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

Referenced by DCH_to_char().

4421 {
4422  return date2j(year, mon, mday) - isoweek2j(date2isoyear(year, mon, mday), 1) + 1;
4423 }
int date2isoyear(int year, int mon, int mday)
Definition: timestamp.c:4363
int isoweek2j(int year, int week)
Definition: timestamp.c:4258
int date2j(int y, int m, int d)
Definition: datetime.c:270

◆ dt2time()

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

Definition at line 1749 of file timestamp.c.

References USECS_PER_HOUR, USECS_PER_MINUTE, and USECS_PER_SEC.

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

1750 {
1751  TimeOffset time;
1752 
1753  time = jd;
1754 
1755  *hour = time / USECS_PER_HOUR;
1756  time -= (*hour) * USECS_PER_HOUR;
1757  *min = time / USECS_PER_MINUTE;
1758  time -= (*min) * USECS_PER_MINUTE;
1759  *sec = time / USECS_PER_SEC;
1760  *fsec = time - (*sec * USECS_PER_SEC);
1761 } /* 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 1568 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().

1569 {
1570  TimestampTz result;
1571  struct timeval tp;
1572 
1573  gettimeofday(&tp, NULL);
1574 
1575  result = (TimestampTz) tp.tv_sec -
1577  result = (result * USECS_PER_SEC) + tp.tv_usec;
1578 
1579  return result;
1580 }
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 2002 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().

2003 {
2004  struct pg_tm *t0;
2005  pg_time_t epoch = 0;
2006 
2007  t0 = pg_gmtime(&epoch);
2008 
2009  if (t0 == NULL)
2010  elog(ERROR, "could not convert epoch to timestamp: %m");
2011 
2012  tm->tm_year = t0->tm_year;
2013  tm->tm_mon = t0->tm_mon;
2014  tm->tm_mday = t0->tm_mday;
2015  tm->tm_hour = t0->tm_hour;
2016  tm->tm_min = t0->tm_min;
2017  tm->tm_sec = t0->tm_sec;
2018 
2019  tm->tm_year += 1900;
2020  tm->tm_mon++;
2021 }
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:226
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 1586 of file timestamp.c.

References AdjustTimestampForTypmod(), and GetCurrentTransactionStartTimestamp().

Referenced by ExecEvalSQLValueFunction().

1587 {
1588  TimestampTz ts;
1589 
1591  if (typmod >= 0)
1592  AdjustTimestampForTypmod(&ts, typmod);
1593  return ts;
1594 }
int64 TimestampTz
Definition: timestamp.h:39
void AdjustTimestampForTypmod(Timestamp *time, int32 typmod)
Definition: timestamp.c:336
TimestampTz GetCurrentTransactionStartTimestamp(void)
Definition: xact.c:783

◆ GetSQLLocalTimestamp()

Timestamp GetSQLLocalTimestamp ( int32  typmod)

Definition at line 1600 of file timestamp.c.

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

Referenced by ExecEvalSQLValueFunction().

1601 {
1602  Timestamp ts;
1603 
1605  if (typmod >= 0)
1606  AdjustTimestampForTypmod(&ts, typmod);
1607  return ts;
1608 }
void AdjustTimestampForTypmod(Timestamp *time, int32 typmod)
Definition: timestamp.c:336
static Timestamp timestamptz2timestamp(TimestampTz timestamp)
Definition: timestamp.c:5216
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 1920 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().

1921 {
1922  TimeOffset time;
1923  TimeOffset tfrac;
1924 
1925  tm->tm_year = span.month / MONTHS_PER_YEAR;
1926  tm->tm_mon = span.month % MONTHS_PER_YEAR;
1927  tm->tm_mday = span.day;
1928  time = span.time;
1929 
1930  tfrac = time / USECS_PER_HOUR;
1931  time -= tfrac * USECS_PER_HOUR;
1932  tm->tm_hour = tfrac;
1933  if (!SAMESIGN(tm->tm_hour, tfrac))
1934  ereport(ERROR,
1935  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1936  errmsg("interval out of range")));
1937  tfrac = time / USECS_PER_MINUTE;
1938  time -= tfrac * USECS_PER_MINUTE;
1939  tm->tm_min = tfrac;
1940  tfrac = time / USECS_PER_SEC;
1941  *fsec = time - (tfrac * USECS_PER_SEC);
1942  tm->tm_sec = tfrac;
1943 
1944  return 0;
1945 }
#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:570
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:784
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 4278 of file timestamp.c.

References isoweek2j(), and j2date().

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

4279 {
4280  j2date(isoweek2j(*year, woy), year, mon, mday);
4281 }
void j2date(int jd, int *year, int *month, int *day)
Definition: datetime.c:295
int isoweek2j(int year, int week)
Definition: timestamp.c:4258

◆ isoweek2j()

int isoweek2j ( int  year,
int  week 
)

Definition at line 4258 of file timestamp.c.

References date2j(), and j2day().

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

4259 {
4260  int day0,
4261  day4;
4262 
4263  /* fourth day of current year */
4264  day4 = date2j(year, 1, 4);
4265 
4266  /* day0 == offset to first day of week (Monday) */
4267  day0 = j2day(day4 - 1);
4268 
4269  return ((week - 1) * 7) + (day4 - day0);
4270 }
int j2day(int date)
Definition: datetime.c:330
int date2j(int y, int m, int d)
Definition: datetime.c:270

◆ isoweekdate2date()

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

Definition at line 4291 of file timestamp.c.

References isoweek2j(), and j2date().

Referenced by do_to_timestamp().

4292 {
4293  int jday;
4294 
4295  jday = isoweek2j(*year, isoweek);
4296  /* convert Gregorian week start (Sunday=1) to ISO week start (Monday=1) */
4297  if (wday > 1)
4298  jday += wday - 2;
4299  else
4300  jday += 6;
4301  j2date(jday, year, mon, mday);
4302 }
void j2date(int jd, int *year, int *month, int *day)
Definition: datetime.c:295
int isoweek2j(int year, int week)
Definition: timestamp.c:4258

◆ SetEpochTimestamp()

Timestamp SetEpochTimestamp ( void  )

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

2025 {
2026  Timestamp dt;
2027  struct pg_tm tt,
2028  *tm = &tt;
2029 
2030  GetEpochTime(tm);
2031  /* we don't bother to test for failure ... */
2032  tm2timestamp(tm, 0, NULL, &dt);
2033 
2034  return dt;
2035 } /* 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:1872
void GetEpochTime(struct pg_tm *tm)
Definition: timestamp.c:2002

◆ time_t_to_timestamptz()

TimestampTz time_t_to_timestamptz ( pg_time_t  tm)

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

1689 {
1690  TimestampTz result;
1691 
1692  result = (TimestampTz) tm -
1694  result *= USECS_PER_SEC;
1695 
1696  return result;
1697 }
#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

◆ timestamp2tm()

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

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

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

1644 {
1645  TimestampTz diff = stop_time - start_time;
1646 
1647  if (diff <= 0)
1648  {
1649  *secs = 0;
1650  *microsecs = 0;
1651  }
1652  else
1653  {
1654  *secs = (long) (diff / USECS_PER_SEC);
1655  *microsecs = (int) (diff % USECS_PER_SEC);
1656  }
1657 }
#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 5155 of file timestamp.c.

References pg_get_timezone_offset(), and session_timezone.

Referenced by ATColumnChangeRequiresRewrite().

5156 {
5157  long offset;
5158 
5159  if (pg_get_timezone_offset(session_timezone, &offset) && offset == 0)
5160  return false;
5161  return true;
5162 }
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 1728 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(), and xlog_desc().

1729 {
1730  static char buf[MAXDATELEN + 1];
1731  int tz;
1732  struct pg_tm tt,
1733  *tm = &tt;
1734  fsec_t fsec;
1735  const char *tzn;
1736 
1737  if (TIMESTAMP_NOT_FINITE(t))
1738  EncodeSpecialTimestamp(t, buf);
1739  else if (timestamp2tm(t, &tz, tm, &fsec, &tzn, NULL) == 0)
1740  EncodeDateTime(tm, fsec, true, tz, tzn, USE_ISO_DATES, buf);
1741  else
1742  strlcpy(buf, "(timestamp out of range)", sizeof(buf));
1743 
1744  return buf;
1745 }
#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:1776
void EncodeSpecialTimestamp(Timestamp dt, char *str)
Definition: timestamp.c:1521
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:68
void EncodeDateTime(struct pg_tm *tm, fsec_t fsec, bool print_tz, int tz, const char *tzn, int style, char *str)
Definition: datetime.c:3973
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 1710 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().

1711 {
1712  pg_time_t result;
1713 
1714  result = (pg_time_t) (t / USECS_PER_SEC +
1716 
1717  return result;
1718 }
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 1948 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().

1949 {
1950  double total_months = (double) tm->tm_year * MONTHS_PER_YEAR + tm->tm_mon;
1951 
1952  if (total_months > INT_MAX || total_months < INT_MIN)
1953  return -1;
1954  span->month = total_months;
1955  span->day = tm->tm_mday;
1956  span->time = (((((tm->tm_hour * INT64CONST(60)) +
1957  tm->tm_min) * INT64CONST(60)) +
1958  tm->tm_sec) * USECS_PER_SEC) + fsec;
1959 
1960  return 0;
1961 }
#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 1872 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(), PGTYPEStimestamp_add_interval(), PGTYPEStimestamp_current(), PGTYPEStimestamp_defmt_scan(), PGTYPEStimestamp_from_asc(), SetEpochTimestamp(), timestamp2timestamptz(), timestamp_in(), timestamp_pl_interval(), timestamp_trunc(), timestamp_zone(), timestamptz2timestamp(), timestamptz_in(), timestamptz_pl_interval(), timestamptz_trunc_internal(), timestamptz_zone(), and to_timestamp().

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