PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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)
 

Variables

TimestampTz PgStartTime
 
TimestampTz PgReloadTime
 

Macro Definition Documentation

#define INTERVAL_FULL_PRECISION   (0xFFFF)
#define INTERVAL_MASK (   b)    (1 << (b))
#define INTERVAL_PRECISION (   t)    ((t) & INTERVAL_PRECISION_MASK)

Definition at line 53 of file timestamp.h.

Referenced by AdjustIntervalForTypmod(), interval_transform(), and intervaltypmodout().

#define INTERVAL_PRECISION_MASK   (0xFFFF)

Definition at line 51 of file timestamp.h.

#define INTERVAL_RANGE (   t)    (((t) >> 16) & INTERVAL_RANGE_MASK)
#define INTERVAL_RANGE_MASK   (0x7FFF)

Definition at line 49 of file timestamp.h.

#define INTERVAL_TYPMOD (   p,
 
)    ((((r) & INTERVAL_RANGE_MASK) << 16) | ((p) & INTERVAL_PRECISION_MASK))

Definition at line 52 of file timestamp.h.

Referenced by intervaltypmodin().

#define PG_GETARG_TIMESTAMP (   n)    DatumGetTimestamp(PG_GETARG_DATUM(n))
#define TIMESTAMP_MASK (   b)    (1 << (b))

Definition at line 44 of file timestamp.h.

Function Documentation

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:575
#define ERROR
Definition: elog.h:43
#define MAX_TIMESTAMP_PRECISION
Definition: timestamp.h:53
#define ereport(elevel, rest)
Definition: elog.h:122
#define WARNING
Definition: elog.h:40
int errmsg(const char *fmt,...)
Definition: elog.c:797
int date2isoweek ( int  year,
int  mon,
int  mday 
)

Definition at line 4092 of file timestamp.c.

References date2j(), and j2day().

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

4093 {
4094  float8 result;
4095  int day0,
4096  day4,
4097  dayn;
4098 
4099  /* current day */
4100  dayn = date2j(year, mon, mday);
4101 
4102  /* fourth day of current year */
4103  day4 = date2j(year, 1, 4);
4104 
4105  /* day0 == offset to first day of week (Monday) */
4106  day0 = j2day(day4 - 1);
4107 
4108  /*
4109  * We need the first week containing a Thursday, otherwise this day falls
4110  * into the previous year for purposes of counting weeks
4111  */
4112  if (dayn < day4 - day0)
4113  {
4114  day4 = date2j(year - 1, 1, 4);
4115 
4116  /* day0 == offset to first day of week (Monday) */
4117  day0 = j2day(day4 - 1);
4118  }
4119 
4120  result = (dayn - (day4 - day0)) / 7 + 1;
4121 
4122  /*
4123  * Sometimes the last few days in a year will fall into the first week of
4124  * the next year, so check for this.
4125  */
4126  if (result >= 52)
4127  {
4128  day4 = date2j(year + 1, 1, 4);
4129 
4130  /* day0 == offset to first day of week (Monday) */
4131  day0 = j2day(day4 - 1);
4132 
4133  if (dayn >= day4 - day0)
4134  result = (dayn - (day4 - day0)) / 7 + 1;
4135  }
4136 
4137  return (int) result;
4138 }
double float8
Definition: c.h:375
int j2day(int date)
Definition: datetime.c:352
int date2j(int y, int m, int d)
Definition: datetime.c:292
int date2isoyear ( int  year,
int  mon,
int  mday 
)

Definition at line 4146 of file timestamp.c.

References date2j(), and j2day().

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

