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

References date2j(), and j2day().

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

4044 {
4045  float8 result;
4046  int day0,
4047  day4,
4048  dayn;
4049 
4050  /* current day */
4051  dayn = date2j(year, mon, mday);
4052 
4053  /* fourth day of current year */
4054  day4 = date2j(year, 1, 4);
4055 
4056  /* day0 == offset to first day of week (Monday) */
4057  day0 = j2day(day4 - 1);
4058 
4059  /*
4060  * We need the first week containing a Thursday, otherwise this day falls
4061  * into the previous year for purposes of counting weeks
4062  */
4063  if (dayn < day4 - day0)
4064  {
4065  day4 = date2j(year - 1, 1, 4);
4066 
4067  /* day0 == offset to first day of week (Monday) */
4068  day0 = j2day(day4 - 1);
4069  }
4070 
4071  result = (dayn - (day4 - day0)) / 7 + 1;
4072 
4073  /*
4074  * Sometimes the last few days in a year will fall into the first week of
4075  * the next year, so check for this.
4076  */
4077  if (result >= 52)
4078  {
4079  day4 = date2j(year + 1, 1, 4);
4080 
4081  /* day0 == offset to first day of week (Monday) */
4082  day0 = j2day(day4 - 1);
4083 
4084  if (dayn >= day4 - day0)
4085  result = (dayn - (day4 - day0)) / 7 + 1;
4086  }
4087 
4088  return (int) result;
4089 }
double float8
Definition: c.h:378
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 4097 of file timestamp.c.

References date2j(), and j2day().

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

4098 {
4099  float8 result;
4100  int day0,
4101  day4,
4102  dayn;
4103 
4104  /* current day */
4105  dayn = date2j(year, mon, mday);
4106 
4107  /* fourth day of current year */
4108  day4 = date2j(year, 1, 4);
4109 
4110  /* day0 == offset to first day of week (Monday) */
4111  day0 = j2day(day4 - 1);
4112 
4113  /*
4114  * We need the first week containing a Thursday, otherwise this day falls
4115  * into the previous year for purposes of counting weeks
4116  */
4117  if (dayn < day4 - day0)
4118  {
4119  day4 = date2j(year - 1, 1, 4);
4120 
4121  /* day0 == offset to first day of week (Monday) */
4122  day0 = j2day(day4 - 1);
4123 
4124  year--;
4125  }
4126 
4127  result = (dayn - (day4 - day0)) / 7 + 1;
4128 
4129  /*
4130  * Sometimes the last few days in a year will fall into the first week of
4131  * the next year, so check for this.
4132  */
4133  if (result >= 52)
4134  {
4135  day4 = date2j(year + 1, 1, 4);
4136 
4137  /* day0 == offset to first day of week (Monday) */
4138  day0 = j2day(day4 - 1);
4139 
4140  if (dayn >= day4 - day0)
4141  year++;
4142  }
4143 
4144  return year;
4145 }
double float8
Definition: c.h:378
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 4154 of file timestamp.c.

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

Referenced by DCH_to_char().

4155 {
4156  return date2j(year, mon, mday) - isoweek2j(date2isoyear(year, mon, mday), 1) + 1;
4157 }
int date2isoyear(int year, int mon, int mday)
Definition: timestamp.c:4097
int isoweek2j(int year, int week)
Definition: timestamp.c:3992
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 1730 of file timestamp.c.

References USECS_PER_HOUR, USECS_PER_MINUTE, and USECS_PER_SEC.

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

