PostgreSQL Source Code git master
Loading...
Searching...
No Matches
timestamp.h
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * timestamp.h
4 * Definitions for the SQL "timestamp" and "interval" types.
5 *
6 * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
8 *
9 * src/include/utils/timestamp.h
10 *
11 *-------------------------------------------------------------------------
12 */
13#ifndef TIMESTAMP_H
14#define TIMESTAMP_H
15
16#include "datatype/timestamp.h"
17#include "fmgr.h"
18#include "pgtime.h"
19
20
21/*
22 * Functions for fmgr-callable functions.
23 *
24 * For Timestamp, we make use of the same support routines as for int64.
25 * Therefore Timestamp is pass-by-reference if and only if int64 is!
26 */
27static inline Timestamp
32
33static inline TimestampTz
38
39static inline Interval *
44
45static inline Datum
50
51static inline Datum
56
57static inline Datum
59{
60 return PointerGetDatum(X);
61}
62
63#define PG_GETARG_TIMESTAMP(n) DatumGetTimestamp(PG_GETARG_DATUM(n))
64#define PG_GETARG_TIMESTAMPTZ(n) DatumGetTimestampTz(PG_GETARG_DATUM(n))
65#define PG_GETARG_INTERVAL_P(n) DatumGetIntervalP(PG_GETARG_DATUM(n))
66
67#define PG_RETURN_TIMESTAMP(x) return TimestampGetDatum(x)
68#define PG_RETURN_TIMESTAMPTZ(x) return TimestampTzGetDatum(x)
69#define PG_RETURN_INTERVAL_P(x) return IntervalPGetDatum(x)
70
71
72#define TIMESTAMP_MASK(b) (1 << (b))
73#define INTERVAL_MASK(b) (1 << (b))
74
75/* Macros to handle packing and unpacking the typmod field for intervals */
76#define INTERVAL_FULL_RANGE (0x7FFF)
77#define INTERVAL_RANGE_MASK (0x7FFF)
78#define INTERVAL_FULL_PRECISION (0xFFFF)
79#define INTERVAL_PRECISION_MASK (0xFFFF)
80#define INTERVAL_TYPMOD(p,r) ((((r) & INTERVAL_RANGE_MASK) << 16) | ((p) & INTERVAL_PRECISION_MASK))
81#define INTERVAL_PRECISION(t) ((t) & INTERVAL_PRECISION_MASK)
82#define INTERVAL_RANGE(t) (((t) >> 16) & INTERVAL_RANGE_MASK)
83
84/* Macros for doing timestamp arithmetic without assuming timestamp's units */
85#define TimestampTzPlusMilliseconds(tz,ms) ((tz) + ((ms) * (int64) 1000))
86#define TimestampTzPlusSeconds(tz,s) ((tz) + ((s) * (int64) 1000000))
87
88/* Helper for simple subtraction between two timestamps */
89static inline uint64
97
98/* Set at postmaster start */
100
101/* Set at configuration reload */
103
104
105/* Internal routines (not fmgr-callable) */
106
107extern int32 anytimestamp_typmod_check(bool istz, int32 typmod);
108
113 long *secs, int *microsecs);
118 int msec);
121 int threshold_sec);
122
125
126extern const char *timestamptz_to_str(TimestampTz t);
127
128extern int tm2timestamp(struct pg_tm *tm, fsec_t fsec, int *tzp, Timestamp *result);
129extern int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm *tm,
130 fsec_t *fsec, const char **tzn, pg_tz *attimezone);
131extern void dt2time(Timestamp jd, int *hour, int *min, int *sec, fsec_t *fsec);
132
133extern void interval2itm(Interval span, struct pg_itm *itm);
134extern int itm2interval(struct pg_itm *itm, Interval *span);
135extern int itmin2interval(struct pg_itm_in *itm_in, Interval *span);
136
137extern Timestamp SetEpochTimestamp(void);
138extern void GetEpochTime(struct pg_tm *tm);
139
141
142/* timestamp comparison works for timestamptz also */
143#define timestamptz_cmp_internal(dt1,dt2) timestamp_cmp_internal(dt1, dt2)
144
146 Node *escontext);
148 Node *escontext);
149
152
153extern int isoweek2j(int year, int week);
154extern void isoweek2date(int woy, int *year, int *mon, int *mday);
155extern void isoweekdate2date(int isoweek, int wday, int *year, int *mon, int *mday);
156extern int date2isoweek(int year, int mon, int mday);
157extern int date2isoyear(int year, int mon, int mday);
158extern int date2isoyearday(int year, int mon, int mday);
159
161
162#endif /* TIMESTAMP_H */
#define PGDLLIMPORT
Definition c.h:1423
int32_t int32
Definition c.h:614
uint64_t uint64
Definition c.h:619
int64 Timestamp
Definition timestamp.h:38
int64 TimestampTz
Definition timestamp.h:39
int32 fsec_t
Definition timestamp.h:41
static struct pg_tm tm
Definition localtime.c:104
static time_t start_time
Definition pg_ctl.c:96
int64 pg_time_t
Definition pgtime.h:23
int64 timestamp
static Datum Int64GetDatum(int64 X)
Definition postgres.h:413
static int64 DatumGetInt64(Datum X)
Definition postgres.h:403
static Datum PointerGetDatum(const void *X)
Definition postgres.h:342
uint64_t Datum
Definition postgres.h:70
static Pointer DatumGetPointer(Datum X)
Definition postgres.h:332
static int fb(int x)
Definition nodes.h:135
Definition pgtime.h:35
Definition pgtz.h:66
void dt2time(Timestamp jd, int *hour, int *min, int *sec, fsec_t *fsec)
Definition timestamp.c:1874
int timestamp_cmp_internal(Timestamp dt1, Timestamp dt2)
Definition timestamp.c:2201
long TimestampDifferenceMilliseconds(TimestampTz start_time, TimestampTz stop_time)
Definition timestamp.c:1748
void isoweek2date(int woy, int *year, int *mon, int *mday)
Definition timestamp.c:5255
void GetEpochTime(struct pg_tm *tm)
Definition timestamp.c:2159
int itmin2interval(struct pg_itm_in *itm_in, Interval *span)
Definition timestamp.c:2106
int isoweek2j(int year, int week)
Definition timestamp.c:5235
TimestampTz time_t_to_timestamptz(pg_time_t tm)
Definition timestamp.c:1811
static Datum TimestampTzGetDatum(TimestampTz X)
Definition timestamp.h:52
int date2isoweek(int year, int mon, int mday)
Definition timestamp.c:5286
Timestamp SetEpochTimestamp(void)
Definition timestamp.c:2181
static Datum TimestampGetDatum(Timestamp X)
Definition timestamp.h:46
static Datum IntervalPGetDatum(const Interval *X)
Definition timestamp.h:58
TimestampTz timestamp2timestamptz_safe(Timestamp timestamp, Node *escontext)
Definition timestamp.c:6439
int date2isoyearday(int year, int mon, int mday)
Definition timestamp.c:5398
int tm2timestamp(struct pg_tm *tm, fsec_t fsec, int *tzp, Timestamp *result)
Definition timestamp.c:1997
void TimestampDifference(TimestampTz start_time, TimestampTz stop_time, long *secs, int *microsecs)
Definition timestamp.c:1712
bool TimestampDifferenceExceedsSeconds(TimestampTz start_time, TimestampTz stop_time, int threshold_sec)
Definition timestamp.c:1786
bool TimestampTimestampTzRequiresRewrite(void)
Definition timestamp.c:6408
void isoweekdate2date(int isoweek, int wday, int *year, int *mon, int *mday)
Definition timestamp.c:5268
int32 timestamp_cmp_timestamptz_internal(Timestamp timestampVal, TimestampTz dt2)
Definition timestamp.c:2354
bool TimestampDifferenceExceeds(TimestampTz start_time, TimestampTz stop_time, int msec)
Definition timestamp.c:1772
PGDLLIMPORT TimestampTz PgReloadTime
Definition timestamp.c:48
PGDLLIMPORT TimestampTz PgStartTime
Definition timestamp.c:45
int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm *tm, fsec_t *fsec, const char **tzn, pg_tz *attimezone)
Definition timestamp.c:1901
int32 anytimestamp_typmod_check(bool istz, int32 typmod)
Definition timestamp.c:116
TimestampTz GetCurrentTimestamp(void)
Definition timestamp.c:1636
const char * timestamptz_to_str(TimestampTz t)
Definition timestamp.c:1853
static Interval * DatumGetIntervalP(Datum X)
Definition timestamp.h:40
Timestamp GetSQLLocalTimestamp(int32 typmod)
Definition timestamp.c:1668
void interval2itm(Interval span, struct pg_itm *itm)
Definition timestamp.c:2038
static uint64 TimestampDifferenceMicroseconds(TimestampTz start_time, TimestampTz stop_time)
Definition timestamp.h:90
static Timestamp DatumGetTimestamp(Datum X)
Definition timestamp.h:28
int itm2interval(struct pg_itm *itm, Interval *span)
Definition timestamp.c:2068
pg_time_t timestamptz_to_time_t(TimestampTz t)
Definition timestamp.c:1833
static TimestampTz DatumGetTimestampTz(Datum X)
Definition timestamp.h:34
int date2isoyear(int year, int mon, int mday)
Definition timestamp.c:5341
Timestamp timestamptz2timestamp_safe(TimestampTz timestamp, Node *escontext)
Definition timestamp.c:6511
TimestampTz GetSQLCurrentTimestamp(int32 typmod)
Definition timestamp.c:1654