PostgreSQL Source Code git master
Macros | Functions | Variables
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_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 Datum  IntervalPGetDatum (const Interval *X)
 
 
int32  anytimestamp_typmod_check (bool istz, int32 typmod)
 
 
 
 
void  TimestampDifference (TimestampTz start_time, TimestampTz stop_time, long *secs, int *microsecs)
 
 
 
 
 
 
const char *  timestamptz_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)
 
 
void  GetEpochTime (struct pg_tm *tm)
 
 
 
 
 
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

 
 

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 
)    ((((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,
  s 
)    ((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 
)

Definition at line 125 of file timestamp.c.

126{
127 if (typmod < 0)
129 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
130 errmsg("TIMESTAMP(%d)%s precision must not be negative",
131 typmod, (istz ? " WITH TIME ZONE" : ""))));
132 if (typmod > MAX_TIMESTAMP_PRECISION)
133 {
135 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
136 errmsg("TIMESTAMP(%d)%s precision reduced to maximum allowed, %d",
137 typmod, (istz ? " WITH TIME ZONE" : ""),
140 }
141
142 return typmod;
143}
#define MAX_TIMESTAMP_PRECISION
Definition: timestamp.h:92
int errcode(int sqlerrcode)
Definition: elog.c:854
int errmsg(const char *fmt,...)
Definition: elog.c:1071
#define WARNING
Definition: elog.h:36
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:150

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

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

date2isoweek()

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

Definition at line 5292 of file timestamp.c.

5293{
5294 int day0,
5295 day4,
5296 dayn,
5297 week;
5298
5299 /* current day */
5300 dayn = date2j(year, mon, mday);
5301
5302 /* fourth day of current year */
5303 day4 = date2j(year, 1, 4);
5304
5305 /* day0 == offset to first day of week (Monday) */
5306 day0 = j2day(day4 - 1);
5307
5308 /*
5309 * We need the first week containing a Thursday, otherwise this day falls
5310 * into the previous year for purposes of counting weeks
5311 */
5312 if (dayn < day4 - day0)
5313 {
5314 day4 = date2j(year - 1, 1, 4);
5315
5316 /* day0 == offset to first day of week (Monday) */
5317 day0 = j2day(day4 - 1);
5318 }
5319
5320 week = (dayn - (day4 - day0)) / 7 + 1;
5321
5322 /*
5323 * Sometimes the last few days in a year will fall into the first week of
5324 * the next year, so check for this.
5325 */
5326 if (week >= 52)
5327 {
5328 day4 = date2j(year + 1, 1, 4);
5329
5330 /* day0 == offset to first day of week (Monday) */
5331 day0 = j2day(day4 - 1);
5332
5333 if (dayn >= day4 - day0)
5334 week = (dayn - (day4 - day0)) / 7 + 1;
5335 }
5336
5337 return week;
5338}
int j2day(int date)
Definition: datetime.c:354
int date2j(int year, int month, int day)
Definition: datetime.c:296

References date2j(), and j2day().

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

date2isoyear()

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

Definition at line 5347 of file timestamp.c.

5348{
5349 int day0,
5350 day4,
5351 dayn,
5352 week;
5353
5354 /* current day */
5355 dayn = date2j(year, mon, mday);
5356
5357 /* fourth day of current year */
5358 day4 = date2j(year, 1, 4);
5359
5360 /* day0 == offset to first day of week (Monday) */
5361 day0 = j2day(day4 - 1);
5362
5363 /*
5364 * We need the first week containing a Thursday, otherwise this day falls
5365 * into the previous year for purposes of counting weeks
5366 */
5367 if (dayn < day4 - day0)
5368 {
5369 day4 = date2j(year - 1, 1, 4);
5370
5371 /* day0 == offset to first day of week (Monday) */
5372 day0 = j2day(day4 - 1);
5373
5374 year--;
5375 }
5376
5377 week = (dayn - (day4 - day0)) / 7 + 1;
5378
5379 /*
5380 * Sometimes the last few days in a year will fall into the first week of
5381 * the next year, so check for this.
5382 */
5383 if (week >= 52)
5384 {
5385 day4 = date2j(year + 1, 1, 4);
5386
5387 /* day0 == offset to first day of week (Monday) */
5388 day0 = j2day(day4 - 1);
5389
5390 if (dayn >= day4 - day0)
5391 year++;
5392 }
5393
5394 return year;
5395}

References date2j(), and j2day().

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

date2isoyearday()

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

Definition at line 5404 of file timestamp.c.

5405{
5406 return date2j(year, mon, mday) - isoweek2j(date2isoyear(year, mon, mday), 1) + 1;
5407}
int isoweek2j(int year, int week)
Definition: timestamp.c:5241
int date2isoyear(int year, int mon, int mday)
Definition: timestamp.c:5347

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

Referenced by DCH_to_char().

DatumGetIntervalP()

static Interval * DatumGetIntervalP ( Datum  X )
inlinestatic

Definition at line 40 of file timestamp.h.

41{
42 return (Interval *) DatumGetPointer(X);
43}
static Pointer DatumGetPointer(Datum X)
Definition: postgres.h:322

References DatumGetPointer().

Referenced by abs_interval(), check_timezone(), convert_timevalue_to_scalar(), gbt_intv_compress(), gbt_time_dist(), gbt_time_penalty(), gbt_ts_dist(), generate_series_timestamp_support(), in_range_interval_interval(), interval_dist(), time_dist(), timestamp_mi(), ts_dist(), and tstz_dist().

DatumGetTimestamp()

static Timestamp DatumGetTimestamp ( Datum  X )
inlinestatic

Definition at line 28 of file timestamp.h.

29{
30 return (Timestamp) DatumGetInt64(X);
31}
int64 Timestamp
Definition: timestamp.h:38
static int64 DatumGetInt64(Datum X)
Definition: postgres.h:393

References DatumGetInt64().

Referenced by compareDatetime(), convert_timevalue_to_scalar(), cvt_timestamp_date(), cvt_timestamp_timestamptz(), executeDateTimeMethod(), gbt_num_compress(), generate_series_timestamp(), generate_series_timestamp_support(), in_range_timestamp_interval(), JsonEncodeDateTime(), map_sql_value_to_xml_value(), timestamp_decrement(), and timestamp_increment().

DatumGetTimestampTz()

static TimestampTz DatumGetTimestampTz ( Datum  X )
inlinestatic

Definition at line 34 of file timestamp.h.

35{
36 return (TimestampTz) DatumGetInt64(X);
37}
int64 TimestampTz
Definition: timestamp.h:39

References DatumGetInt64().

Referenced by compareDatetime(), convert_timevalue_to_scalar(), cvt_timestamptz_date(), cvt_timestamptz_timestamp(), executeDateTimeMethod(), gbt_tstz_compress(), get_role_password(), JsonEncodeDateTime(), uuidv7_interval(), and validateRecoveryParameters().

dt2time()

void dt2time ( Timestamp  jd,
int *  hour,
int *  min,
int *  sec,
fsec_tfsec 
)

Definition at line 1883 of file timestamp.c.

1884{
1885 TimeOffset time;
1886
1887 time = jd;
1888
1889 *hour = time / USECS_PER_HOUR;
1890 time -= (*hour) * USECS_PER_HOUR;
1891 *min = time / USECS_PER_MINUTE;
1892 time -= (*min) * USECS_PER_MINUTE;
1893 *sec = time / USECS_PER_SEC;
1894 *fsec = time - (*sec * USECS_PER_SEC);
1895} /* 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 USECS_PER_HOUR, USECS_PER_MINUTE, and USECS_PER_SEC.

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

GetCurrentTimestamp()

TimestampTz GetCurrentTimestamp ( void  )

Definition at line 1645 of file timestamp.c.

1646{
1647 TimestampTz result;
1648 struct timeval tp;
1649
1650 gettimeofday(&tp, NULL);
1651
1652 result = (TimestampTz) tp.tv_sec -
1654 result = (result * USECS_PER_SEC) + tp.tv_usec;
1655
1656 return result;
1657}
#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 gettimeofday(), POSTGRES_EPOCH_JDATE, 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(), KnownAssignedXidsCompress(), launcher_determine_sleep(), libpqsrv_cancel(), LockBufferForCleanup(), log_disconnections(), LogCheckpointEnd(), logicalrep_worker_launch(), LogicalRepApplyLoop(), maybe_start_bgworkers(), pa_send_data(), PerformAuthentication(), PerformWalRecovery(), pg_sleep(), pgfdw_abort_cleanup_begin(), pgfdw_cancel_query(), pgfdw_exec_cleanup_query(), pgfdw_finish_abort_cleanup(), pgfdw_get_cleanup_result(), pgss_shmem_startup(), pgstat_build_snapshot(), pgstat_report_activity(), pgstat_report_analyze(), pgstat_report_archiver(), pgstat_report_autovac(), pgstat_report_checksum_failures_in_db(), pgstat_report_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(), process_syncing_tables_for_apply(), 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(), WaitForWalSummarization(), WaitForWALToBecomeAvailable(), WalReceiverMain(), WalSndKeepalive(), WalSndLoop(), WalSndUpdateProgress(), WalSndWaitForWal(), WalSndWriteData(), XLogBackgroundFlush(), XLogFileRead(), XLogPrefetchResetStats(), XLogPrefetchShmemInit(), XLogRestorePoint(), XLogSendPhysical(), XLogWalRcvSendHSFeedback(), and XLogWalRcvSendReply().

GetEpochTime()

void GetEpochTime ( struct pg_tmtm )

Definition at line 2168 of file timestamp.c.

2169{
2170 struct pg_tm *t0;
2171 pg_time_t epoch = 0;
2172
2173 t0 = pg_gmtime(&epoch);
2174
2175 if (t0 == NULL)
2176 elog(ERROR, "could not convert epoch to timestamp: %m");
2177
2178 tm->tm_year = t0->tm_year;
2179 tm->tm_mon = t0->tm_mon;
2180 tm->tm_mday = t0->tm_mday;
2181 tm->tm_hour = t0->tm_hour;
2182 tm->tm_min = t0->tm_min;
2183 tm->tm_sec = t0->tm_sec;
2184
2185 tm->tm_year += 1900;
2186 tm->tm_mon++;
2187}
#define elog(elevel,...)
Definition: elog.h:226
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:1389
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, pg_gmtime(), tm, pg_tm::tm_hour, pg_tm::tm_mday, pg_tm::tm_min, pg_tm::tm_mon, pg_tm::tm_sec, and pg_tm::tm_year.

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

GetSQLCurrentTimestamp()

TimestampTz GetSQLCurrentTimestamp ( int32  typmod )

Definition at line 1663 of file timestamp.c.

1664{
1665 TimestampTz ts;
1666
1668 if (typmod >= 0)
1669 AdjustTimestampForTypmod(&ts, typmod, NULL);
1670 return ts;
1671}
bool AdjustTimestampForTypmod(Timestamp *time, int32 typmod, Node *escontext)
Definition: timestamp.c:368
TimestampTz GetCurrentTransactionStartTimestamp(void)
Definition: xact.c:870

References AdjustTimestampForTypmod(), and GetCurrentTransactionStartTimestamp().

Referenced by ExecEvalSQLValueFunction().

GetSQLLocalTimestamp()

Timestamp GetSQLLocalTimestamp ( int32  typmod )

Definition at line 1677 of file timestamp.c.

1678{
1679 Timestamp ts;
1680
1682 if (typmod >= 0)
1683 AdjustTimestampForTypmod(&ts, typmod, NULL);
1684 return ts;
1685}
static Timestamp timestamptz2timestamp(TimestampTz timestamp)
Definition: timestamp.c:6516

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

Referenced by ExecEvalSQLValueFunction().

interval2itm()

void interval2itm ( Interval  span,
struct pg_itmitm 
)

Definition at line 2047 of file timestamp.c.

2048{
2049 TimeOffset time;
2050 TimeOffset tfrac;
2051
2052 itm->tm_year = span.month / MONTHS_PER_YEAR;
2053 itm->tm_mon = span.month % MONTHS_PER_YEAR;
2054 itm->tm_mday = span.day;
2055 time = span.time;
2056
2057 tfrac = time / USECS_PER_HOUR;
2058 time -= tfrac * USECS_PER_HOUR;
2059 itm->tm_hour = tfrac;
2060 tfrac = time / USECS_PER_MINUTE;
2061 time -= tfrac * USECS_PER_MINUTE;
2062 itm->tm_min = (int) tfrac;
2063 tfrac = time / USECS_PER_SEC;
2064 time -= tfrac * USECS_PER_SEC;
2065 itm->tm_sec = (int) tfrac;
2066 itm->tm_usec = (int) time;
2067}
#define MONTHS_PER_YEAR
Definition: timestamp.h:108
int32 day
Definition: timestamp.h:51
int32 month
Definition: timestamp.h:52
TimeOffset time
Definition: timestamp.h:49
int64 tm_hour
Definition: timestamp.h:70
int tm_year
Definition: timestamp.h:73
int tm_mon
Definition: timestamp.h:72
int tm_mday
Definition: timestamp.h:71
int tm_sec
Definition: timestamp.h:68
int tm_min
Definition: timestamp.h:69
int tm_usec
Definition: timestamp.h:67

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

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

IntervalPGetDatum()

static Datum IntervalPGetDatum ( const IntervalX )
inlinestatic

Definition at line 58 of file timestamp.h.

59{
60 return PointerGetDatum(X);
61}
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:332

References PointerGetDatum().

Referenced by abs_interval(), gbt_intv_ssup_cmp(), gbt_intveq(), gbt_intvge(), gbt_intvgt(), gbt_intvkey_cmp(), gbt_intvle(), gbt_intvlt(), in_range_date_interval(), in_range_interval_interval(), in_range_timestamp_interval(), interval_avg(), leftmostvalue_interval(), pg_stat_get_wal_senders(), pg_timezone_abbrevs_abbrevs(), pg_timezone_abbrevs_zone(), timestamp_mi(), and uuidv7_interval().

isoweek2date()

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

Definition at line 5261 of file timestamp.c.

5262{
5263 j2date(isoweek2j(*year, woy), year, mon, mday);
5264}
void j2date(int jd, int *year, int *month, int *day)
Definition: datetime.c:321

References isoweek2j(), and j2date().

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

isoweek2j()

int isoweek2j ( int  year,
int  week 
)

Definition at line 5241 of file timestamp.c.

5242{
5243 int day0,
5244 day4;
5245
5246 /* fourth day of current year */
5247 day4 = date2j(year, 1, 4);
5248
5249 /* day0 == offset to first day of week (Monday) */
5250 day0 = j2day(day4 - 1);
5251
5252 return ((week - 1) * 7) + (day4 - day0);
5253}

References date2j(), and j2day().

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

isoweekdate2date()

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

Definition at line 5274 of file timestamp.c.

5275{
5276 int jday;
5277
5278 jday = isoweek2j(*year, isoweek);
5279 /* convert Gregorian week start (Sunday=1) to ISO week start (Monday=1) */
5280 if (wday > 1)
5281 jday += wday - 2;
5282 else
5283 jday += 6;
5284 j2date(jday, year, mon, mday);
5285}

References isoweek2j(), and j2date().

Referenced by do_to_timestamp().

itm2interval()

int itm2interval ( struct pg_itmitm,
Intervalspan 
)

Definition at line 2077 of file timestamp.c.

2078{
2079 int64 total_months = (int64) itm->tm_year * MONTHS_PER_YEAR + itm->tm_mon;
2080
2081 if (total_months > INT_MAX || total_months < INT_MIN)
2082 return -1;
2083 span->month = (int32) total_months;
2084 span->day = itm->tm_mday;
2086 &span->time))
2087 return -1;
2088 /* tm_min, tm_sec are 32 bits, so intermediate products can't overflow */
2090 &span->time))
2091 return -1;
2092 if (pg_add_s64_overflow(span->time, itm->tm_sec * USECS_PER_SEC,
2093 &span->time))
2094 return -1;
2095 if (pg_add_s64_overflow(span->time, itm->tm_usec,
2096 &span->time))
2097 return -1;
2098 if (INTERVAL_NOT_FINITE(span))
2099 return -1;
2100 return 0;
2101}
int64_t int64
Definition: c.h:535
int32_t int32
Definition: c.h:534
#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
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:81

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

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

