PostgreSQL Source Code git master
Loading...
Searching...
No Matches
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 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 TimestampTzPlusSeconds(tz, s)   ((tz) + ((s) * (int64) 1000000))
 
#define timestamptz_cmp_internal(dt1, dt2)   timestamp_cmp_internal(dt1, dt2)
 

Functions

static Timestamp DatumGetTimestamp (Datum X)
 
static TimestampTz DatumGetTimestampTz (Datum X)
 
static IntervalDatumGetIntervalP (Datum X)
 
static Datum TimestampGetDatum (Timestamp X)
 
static Datum TimestampTzGetDatum (TimestampTz X)
 
static Datum IntervalPGetDatum (const Interval *X)
 
static uint64 TimestampDifferenceMicroseconds (TimestampTz start_time, TimestampTz stop_time)
 
int32 anytimestamp_typmod_check (bool istz, int32 typmod)
 
TimestampTz GetCurrentTimestamp (void)
 
TimestampTz GetSQLCurrentTimestamp (int32 typmod)
 
Timestamp GetSQLLocalTimestamp (int32 typmod)
 
void TimestampDifference (TimestampTz start_time, TimestampTz stop_time, long *secs, int *microsecs)
 
long TimestampDifferenceMilliseconds (TimestampTz start_time, TimestampTz stop_time)
 
bool TimestampDifferenceExceeds (TimestampTz start_time, TimestampTz stop_time, int msec)
 
bool TimestampDifferenceExceedsSeconds (TimestampTz start_time, TimestampTz stop_time, int threshold_sec)
 
TimestampTz time_t_to_timestamptz (pg_time_t tm)
 
pg_time_t timestamptz_to_time_t (TimestampTz t)
 
const chartimestamptz_to_str (TimestampTz t)
 
int tm2timestamp (struct pg_tm *tm, fsec_t fsec, int *tzp, Timestamp *result)
 
int timestamp2tm (Timestamp dt, int *tzp, struct pg_tm *tm, fsec_t *fsec, const char **tzn, pg_tz *attimezone)
 
void dt2time (Timestamp jd, int *hour, int *min, int *sec, fsec_t *fsec)
 
void interval2itm (Interval span, struct pg_itm *itm)
 
int itm2interval (struct pg_itm *itm, Interval *span)
 
int itmin2interval (struct pg_itm_in *itm_in, Interval *span)
 
Timestamp SetEpochTimestamp (void)
 
void GetEpochTime (struct pg_tm *tm)
 
int timestamp_cmp_internal (Timestamp dt1, Timestamp dt2)
 
TimestampTz timestamp2timestamptz_safe (Timestamp timestamp, Node *escontext)
 
Timestamp timestamptz2timestamp_safe (TimestampTz timestamp, Node *escontext)
 
int32 timestamp_cmp_timestamptz_internal (Timestamp timestampVal, TimestampTz dt2)
 
int isoweek2j (int year, int week)
 
void isoweek2date (int woy, int *year, int *mon, int *mday)
 
void isoweekdate2date (int isoweek, int wday, int *year, int *mon, int *mday)
 
int date2isoweek (int year, int mon, int mday)
 
int date2isoyear (int year, int mon, int mday)
 
int date2isoyearday (int year, int mon, int mday)
 
bool TimestampTimestampTzRequiresRewrite (void)
 

Variables

PGDLLIMPORT TimestampTz PgStartTime
 
PGDLLIMPORT TimestampTz PgReloadTime
 

Macro Definition Documentation

◆ INTERVAL_FULL_PRECISION

#define INTERVAL_FULL_PRECISION   (0xFFFF)

Definition at line 78 of file timestamp.h.

◆ INTERVAL_FULL_RANGE

#define INTERVAL_FULL_RANGE   (0x7FFF)

Definition at line 76 of file timestamp.h.

◆ INTERVAL_MASK

#define INTERVAL_MASK (   b)    (1 << (b))

Definition at line 73 of file timestamp.h.

◆ INTERVAL_PRECISION

#define INTERVAL_PRECISION (   t)    ((t) & INTERVAL_PRECISION_MASK)

Definition at line 81 of file timestamp.h.

◆ INTERVAL_PRECISION_MASK

#define INTERVAL_PRECISION_MASK   (0xFFFF)

Definition at line 79 of file timestamp.h.

◆ INTERVAL_RANGE

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

Definition at line 82 of file timestamp.h.

◆ INTERVAL_RANGE_MASK

#define INTERVAL_RANGE_MASK   (0x7FFF)

Definition at line 77 of file timestamp.h.

◆ INTERVAL_TYPMOD

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

Definition at line 80 of file timestamp.h.

◆ PG_GETARG_INTERVAL_P

#define PG_GETARG_INTERVAL_P (   n)    DatumGetIntervalP(PG_GETARG_DATUM(n))

Definition at line 65 of file timestamp.h.

◆ PG_GETARG_TIMESTAMP

#define PG_GETARG_TIMESTAMP (   n)    DatumGetTimestamp(PG_GETARG_DATUM(n))

Definition at line 63 of file timestamp.h.

◆ PG_GETARG_TIMESTAMPTZ

#define PG_GETARG_TIMESTAMPTZ (   n)    DatumGetTimestampTz(PG_GETARG_DATUM(n))

Definition at line 64 of file timestamp.h.