4147 {
4148  float8 result;
4149  int day0,
4150  day4,
4151  dayn;
4152 
4153  /* current day */
4154  dayn = date2j(year, mon, mday);
4155 
4156  /* fourth day of current year */
4157  day4 = date2j(year, 1, 4);
4158 
4159  /* day0 == offset to first day of week (Monday) */
4160  day0 = j2day(day4 - 1);
4161 
4162  /*
4163  * We need the first week containing a Thursday, otherwise this day falls
4164  * into the previous year for purposes of counting weeks
4165  */
4166  if (dayn < day4 - day0)
4167  {
4168  day4 = date2j(year - 1, 1, 4);
4169 
4170  /* day0 == offset to first day of week (Monday) */
4171  day0 = j2day(day4 - 1);
4172 
4173  year--;
4174  }
4175 
4176  result = (dayn - (day4 - day0)) / 7 + 1;
4177 
4178  /*
4179  * Sometimes the last few days in a year will fall into the first week of
4180  * the next year, so check for this.
4181  */
4182  if (result >= 52)
4183  {
4184  day4 = date2j(year + 1, 1, 4);
4185 
4186  /* day0 == offset to first day of week (Monday) */
4187  day0 = j2day(day4 - 1);
4188 
4189  if (dayn >= day4 - day0)
4190  year++;
4191  }
4192 
4193  return year;
4194 }
double float8
Definition: c.h:375
int j2day(int date)
Definition: datetime.c:352
int date2j(int y, int m, int d)
Definition: datetime.c:292
int date2isoyearday ( int  year,
int  mon,
int  mday 
)

Definition at line 4203 of file timestamp.c.

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

Referenced by DCH_to_char().

4204 {
4205  return date2j(year, mon, mday) - isoweek2j(date2isoyear(year, mon, mday), 1) + 1;
4206 }
int date2isoyear(int year, int mon, int mday)
Definition: timestamp.c:4146
int isoweek2j(int year, int week)
Definition: timestamp.c:4041
int date2j(int y, int m, int d)
Definition: datetime.c:292
void dt2time ( Timestamp  dt,
int *  hour,
int *  min,
int *  sec,
fsec_t fsec 
)

Definition at line 1731 of file timestamp.c.

References USECS_PER_HOUR, USECS_PER_MINUTE, and USECS_PER_SEC.

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