itmin2interval()

int itmin2interval ( struct pg_itm_initm_in,
Intervalspan 
)

Definition at line 2115 of file timestamp.c.

2116{
2117 int64 total_months = (int64) itm_in->tm_year * MONTHS_PER_YEAR + itm_in->tm_mon;
2118
2119 if (total_months > INT_MAX || total_months < INT_MIN)
2120 return -1;
2121 span->month = (int32) total_months;
2122 span->day = itm_in->tm_mday;
2123 span->time = itm_in->tm_usec;
2124 return 0;
2125}
int tm_mon
Definition: timestamp.h:86
int tm_year
Definition: timestamp.h:87
int tm_mday
Definition: timestamp.h:85
int64 tm_usec
Definition: timestamp.h:84

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

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

SetEpochTimestamp()

Timestamp SetEpochTimestamp ( void  )

Definition at line 2190 of file timestamp.c.

2191{
2192 Timestamp dt;
2193 struct pg_tm tt,
2194 *tm = &tt;
2195
2197 /* we don't bother to test for failure ... */
2198 tm2timestamp(tm, 0, NULL, &dt);
2199
2200 return dt;
2201} /* SetEpochTimestamp() */
void GetEpochTime(struct pg_tm *tm)
Definition: timestamp.c:2168
int tm2timestamp(struct pg_tm *tm, fsec_t fsec, int *tzp, Timestamp *result)
Definition: timestamp.c:2006

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

