Age Owner TLA Line data Source code
1 : /*-------------------------------------------------------------------------
2 : *
3 : * timestamp.h
4 : * Definitions for the SQL "timestamp" and "interval" types.
5 : *
6 : * Portions Copyright (c) 1996-2023, 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 : */
27 : static inline Timestamp
194 peter 28 GNC 619604 : DatumGetTimestamp(Datum X)
29 : {
30 619604 : return (Timestamp) DatumGetInt64(X);
31 : }
7658 lockhart 32 ECB :
33 : static inline TimestampTz
194 peter 34 GNC 118029 : DatumGetTimestampTz(Datum X)
35 : {
36 118029 : return (TimestampTz) DatumGetInt64(X);
37 : }
38 :
39 : static inline Interval *
40 276847 : DatumGetIntervalP(Datum X)
41 : {
42 276847 : return (Interval *) DatumGetPointer(X);
43 : }
44 :
45 : static inline Datum
46 201886 : TimestampGetDatum(Timestamp X)
47 : {
48 201886 : return Int64GetDatum(X);
49 : }
50 :
51 : static inline Datum
52 120940 : TimestampTzGetDatum(TimestampTz X)
53 : {
54 120940 : return Int64GetDatum(X);
55 : }
56 :
57 : static inline Datum
58 61918 : IntervalPGetDatum(const Interval *X)
59 : {
60 61918 : return PointerGetDatum(X);
61 : }
7658 lockhart 62 ECB :
63 : #define PG_GETARG_TIMESTAMP(n) DatumGetTimestamp(PG_GETARG_DATUM(n))
5497 tgl 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)
7656 70 :
71 :
72 : #define TIMESTAMP_MASK(b) (1 << (b))
73 : #define INTERVAL_MASK(b) (1 << (b))
7553 lockhart 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 :
8453 88 :
89 : /* Set at postmaster start */
366 rhaas 90 : extern PGDLLIMPORT TimestampTz PgStartTime;
91 :
92 : /* Set at configuration reload */
93 : extern PGDLLIMPORT TimestampTz PgReloadTime;
94 :
95 :
96 : /* Internal routines (not fmgr-callable) */
97 :
98 : extern TimestampTz GetCurrentTimestamp(void);
99 : extern void TimestampDifference(TimestampTz start_time, TimestampTz stop_time,
100 : long *secs, int *microsecs);
101 : extern long TimestampDifferenceMilliseconds(TimestampTz start_time,
102 : TimestampTz stop_time);
103 : extern bool TimestampDifferenceExceeds(TimestampTz start_time,
104 : TimestampTz stop_time,
105 : int msec);
106 :
107 : extern TimestampTz time_t_to_timestamptz(pg_time_t tm);
108 : extern pg_time_t timestamptz_to_time_t(TimestampTz t);
109 :
110 : extern const char *timestamptz_to_str(TimestampTz t);
111 :
112 : extern int tm2timestamp(struct pg_tm *tm, fsec_t fsec, int *tzp, Timestamp *result);
113 : extern int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm *tm,
114 : fsec_t *fsec, const char **tzn, pg_tz *attimezone);
115 : extern void dt2time(Timestamp jd, int *hour, int *min, int *sec, fsec_t *fsec);
116 :
117 : extern void interval2itm(Interval span, struct pg_itm *itm);
118 : extern int itm2interval(struct pg_itm *itm, Interval *span);
119 : extern int itmin2interval(struct pg_itm_in *itm_in, Interval *span);
120 :
121 : extern Timestamp SetEpochTimestamp(void);
122 : extern void GetEpochTime(struct pg_tm *tm);
123 :
124 : extern int timestamp_cmp_internal(Timestamp dt1, Timestamp dt2);
125 :
126 : /* timestamp comparison works for timestamptz also */
127 : #define timestamptz_cmp_internal(dt1,dt2) timestamp_cmp_internal(dt1, dt2)
128 :
129 : extern TimestampTz timestamp2timestamptz_opt_overflow(Timestamp timestamp,
130 : int *overflow);
131 : extern int32 timestamp_cmp_timestamptz_internal(Timestamp timestampVal,
132 : TimestampTz dt2);
133 :
134 : extern int isoweek2j(int year, int week);
135 : extern void isoweek2date(int woy, int *year, int *mon, int *mday);
136 : extern void isoweekdate2date(int isoweek, int wday, int *year, int *mon, int *mday);
137 : extern int date2isoweek(int year, int mon, int mday);
138 : extern int date2isoyear(int year, int mon, int mday);
139 : extern int date2isoyearday(int year, int mon, int mday);
140 :
141 : extern bool TimestampTimestampTzRequiresRewrite(void);
142 :
143 : #endif /* TIMESTAMP_H */
|