PostgreSQL Source Code  git master
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-2021, 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  * Macros 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  */
27 #define DatumGetTimestamp(X) ((Timestamp) DatumGetInt64(X))
28 #define DatumGetTimestampTz(X) ((TimestampTz) DatumGetInt64(X))
29 #define DatumGetIntervalP(X) ((Interval *) DatumGetPointer(X))
30 
31 #define TimestampGetDatum(X) Int64GetDatum(X)
32 #define TimestampTzGetDatum(X) Int64GetDatum(X)
33 #define IntervalPGetDatum(X) PointerGetDatum(X)
34 
35 #define PG_GETARG_TIMESTAMP(n) DatumGetTimestamp(PG_GETARG_DATUM(n))
36 #define PG_GETARG_TIMESTAMPTZ(n) DatumGetTimestampTz(PG_GETARG_DATUM(n))
37 #define PG_GETARG_INTERVAL_P(n) DatumGetIntervalP(PG_GETARG_DATUM(n))
38 
39 #define PG_RETURN_TIMESTAMP(x) return TimestampGetDatum(x)
40 #define PG_RETURN_TIMESTAMPTZ(x) return TimestampTzGetDatum(x)
41 #define PG_RETURN_INTERVAL_P(x) return IntervalPGetDatum(x)
42 
43 
44 #define TIMESTAMP_MASK(b) (1 << (b))
45 #define INTERVAL_MASK(b) (1 << (b))
46 
47 /* Macros to handle packing and unpacking the typmod field for intervals */
48 #define INTERVAL_FULL_RANGE (0x7FFF)
49 #define INTERVAL_RANGE_MASK (0x7FFF)
50 #define INTERVAL_FULL_PRECISION (0xFFFF)
51 #define INTERVAL_PRECISION_MASK (0xFFFF)
52 #define INTERVAL_TYPMOD(p,r) ((((r) & INTERVAL_RANGE_MASK) << 16) | ((p) & INTERVAL_PRECISION_MASK))
53 #define INTERVAL_PRECISION(t) ((t) & INTERVAL_PRECISION_MASK)
54 #define INTERVAL_RANGE(t) (((t) >> 16) & INTERVAL_RANGE_MASK)
55 
56 #define TimestampTzPlusMilliseconds(tz,ms) ((tz) + ((ms) * (int64) 1000))
57 
58 
59 /* Set at postmaster start */
61 
62 /* Set at configuration reload */
64 
65 
66 /* Internal routines (not fmgr-callable) */
67 
68 extern int32 anytimestamp_typmod_check(bool istz, int32 typmod);
69 
70 extern TimestampTz GetCurrentTimestamp(void);
72 extern Timestamp GetSQLLocalTimestamp(int32 typmod);
74  long *secs, int *microsecs);
76  TimestampTz stop_time);
78  TimestampTz stop_time,
79  int msec);
80 
83 
84 extern const char *timestamptz_to_str(TimestampTz t);
85 
86 extern int tm2timestamp(struct pg_tm *tm, fsec_t fsec, int *tzp, Timestamp *dt);
87 extern int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm *tm,
88  fsec_t *fsec, const char **tzn, pg_tz *attimezone);
89 extern void dt2time(Timestamp dt, int *hour, int *min, int *sec, fsec_t *fsec);
90 
91 extern int interval2tm(Interval span, struct pg_tm *tm, fsec_t *fsec);
92 extern int tm2interval(struct pg_tm *tm, fsec_t fsec, Interval *span);
93 
94 extern Timestamp SetEpochTimestamp(void);
95 extern void GetEpochTime(struct pg_tm *tm);
96 
97 extern int timestamp_cmp_internal(Timestamp dt1, Timestamp dt2);
98 
99 /* timestamp comparison works for timestamptz also */
100 #define timestamptz_cmp_internal(dt1,dt2) timestamp_cmp_internal(dt1, dt2)
101 
103  int *overflow);
105  TimestampTz dt2);
106 
107 extern int isoweek2j(int year, int week);
108 extern void isoweek2date(int woy, int *year, int *mon, int *mday);
109 extern void isoweekdate2date(int isoweek, int wday, int *year, int *mon, int *mday);
110 extern int date2isoweek(int year, int mon, int mday);
111 extern int date2isoyear(int year, int mon, int mday);
112 extern int date2isoyearday(int year, int mon, int mday);
113 
114 extern bool TimestampTimestampTzRequiresRewrite(void);
115 
116 #endif /* TIMESTAMP_H */
const char * timestamptz_to_str(TimestampTz t)
Definition: timestamp.c:1772
void TimestampDifference(TimestampTz start_time, TimestampTz stop_time, long *secs, int *microsecs)
Definition: timestamp.c:1654
int64 pg_time_t
Definition: pgtime.h:23
int64 timestamp
int64 TimestampTz
Definition: timestamp.h:39
long TimestampDifferenceMilliseconds(TimestampTz start_time, TimestampTz stop_time)
Definition: timestamp.c:1691
int timestamp_cmp_internal(Timestamp dt1, Timestamp dt2)
Definition: timestamp.c:2088
int isoweek2j(int year, int week)
Definition: timestamp.c:4281
int tm2timestamp(struct pg_tm *tm, fsec_t fsec, int *tzp, Timestamp *dt)
Definition: timestamp.c:1916
TimestampTz time_t_to_timestamptz(pg_time_t tm)
Definition: timestamp.c:1730
Definition: pgtime.h:25
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1578
static time_t start_time
Definition: pg_ctl.c:99
signed int int32
Definition: c.h:417
static struct pg_tm tm
Definition: localtime.c:102
void isoweekdate2date(int isoweek, int wday, int *year, int *mon, int *mday)
Definition: timestamp.c:4314
TimestampTz GetSQLCurrentTimestamp(int32 typmod)
Definition: timestamp.c:1596
TimestampTz timestamp2timestamptz_opt_overflow(Timestamp timestamp, int *overflow)
Definition: timestamp.c:5200
void isoweek2date(int woy, int *year, int *mon, int *mday)
Definition: timestamp.c:4301
Timestamp GetSQLLocalTimestamp(int32 typmod)
Definition: timestamp.c:1610
int tm2interval(struct pg_tm *tm, fsec_t fsec, Interval *span)
Definition: timestamp.c:1992
bool TimestampDifferenceExceeds(TimestampTz start_time, TimestampTz stop_time, int msec)
Definition: timestamp.c:1709
int32 fsec_t
Definition: timestamp.h:41
void GetEpochTime(struct pg_tm *tm)
Definition: timestamp.c:2046
pg_time_t timestamptz_to_time_t(TimestampTz t)
Definition: timestamp.c:1752
int64 Timestamp
Definition: timestamp.h:38
bool TimestampTimestampTzRequiresRewrite(void)
Definition: timestamp.c:5169
int32 timestamp_cmp_timestamptz_internal(Timestamp timestampVal, TimestampTz dt2)
Definition: timestamp.c:2192
Definition: pgtz.h:65
int date2isoyear(int year, int mon, int mday)
Definition: timestamp.c:4387
TimestampTz PgStartTime
Definition: timestamp.c:50
int interval2tm(Interval span, struct pg_tm *tm, fsec_t *fsec)
Definition: timestamp.c:1964
Timestamp SetEpochTimestamp(void)
Definition: timestamp.c:2068
int date2isoweek(int year, int mon, int mday)
Definition: timestamp.c:4332
int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm *tm, fsec_t *fsec, const char **tzn, pg_tz *attimezone)
Definition: timestamp.c:1820
void dt2time(Timestamp dt, int *hour, int *min, int *sec, fsec_t *fsec)
Definition: timestamp.c:1793
int date2isoyearday(int year, int mon, int mday)
Definition: timestamp.c:4444
int32 anytimestamp_typmod_check(bool istz, int32 typmod)
Definition: timestamp.c:102
TimestampTz PgReloadTime
Definition: timestamp.c:53