Referenced by dttofmtasc_replace(), PGTYPEStimestamp_from_asc(), timestamp_in(), timestamp_part_common(), timestamptz_in(), and timestamptz_part_common().

time_t_to_timestamptz()

TimestampTz time_t_to_timestamptz ( pg_time_t  tm )

Definition at line 1820 of file timestamp.c.

1821{
1822 TimestampTz result;
1823
1824 result = (TimestampTz) tm -
1826 result *= USECS_PER_SEC;
1827
1828 return result;
1829}

References POSTGRES_EPOCH_JDATE, SECS_PER_DAY, tm, UNIX_EPOCH_JDATE, and USECS_PER_SEC.

Referenced by pg_control_checkpoint(), pg_control_system(), pg_ls_dir_files(), and pg_stat_file().

timestamp2timestamptz_opt_overflow()

TimestampTz timestamp2timestamptz_opt_overflow ( Timestamp  timestamp,
int *  overflow 
)

Definition at line 6445 of file timestamp.c.

6446{
6447 TimestampTz result;
6448 struct pg_tm tt,
6449 *tm = &tt;
6450 fsec_t fsec;
6451 int tz;
6452
6453 if (overflow)
6454 *overflow = 0;
6455
6457 return timestamp;
6458
6459 /* timestamp2tm should not fail on valid timestamps, but cope */
6460 if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) == 0)
6461 {
6463
6464 result = dt2local(timestamp, -tz);
6465
6466 if (IS_VALID_TIMESTAMP(result))
6467 return result;
6468 }
6469
6470 if (overflow)
6471 {
6472 if (timestamp < 0)
6473 {
6474 *overflow = -1;
6475 TIMESTAMP_NOBEGIN(result);
6476 }
6477 else
6478 {
6479 *overflow = 1;
6480 TIMESTAMP_NOEND(result);
6481 }
6482 return result;
6483 }
6484
6485 ereport(ERROR,
6486 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
6487 errmsg("timestamp out of range")));
6488
6489 return 0;
6490}
int DetermineTimeZoneOffset(struct pg_tm *tm, pg_tz *tzp)
Definition: datetime.c:1604
static Timestamp dt2local(Timestamp dt, int timezone)
Definition: timestamp.c:2134
int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm *tm, fsec_t *fsec, const char **tzn, pg_tz *attimezone)
Definition: timestamp.c:1910
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
PGDLLIMPORT pg_tz * session_timezone
Definition: pgtz.c:28
int64 timestamp

