PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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-2017, 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  * or float8. Therefore Timestamp is pass-by-reference if and only if
26  * int64 or float8 is!
27  */
28 #ifdef HAVE_INT64_TIMESTAMP
29 
30 #define DatumGetTimestamp(X) ((Timestamp) DatumGetInt64(X))
31 #define DatumGetTimestampTz(X) ((TimestampTz) DatumGetInt64(X))
32 #define DatumGetIntervalP(X) ((Interval *) DatumGetPointer(X))
33 
34 #define TimestampGetDatum(X) Int64GetDatum(X)
35 #define TimestampTzGetDatum(X) Int64GetDatum(X)
36 #define IntervalPGetDatum(X) PointerGetDatum(X)
37 
38 #define PG_GETARG_TIMESTAMP(n) DatumGetTimestamp(PG_GETARG_DATUM(n))
39 #define PG_GETARG_TIMESTAMPTZ(n) DatumGetTimestampTz(PG_GETARG_DATUM(n))
40 #define PG_GETARG_INTERVAL_P(n) DatumGetIntervalP(PG_GETARG_DATUM(n))
41 
42 #define PG_RETURN_TIMESTAMP(x) return TimestampGetDatum(x)
43 #define PG_RETURN_TIMESTAMPTZ(x) return TimestampTzGetDatum(x)
44 #define PG_RETURN_INTERVAL_P(x) return IntervalPGetDatum(x)
45 #else /* !HAVE_INT64_TIMESTAMP */
46 
47 #define DatumGetTimestamp(X) ((Timestamp) DatumGetFloat8(X))
48 #define DatumGetTimestampTz(X) ((TimestampTz) DatumGetFloat8(X))
49 #define DatumGetIntervalP(X) ((Interval *) DatumGetPointer(X))
50 
51 #define TimestampGetDatum(X) Float8GetDatum(X)
52 #define TimestampTzGetDatum(X) Float8GetDatum(X)
53 #define IntervalPGetDatum(X) PointerGetDatum(X)
54 
55 #define PG_GETARG_TIMESTAMP(n) DatumGetTimestamp(PG_GETARG_DATUM(n))
56 #define PG_GETARG_TIMESTAMPTZ(n) DatumGetTimestampTz(PG_GETARG_DATUM(n))
57 #define PG_GETARG_INTERVAL_P(n) DatumGetIntervalP(PG_GETARG_DATUM(n))
58 
59 #define PG_RETURN_TIMESTAMP(x) return TimestampGetDatum(x)
60 #define PG_RETURN_TIMESTAMPTZ(x) return TimestampTzGetDatum(x)
61 #define PG_RETURN_INTERVAL_P(x) return IntervalPGetDatum(x)
62 #endif /* HAVE_INT64_TIMESTAMP */
63 
64 
65 #define TIMESTAMP_MASK(b) (1 << (b))
66 #define INTERVAL_MASK(b) (1 << (b))
67 
68 /* Macros to handle packing and unpacking the typmod field for intervals */
69 #define INTERVAL_FULL_RANGE (0x7FFF)
70 #define INTERVAL_RANGE_MASK (0x7FFF)
71 #define INTERVAL_FULL_PRECISION (0xFFFF)
72 #define INTERVAL_PRECISION_MASK (0xFFFF)
73 #define INTERVAL_TYPMOD(p,r) ((((r) & INTERVAL_RANGE_MASK) << 16) | ((p) & INTERVAL_PRECISION_MASK))
74 #define INTERVAL_PRECISION(t) ((t) & INTERVAL_PRECISION_MASK)
75 #define INTERVAL_RANGE(t) (((t) >> 16) & INTERVAL_RANGE_MASK)
76 
77 #ifdef HAVE_INT64_TIMESTAMP
78 #define TimestampTzPlusMilliseconds(tz,ms) ((tz) + ((ms) * (int64) 1000))
79 #else
80 #define TimestampTzPlusMilliseconds(tz,ms) ((tz) + ((ms) / 1000.0))
81 #endif
82 
83 
84 /* Set at postmaster start */
86 
87 /* Set at configuration reload */
89 
90 
91 /* Internal routines (not fmgr-callable) */
92 
93 extern int32 anytimestamp_typmod_check(bool istz, int32 typmod);
94 
95 extern TimestampTz GetCurrentTimestamp(void);
97 extern Timestamp GetSQLLocalTimestamp(int32 typmod);
99  long *secs, int *microsecs);
101  TimestampTz stop_time,
102  int msec);
103 
104 /*
105  * Prototypes for functions to deal with integer timestamps, when the native
106  * format is float timestamps.
107  */
108 #ifndef HAVE_INT64_TIMESTAMP
109 extern int64 GetCurrentIntegerTimestamp(void);
111 #else
112 #define GetCurrentIntegerTimestamp() GetCurrentTimestamp()
113 #define IntegerTimestampToTimestampTz(timestamp) (timestamp)
114 #endif
115 
118 
119 extern const char *timestamptz_to_str(TimestampTz t);
120 
121 extern int tm2timestamp(struct pg_tm * tm, fsec_t fsec, int *tzp, Timestamp *dt);
122 extern int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm * tm,
123  fsec_t *fsec, const char **tzn, pg_tz *attimezone);
124 extern void dt2time(Timestamp dt, int *hour, int *min, int *sec, fsec_t *fsec);
125 
126 extern int interval2tm(Interval span, struct pg_tm * tm, fsec_t *fsec);
127 extern int tm2interval(struct pg_tm * tm, fsec_t fsec, Interval *span);
128 
129 extern Timestamp SetEpochTimestamp(void);
130 extern void GetEpochTime(struct pg_tm * tm);
131 
132 extern int timestamp_cmp_internal(Timestamp dt1, Timestamp dt2);
133 
134 /* timestamp comparison works for timestamptz also */
135 #define timestamptz_cmp_internal(dt1,dt2) timestamp_cmp_internal(dt1, dt2)
136 
137 extern int isoweek2j(int year, int week);
138 extern void isoweek2date(int woy, int *year, int *mon, int *mday);
139 extern void isoweekdate2date(int isoweek, int wday, int *year, int *mon, int *mday);
140 extern int date2isoweek(int year, int mon, int mday);
141 extern int date2isoyear(int year, int mon, int mday);
142 extern int date2isoyearday(int year, int mon, int mday);
143 
144 #endif /* TIMESTAMP_H */
const char * timestamptz_to_str(TimestampTz t)
Definition: timestamp.c:1893
void TimestampDifference(TimestampTz start_time, TimestampTz stop_time, long *secs, int *microsecs)
Definition: timestamp.c:1790
int64 pg_time_t
Definition: pgtime.h:23
double fsec_t
Definition: timestamp.h:53
int timestamp_cmp_internal(Timestamp dt1, Timestamp dt2)
Definition: timestamp.c:2295
int isoweek2j(int year, int week)
Definition: timestamp.c:4382
int tm2timestamp(struct pg_tm *tm, fsec_t fsec, int *tzp, Timestamp *dt)
Definition: timestamp.c:2088
TimestampTz time_t_to_timestamptz(pg_time_t tm)
Definition: timestamp.c:1845
Definition: pgtime.h:25
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1687
static time_t start_time
Definition: pg_ctl.c:94
signed int int32
Definition: c.h:253
static struct pg_tm tm
Definition: localtime.c:103
double TimestampTz
Definition: timestamp.h:51
void isoweekdate2date(int isoweek, int wday, int *year, int *mon, int *mday)
Definition: timestamp.c:4415
TimestampTz GetSQLCurrentTimestamp(int32 typmod)
Definition: timestamp.c:1754
void isoweek2date(int woy, int *year, int *mon, int *mday)
Definition: timestamp.c:4402
Timestamp GetSQLLocalTimestamp(int32 typmod)
Definition: timestamp.c:1768
int tm2interval(struct pg_tm *tm, fsec_t fsec, Interval *span)
Definition: timestamp.c:2186
bool TimestampDifferenceExceeds(TimestampTz start_time, TimestampTz stop_time, int msec)
Definition: timestamp.c:1820
void GetEpochTime(struct pg_tm *tm)
Definition: timestamp.c:2254
pg_time_t timestamptz_to_time_t(TimestampTz t)
Definition: timestamp.c:1870
double Timestamp
Definition: timestamp.h:50
Definition: pgtz.h:59
int date2isoyear(int year, int mon, int mday)
Definition: timestamp.c:4487
TimestampTz PgStartTime
Definition: timestamp.c:48
int interval2tm(Interval span, struct pg_tm *tm, fsec_t *fsec)
Definition: timestamp.c:2140
double timestamp
Timestamp SetEpochTimestamp(void)
Definition: timestamp.c:2273
TimestampTz IntegerTimestampToTimestampTz(int64 timestamp)
Definition: timestamp.c:1739
int date2isoweek(int year, int mon, int mday)
Definition: timestamp.c:4433
int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm *tm, fsec_t *fsec, const char **tzn, pg_tz *attimezone)
Definition: timestamp.c:1950
void dt2time(Timestamp dt, int *hour, int *min, int *sec, fsec_t *fsec)
Definition: timestamp.c:1914
int date2isoyearday(int year, int mon, int mday)
Definition: timestamp.c:4544
int32 anytimestamp_typmod_check(bool istz, int32 typmod)
Definition: timestamp.c:101
TimestampTz PgReloadTime
Definition: timestamp.c:51
int64 GetCurrentIntegerTimestamp(void)
Definition: timestamp.c:1715