1
0
mirror of https://github.com/postgres/postgres.git synced 2026-01-27 21:43:08 +03:00
Files
postgres/src/include/utils/date.h
Tom Lane 3628af4210 Add a macro for the declared typlen of type timetz.
pg_type.typlen says 12 for the size of timetz, but sizeof(TimeTzADT)
will be 16 on most platforms due to alignment padding.  Using the
sizeof number is no problem for usages such as palloc'ing a result
datum, but in usages such as datumCopy we really ought to match
what pg_type says.  Add a macro TIMETZ_TYPLEN so that we have a
symbolic way to write that rather than hard-coding "12".

I cannot find any place where we've needed this so far, but an
upcoming patch requires it.

Author: Tom Lane <tgl@sss.pgh.pa.us>
Reviewed-by: Chao Li <li.evan.chao@gmail.com>
Discussion: https://postgr.es/m/2329959.1765047648@sss.pgh.pa.us
2025-12-07 11:33:35 -05:00

129 lines
3.8 KiB
C

/*-------------------------------------------------------------------------
*
* date.h
* Definitions for the SQL "date" and "time" types.
*
*
* Portions Copyright (c) 1996-2025, 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;
/*
* sizeof(TimeTzADT) will be 16 on most platforms due to alignment padding.
* However, timetz's typlen is 12 according to pg_type. In most places
* we can get away with using sizeof(TimeTzADT), but where it's important
* to match the declared typlen, use TIMETZ_TYPLEN.
*/
#define TIMETZ_TYPLEN 12
/*
* 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 int32 anytime_typmod_check(bool istz, int32 typmod);
extern double date2timestamp_no_overflow(DateADT dateVal);
extern Timestamp date2timestamp_safe(DateADT dateVal, Node *escontext);
extern TimestampTz date2timestamptz_safe(DateADT dateVal, Node *escontext);
extern DateADT timestamp2date_safe(Timestamp timestamp, Node *escontext);
extern DateADT timestamptz2date_safe(TimestampTz timestamp, Node *escontext);
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 DateADT GetSQLCurrentDate(void);
extern TimeTzADT *GetSQLCurrentTime(int32 typmod);
extern TimeADT GetSQLLocalTime(int32 typmod);
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 */