References DetermineTimeZoneOffset(), dt2local(), ereport, errcode(), errmsg(), ERROR, IS_VALID_TIMESTAMP, session_timezone, timestamp2tm(), TIMESTAMP_NOBEGIN, TIMESTAMP_NOEND, TIMESTAMP_NOT_FINITE, and tm.

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

timestamp2tm()

int timestamp2tm ( Timestamp  dt,
int *  tzp,
struct pg_tmtm,
fsec_tfsec,
const char **  tzn,
pg_tzattimezone 
)

Definition at line 1910 of file timestamp.c.

1911{
1913 Timestamp time;
1914 pg_time_t utime;
1915
1916 /* Use session timezone if caller asks for default */
1917 if (attimezone == NULL)
1918 attimezone = session_timezone;
1919
1920 time = dt;
1921 TMODULO(time, date, USECS_PER_DAY);
1922
1923 if (time < INT64CONST(0))
1924 {
1925 time += USECS_PER_DAY;
1926 date -= 1;
1927 }
1928
1929 /* add offset to go from J2000 back to standard Julian date */
1931
1932 /* Julian day routine does not work for negative Julian days */
1933 if (date < 0 || date > (Timestamp) INT_MAX)
1934 return -1;
1935
1936 j2date((int) date, &tm->tm_year, &tm->tm_mon, &tm->tm_mday);
1937 dt2time(time, &tm->tm_hour, &tm->tm_min, &tm->tm_sec, fsec);
1938
1939 /* Done if no TZ conversion wanted */
1940 if (tzp == NULL)
1941 {
1942 tm->tm_isdst = -1;
1943 tm->tm_gmtoff = 0;
1944 tm->tm_zone = NULL;
1945 if (tzn != NULL)
1946 *tzn = NULL;
1947 return 0;
1948 }
1949
1950 /*
1951 * If the time falls within the range of pg_time_t, use pg_localtime() to
1952 * rotate to the local time zone.
1953 *
1954 * First, convert to an integral timestamp, avoiding possibly
1955 * platform-specific roundoff-in-wrong-direction errors, and adjust to
1956 * Unix epoch. Then see if we can convert to pg_time_t without loss. This
1957 * coding avoids hardwiring any assumptions about the width of pg_time_t,
1958 * so it should behave sanely on machines without int64.
1959 */
1960 dt = (dt - *fsec) / USECS_PER_SEC +
1962 utime = (pg_time_t) dt;
1963 if ((Timestamp) utime == dt)
1964 {
1965 struct pg_tm *tx = pg_localtime(&utime, attimezone);
1966
1967 tm->tm_year = tx->tm_year + 1900;
1968 tm->tm_mon = tx->tm_mon + 1;
1969 tm->tm_mday = tx->tm_mday;
1970 tm->tm_hour = tx->tm_hour;
1971 tm->tm_min = tx->tm_min;
1972 tm->tm_sec = tx->tm_sec;
1973 tm->tm_isdst = tx->tm_isdst;
1974 tm->tm_gmtoff = tx->tm_gmtoff;
1975 tm->tm_zone = tx->tm_zone;
1976 *tzp = -tm->tm_gmtoff;
1977 if (tzn != NULL)
1978 *tzn = tm->tm_zone;
1979 }
1980 else
1981 {
1982 /*
1983 * When out of range of pg_time_t, treat as GMT
1984 */
1985 *tzp = 0;
1986 /* Mark this as *no* time zone available */
1987 tm->tm_isdst = -1;
1988 tm->tm_gmtoff = 0;
1989 tm->tm_zone = NULL;
1990 if (tzn != NULL)
1991 *tzn = NULL;
1992 }
1993
1994 return 0;
1995}
void dt2time(Timestamp jd, int *hour, int *min, int *sec, fsec_t *fsec)
Definition: timestamp.c:1883
#define INT64CONST(x)
Definition: c.h:552
#define USECS_PER_DAY
Definition: timestamp.h:131
#define TMODULO(t, q, u)
Definition: datetime.h:248
struct pg_tm * pg_localtime(const pg_time_t *timep, const pg_tz *tz)
Definition: localtime.c:1344
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(), 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(), make_timestamptz_at_timezone(), map_sql_value_to_xml_value(), PGTYPEStimestamp_add_interval(), PGTYPEStimestamp_fmt_asc(), PGTYPEStimestamp_to_asc(), timestamp2date_opt_overflow(), timestamp2timestamptz_opt_overflow(), timestamp_age(), timestamp_out(), timestamp_part_common(), timestamp_pl_interval(), timestamp_recv(), timestamp_time(), timestamp_to_char(), timestamp_trunc(), timestamp_zone(), timestamptz2date_opt_overflow(), timestamptz2timestamp_opt_overflow(), timestamptz_age(), timestamptz_out(), timestamptz_part_common(), timestamptz_pl_interval_internal(), timestamptz_recv(), timestamptz_time(), timestamptz_timetz(), timestamptz_to_char(), timestamptz_to_str(), timestamptz_trunc_internal(), timestamptz_zone(), and timetz_zone().

