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  * 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,
77  int msec);
78 
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 #endif /* TIMESTAMP_H */
const char * timestamptz_to_str(TimestampTz t)
Definition: timestamp.c:1710
void TimestampDifference(TimestampTz start_time, TimestampTz stop_time, long *secs, int *microsecs)
Definition: timestamp.c:1624
int64 pg_time_t
Definition: pgtime.h:23
int64 TimestampTz
Definition: timestamp.h:39
int timestamp_cmp_internal(Timestamp dt1, Timestamp dt2)
Definition: timestamp.c:2023
int isoweek2j(int year, int week)
Definition: timestamp.c:4041
int tm2timestamp(struct pg_tm *tm, fsec_t fsec, int *tzp, Timestamp *dt)
Definition: timestamp.c:1854
TimestampTz time_t_to_timestamptz(pg_time_t tm)
Definition: timestamp.c:1670
Definition: pgtime.h:25
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1570
static time_t start_time
Definition: pg_ctl.c:103
signed int int32
Definition: c.h:246
static struct pg_tm tm
Definition: localtime.c:107
void isoweekdate2date(int isoweek, int wday, int *year, int *mon, int *mday)
Definition: timestamp.c:4074
TimestampTz GetSQLCurrentTimestamp(int32 typmod)
Definition: timestamp.c:1588
void isoweek2date(int woy, int *year, int *mon, int *mday)
Definition: timestamp.c:4061
Timestamp GetSQLLocalTimestamp(int32 typmod)
Definition: timestamp.c:1602
int tm2interval(struct pg_tm *tm, fsec_t fsec, Interval *span)
Definition: timestamp.c:1930
bool TimestampDifferenceExceeds(TimestampTz start_time, TimestampTz stop_time, int msec)
Definition: timestamp.c:1649
int32 fsec_t
Definition: timestamp.h:41
void GetEpochTime(struct pg_tm *tm)
Definition: timestamp.c:1984
pg_time_t timestamptz_to_time_t(TimestampTz t)
Definition: timestamp.c:1692
int64 Timestamp
Definition: timestamp.h:38
Definition: pgtz.h:59
int date2isoyear(int year, int mon, int mday)
Definition: timestamp.c:4146
TimestampTz PgStartTime
Definition: timestamp.c:49
int interval2tm(Interval span, struct pg_tm *tm, fsec_t *fsec)
Definition: timestamp.c:1902
Timestamp SetEpochTimestamp(void)
Definition: timestamp.c:2003
int date2isoweek(int year, int mon, int mday)
Definition: timestamp.c:4092
int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm *tm, fsec_t *fsec, const char **tzn, pg_tz *attimezone)
Definition: timestamp.c:1758
void dt2time(Timestamp dt, int *hour, int *min, int *sec, fsec_t *fsec)
Definition: timestamp.c:1731
int date2isoyearday(int year, int mon, int mday)
Definition: timestamp.c:4203
int32 anytimestamp_typmod_check(bool istz, int32 typmod)
Definition: timestamp.c:102
TimestampTz PgReloadTime
Definition: timestamp.c:52