◆ PG_RETURN_INTERVAL_P

#define PG_RETURN_INTERVAL_P (   x)    return IntervalPGetDatum(x)

Definition at line 69 of file timestamp.h.

◆ PG_RETURN_TIMESTAMP

#define PG_RETURN_TIMESTAMP (   x)    return TimestampGetDatum(x)

Definition at line 67 of file timestamp.h.

◆ PG_RETURN_TIMESTAMPTZ

#define PG_RETURN_TIMESTAMPTZ (   x)    return TimestampTzGetDatum(x)

Definition at line 68 of file timestamp.h.

◆ TIMESTAMP_MASK

#define TIMESTAMP_MASK (   b)    (1 << (b))

Definition at line 72 of file timestamp.h.

◆ timestamptz_cmp_internal

#define timestamptz_cmp_internal (   dt1,
  dt2 
)    timestamp_cmp_internal(dt1, dt2)

Definition at line 143 of file timestamp.h.

◆ TimestampTzPlusMilliseconds

#define TimestampTzPlusMilliseconds (   tz,
  ms 
)    ((tz) + ((ms) * (int64) 1000))

Definition at line 85 of file timestamp.h.

◆ TimestampTzPlusSeconds

#define TimestampTzPlusSeconds (   tz,
 
)    ((tz) + ((s) * (int64) 1000000))

Definition at line 86 of file timestamp.h.

Function Documentation

◆ anytimestamp_typmod_check()

int32 anytimestamp_typmod_check ( bool  istz,
int32  typmod 
)
extern

Definition at line 116 of file timestamp.c.

117{
118 if (typmod < 0)
121 errmsg("TIMESTAMP(%d)%s precision must not be negative",
122 typmod, (istz ? " WITH TIME ZONE" : ""))));
123 if (typmod > MAX_TIMESTAMP_PRECISION)
124 {
127 errmsg("TIMESTAMP(%d)%s precision reduced to maximum allowed, %d",
128 typmod, (istz ? " WITH TIME ZONE" : ""),
131 }
132
133 return typmod;
134}
#define MAX_TIMESTAMP_PRECISION
Definition timestamp.h:92
int errcode(int sqlerrcode)
Definition elog.c:875
#define WARNING
Definition elog.h:37
#define ERROR
Definition elog.h:40
#define ereport(elevel,...)
Definition elog.h:152
static char * errmsg
static int fb(int x)

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

Referenced by anytimestamp_typmodin(), executeDateTimeMethod(), and transformSQLValueFunction().

◆ date2isoweek()

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

Definition at line 5326 of file timestamp.c.

5327{
5328 int day0,
5329 day4,
5330 dayn,
5331 week;
5332
5333 /* current day */
5334 dayn = date2j(year, mon, mday);
5335
5336 /* fourth day of current year */
5337 day4 = date2j(year, 1, 4);
5338
5339 /* day0 == offset to first day of week (Monday) */
5340 day0 = j2day(day4 - 1);
5341
5342 /*
5343 * We need the first week containing a Thursday, otherwise this day falls
5344 * into the previous year for purposes of counting weeks
5345 */
5346 if (dayn < day4 - day0)
5347 {
5348 day4 = date2j(year - 1, 1, 4);
5349
5350 /* day0 == offset to first day of week (Monday) */
5351 day0 = j2day(day4 - 1);
5352 }
5353
5354 week = (dayn - (day4 - day0)) / 7 + 1;
5355
5356 /*
5357 * Sometimes the last few days in a year will fall into the first week of
5358 * the next year, so check for this.
5359 */
5360 if (week >= 52)
5361 {
5362 day4 = date2j(year + 1, 1, 4);
5363
5364 /* day0 == offset to first day of week (Monday) */
5365 day0 = j2day(day4 - 1);
5366
5367 if (dayn >= day4 - day0)
5368 week = (dayn - (day4 - day0)) / 7 + 1;
5369 }
5370
5371 return week;
5372}
int j2day(int date)
Definition datetime.c:355
int date2j(int year, int month, int day)
Definition datetime.c:297

References date2j(), fb(), and j2day().

Referenced by DCH_to_char(), extract_date(), timestamp_part_common(), timestamp_trunc(), timestamptz_part_common(), and timestamptz_trunc_internal().

◆ date2isoyear()

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

Definition at line 5382 of file timestamp.c.

5383{
5384 int day0,
5385 day4,
5386 dayn,
5387 week;
5388
5389 /* current day */
5390 dayn = date2j(year, mon, mday);
5391
5392 /* fourth day of current year */
5393 day4 = date2j(year, 1, 4);
5394
5395 /* day0 == offset to first day of week (Monday) */
5396 day0 = j2day(day4 - 1);
5397
5398 /*
5399 * We need the first week containing a Thursday, otherwise this day falls
5400 * into the previous year for purposes of counting weeks
5401 */
5402 if (dayn < day4 - day0)
5403 {
5404 day4 = date2j(year - 1, 1, 4);
5405
5406 /* day0 == offset to first day of week (Monday) */
5407 day0 = j2day(day4 - 1);
5408
5409 year--;
5410 }
5411
5412 week = (dayn - (day4 - day0)) / 7 + 1;
5413
5414 /*
5415 * Sometimes the last few days in a year will fall into the first week of
5416 * the next year, so check for this.
5417 */
5418 if (week >= 52)
5419 {
5420 day4 = date2j(year + 1, 1, 4);
5421
5422 /* day0 == offset to first day of week (Monday) */
5423 day0 = j2day(day4 - 1);
5424
5425 if (dayn >= day4 - day0)
5426 year++;
5427 }
5428
5429 return year;
5430}