timestamp_cmp_internal()

int timestamp_cmp_internal ( Timestamp  dt1,
Timestamp  dt2 
)

Definition at line 2210 of file timestamp.c.

2211{
2212 return (dt1 < dt2) ? -1 : ((dt1 > dt2) ? 1 : 0);
2213}

Referenced by date_cmp_timestamp_internal(), generate_series_timestamp(), generate_series_timestamptz_internal(), timestamp_cmp(), timestamp_eq(), timestamp_ge(), timestamp_gt(), timestamp_larger(), timestamp_le(), timestamp_lt(), timestamp_ne(), and timestamp_smaller().

timestamp_cmp_timestamptz_internal()

int32 timestamp_cmp_timestamptz_internal ( Timestamp  timestampVal,
TimestampTz  dt2 
)

Definition at line 2363 of file timestamp.c.

2364{
2365 TimestampTz dt1;
2366 int overflow;
2367
2368 dt1 = timestamp2timestamptz_opt_overflow(timestampVal, &overflow);
2369 if (overflow > 0)
2370 {
2371 /* dt1 is larger than any finite timestamp, but less than infinity */
2372 return TIMESTAMP_IS_NOEND(dt2) ? -1 : +1;
2373 }
2374 if (overflow < 0)
2375 {
2376 /* dt1 is less than any finite timestamp, but more than -infinity */
2377 return TIMESTAMP_IS_NOBEGIN(dt2) ? +1 : -1;
2378 }
2379
2380 return timestamptz_cmp_internal(dt1, dt2);
2381}
TimestampTz timestamp2timestamptz_opt_overflow(Timestamp timestamp, int *overflow)
Definition: timestamp.c:6445
#define TIMESTAMP_IS_NOEND(j)
Definition: timestamp.h:167
#define TIMESTAMP_IS_NOBEGIN(j)
Definition: timestamp.h:162
#define timestamptz_cmp_internal(dt1, dt2)
Definition: timestamp.h:143

