summaryrefslogtreecommitdiff
path: root/src/include/utils/date.h
blob: 5fd886b3dba6450fea0f28a3c9b3a08260ba5ab5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*-------------------------------------------------------------------------
 *
 * date.h
 *	  Definitions for the SQL "date" and "time" types.
 *
 *
 * Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group
 * Portions Copyright (c) 1994, Regents of the University of California
 *
 * src/include/utils/date.h
 *
 *-------------------------------------------------------------------------
 */
#ifndef DATE_H
#define DATE_H

#include <math.h>

#include "datatype/timestamp.h"
#include "fmgr.h"
#include "pgtime.h"

typedef int32 DateADT;

typedef int64 TimeADT;

typedef struct
{
	TimeADT		time;			/* all time units other than months and years */
	int32		zone;			/* numeric time zone, in seconds */
} TimeTzADT;

/*
 * Infinity and minus infinity must be the max and min values of DateADT.
 */
#define DATEVAL_NOBEGIN		((DateADT) PG_INT32_MIN)
#define DATEVAL_NOEND		((DateADT) PG_INT32_MAX)

#define DATE_NOBEGIN(j)		((j) = DATEVAL_NOBEGIN)
#define DATE_IS_NOBEGIN(j)	((j) == DATEVAL_NOBEGIN)
#define DATE_NOEND(j)		((j) = DATEVAL_NOEND)
#define DATE_IS_NOEND(j)	((j) == DATEVAL_NOEND)
#define DATE_NOT_FINITE(j)	(DATE_IS_NOBEGIN(j) || DATE_IS_NOEND(j))

#define MAX_TIME_PRECISION 6

/*
 * Functions for fmgr-callable functions.
 *
 * For TimeADT, we make use of the same support routines as for int64.
 * Therefore TimeADT is pass-by-reference if and only if int64 is!
 */
static inline DateADT
DatumGetDateADT(Datum X)
{
	return (DateADT) DatumGetInt32(X);
}

static inline TimeADT
DatumGetTimeADT(Datum X)
{
	return (TimeADT) DatumGetInt64(X);
}

static inline TimeTzADT *
DatumGetTimeTzADTP(Datum X)
{
	return (TimeTzADT *) DatumGetPointer(X);
}

static inline Datum
DateADTGetDatum(DateADT X)
{
	return Int32GetDatum(X);
}

static inline Datum
TimeADTGetDatum(TimeADT X)
{
	return Int64GetDatum(X);
}

static inline Datum
TimeTzADTPGetDatum(const TimeTzADT *X)
{
	return PointerGetDatum(X);
}

#define PG_GETARG_DATEADT(n)	 DatumGetDateADT(PG_GETARG_DATUM(n))
#define PG_GETARG_TIMEADT(n)	 DatumGetTimeADT(PG_GETARG_DATUM(n))
#define PG_GETARG_TIMETZADT_P(n) DatumGetTimeTzADTP(PG_GETARG_DATUM(n))

#define PG_RETURN_DATEADT(x)	 return DateADTGetDatum(x)
#define PG_RETURN_TIMEADT(x)	 return TimeADTGetDatum(x)
#define PG_RETURN_TIMETZADT_P(x) return TimeTzADTPGetDatum(x)


/* date.c */
extern double date2timestamp_no_overflow(DateADT dateVal);
extern Timestamp date2timestamp_opt_overflow(DateADT dateVal, int *overflow);
extern TimestampTz date2timestamptz_opt_overflow(DateADT dateVal, int *overflow);
extern int32 date_cmp_timestamp_internal(DateADT dateVal, Timestamp dt2);
extern int32 date_cmp_timestamptz_internal(DateADT dateVal, TimestampTz dt2);

extern void EncodeSpecialDate(DateADT dt, char *str);
extern int	time2tm(TimeADT time, struct pg_tm *tm, fsec_t *fsec);
extern int	timetz2tm(TimeTzADT *time, struct pg_tm *tm, fsec_t *fsec, int *tzp);
extern int	tm2time(struct pg_tm *tm, fsec_t fsec, TimeADT *result);
extern int	tm2timetz(struct pg_tm *tm, fsec_t fsec, int tz, TimeTzADT *result);
extern bool time_overflows(int hour, int min, int sec, fsec_t fsec);
extern bool float_time_overflows(int hour, int min, double sec);
extern void AdjustTimeForTypmod(TimeADT *time, int32 typmod);

#endif							/* DATE_H */