References date2j(), fb(), and j2day().

Referenced by date2isoyearday(), DCH_to_char(), extract_date(), timestamp_part_common(), and timestamptz_part_common().

◆ date2isoyearday()

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

Definition at line 5440 of file timestamp.c.

5441{
5442 return date2j(year, mon, mday) - isoweek2j(date2isoyear(year, mon, mday), 1) + 1;
5443}
int isoweek2j(int year, int week)
Definition timestamp.c:5272
int date2isoyear(int year, int mon, int mday)
Definition timestamp.c:5382

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

Referenced by DCH_to_char().

◆ DatumGetIntervalP()

◆ DatumGetTimestamp()

◆ DatumGetTimestampTz()

◆ dt2time()

void dt2time ( Timestamp  jd,
int hour,
int min,
int sec,
fsec_t fsec 
)
extern

Definition at line 1891 of file timestamp.c.

1892{
1893 TimeOffset time;
1894
1895 time = jd;
1896
1897 *hour = time / USECS_PER_HOUR;
1898 time -= (*hour) * USECS_PER_HOUR;
1899 *min = time / USECS_PER_MINUTE;
1900 time -= (*min) * USECS_PER_MINUTE;
1901 *sec = time / USECS_PER_SEC;
1902 *fsec = time - (*sec * USECS_PER_SEC);
1903} /* dt2time() */
#define USECS_PER_HOUR
Definition timestamp.h:132
#define USECS_PER_SEC
Definition timestamp.h:134
#define USECS_PER_MINUTE
Definition timestamp.h:133
int64 TimeOffset
Definition timestamp.h:40

References fb(), USECS_PER_HOUR, USECS_PER_MINUTE, and USECS_PER_SEC.

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

◆ GetCurrentTimestamp()

TimestampTz GetCurrentTimestamp ( void  )
extern

Definition at line 1649 of file timestamp.c.

1650{
1652 struct timeval tp;
1653
1654 gettimeofday(&tp, NULL);
1655
1656 result = (TimestampTz) tp.tv_sec -
1658 result = (result * USECS_PER_SEC) + tp.tv_usec;
1659
1660 return result;
1661}
uint32 result
#define UNIX_EPOCH_JDATE
Definition timestamp.h:234
#define SECS_PER_DAY
Definition timestamp.h:126
#define POSTGRES_EPOCH_JDATE
Definition timestamp.h:235
int gettimeofday(struct timeval *tp, void *tzp)

References fb(), gettimeofday(), POSTGRES_EPOCH_JDATE, result, SECS_PER_DAY, UNIX_EPOCH_JDATE, and USECS_PER_SEC.

Referenced by ApplyLauncherMain(), asyncQueueFillWarning(), autoprewarm_main(), AutoVacLauncherMain(), BackendStartup(), BackgroundWriterMain(), bbsink_copystream_archive_contents(), bbsink_copystream_end_archive(), bbsink_copystream_new(), bbsink_throttle_begin_backup(), check_log_duration(), CheckPointGuts(), CleanupBackend(), clock_timestamp(), CreateCheckPoint(), CreateEndOfRecoveryRecord(), CreateOverwriteContrecordRecord(), CreateRestartPoint(), dblink_cancel_query(), DetermineSleepTime(), disable_timeout(), disable_timeouts(), do_analyze_rel(), do_start_worker(), enable_startup_progress_timeout(), enable_timeout_after(), enable_timeout_at(), enable_timeout_every(), enable_timeouts(), entry_alloc(), entry_reset(), get_candidate_xid(), get_role_password(), GetCurrentTransactionStopTimestamp(), GetReplicationApplyDelay(), handle_sig_alarm(), has_startup_progress_timeout_expired(), heap_vacuum_rel(), initialize_prng(), InitProcessGlobals(), InvalidatePossiblyObsoleteSlot(), IoWorkerMain(), KnownAssignedXidsCompress(), launch_sync_worker(), launcher_determine_sleep(), libpqsrv_cancel(), LockBufferForCleanup(), log_disconnections(), LogCheckpointEnd(), logicalrep_worker_launch(), LogicalRepApplyLoop(), maybe_start_bgworkers(), maybe_start_io_workers(), pa_send_data(), PerformAuthentication(), PerformWalRecovery(), pg_promote(), pg_sleep(), pg_stash_advice_worker_main(), pgfdw_abort_cleanup_begin(), pgfdw_cancel_query(), pgfdw_exec_cleanup_query(), pgfdw_finish_abort_cleanup(), pgfdw_get_cleanup_result(), pgss_shmem_init(), pgstat_build_snapshot(), pgstat_report_activity(), pgstat_report_analyze(), pgstat_report_archiver(), pgstat_report_autovac(), pgstat_report_checksum_failures_in_db(), pgstat_report_replslotsync(), pgstat_report_stat(), pgstat_report_vacuum(), pgstat_reset(), pgstat_reset_after_failure(), pgstat_reset_counters(), pgstat_reset_of_kind(), pgstat_reset_slru(), PostgresMain(), PostgresSingleUserMain(), postmaster_child_launch(), PostmasterMain(), PrepareTransaction(), ProcessConfigFileInternal(), ProcessPendingWrites(), ProcessRepliesIfAny(), ProcessStandbyPSRequestMessage(), ProcessStandbyReplyMessage(), ProcessWalSndrMessage(), ProcSleep(), rebuild_database_list(), RecordTransactionAbort(), RecordTransactionAbortPrepared(), RecordTransactionCommitPrepared(), recoveryApplyDelay(), ReplicationSlotRelease(), request_publisher_status(), reschedule_timeouts(), ResolveRecoveryConflictWithBufferPin(), ResolveRecoveryConflictWithLock(), ResolveRecoveryConflictWithVirtualXIDs(), RestoreSlotFromDisk(), send_feedback(), SetCurrentStatementStartTimestamp(), SetupApplyOrSyncWorker(), should_stop_conflict_info_retention(), StartBackgroundWorker(), StartTransaction(), test_pattern(), test_random(), test_random_operations(), throttle(), update_synced_slots_inactive_since(), UpdateWorkerStats(), wait_for_local_flush(), WaitExceedsMaxStandbyDelay(), WaitForLSN(), WaitForWalSummarization(), WaitForWALToBecomeAvailable(), WalReceiverMain(), WalSndCheckShutdownTimeout(), WalSndDone(), WalSndKeepalive(), WalSndLoop(), WalSndUpdateProgress(), WalSndWaitForWal(), WalSndWriteData(), XLogBackgroundFlush(), XLogFileRead(), XLogPrefetchResetStats(), XLogPrefetchShmemInit(), XLogRestorePoint(), XLogSendPhysical(), XLogWalRcvSendHSFeedback(), and XLogWalRcvSendReply().