1732 {
1733  TimeOffset time;
1734 
1735  time = jd;
1736 
1737  *hour = time / USECS_PER_HOUR;
1738  time -= (*hour) * USECS_PER_HOUR;
1739  *min = time / USECS_PER_MINUTE;
1740  time -= (*min) * USECS_PER_MINUTE;
1741  *sec = time / USECS_PER_SEC;
1742  *fsec = time - (*sec * USECS_PER_SEC);
1743 } /* 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
TimestampTz GetCurrentTimestamp ( void  )

Definition at line 1570 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(), BackendInitialize(), 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(), enable_timeout_after(), enable_timeout_at(), enable_timeouts(), get_role_password(), GetCurrentTransactionStopTimestamp(), GetReplicationApplyDelay(), GetSnapshotCurrentTimestamp(), handle_sig_alarm(), launcher_determine_sleep(), lazy_vacuum_rel(), 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_bestart(), pgstat_read_statsfiles(), pgstat_recv_inquiry(), pgstat_recv_resetsharedcounter(), pgstat_recv_resetsinglecounter(), pgstat_report_activity(), pgstat_report_analyze(), pgstat_report_autovac(), 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(), StartupXLOG(), throttle(), UpdateWorkerStats(), WaitExceedsMaxStandbyDelay(), WaitForWALToBecomeAvailable(), WalReceiverMain(), WalSndKeepalive(), WalSndLoop(), WalSndUpdateProgress(), WalSndWaitForWal(), WalSndWriteData(), XLogBackgroundFlush(), XLogFileRead(), XLogRestorePoint(), XLogSendPhysical(), XLogWalRcvSendHSFeedback(), and XLogWalRcvSendReply().

1571 {
1572  TimestampTz result;
1573  struct timeval tp;
1574 
1575  gettimeofday(&tp, NULL);
1576 
1577  result = (TimestampTz) tp.tv_sec -
1579  result = (result * USECS_PER_SEC) + tp.tv_usec;
1580 
1581  return result;
1582 }
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
void GetEpochTime ( struct pg_tm tm)

Definition at line 1984 of file timestamp.c.

References epoch, 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().

1985 {
1986  struct pg_tm *t0;
1987  pg_time_t epoch = 0;
1988 
1989  t0 = pg_gmtime(&epoch);
1990 
1991  tm->tm_year = t0->tm_year;
1992  tm->tm_mon = t0->tm_mon;
1993  tm->tm_mday = t0->tm_mday;
1994  tm->tm_hour = t0->tm_hour;
1995  tm->tm_min = t0->tm_min;
1996  tm->tm_sec = t0->tm_sec;
1997 
1998  tm->tm_year += 1900;
1999  tm->tm_mon++;
2000 }
int64 pg_time_t
Definition: pgtime.h:23
struct pg_tm * pg_gmtime(const pg_time_t *timep)
Definition: localtime.c:1355
int tm_hour
Definition: pgtime.h:29
Definition: pgtime.h:25
int tm_mday
Definition: pgtime.h:30
int tm_mon
Definition: pgtime.h:31
int tm_year
Definition: pgtime.h:32
static const unsigned __int64 epoch
Definition: gettimeofday.c:34
int tm_sec
Definition: pgtime.h:27
int tm_min
Definition: pgtime.h:28
TimestampTz GetSQLCurrentTimestamp ( int32  typmod)

Definition at line 1588 of file timestamp.c.

References AdjustTimestampForTypmod(), and GetCurrentTransactionStartTimestamp().

Referenced by ExecEvalSQLValueFunction().

1589 {
1590  TimestampTz ts;
1591 
1593  if (typmod >= 0)
1594  AdjustTimestampForTypmod(&ts, typmod);
1595  return ts;
1596 }
int64 TimestampTz
Definition: timestamp.h:39
static void AdjustTimestampForTypmod(Timestamp *time, int32 typmod)
Definition: timestamp.c:334
TimestampTz GetCurrentTransactionStartTimestamp(void)
Definition: xact.c:701
Timestamp GetSQLLocalTimestamp ( int32  typmod)

Definition at line 1602 of file timestamp.c.

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

Referenced by ExecEvalSQLValueFunction().

1603 {
1604  Timestamp ts;
1605 
1607  if (typmod >= 0)
1608  AdjustTimestampForTypmod(&ts, typmod);
1609  return ts;
1610 }
static void AdjustTimestampForTypmod(Timestamp *time, int32 typmod)
Definition: timestamp.c:334
static Timestamp timestamptz2timestamp(TimestampTz timestamp)
Definition: timestamp.c:5006
int64 Timestamp
Definition: timestamp.h:38
TimestampTz GetCurrentTransactionStartTimestamp(void)
Definition: xact.c:701
int interval2tm ( Interval  span,
struct pg_tm tm,
fsec_t fsec 
)

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

1903 {
1904  TimeOffset time;
1905  TimeOffset tfrac;
1906 
1907  tm->tm_year = span.month / MONTHS_PER_YEAR;
1908  tm->tm_mon = span.month % MONTHS_PER_YEAR;
1909  tm->tm_mday = span.day;
1910  time = span.time;
1911 
1912  tfrac = time / USECS_PER_HOUR;
1913  time -= tfrac * USECS_PER_HOUR;
1914  tm->tm_hour = tfrac;
1915  if (!SAMESIGN(tm->tm_hour, tfrac))
1916  ereport(ERROR,
1917  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1918  errmsg("interval out of range")));
1919  tfrac = time / USECS_PER_MINUTE;
1920  time -= tfrac * USECS_PER_MINUTE;
1921  tm->tm_min = tfrac;
1922  tfrac = time / USECS_PER_SEC;
1923  *fsec = time - (tfrac * USECS_PER_SEC);
1924  tm->tm_sec = tfrac;
1925 
1926  return 0;
1927 }
#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:575
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:122
#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:797
int tm_sec
Definition: pgtime.h:27
int tm_min
Definition: pgtime.h:28
void isoweek2date ( int  woy,
int *  year,
int *  mon,
int *  mday 
)

Definition at line 4061 of file timestamp.c.

References isoweek2j(), and j2date().

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

4062 {
4063  j2date(isoweek2j(*year, woy), year, mon, mday);
4064 }
void j2date(int jd, int *year, int *month, int *day)
Definition: datetime.c:317
int isoweek2j(int year, int week)
Definition: timestamp.c:4041
int isoweek2j ( int  year,
int  week 
)

Definition at line 4041 of file timestamp.c.

References date2j(), and j2day().

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

4042 {
4043  int day0,
4044  day4;
4045 
4046  /* fourth day of current year */
4047  day4 = date2j(year, 1, 4);
4048 
4049  /* day0 == offset to first day of week (Monday) */
4050  day0 = j2day(day4 - 1);
4051 
4052  return ((week - 1) * 7) + (day4 - day0);
4053 }
int j2day(int date)
Definition: datetime.c:352
int date2j(int y, int m, int d)
Definition: datetime.c:292
void isoweekdate2date ( int  isoweek,
int  wday,
int *  year,
int *  mon,
int *  mday 
)