References timestamp2timestamptz_opt_overflow(), TIMESTAMP_IS_NOBEGIN, TIMESTAMP_IS_NOEND, and timestamptz_cmp_internal.

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

TimestampDifference()

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

Definition at line 1721 of file timestamp.c.

1723{
1724 TimestampTz diff = stop_time - start_time;
1725
1726 if (diff <= 0)
1727 {
1728 *secs = 0;
1729 *microsecs = 0;
1730 }
1731 else
1732 {
1733 *secs = (long) (diff / USECS_PER_SEC);
1734 *microsecs = (int) (diff % USECS_PER_SEC);
1735 }
1736}
static time_t start_time
Definition: pg_ctl.c:95

References start_time, and USECS_PER_SEC.

Referenced by check_log_duration(), has_startup_progress_timeout_expired(), heap_vacuum_rel(), InvalidatePossiblyObsoleteSlot(), launcher_determine_sleep(), log_disconnections(), LogRecoveryConflict(), pgstat_report_activity(), pgstat_update_dbstats(), ProcSleep(), schedule_alarm(), and TimestampDifferenceExceedsSeconds().

TimestampDifferenceExceeds()

bool TimestampDifferenceExceeds ( TimestampTz  start_time,
TimestampTz  stop_time,
int  msec 
)

Definition at line 1781 of file timestamp.c.

1784{
1785 TimestampTz diff = stop_time - start_time;
1786
1787 return (diff >= msec * INT64CONST(1000));
1788}

References INT64CONST, and start_time.

Referenced by asyncQueueFillWarning(), AutoVacLauncherMain(), do_analyze_rel(), do_start_worker(), get_candidate_xid(), heap_vacuum_rel(), LockBufferForCleanup(), logicalrep_worker_launch(), maybe_start_bgworkers(), pa_send_data(), pgstat_report_stat(), process_syncing_tables_for_apply(), ProcSleep(), ResolveRecoveryConflictWithVirtualXIDs(), send_feedback(), should_stop_conflict_info_retention(), update_most_recent_deletion_info(), wait_for_local_flush(), WaitForWALToBecomeAvailable(), WalSndLoop(), WalSndUpdateProgress(), WalSndWaitForWal(), and XLogBackgroundFlush().