◆ GetEpochTime()

void GetEpochTime ( struct pg_tm tm)
extern

Definition at line 2180 of file timestamp.c.

2181{
2182 struct pg_tm *t0;
2183 pg_time_t epoch = 0;
2184
2185 t0 = pg_gmtime(&epoch);
2186
2187 if (t0 == NULL)
2188 elog(ERROR, "could not convert epoch to timestamp: %m");
2189
2190 tm->tm_year = t0->tm_year;
2191 tm->tm_mon = t0->tm_mon;
2192 tm->tm_mday = t0->tm_mday;
2193 tm->tm_hour = t0->tm_hour;
2194 tm->tm_min = t0->tm_min;
2195 tm->tm_sec = t0->tm_sec;
2196
2197 tm->tm_year += 1900;
2198 tm->tm_mon++;
2199}
#define elog(elevel,...)
Definition elog.h:228
static struct pg_tm tm
Definition localtime.c:104
int64 pg_time_t
Definition pgtime.h:23
struct pg_tm * pg_gmtime(const pg_time_t *timep)
Definition localtime.c:1392
Definition pgtime.h:35
int tm_hour
Definition pgtime.h:38
int tm_mday
Definition pgtime.h:39
int tm_mon
Definition pgtime.h:40
int tm_min
Definition pgtime.h:37
int tm_sec
Definition pgtime.h:36
int tm_year
Definition pgtime.h:41
static const unsigned __int64 epoch

References elog, epoch, ERROR, fb(), pg_gmtime(), tm, pg_tm::tm_hour, pg_tm::tm_mday, pg_tm::tm_min, pg_tm::tm_mon, pg_tm::tm_sec, and pg_tm::tm_year.

Referenced by date_in(), PGTYPESdate_from_asc(), SetEpochTimestamp(), and SetEpochTimestamp().

◆ GetSQLCurrentTimestamp()

TimestampTz GetSQLCurrentTimestamp ( int32  typmod)
extern

Definition at line 1667 of file timestamp.c.

1668{
1669 TimestampTz ts;
1670
1672 if (typmod >= 0)
1673 AdjustTimestampForTypmod(&ts, typmod, NULL);
1674 return ts;
1675}
bool AdjustTimestampForTypmod(Timestamp *time, int32 typmod, Node *escontext)
Definition timestamp.c:363
TimestampTz GetCurrentTransactionStartTimestamp(void)
Definition xact.c:872

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

Referenced by ExecEvalSQLValueFunction().

◆ GetSQLLocalTimestamp()

Timestamp GetSQLLocalTimestamp ( int32  typmod)
extern

Definition at line 1681 of file timestamp.c.

1682{
1683 Timestamp ts;
1684
1686 if (typmod >= 0)
1687 AdjustTimestampForTypmod(&ts, typmod, NULL);
1688 return ts;
1689}
static Timestamp timestamptz2timestamp(TimestampTz timestamp)
Definition timestamp.c:6555

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

Referenced by ExecEvalSQLValueFunction().

◆ interval2itm()

void interval2itm ( Interval  span,
struct pg_itm itm 
)
extern

Definition at line 2057 of file timestamp.c.