Definition at line 4074 of file timestamp.c.

References isoweek2j(), and j2date().

Referenced by do_to_timestamp().

4075 {
4076  int jday;
4077 
4078  jday = isoweek2j(*year, isoweek);
4079  /* convert Gregorian week start (Sunday=1) to ISO week start (Monday=1) */
4080  if (wday > 1)
4081  jday += wday - 2;
4082  else
4083  jday += 6;
4084  j2date(jday, year, mon, mday);
4085 }
void j2date(int jd, int *year, int *month, int *day)
Definition: datetime.c:317
int isoweek2j(int year, int week)
Definition: timestamp.c:4041
Timestamp SetEpochTimestamp ( void  )

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

2004 {
2005  Timestamp dt;
2006  struct pg_tm tt,
2007  *tm = &tt;
2008 
2009  GetEpochTime(tm);
2010  /* we don't bother to test for failure ... */
2011  tm2timestamp(tm, 0, NULL, &dt);
2012 
2013  return dt;
2014 } /* SetEpochTimestamp() */
Definition: pgtime.h:25
static struct pg_tm tm
Definition: localtime.c:107
int64 Timestamp
Definition: timestamp.h:38
int tm2timestamp(struct pg_tm *tm, fsec_t fsec, int *tzp, Timestamp *result)
Definition: timestamp.c:1854
void GetEpochTime(struct pg_tm *tm)
Definition: timestamp.c:1984
TimestampTz time_t_to_timestamptz ( pg_time_t  tm)

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

1671 {
1672  TimestampTz result;
1673 
1674  result = (TimestampTz) tm -
1676  result *= USECS_PER_SEC;
1677 
1678  return result;
1679 }
#define USECS_PER_SEC
Definition: timestamp.h:94
int64 TimestampTz
Definition: timestamp.h:39
static struct pg_tm tm
Definition: localtime.c:107
#define SECS_PER_DAY
Definition: timestamp.h:86
#define UNIX_EPOCH_JDATE
Definition: timestamp.h:162
#define POSTGRES_EPOCH_JDATE
Definition: timestamp.h:163
int timestamp2tm ( Timestamp  dt,
int *  tzp,
struct pg_tm tm,
fsec_t fsec,
const char **  tzn,
pg_tz attimezone 
)

Definition at line 1758 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 datum_to_json(), datum_to_jsonb(), DetermineTimeZoneAbbrevOffsetTS(), GetCurrentDateTime(), GetCurrentTimeUsec(), GetSQLCurrentDate(), GetSQLCurrentTime(), GetSQLLocalTime(), make_timestamptz_at_timezone(), map_sql_value_to_xml_value(), pg_timezone_names(), PGTYPEStimestamp_add_interval(), PGTYPEStimestamp_fmt_asc(), PGTYPEStimestamp_to_asc(), timestamp2timestamptz(), timestamp_abstime(), timestamp_age(), timestamp_date(), timestamp_out(), timestamp_part(), timestamp_pl_interval(), timestamp_recv(), timestamp_time(), timestamp_to_char(), timestamp_trunc(), timestamp_zone(), timestamptz2timestamp(), timestamptz_abstime(), 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(), and timestamptz_zone().