1731 {
1732  TimeOffset time;
1733 
1734  time = jd;
1735 
1736  *hour = time / USECS_PER_HOUR;
1737  time -= (*hour) * USECS_PER_HOUR;
1738  *min = time / USECS_PER_MINUTE;
1739  time -= (*min) * USECS_PER_MINUTE;
1740  *sec = time / USECS_PER_SEC;
1741  *fsec = time - (*sec * USECS_PER_SEC);
1742 } /* 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 1569 of file timestamp.c.

References gettimeofday(), NULL, POSTGRES_EPOCH_JDATE, SECS_PER_DAY, UNIX_EPOCH_JDATE, and USECS_PER_SEC.

Referenced by ApplyLauncherMain(), ApplyLoop(), ApplyWorkerMain(), assign_backendlist_entry(), asyncQueueFillWarning(), 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_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(), maybe_start_bgworker(), perform_base_backup(), 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(), ProcessRepliesIfAny(), 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(), WalSndWaitForWal(), WalSndWriteData(), XLogBackgroundFlush(), XLogFileRead(), XLogRestorePoint(), XLogSendPhysical(), XLogWalRcvSendHSFeedback(), and XLogWalRcvSendReply().

1570 {
1571  TimestampTz result;
1572  struct timeval tp;
1573 
1574  gettimeofday(&tp, NULL);
1575 
1576  result = (TimestampTz) tp.tv_sec -
1578  result = (result * USECS_PER_SEC) + tp.tv_usec;
1579 
1580  return result;
1581 }
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 NULL
Definition: c.h:226
#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 1983 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().

1984 {
1985  struct pg_tm *t0;
1986  pg_time_t epoch = 0;
1987 
1988  t0 = pg_gmtime(&epoch);
1989 
1990  tm->tm_year = t0->tm_year;
1991  tm->tm_mon = t0->tm_mon;
1992  tm->tm_mday = t0->tm_mday;
1993  tm->tm_hour = t0->tm_hour;
1994  tm->tm_min = t0->tm_min;
1995  tm->tm_sec = t0->tm_sec;
1996 
1997  tm->tm_year += 1900;
1998  tm->tm_mon++;
1999 }
int64 pg_time_t
Definition: pgtime.h:23
struct pg_tm * pg_gmtime(const pg_time_t *timep)
Definition: localtime.c:1295
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 1587 of file timestamp.c.

References AdjustTimestampForTypmod(), and GetCurrentTransactionStartTimestamp().

Referenced by ExecEvalSQLValueFunction().

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

Definition at line 1601 of file timestamp.c.

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

Referenced by ExecEvalSQLValueFunction().

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

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

1902 {
1903  TimeOffset time;
1904  TimeOffset tfrac;
1905 
1906  tm->tm_year = span.month / MONTHS_PER_YEAR;
1907  tm->tm_mon = span.month % MONTHS_PER_YEAR;
1908  tm->tm_mday = span.day;
1909  time = span.time;
1910 
1911  tfrac = time / USECS_PER_HOUR;
1912  time -= tfrac * USECS_PER_HOUR;
1913  tm->tm_hour = tfrac;
1914  if (!SAMESIGN(tm->tm_hour, tfrac))
1915  ereport(ERROR,
1916  (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
1917  errmsg("interval out of range")));
1918  tfrac = time / USECS_PER_MINUTE;
1919  time -= tfrac * USECS_PER_MINUTE;
1920  tm->tm_min = tfrac;
1921  tfrac = time / USECS_PER_SEC;
1922  *fsec = time - (tfrac * USECS_PER_SEC);
1923  tm->tm_sec = tfrac;
1924 
1925  return 0;
1926 }
#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:45
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 4012 of file timestamp.c.

References isoweek2j(), and j2date().

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

4013 {
4014  j2date(isoweek2j(*year, woy), year, mon, mday);
4015 }
void j2date(int jd, int *year, int *month, int *day)
Definition: datetime.c:317
int isoweek2j(int year, int week)
Definition: timestamp.c:3992
int isoweek2j ( int  year,
int  week 
)

Definition at line 3992 of file timestamp.c.

References date2j(), and j2day().

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

3993 {
3994  int day0,
3995  day4;
3996 
3997  /* fourth day of current year */
3998  day4 = date2j(year, 1, 4);
3999 
4000  /* day0 == offset to first day of week (Monday) */
4001  day0 = j2day(day4 - 1);
4002 
4003  return ((week - 1) * 7) + (day4 - day0);
4004 }
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 4025 of file timestamp.c.