2058{
2059 TimeOffset time;
2061
2062 itm->tm_year = span.month / MONTHS_PER_YEAR;
2063 itm->tm_mon = span.month % MONTHS_PER_YEAR;
2064 itm->tm_mday = span.day;
2065 time = span.time;
2066
2067 tfrac = time / USECS_PER_HOUR;
2068 time -= tfrac * USECS_PER_HOUR;
2069 itm->tm_hour = tfrac;
2070 tfrac = time / USECS_PER_MINUTE;
2071 time -= tfrac * USECS_PER_MINUTE;
2072 itm->tm_min = (int) tfrac;
2073 tfrac = time / USECS_PER_SEC;
2074 time -= tfrac * USECS_PER_SEC;
2075 itm->tm_sec = (int) tfrac;
2076 itm->tm_usec = (int) time;
2077}
#define MONTHS_PER_YEAR
Definition timestamp.h:108

References fb(), MONTHS_PER_YEAR, USECS_PER_HOUR, USECS_PER_MINUTE, and USECS_PER_SEC.

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

◆ IntervalPGetDatum()

◆ isoweek2date()

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

Definition at line 5293 of file timestamp.c.

5294{
5295 j2date(isoweek2j(*year, woy), year, mon, mday);
5296}
void j2date(int jd, int *year, int *month, int *day)
Definition datetime.c:322

References fb(), isoweek2j(), and j2date().

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

◆ isoweek2j()

int isoweek2j ( int  year,
int  week 
)
extern

Definition at line 5272 of file timestamp.c.

5273{
5274 int day0,
5275 day4;
5276
5277 /* fourth day of current year */
5278 day4 = date2j(year, 1, 4);
5279
5280 /* day0 == offset to first day of week (Monday) */
5281 day0 = j2day(day4 - 1);
5282
5283 return ((week - 1) * 7) + (day4 - day0);
5284}

References date2j(), fb(), and j2day().

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

◆ isoweekdate2date()

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

Definition at line 5307 of file timestamp.c.

5308{
5309 int jday;
5310
5311 jday = isoweek2j(*year, isoweek);
5312 /* convert Gregorian week start (Sunday=1) to ISO week start (Monday=1) */
5313 if (wday > 1)
5314 jday += wday - 2;
5315 else
5316 jday += 6;
5317 j2date(jday, year, mon, mday);
5318}

References fb(), isoweek2j(), and j2date().

Referenced by do_to_timestamp().

◆ itm2interval()

int itm2interval ( struct pg_itm itm,
Interval span 
)
extern

Definition at line 2088 of file timestamp.c.

2089{
2090 int64 total_months = (int64) itm->tm_year * MONTHS_PER_YEAR + itm->tm_mon;
2091
2093 return -1;
2094 span->month = (int32) total_months;
2095 span->day = itm->tm_mday;
2097 &span->time))
2098 return -1;
2099 /* tm_min, tm_sec are 32 bits, so intermediate products can't overflow */
2100 if (pg_add_s64_overflow(span->time, itm->tm_min * USECS_PER_MINUTE,
2101 &span->time))
2102 return -1;
2103 if (pg_add_s64_overflow(span->time, itm->tm_sec * USECS_PER_SEC,
2104 &span->time))
2105 return -1;
2106 if (pg_add_s64_overflow(span->time, itm->tm_usec,
2107 &span->time))
2108 return -1;
2110 return -1;
2111 return 0;
2112}
int64_t int64
Definition c.h:621
int32_t int32
Definition c.h:620
#define INTERVAL_NOT_FINITE(i)
Definition timestamp.h:195
static bool pg_mul_s64_overflow(int64 a, int64 b, int64 *result)
Definition int.h:293
static bool pg_add_s64_overflow(int64 a, int64 b, int64 *result)
Definition int.h:235

References fb(), INTERVAL_NOT_FINITE, MONTHS_PER_YEAR, pg_add_s64_overflow(), pg_mul_s64_overflow(), USECS_PER_HOUR, USECS_PER_MINUTE, and USECS_PER_SEC.

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

◆ itmin2interval()

int itmin2interval ( struct pg_itm_in itm_in,
Interval span 
)
extern

Definition at line 2127 of file timestamp.c.

2128{
2129 int64 total_months = (int64) itm_in->tm_year * MONTHS_PER_YEAR + itm_in->tm_mon;
2130
2132 return -1;
2133 span->month = (int32) total_months;
2134 span->day = itm_in->tm_mday;
2135 span->time = itm_in->tm_usec;
2136 return 0;
2137}

References fb(), and MONTHS_PER_YEAR.

Referenced by interval_in(), pg_timezone_abbrevs_abbrevs(), pg_timezone_abbrevs_zone(), and pg_timezone_names().

◆ SetEpochTimestamp()

Timestamp SetEpochTimestamp ( void  )
extern

Definition at line 2202 of file timestamp.c.

2203{
2204 Timestamp dt;
2205 struct pg_tm tt,
2206 *tm = &tt;
2207
2209 /* we don't bother to test for failure ... */
2210 tm2timestamp(tm, 0, NULL, &dt);
2211
2212 return dt;
2213} /* SetEpochTimestamp() */
void GetEpochTime(struct pg_tm *tm)
Definition timestamp.c:2180
int tm2timestamp(struct tm *tm, fsec_t fsec, int *tzp, timestamp *result)
Definition timestamp.c:38

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

◆ time_t_to_timestamptz()

TimestampTz time_t_to_timestamptz ( pg_time_t  tm)
extern

◆ timestamp2timestamptz_safe()

TimestampTz timestamp2timestamptz_safe ( Timestamp  timestamp,
Node escontext 
)
extern

Definition at line 6493 of file timestamp.c.