1759 {
1760  Timestamp date;
1761  Timestamp time;
1762  pg_time_t utime;
1763 
1764  /* Use session timezone if caller asks for default */
1765  if (attimezone == NULL)
1766  attimezone = session_timezone;
1767 
1768  time = dt;
1769  TMODULO(time, date, USECS_PER_DAY);
1770 
1771  if (time < INT64CONST(0))
1772  {
1773  time += USECS_PER_DAY;
1774  date -= 1;
1775  }
1776 
1777  /* add offset to go from J2000 back to standard Julian date */
1778  date += POSTGRES_EPOCH_JDATE;
1779 
1780  /* Julian day routine does not work for negative Julian days */
1781  if (date < 0 || date > (Timestamp) INT_MAX)
1782  return -1;
1783 
1784  j2date((int) date, &tm->tm_year, &tm->tm_mon, &tm->tm_mday);
1785  dt2time(time, &tm->tm_hour, &tm->tm_min, &tm->tm_sec, fsec);
1786 
1787  /* Done if no TZ conversion wanted */
1788  if (tzp == NULL)
1789  {
1790  tm->tm_isdst = -1;
1791  tm->tm_gmtoff = 0;
1792  tm->tm_zone = NULL;
1793  if (tzn != NULL)
1794  *tzn = NULL;
1795  return 0;
1796  }
1797 
1798  /*
1799  * If the time falls within the range of pg_time_t, use pg_localtime() to
1800  * rotate to the local time zone.
1801  *
1802  * First, convert to an integral timestamp, avoiding possibly
1803  * platform-specific roundoff-in-wrong-direction errors, and adjust to
1804  * Unix epoch. Then see if we can convert to pg_time_t without loss. This
1805  * coding avoids hardwiring any assumptions about the width of pg_time_t,
1806  * so it should behave sanely on machines without int64.
1807  */
1808  dt = (dt - *fsec) / USECS_PER_SEC +
1810  utime = (pg_time_t) dt;
1811  if ((Timestamp) utime == dt)
1812  {
1813  struct pg_tm *tx = pg_localtime(&utime, attimezone);
1814 
1815  tm->tm_year = tx->tm_year + 1900;
1816  tm->tm_mon = tx->tm_mon + 1;
1817  tm->tm_mday = tx->tm_mday;
1818  tm->tm_hour = tx->tm_hour;
1819  tm->tm_min = tx->tm_min;
1820  tm->tm_sec = tx->tm_sec;
1821  tm->tm_isdst = tx->tm_isdst;
1822  tm->tm_gmtoff = tx->tm_gmtoff;
1823  tm->tm_zone = tx->tm_zone;
1824  *tzp = -tm->tm_gmtoff;
1825  if (tzn != NULL)
1826  *tzn = tm->tm_zone;
1827  }
1828  else
1829  {
1830  /*
1831  * When out of range of pg_time_t, treat as GMT
1832  */
1833  *tzp = 0;
1834  /* Mark this as *no* time zone available */
1835  tm->tm_isdst = -1;
1836  tm->tm_gmtoff = 0;
1837  tm->tm_zone = NULL;
1838  if (tzn != NULL)
1839  *tzn = NULL;
1840  }
1841 
1842  return 0;
1843 }
int64 pg_time_t
Definition: pgtime.h:23
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:8
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:317
int64 Timestamp
Definition: timestamp.h:38
void dt2time(Timestamp jd, int *hour, int *min, int *sec, fsec_t *fsec)
Definition: timestamp.c:1731
struct pg_tm * pg_localtime(const pg_time_t *timep, const pg_tz *tz)
Definition: localtime.c:1314
int tm_year
Definition: pgtime.h:32
pg_tz * session_timezone
Definition: pgtz.c:28
#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:251
void TimestampDifference ( TimestampTz  start_time,
TimestampTz  stop_time,
long *  secs,
int *  microsecs 
)

Definition at line 1624 of file timestamp.c.

References start_time, and USECS_PER_SEC.

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

1626 {
1627  TimestampTz diff = stop_time - start_time;
1628 
1629  if (diff <= 0)
1630  {
1631  *secs = 0;
1632  *microsecs = 0;
1633  }
1634  else
1635  {
1636  *secs = (long) (diff / USECS_PER_SEC);
1637  *microsecs = (int) (diff % USECS_PER_SEC);
1638  }
1639 }
#define USECS_PER_SEC
Definition: timestamp.h:94
int64 TimestampTz
Definition: timestamp.h:39
static time_t start_time
Definition: pg_ctl.c:103
const char* timestamptz_to_str ( TimestampTz  t)

Definition at line 1710 of file timestamp.c.

References buf, EncodeDateTime(), EncodeSpecialTimestamp(), MAXDATELEN, 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(), ProcessWalSndrMessage(), readRecoveryCommandFile(), recoveryStopsAfter(), recoveryStopsBefore(), StartupXLOG(), xact_desc_abort(), xact_desc_commit(), and xlog_desc().