References isoweek2j(), and j2date().

Referenced by do_to_timestamp().

4026 {
4027  int jday;
4028 
4029  jday = isoweek2j(*year, isoweek);
4030  /* convert Gregorian week start (Sunday=1) to ISO week start (Monday=1) */
4031  if (wday > 1)
4032  jday += wday - 2;
4033  else
4034  jday += 6;
4035  j2date(jday, year, mon, mday);
4036 }
void j2date(int jd, int *year, int *month, int *day)
Definition: datetime.c:317
int isoweek2j(int year, int week)
Definition: timestamp.c:3992
Timestamp SetEpochTimestamp ( void  )

Definition at line 2002 of file timestamp.c.

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

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

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

Definition at line 1669 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(), and pg_stat_file().

1670 {
1671  TimestampTz result;
1672 
1673  result = (TimestampTz) tm -
1675  result *= USECS_PER_SEC;
1676 
1677  return result;
1678 }
#define USECS_PER_SEC
Definition: timestamp.h:94
int64 TimestampTz
Definition: timestamp.h:39
static struct pg_tm tm
Definition: localtime.c:103
#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 1757 of file timestamp.c.

References dt2time(), INT64CONST, j2date(), NULL, 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().

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

References start_time, and USECS_PER_SEC.

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

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

Definition at line 1709 of file timestamp.c.

References buf, EncodeDateTime(), EncodeSpecialTimestamp(), MAXDATELEN, NULL, 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().

1710 {
1711  static char buf[MAXDATELEN + 1];
1712  int tz;
1713  struct pg_tm tt,
1714  *tm = &tt;
1715  fsec_t fsec;
1716  const char *tzn;
1717 
1718  if (TIMESTAMP_NOT_FINITE(t))
1719  EncodeSpecialTimestamp(t, buf);
1720  else if (timestamp2tm(t, &tz, tm, &fsec, &tzn, NULL) == 0)
1721  EncodeDateTime(tm, fsec, true, tz, tzn, USE_ISO_DATES, buf);
1722  else
1723  strlcpy(buf, "(timestamp out of range)", sizeof(buf));
1724 
1725  return buf;
1726 }
#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:1757
void EncodeSpecialTimestamp(Timestamp dt, char *str)
Definition: timestamp.c:1522
Definition: pgtime.h:25
#define USE_ISO_DATES
Definition: miscadmin.h:210
static struct pg_tm tm
Definition: localtime.c:103
#define TIMESTAMP_NOT_FINITE(j)
Definition: timestamp.h:122
static char * buf
Definition: pg_test_fsync.c:65
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
#define NULL
Definition: c.h:226
pg_time_t timestamptz_to_time_t ( TimestampTz  t)

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

1692 {
1693  pg_time_t result;
1694 
1695  result = (pg_time_t) (t / USECS_PER_SEC +
1697 
1698  return result;
1699 }
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 1929 of file timestamp.c.

References Interval::day, INT64CONST, 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().

1930 {
1931  double total_months = (double) tm->tm_year * MONTHS_PER_YEAR + tm->tm_mon;
1932 
1933  if (total_months > INT_MAX || total_months < INT_MIN)
1934  return -1;
1935  span->month = total_months;
1936  span->day = tm->tm_mday;
1937  span->time = (((((tm->tm_hour * INT64CONST(60)) +
1938  tm->tm_min) * INT64CONST(60)) +
1939  tm->tm_sec) * USECS_PER_SEC) + fsec;
1940 
1941  return 0;
1942 }
#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
#define INT64CONST(x)
Definition: c.h:307
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 1853 of file timestamp.c.

References date2j(), dt2local(), IS_VALID_JULIAN, IS_VALID_TIMESTAMP, NULL, 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().

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

Variable Documentation

TimestampTz PgReloadTime

Definition at line 51 of file timestamp.c.

Referenced by pg_conf_load_time().

TimestampTz PgStartTime

Definition at line 48 of file timestamp.c.

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