6494{
6496 struct pg_tm tt,
6497 *tm = &tt;
6498 fsec_t fsec;
6499 int tz;
6500
6502 return timestamp;
6503
6504 /* timestamp2tm should not fail on valid timestamps, but cope */
6505 if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) == 0)
6506 {
6508
6509 result = dt2local(timestamp, -tz);
6510
6512 return result;
6513 }
6514
6515 if (timestamp < 0)
6517 else
6519
6520 ereturn(escontext, result,
6522 errmsg("timestamp out of range")));
6523}
int DetermineTimeZoneOffset(struct pg_tm *tm, pg_tz *tzp)
Definition datetime.c:1608
int32 fsec_t
Definition timestamp.h:41
#define TIMESTAMP_NOBEGIN(j)
Definition timestamp.h:159
#define IS_VALID_TIMESTAMP(t)
Definition timestamp.h:267
#define TIMESTAMP_NOT_FINITE(j)
Definition timestamp.h:169
#define TIMESTAMP_NOEND(j)
Definition timestamp.h:164
#define ereturn(context, dummy_value,...)
Definition elog.h:280
static int timestamp2tm(timestamp dt, int *tzp, struct tm *tm, fsec_t *fsec, const char **tzn)
Definition timestamp.c:90
static timestamp dt2local(timestamp dt, int tz)
Definition timestamp.c:23
PGDLLIMPORT pg_tz * session_timezone
Definition pgtz.c:28
int64 timestamp

References DetermineTimeZoneOffset(), dt2local(), ereturn, errcode(), errmsg, fb(), IS_VALID_TIMESTAMP, result, session_timezone, timestamp2tm(), TIMESTAMP_NOBEGIN, TIMESTAMP_NOEND, TIMESTAMP_NOT_FINITE, and tm.

Referenced by cvt_timestamp_timestamptz(), timestamp2timestamptz(), timestamp_cmp_timestamptz_internal(), and timestamp_timestamptz().

◆ timestamp2tm()

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

Definition at line 1918 of file timestamp.c.

1919{
1921 Timestamp time;
1923
1924 /* Use session timezone if caller asks for default */
1925 if (attimezone == NULL)
1926 attimezone = session_timezone;
1927
1928 time = dt;
1929 TMODULO(time, date, USECS_PER_DAY);
1930
1931 if (time < INT64CONST(0))
1932 {
1933 time += USECS_PER_DAY;
1934 date -= 1;
1935 }
1936
1937 /* add offset to go from J2000 back to standard Julian date */
1939
1940 /* Julian day routine does not work for negative Julian days */
1942 return -1;
1943
1944 j2date((int) date, &tm->tm_year, &tm->tm_mon, &tm->tm_mday);
1945 dt2time(time, &tm->tm_hour, &tm->tm_min, &tm->tm_sec, fsec);
1946
1947 /* Done if no TZ conversion wanted */
1948 if (tzp == NULL)
1949 {
1950 tm->tm_isdst = -1;
1951 tm->tm_gmtoff = 0;
1952 tm->tm_zone = NULL;
1953 if (tzn != NULL)
1954 *tzn = NULL;
1955 return 0;
1956 }
1957
1958 /*
1959 * If the time falls within the range of pg_time_t, use pg_localtime() to
1960 * rotate to the local time zone.
1961 *
1962 * First, convert to an integral timestamp, avoiding possibly
1963 * platform-specific roundoff-in-wrong-direction errors, and adjust to
1964 * Unix epoch. Then see if we can convert to pg_time_t without loss. This
1965 * coding avoids hardwiring any assumptions about the width of pg_time_t,
1966 * so it should behave sanely on machines without int64.
1967 */
1968 dt = (dt - *fsec) / USECS_PER_SEC +
1970 utime = (pg_time_t) dt;
1971 if ((Timestamp) utime == dt)
1972 {
1973 struct pg_tm *tx = pg_localtime(&utime, attimezone);
1974
1975 tm->tm_year = tx->tm_year + 1900;
1976 tm->tm_mon = tx->tm_mon + 1;
1977 tm->tm_mday = tx->tm_mday;
1978 tm->tm_hour = tx->tm_hour;
1979 tm->tm_min = tx->tm_min;
1980 tm->tm_sec = tx->tm_sec;
1981 tm->tm_isdst = tx->tm_isdst;
1982 tm->tm_gmtoff = tx->tm_gmtoff;
1983 tm->tm_zone = tx->tm_zone;
1984 *tzp = -tm->tm_gmtoff;
1985 if (tzn != NULL)
1986 *tzn = tm->tm_zone;
1987 }
1988 else
1989 {
1990 /*
1991 * When out of range of pg_time_t, treat as GMT
1992 */
1993 *tzp = 0;
1994 /* Mark this as *no* time zone available */
1995 tm->tm_isdst = -1;
1996 tm->tm_gmtoff = 0;
1997 tm->tm_zone = NULL;
1998 if (tzn != NULL)
1999 *tzn = NULL;
2000 }
2001
2002 return 0;
2003}
void dt2time(Timestamp jd, int *hour, int *min, int *sec, fsec_t *fsec)
Definition timestamp.c:1891
#define INT64CONST(x)
Definition c.h:630
#define USECS_PER_DAY
Definition timestamp.h:131
#define TMODULO(t, q, u)
Definition datetime.h:250
struct pg_tm * pg_localtime(const pg_time_t *timep, const pg_tz *tz)
Definition localtime.c:1347
long date
Definition pgtypes_date.h:9
const char * tm_zone
Definition pgtime.h:46
int tm_isdst
Definition pgtime.h:44
long int tm_gmtoff
Definition pgtime.h:45

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