1711 {
1712  static char buf[MAXDATELEN + 1];
1713  int tz;
1714  struct pg_tm tt,
1715  *tm = &tt;
1716  fsec_t fsec;
1717  const char *tzn;
1718 
1719  if (TIMESTAMP_NOT_FINITE(t))
1720  EncodeSpecialTimestamp(t, buf);
1721  else if (timestamp2tm(t, &tz, tm, &fsec, &tzn, NULL) == 0)
1722  EncodeDateTime(tm, fsec, true, tz, tzn, USE_ISO_DATES, buf);
1723  else
1724  strlcpy(buf, "(timestamp out of range)", sizeof(buf));
1725 
1726  return buf;
1727 }
#define MAXDATELEN
Definition: datetime.h:203
int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm *tm, fsec_t *fsec, const char **tzn, pg_tz *attimezone)
Definition: timestamp.c:1758
void EncodeSpecialTimestamp(Timestamp dt, char *str)
Definition: timestamp.c:1523
Definition: pgtime.h:25
#define USE_ISO_DATES
Definition: miscadmin.h:211
static struct pg_tm tm
Definition: localtime.c:107
#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:3999
int32 fsec_t
Definition: timestamp.h:41
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
pg_time_t timestamptz_to_time_t ( TimestampTz  t)

Definition at line 1692 of file timestamp.c.

References POSTGRES_EPOCH_JDATE, SECS_PER_DAY, UNIX_EPOCH_JDATE, and USECS_PER_SEC.

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

1693 {
1694  pg_time_t result;
1695 
1696  result = (pg_time_t) (t / USECS_PER_SEC +
1698 
1699  return result;
1700 }
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
int tm2interval ( struct pg_tm tm,
fsec_t  fsec,
Interval span 
)

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

1931 {
1932  double total_months = (double) tm->tm_year * MONTHS_PER_YEAR + tm->tm_mon;
1933 
1934  if (total_months > INT_MAX || total_months < INT_MIN)
1935  return -1;
1936  span->month = total_months;
1937  span->day = tm->tm_mday;
1938  span->time = (((((tm->tm_hour * INT64CONST(60)) +
1939  tm->tm_min) * INT64CONST(60)) +
1940  tm->tm_sec) * USECS_PER_SEC) + fsec;
1941 
1942  return 0;
1943 }
#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
int tm2timestamp ( struct pg_tm tm,
fsec_t  fsec,
int *  tzp,
Timestamp dt 
)

Definition at line 1854 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 abstime_timestamp(), abstime_timestamptz(), 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(), timestamptz_zone(), and to_timestamp().

1855 {
1856  TimeOffset date;
1857  TimeOffset time;
1858 
1859  /* Prevent overflow in Julian-day routines */
1860  if (!IS_VALID_JULIAN(tm->tm_year, tm->tm_mon, tm->tm_mday))
1861  {
1862  *result = 0; /* keep compiler quiet */
1863  return -1;
1864  }
1865 
1866  date = date2j(tm->tm_year, tm->tm_mon, tm->tm_mday) - POSTGRES_EPOCH_JDATE;
1867  time = time2t(tm->tm_hour, tm->tm_min, tm->tm_sec, fsec);
1868 
1869  *result = date * USECS_PER_DAY + time;
1870  /* check for major overflow */
1871  if ((*result - time) / USECS_PER_DAY != date)
1872  {
1873  *result = 0; /* keep compiler quiet */
1874  return -1;
1875  }
1876  /* check for just-barely overflow (okay except time-of-day wraps) */
1877  /* caution: we want to allow 1999-12-31 24:00:00 */
1878  if ((*result < 0 && date > 0) ||
1879  (*result > 0 && date < -1))
1880  {
1881  *result = 0; /* keep compiler quiet */
1882  return -1;
1883  }
1884  if (tzp != NULL)
1885  *result = dt2local(*result, -(*tzp));
1886 
1887  /* final range check catches just-out-of-range timestamps */
1888  if (!IS_VALID_TIMESTAMP(*result))
1889  {
1890  *result = 0; /* keep compiler quiet */
1891  return -1;
1892  }
1893 
1894  return 0;
1895 }
static TimeOffset time2t(const int hour, const int min, const int sec, const fsec_t fsec)
Definition: timestamp.c:1946
int tm_hour
Definition: pgtime.h:29
long date
Definition: pgtypes_date.h:8
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:292
#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:1952
int tm_sec
Definition: pgtime.h:27
int tm_min
Definition: pgtime.h:28

Variable Documentation

TimestampTz PgReloadTime

Definition at line 52 of file timestamp.c.

Referenced by pg_conf_load_time().

TimestampTz PgStartTime

Definition at line 49 of file timestamp.c.

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