TimestampDifferenceExceedsSeconds()

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

Definition at line 1795 of file timestamp.c.

1798{
1799 long secs;
1800 int usecs;
1801
1802 /* Calculate the difference in seconds */
1803 TimestampDifference(start_time, stop_time, &secs, &usecs);
1804
1805 return (secs >= threshold_sec);
1806}
void TimestampDifference(TimestampTz start_time, TimestampTz stop_time, long *secs, int *microsecs)
Definition: timestamp.c:1721

References 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:539

References start_time.

Referenced by PostgresMain().

TimestampDifferenceMilliseconds()

long TimestampDifferenceMilliseconds ( TimestampTz  start_time,
TimestampTz  stop_time 
)

Definition at line 1757 of file timestamp.c.

1758{
1759 TimestampTz diff;
1760
1761 /* Deal with zero or negative elapsed time quickly. */
1762 if (start_time >= stop_time)
1763 return 0;
1764 /* To not fail with timestamp infinities, we must detect overflow. */
1765 if (pg_sub_s64_overflow(stop_time, start_time, &diff))
1766 return (long) INT_MAX;
1767 if (diff >= (INT_MAX * INT64CONST(1000) - 999))
1768 return (long) INT_MAX;
1769 else
1770 return (long) ((diff + 999) / 1000);
1771}
static bool pg_sub_s64_overflow(int64 a, int64 b, int64 *result)
Definition: int.h:262

References INT64CONST, pg_sub_s64_overflow(), and start_time.

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

TimestampGetDatum()

static Datum TimestampGetDatum ( Timestamp  X )
inlinestatic

Definition at line 46 of file timestamp.h.

47{
48 return Int64GetDatum(X);
49}
static Datum Int64GetDatum(int64 X)
Definition: postgres.h:403

References Int64GetDatum().

Referenced by cvt_date_timestamp(), cvt_timestamptz_timestamp(), date_mi_interval(), date_pl_interval(), ExecEvalSQLValueFunction(), executeDateTimeMethod(), gbt_num_fetch(), gbt_ts_dist(), gbt_ts_ssup_cmp(), gbt_tseq(), gbt_tsge(), gbt_tsgt(), gbt_tskey_cmp(), gbt_tsle(), gbt_tslt(), generate_series_timestamp(), generate_series_timestamp_support(), in_range_date_interval(), in_range_timestamp_interval(), leftmostvalue_timestamp(), parse_datetime(), timestamp_decrement(), timestamp_increment(), timestamp_mi_interval(), and timestamp_skipsupport().

TimestampTimestampTzRequiresRewrite()

bool TimestampTimestampTzRequiresRewrite ( void  )

Definition at line 6414 of file timestamp.c.

6415{
6416 long offset;
6417
6418 if (pg_get_timezone_offset(session_timezone, &offset) && offset == 0)
6419 return false;
6420 return true;
6421}
bool pg_get_timezone_offset(const pg_tz *tz, long int *gmtoff)
Definition: localtime.c:1965

References pg_get_timezone_offset(), and session_timezone.

Referenced by ATColumnChangeRequiresRewrite().

timestamptz2timestamp_opt_overflow()

Timestamp timestamptz2timestamp_opt_overflow ( TimestampTz  timestamp,
int *  overflow 
)

Definition at line 6532 of file timestamp.c.

6533{
6534 Timestamp result;
6535 struct pg_tm tt,
6536 *tm = &tt;
6537 fsec_t fsec;
6538 int tz;
6539
6540 if (overflow)
6541 *overflow = 0;
6542
6544 result = timestamp;
6545 else
6546 {
6547 if (timestamp2tm(timestamp, &tz, tm, &fsec, NULL, NULL) != 0)
6548 {
6549 if (overflow)
6550 {
6551 if (timestamp < 0)
6552 {
6553 *overflow = -1;
6554 TIMESTAMP_NOBEGIN(result);
6555 }
6556 else
6557 {
6558 *overflow = 1;
6559 TIMESTAMP_NOEND(result);
6560 }
6561 return result;
6562 }
6563 ereport(ERROR,
6564 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
6565 errmsg("timestamp out of range")));
6566 }
6567 if (tm2timestamp(tm, fsec, NULL, &result) != 0)
6568 {
6569 if (overflow)
6570 {
6571 if (timestamp < 0)
6572 {
6573 *overflow = -1;
6574 TIMESTAMP_NOBEGIN(result);
6575 }
6576 else
6577 {
6578 *overflow = 1;
6579 TIMESTAMP_NOEND(result);
6580 }
6581 return result;
6582 }
6583 ereport(ERROR,
6584 (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
6585 errmsg("timestamp out of range")));
6586 }
6587 }
6588 return result;
6589}