Referenced by DetermineTimeZoneAbbrevOffsetTS(), executeDateTimeMethod(), GetCurrentTimeUsec(), JsonEncodeDateTime(), map_sql_value_to_xml_value(), pg_get_role_ddl_internal(), pg_timezone_names(), timestamp2date_safe(), timestamp_time(), timestamp_to_char(), timestamptz2date_safe(), timestamptz_time(), timestamptz_timetz(), timestamptz_to_char(), and timetz_zone().

◆ timestamp_cmp_internal()

int timestamp_cmp_internal ( Timestamp  dt1,
Timestamp  dt2 
)
extern

◆ timestamp_cmp_timestamptz_internal()

int32 timestamp_cmp_timestamptz_internal ( Timestamp  timestampVal,
TimestampTz  dt2 
)
extern

Definition at line 2375 of file timestamp.c.

2376{
2379
2381 if (escontext.error_occurred)
2382 {
2384 {
2385 /* dt1 is larger than any finite timestamp, but less than infinity */
2386 return TIMESTAMP_IS_NOEND(dt2) ? -1 : +1;
2387 }
2389 {
2390 /* dt1 is less than any finite timestamp, but more than -infinity */
2391 return TIMESTAMP_IS_NOBEGIN(dt2) ? +1 : -1;
2392 }
2393 }
2394
2396}
TimestampTz timestamp2timestamptz_safe(Timestamp timestamp, Node *escontext)
Definition timestamp.c:6493
#define TIMESTAMP_IS_NOEND(j)
Definition timestamp.h:167
#define TIMESTAMP_IS_NOBEGIN(j)
Definition timestamp.h:162
Definition nodes.h:135
#define timestamptz_cmp_internal(dt1, dt2)
Definition timestamp.h:143

References ErrorSaveContext::error_occurred, fb(), timestamp2timestamptz_safe(), TIMESTAMP_IS_NOBEGIN, TIMESTAMP_IS_NOEND, and timestamptz_cmp_internal.

Referenced by cmpTimestampToTimestampTz(), timestamp_cmp_timestamptz(), timestamp_eq_timestamptz(), timestamp_ge_timestamptz(), timestamp_gt_timestamptz(), timestamp_le_timestamptz(), timestamp_lt_timestamptz(), timestamp_ne_timestamptz(), timestamptz_cmp_timestamp(), timestamptz_eq_timestamp(), timestamptz_ge_timestamp(), timestamptz_gt_timestamp(), timestamptz_le_timestamp(), timestamptz_lt_timestamp(), and timestamptz_ne_timestamp().

◆ TimestampDifference()

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

◆ TimestampDifferenceExceeds()

◆ TimestampDifferenceExceedsSeconds()

bool TimestampDifferenceExceedsSeconds ( TimestampTz  start_time,
TimestampTz  stop_time,
int  threshold_sec 
)
extern

Definition at line 1803 of file timestamp.c.

1806{
1807 long secs;
1808 int usecs;
1809
1810 /* Calculate the difference in seconds */
1812
1813 return (secs >= threshold_sec);
1814}
void TimestampDifference(TimestampTz start_time, TimestampTz stop_time, long *secs, int *microsecs)
Definition timestamp.c:1729

References fb(), start_time, and TimestampDifference().

Referenced by DetermineSlotInvalidationCause().

◆ TimestampDifferenceMicroseconds()

static uint64 TimestampDifferenceMicroseconds ( TimestampTz  start_time,
TimestampTz  stop_time 
)
inlinestatic

Definition at line 90 of file timestamp.h.

92{
93 if (start_time >= stop_time)
94 return 0;
95 return (uint64) stop_time - start_time;
96}
uint64_t uint64
Definition c.h:625

References fb(), and start_time.

Referenced by get_role_password(), and PostgresMain().

◆ TimestampDifferenceMilliseconds()

long TimestampDifferenceMilliseconds ( TimestampTz  start_time,
TimestampTz  stop_time 
)
extern

Definition at line 1765 of file timestamp.c.

1766{
1768
1769 /* Deal with zero or negative elapsed time quickly. */
1770 if (start_time >= stop_time)
1771 return 0;
1772 /* To not fail with timestamp infinities, we must detect overflow. */
1774 return (long) INT_MAX;
1775 if (diff >= (INT_MAX * INT64CONST(1000) - 999))
1776 return (long) INT_MAX;
1777 else
1778 return (long) ((diff + 999) / 1000);
1779}
static bool pg_sub_s64_overflow(int64 a, int64 b, int64 *result)
Definition int.h:262

References fb(), INT64CONST, pg_sub_s64_overflow(), and start_time.

Referenced by ApplyLauncherMain(), autoprewarm_main(), bbsink_copystream_archive_contents(), DetermineSleepTime(), do_analyze_rel(), GetReplicationApplyDelay(), GetReplicationTransferLatency(), IoWorkerMain(), libpqsrv_cancel(), LogCheckpointEnd(), pg_stash_advice_worker_main(), pgfdw_get_cleanup_result(), pgstat_report_analyze(), pgstat_report_vacuum(), recoveryApplyDelay(), wait_for_local_flush(), WaitForLSN(), WaitForWalSummarization(), WaitForWALToBecomeAvailable(), WalReceiverMain(), and WalSndComputeSleeptime().

