1 | /*------------------------------------------------------------------------- |
2 | * |
3 | * timestamp.h |
4 | * Definitions for the SQL "timestamp" and "interval" types. |
5 | * |
6 | * Portions Copyright (c) 1996-2019, 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 */ |
60 | extern TimestampTz PgStartTime; |
61 | |
62 | /* Set at configuration reload */ |
63 | extern TimestampTz PgReloadTime; |
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); |
71 | extern TimestampTz GetSQLCurrentTimestamp(int32 typmod); |
72 | extern Timestamp GetSQLLocalTimestamp(int32 typmod); |
73 | extern void TimestampDifference(TimestampTz start_time, TimestampTz stop_time, |
74 | long *secs, int *microsecs); |
75 | extern bool TimestampDifferenceExceeds(TimestampTz start_time, |
76 | TimestampTz stop_time, |
77 | int msec); |
78 | |
79 | extern TimestampTz time_t_to_timestamptz(pg_time_t tm); |
80 | extern pg_time_t timestamptz_to_time_t(TimestampTz t); |
81 | |
82 | extern const char *timestamptz_to_str(TimestampTz t); |
83 | |
84 | extern int tm2timestamp(struct pg_tm *tm, fsec_t fsec, int *tzp, Timestamp *dt); |
85 | extern int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm *tm, |
86 | fsec_t *fsec, const char **tzn, pg_tz *attimezone); |
87 | extern void dt2time(Timestamp dt, int *hour, int *min, int *sec, fsec_t *fsec); |
88 | |
89 | extern int interval2tm(Interval span, struct pg_tm *tm, fsec_t *fsec); |
90 | extern int tm2interval(struct pg_tm *tm, fsec_t fsec, Interval *span); |
91 | |
92 | extern Timestamp SetEpochTimestamp(void); |
93 | extern void GetEpochTime(struct pg_tm *tm); |
94 | |
95 | extern int timestamp_cmp_internal(Timestamp dt1, Timestamp dt2); |
96 | |
97 | /* timestamp comparison works for timestamptz also */ |
98 | #define timestamptz_cmp_internal(dt1,dt2) timestamp_cmp_internal(dt1, dt2) |
99 | |
100 | extern int isoweek2j(int year, int week); |
101 | extern void isoweek2date(int woy, int *year, int *mon, int *mday); |
102 | extern void isoweekdate2date(int isoweek, int wday, int *year, int *mon, int *mday); |
103 | extern int date2isoweek(int year, int mon, int mday); |
104 | extern int date2isoyear(int year, int mon, int mday); |
105 | extern int date2isoyearday(int year, int mon, int mday); |
106 | |
107 | extern bool TimestampTimestampTzRequiresRewrite(void); |
108 | |
109 | #endif /* TIMESTAMP_H */ |
110 | |