LCOV - code coverage report
Current view: top level - src/include/utils - timestamp.h (source / functions) Coverage Total Hit
Test: Code coverage Lines: 70.6 % 17 12
Test Date: 2026-01-26 10:56:24 Functions: 85.7 % 7 6
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 0.0 % 2 0

             Branch data     Line data    Source code
       1                 :             : /*-------------------------------------------------------------------------
       2                 :             :  *
       3                 :             :  * timestamp.h
       4                 :             :  *        Definitions for the SQL "timestamp" and "interval" types.
       5                 :             :  *
       6                 :             :  * Portions Copyright (c) 1996-2026, 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                 :             :  * Functions 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                 :             : static inline Timestamp
      28                 :      152750 : DatumGetTimestamp(Datum X)
      29                 :             : {
      30                 :      152750 :         return (Timestamp) DatumGetInt64(X);
      31                 :             : }
      32                 :             : 
      33                 :             : static inline TimestampTz
      34                 :       27089 : DatumGetTimestampTz(Datum X)
      35                 :             : {
      36                 :       27089 :         return (TimestampTz) DatumGetInt64(X);
      37                 :             : }
      38                 :             : 
      39                 :             : static inline Interval *
      40                 :      112806 : DatumGetIntervalP(Datum X)
      41                 :             : {
      42                 :      112806 :         return (Interval *) DatumGetPointer(X);
      43                 :             : }
      44                 :             : 
      45                 :             : static inline Datum
      46                 :      485009 : TimestampGetDatum(Timestamp X)
      47                 :             : {
      48                 :      485009 :         return Int64GetDatum(X);
      49                 :             : }
      50                 :             : 
      51                 :             : static inline Datum
      52                 :       21572 : TimestampTzGetDatum(TimestampTz X)
      53                 :             : {
      54                 :       21572 :         return Int64GetDatum(X);
      55                 :             : }
      56                 :             : 
      57                 :             : static inline Datum
      58                 :       26297 : IntervalPGetDatum(const Interval *X)
      59                 :             : {
      60                 :       26297 :         return PointerGetDatum(X);
      61                 :             : }
      62                 :             : 
      63                 :             : #define PG_GETARG_TIMESTAMP(n) DatumGetTimestamp(PG_GETARG_DATUM(n))
      64                 :             : #define PG_GETARG_TIMESTAMPTZ(n) DatumGetTimestampTz(PG_GETARG_DATUM(n))
      65                 :             : #define PG_GETARG_INTERVAL_P(n) DatumGetIntervalP(PG_GETARG_DATUM(n))
      66                 :             : 
      67                 :             : #define PG_RETURN_TIMESTAMP(x) return TimestampGetDatum(x)
      68                 :             : #define PG_RETURN_TIMESTAMPTZ(x) return TimestampTzGetDatum(x)
      69                 :             : #define PG_RETURN_INTERVAL_P(x) return IntervalPGetDatum(x)
      70                 :             : 
      71                 :             : 
      72                 :             : #define TIMESTAMP_MASK(b) (1 << (b))
      73                 :             : #define INTERVAL_MASK(b) (1 << (b))
      74                 :             : 
      75                 :             : /* Macros to handle packing and unpacking the typmod field for intervals */
      76                 :             : #define INTERVAL_FULL_RANGE (0x7FFF)
      77                 :             : #define INTERVAL_RANGE_MASK (0x7FFF)
      78                 :             : #define INTERVAL_FULL_PRECISION (0xFFFF)
      79                 :             : #define INTERVAL_PRECISION_MASK (0xFFFF)
      80                 :             : #define INTERVAL_TYPMOD(p,r) ((((r) & INTERVAL_RANGE_MASK) << 16) | ((p) & INTERVAL_PRECISION_MASK))
      81                 :             : #define INTERVAL_PRECISION(t) ((t) & INTERVAL_PRECISION_MASK)
      82                 :             : #define INTERVAL_RANGE(t) (((t) >> 16) & INTERVAL_RANGE_MASK)
      83                 :             : 
      84                 :             : /* Macros for doing timestamp arithmetic without assuming timestamp's units */
      85                 :             : #define TimestampTzPlusMilliseconds(tz,ms) ((tz) + ((ms) * (int64) 1000))
      86                 :             : #define TimestampTzPlusSeconds(tz,s) ((tz) + ((s) * (int64) 1000000))
      87                 :             : 
      88                 :             : /* Helper for simple subtraction between two timestamps */
      89                 :             : static inline uint64
      90                 :           0 : TimestampDifferenceMicroseconds(TimestampTz start_time,
      91                 :             :                                                                 TimestampTz stop_time)
      92                 :             : {
      93         [ #  # ]:           0 :         if (start_time >= stop_time)
      94                 :           0 :                 return 0;
      95                 :           0 :         return (uint64) stop_time - start_time;
      96                 :           0 : }
      97                 :             : 
      98                 :             : /* Set at postmaster start */
      99                 :             : extern PGDLLIMPORT TimestampTz PgStartTime;
     100                 :             : 
     101                 :             : /* Set at configuration reload */
     102                 :             : extern PGDLLIMPORT TimestampTz PgReloadTime;
     103                 :             : 
     104                 :             : 
     105                 :             : /* Internal routines (not fmgr-callable) */
     106                 :             : 
     107                 :             : extern int32 anytimestamp_typmod_check(bool istz, int32 typmod);
     108                 :             : 
     109                 :             : extern TimestampTz GetCurrentTimestamp(void);
     110                 :             : extern TimestampTz GetSQLCurrentTimestamp(int32 typmod);
     111                 :             : extern Timestamp GetSQLLocalTimestamp(int32 typmod);
     112                 :             : extern void TimestampDifference(TimestampTz start_time, TimestampTz stop_time,
     113                 :             :                                                                 long *secs, int *microsecs);
     114                 :             : extern long TimestampDifferenceMilliseconds(TimestampTz start_time,
     115                 :             :                                                                                         TimestampTz stop_time);
     116                 :             : extern bool TimestampDifferenceExceeds(TimestampTz start_time,
     117                 :             :                                                                            TimestampTz stop_time,
     118                 :             :                                                                            int msec);
     119                 :             : extern bool TimestampDifferenceExceedsSeconds(TimestampTz start_time,
     120                 :             :                                                                                           TimestampTz stop_time,
     121                 :             :                                                                                           int threshold_sec);
     122                 :             : 
     123                 :             : extern TimestampTz time_t_to_timestamptz(pg_time_t tm);
     124                 :             : extern pg_time_t timestamptz_to_time_t(TimestampTz t);
     125                 :             : 
     126                 :             : extern const char *timestamptz_to_str(TimestampTz t);
     127                 :             : 
     128                 :             : extern int      tm2timestamp(struct pg_tm *tm, fsec_t fsec, int *tzp, Timestamp *result);
     129                 :             : extern int      timestamp2tm(Timestamp dt, int *tzp, struct pg_tm *tm,
     130                 :             :                                                  fsec_t *fsec, const char **tzn, pg_tz *attimezone);
     131                 :             : extern void dt2time(Timestamp jd, int *hour, int *min, int *sec, fsec_t *fsec);
     132                 :             : 
     133                 :             : extern void interval2itm(Interval span, struct pg_itm *itm);
     134                 :             : extern int      itm2interval(struct pg_itm *itm, Interval *span);
     135                 :             : extern int      itmin2interval(struct pg_itm_in *itm_in, Interval *span);
     136                 :             : 
     137                 :             : extern Timestamp SetEpochTimestamp(void);
     138                 :             : extern void GetEpochTime(struct pg_tm *tm);
     139                 :             : 
     140                 :             : extern int      timestamp_cmp_internal(Timestamp dt1, Timestamp dt2);
     141                 :             : 
     142                 :             : /* timestamp comparison works for timestamptz also */
     143                 :             : #define timestamptz_cmp_internal(dt1,dt2)       timestamp_cmp_internal(dt1, dt2)
     144                 :             : 
     145                 :             : extern TimestampTz timestamp2timestamptz_safe(Timestamp timestamp,
     146                 :             :                                                                                           Node *escontext);
     147                 :             : extern Timestamp timestamptz2timestamp_safe(TimestampTz timestamp,
     148                 :             :                                                                                         Node *escontext);
     149                 :             : 
     150                 :             : extern int32 timestamp_cmp_timestamptz_internal(Timestamp timestampVal,
     151                 :             :                                                                                                 TimestampTz dt2);
     152                 :             : 
     153                 :             : extern int      isoweek2j(int year, int week);
     154                 :             : extern void isoweek2date(int woy, int *year, int *mon, int *mday);
     155                 :             : extern void isoweekdate2date(int isoweek, int wday, int *year, int *mon, int *mday);
     156                 :             : extern int      date2isoweek(int year, int mon, int mday);
     157                 :             : extern int      date2isoyear(int year, int mon, int mday);
     158                 :             : extern int      date2isoyearday(int year, int mon, int mday);
     159                 :             : 
     160                 :             : extern bool TimestampTimestampTzRequiresRewrite(void);
     161                 :             : 
     162                 :             : #endif                                                  /* TIMESTAMP_H */
        

Generated by: LCOV version 2.3.2-1