References ereport, errcode(), errmsg(), ERROR, timestamp2tm(), TIMESTAMP_NOBEGIN, TIMESTAMP_NOEND, TIMESTAMP_NOT_FINITE, tm, and tm2timestamp().

Referenced by cvt_timestamptz_timestamp(), and timestamptz2timestamp().

timestamptz_to_str()

const char * timestamptz_to_str ( TimestampTz  t )

Definition at line 1862 of file timestamp.c.

1863{
1864 static char buf[MAXDATELEN + 1];
1865 int tz;
1866 struct pg_tm tt,
1867 *tm = &tt;
1868 fsec_t fsec;
1869 const char *tzn;
1870
1871 if (TIMESTAMP_NOT_FINITE(t))
1873 else if (timestamp2tm(t, &tz, tm, &fsec, &tzn, NULL) == 0)
1874 EncodeDateTime(tm, fsec, true, tz, tzn, USE_ISO_DATES, buf);
1875 else
1876 strlcpy(buf, "(timestamp out of range)", sizeof(buf));
1877
1878 return buf;
1879}
void EncodeDateTime(struct pg_tm *tm, fsec_t fsec, bool print_tz, int tz, const char *tzn, int style, char *str)
Definition: datetime.c:4464
void EncodeSpecialTimestamp(Timestamp dt, char *str)
Definition: timestamp.c:1587
#define MAXDATELEN
Definition: datetime.h:200
#define USE_ISO_DATES
Definition: miscadmin.h:236
static char * buf
Definition: pg_test_fsync.c:72
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45

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

Referenced by CreateRestartPoint(), 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 )

Definition at line 1842 of file timestamp.c.

1843{
1844 pg_time_t result;
1845
1846 result = (pg_time_t) (t / USECS_PER_SEC +
1848
1849 return result;
1850}

References POSTGRES_EPOCH_JDATE, SECS_PER_DAY, UNIX_EPOCH_JDATE, and USECS_PER_SEC.

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

TimestampTzGetDatum()

static Datum TimestampTzGetDatum ( TimestampTz  X )
inlinestatic

Definition at line 52 of file timestamp.h.

53{
54 return Int64GetDatum(X);
55}

References Int64GetDatum().

Referenced by cvt_date_timestamptz(), cvt_timestamp_timestamptz(), ExecEvalSQLValueFunction(), executeDateTimeMethod(), generate_series_timestamptz_internal(), parse_datetime(), pg_control_checkpoint(), pg_control_system(), pg_cursor(), pg_get_replication_slots(), pg_last_committed_xact(), pg_lock_status(), pg_ls_dir_files(), pg_prepared_statement(), pg_prepared_xact(), pg_stat_file(), pg_stat_get_activity(), pg_stat_get_archiver(), pg_stat_get_recovery_prefetch(), pg_stat_get_replication_slot(), pg_stat_get_slru(), pg_stat_get_subscription(), pg_stat_get_subscription_stats(), pg_stat_get_wal_receiver(), pg_stat_get_wal_senders(), pg_stat_io_build_tuples(), pg_stat_statements_info(), pg_stat_statements_internal(), pg_stat_wal_build_tuple(), pg_xact_commit_timestamp_origin(), and uuidv7_interval().

tm2timestamp()

int tm2timestamp ( struct pg_tmtm,
fsec_t  fsec,
int *  tzp,
Timestampresult 
)

Definition at line 2006 of file timestamp.c.

2007{
2009 TimeOffset time;
2010
2011 /* Prevent overflow in Julian-day routines */
2013 {
2014 *result = 0; /* keep compiler quiet */
2015 return -1;
2016 }
2017
2019 time = time2t(tm->tm_hour, tm->tm_min, tm->tm_sec, fsec);
2020
2022 pg_add_s64_overflow(*result, time, result)))
2023 {
2024 *result = 0; /* keep compiler quiet */
2025 return -1;
2026 }
2027 if (tzp != NULL)
2028 *result = dt2local(*result, -(*tzp));
2029
2030 /* final range check catches just-out-of-range timestamps */
2031 if (!IS_VALID_TIMESTAMP(*result))
2032 {
2033 *result = 0; /* keep compiler quiet */
2034 return -1;
2035 }
2036
2037 return 0;
2038}
static TimeOffset time2t(const int hour, const int min, const int sec, const fsec_t fsec)
Definition: timestamp.c:2128
#define unlikely(x)
Definition: c.h:402
#define IS_VALID_JULIAN(y, m, d)
Definition: timestamp.h:227

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

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

Variable Documentation

PgReloadTime

PGDLLIMPORT TimestampTz PgReloadTime
extern

Definition at line 57 of file timestamp.c.

Referenced by pg_conf_load_time(), and ProcessConfigFileInternal().

PgStartTime

PGDLLIMPORT TimestampTz PgStartTime
extern

Definition at line 54 of file timestamp.c.

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

AltStyle によって変換されたページ (->オリジナル) /