◆ TimestampGetDatum()

◆ TimestampTimestampTzRequiresRewrite()

bool TimestampTimestampTzRequiresRewrite ( void  )
extern

Definition at line 6456 of file timestamp.c.

6457{
6458 long offset;
6459
6460 if (pg_get_timezone_offset(session_timezone, &offset) && offset == 0)
6461 return false;
6462 return true;
6463}
bool pg_get_timezone_offset(const pg_tz *tz, long int *gmtoff)
Definition localtime.c:1968

References pg_get_timezone_offset(), and session_timezone.

Referenced by ATColumnChangeRequiresRewrite().

◆ timestamptz2timestamp_safe()

Timestamp timestamptz2timestamp_safe ( TimestampTz  timestamp,
Node escontext 
)
extern

Definition at line 6571 of file timestamp.c.

6572{
6574 struct pg_tm tt,
6575 *tm = &tt;
6576 fsec_t fsec;
6577 int tz;
6578
6580 result = timestamp;
6581 else
6582 {
6583 if (timestamp2tm(timestamp, &tz, tm, &fsec, NULL, NULL) != 0)
6584 {
6585 if (timestamp < 0)
6587 else
6589
6590 ereturn(escontext, result,
6592 errmsg("timestamp out of range")));
6593 }
6594 if (tm2timestamp(tm, fsec, NULL, &result) != 0)
6595 {
6596 if (timestamp < 0)
6598 else
6600
6601 ereturn(escontext, result,
6603 errmsg("timestamp out of range")));
6604 }
6605 }
6606 return result;
6607}

References ereturn, errcode(), errmsg, fb(), result, timestamp2tm(), TIMESTAMP_NOBEGIN, TIMESTAMP_NOEND, TIMESTAMP_NOT_FINITE, tm, and tm2timestamp().

Referenced by cvt_timestamptz_timestamp(), timestamptz2timestamp(), and timestamptz_timestamp().

◆ timestamptz_to_str()

const char * timestamptz_to_str ( TimestampTz  t)
extern

Definition at line 1870 of file timestamp.c.

1871{
1872 static char buf[MAXDATELEN + 1];
1873 int tz;
1874 struct pg_tm tt,
1875 *tm = &tt;
1876 fsec_t fsec;
1877 const char *tzn;
1878
1879 if (TIMESTAMP_NOT_FINITE(t))
1881 else if (timestamp2tm(t, &tz, tm, &fsec, &tzn, NULL) == 0)
1882 EncodeDateTime(tm, fsec, true, tz, tzn, USE_ISO_DATES, buf);
1883 else
1884 strlcpy(buf, "(timestamp out of range)", sizeof(buf));
1885
1886 return buf;
1887}
void EncodeDateTime(struct pg_tm *tm, fsec_t fsec, bool print_tz, int tz, const char *tzn, int style, char *str)
Definition datetime.c:4496
void EncodeSpecialTimestamp(Timestamp dt, char *str)
Definition timestamp.c:1591
#define MAXDATELEN
Definition datetime.h:200
#define USE_ISO_DATES
Definition miscadmin.h:240
static char buf[DEFAULT_XLOG_SEG_SIZE]
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition strlcpy.c:45

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

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

◆ timestamptz_to_time_t()

pg_time_t timestamptz_to_time_t ( TimestampTz  t)
extern

◆ TimestampTzGetDatum()

◆ tm2timestamp()

int tm2timestamp ( struct pg_tm tm,
fsec_t  fsec,
int tzp,
Timestamp result 
)
extern

Definition at line 2015 of file timestamp.c.

2016{
2018 TimeOffset time;
2019
2020 /* Prevent overflow in Julian-day routines */
2022 {
2023 *result = 0; /* keep compiler quiet */
2024 return -1;
2025 }
2026
2028 time = time2t(tm->tm_hour, tm->tm_min, tm->tm_sec, fsec);
2029
2032 {
2033 *result = 0; /* keep compiler quiet */
2034 return -1;
2035 }
2036 if (tzp != NULL)
2037 *result = dt2local(*result, -(*tzp));
2038
2039 /* final range check catches just-out-of-range timestamps */
2041 {
2042 *result = 0; /* keep compiler quiet */
2043 return -1;
2044 }
2045
2046 return 0;
2047}
#define unlikely(x)
Definition c.h:438
#define IS_VALID_JULIAN(y, m, d)
Definition timestamp.h:227
static int64 time2t(const int hour, const int min, const int sec, const fsec_t fsec)
Definition timestamp.c:17

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

Referenced by check_recovery_target_time(), parse_datetime(), PGTYPEStimestamp_defmt_scan(), and to_timestamp().

Variable Documentation

◆ PgReloadTime

PGDLLIMPORT TimestampTz PgReloadTime
extern

Definition at line 48 of file timestamp.c.

Referenced by pg_conf_load_time(), and ProcessConfigFileInternal().

◆ PgStartTime

PGDLLIMPORT TimestampTz PgStartTime
extern

Definition at line 45 of file timestamp.c.

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