Header And Logo

PostgreSQL
| The world's most advanced open source database.

Defines | Functions

nabstime.c File Reference

#include "postgres.h"
#include <ctype.h>
#include <float.h>
#include <limits.h>
#include <time.h>
#include "libpq/pqformat.h"
#include "miscadmin.h"
#include "utils/builtins.h"
#include "utils/datetime.h"
#include "utils/nabstime.h"
Include dependency graph for nabstime.c:

Go to the source code of this file.

Defines

#define MIN_DAYNUM   (-24856)
#define MAX_DAYNUM   24854
#define IsSpace(C)   ((C) == ' ')
#define T_INTERVAL_INVAL   0
#define T_INTERVAL_VALID   1
#define T_INTERVAL_LEN   80
#define INVALID_INTERVAL_STR   "Undefined Range"
#define INVALID_INTERVAL_STR_LEN   (sizeof(INVALID_INTERVAL_STR)-1)
#define ABSTIMEMIN(t1, t2)
#define ABSTIMEMAX(t1, t2)

Functions

static AbsoluteTime tm2abstime (struct pg_tm *tm, int tz)
static void reltime2tm (RelativeTime time, struct pg_tm *tm)
static void parsetinterval (char *i_string, AbsoluteTime *i_start, AbsoluteTime *i_end)
AbsoluteTime GetCurrentAbsoluteTime (void)
void abstime2tm (AbsoluteTime _time, int *tzp, struct pg_tm *tm, char **tzn)
Datum abstimein (PG_FUNCTION_ARGS)
Datum abstimeout (PG_FUNCTION_ARGS)
Datum abstimerecv (PG_FUNCTION_ARGS)
Datum abstimesend (PG_FUNCTION_ARGS)
Datum abstime_finite (PG_FUNCTION_ARGS)
static int abstime_cmp_internal (AbsoluteTime a, AbsoluteTime b)
Datum abstimeeq (PG_FUNCTION_ARGS)
Datum abstimene (PG_FUNCTION_ARGS)
Datum abstimelt (PG_FUNCTION_ARGS)
Datum abstimegt (PG_FUNCTION_ARGS)
Datum abstimele (PG_FUNCTION_ARGS)
Datum abstimege (PG_FUNCTION_ARGS)
Datum btabstimecmp (PG_FUNCTION_ARGS)
Datum timestamp_abstime (PG_FUNCTION_ARGS)
Datum abstime_timestamp (PG_FUNCTION_ARGS)
Datum timestamptz_abstime (PG_FUNCTION_ARGS)
Datum abstime_timestamptz (PG_FUNCTION_ARGS)
Datum reltimein (PG_FUNCTION_ARGS)
Datum reltimeout (PG_FUNCTION_ARGS)
Datum reltimerecv (PG_FUNCTION_ARGS)
Datum reltimesend (PG_FUNCTION_ARGS)
Datum tintervalin (PG_FUNCTION_ARGS)
Datum tintervalout (PG_FUNCTION_ARGS)
Datum tintervalrecv (PG_FUNCTION_ARGS)
Datum tintervalsend (PG_FUNCTION_ARGS)
Datum interval_reltime (PG_FUNCTION_ARGS)
Datum reltime_interval (PG_FUNCTION_ARGS)
Datum mktinterval (PG_FUNCTION_ARGS)
Datum timepl (PG_FUNCTION_ARGS)
Datum timemi (PG_FUNCTION_ARGS)
Datum intinterval (PG_FUNCTION_ARGS)
Datum tintervalrel (PG_FUNCTION_ARGS)
Datum timenow (PG_FUNCTION_ARGS)
static int reltime_cmp_internal (RelativeTime a, RelativeTime b)
Datum reltimeeq (PG_FUNCTION_ARGS)
Datum reltimene (PG_FUNCTION_ARGS)
Datum reltimelt (PG_FUNCTION_ARGS)
Datum reltimegt (PG_FUNCTION_ARGS)
Datum reltimele (PG_FUNCTION_ARGS)
Datum reltimege (PG_FUNCTION_ARGS)
Datum btreltimecmp (PG_FUNCTION_ARGS)
Datum tintervalsame (PG_FUNCTION_ARGS)
static int tinterval_cmp_internal (TimeInterval a, TimeInterval b)
Datum tintervaleq (PG_FUNCTION_ARGS)
Datum tintervalne (PG_FUNCTION_ARGS)
Datum tintervallt (PG_FUNCTION_ARGS)
Datum tintervalle (PG_FUNCTION_ARGS)
Datum tintervalgt (PG_FUNCTION_ARGS)
Datum tintervalge (PG_FUNCTION_ARGS)
Datum bttintervalcmp (PG_FUNCTION_ARGS)
Datum tintervalleneq (PG_FUNCTION_ARGS)
Datum tintervallenne (PG_FUNCTION_ARGS)
Datum tintervallenlt (PG_FUNCTION_ARGS)
Datum tintervallengt (PG_FUNCTION_ARGS)
Datum tintervallenle (PG_FUNCTION_ARGS)
Datum tintervallenge (PG_FUNCTION_ARGS)
Datum tintervalct (PG_FUNCTION_ARGS)
Datum tintervalov (PG_FUNCTION_ARGS)
Datum tintervalstart (PG_FUNCTION_ARGS)
Datum tintervalend (PG_FUNCTION_ARGS)
Datum timeofday (PG_FUNCTION_ARGS)

Define Documentation

#define ABSTIMEMAX (   t1,
  t2 
)
Value:

Definition at line 63 of file nabstime.c.

Referenced by mktinterval(), and tintervalin().

#define ABSTIMEMIN (   t1,
  t2 
)
Value:

Definition at line 59 of file nabstime.c.

Referenced by mktinterval(), and tintervalin().

#define INVALID_INTERVAL_STR   "Undefined Range"

Definition at line 56 of file nabstime.c.

Referenced by parsetinterval(), and tintervalout().

#define INVALID_INTERVAL_STR_LEN   (sizeof(INVALID_INTERVAL_STR)-1)

Definition at line 57 of file nabstime.c.

#define IsSpace (   C  )     ((C) == ' ')

Definition at line 43 of file nabstime.c.

Referenced by parsetinterval().

#define MAX_DAYNUM   24854

Definition at line 32 of file nabstime.c.

Referenced by tm2abstime().

#define MIN_DAYNUM   (-24856)

Definition at line 31 of file nabstime.c.

#define T_INTERVAL_INVAL   0
#define T_INTERVAL_LEN   80

Definition at line 55 of file nabstime.c.

Referenced by tintervalout().

#define T_INTERVAL_VALID   1

Definition at line 46 of file nabstime.c.

Referenced by intinterval(), and tintervalrel().


Function Documentation

void abstime2tm ( AbsoluteTime  _time,
int *  tzp,
struct pg_tm tm,
char **  tzn 
)

Definition at line 98 of file nabstime.c.

References CTimeZone, ereport, errcode(), errmsg(), HasCTZSet, MAXTZLEN, NULL, pg_gmtime(), pg_localtime(), session_timezone, StrNCpy, pg_tm::tm_gmtoff, pg_tm::tm_hour, pg_tm::tm_isdst, pg_tm::tm_mday, pg_tm::tm_min, pg_tm::tm_mon, pg_tm::tm_sec, pg_tm::tm_year, pg_tm::tm_zone, and WARNING.

Referenced by abstime_date(), abstime_timestamp(), abstime_timestamptz(), abstimeout(), and GetCurrentDateTime().

{
    pg_time_t   time = (pg_time_t) _time;
    struct pg_tm *tx;

    /*
     * If HasCTZSet is true then we have a brute force time zone specified. Go
     * ahead and rotate to the local time zone since we will later bypass any
     * calls which adjust the tm fields.
     */
    if (HasCTZSet && (tzp != NULL))
        time -= CTimeZone;

    if (!HasCTZSet && tzp != NULL)
        tx = pg_localtime(&time, session_timezone);
    else
        tx = pg_gmtime(&time);

    tm->tm_year = tx->tm_year + 1900;
    tm->tm_mon = tx->tm_mon + 1;
    tm->tm_mday = tx->tm_mday;
    tm->tm_hour = tx->tm_hour;
    tm->tm_min = tx->tm_min;
    tm->tm_sec = tx->tm_sec;
    tm->tm_isdst = tx->tm_isdst;

    tm->tm_gmtoff = tx->tm_gmtoff;
    tm->tm_zone = tx->tm_zone;

    if (tzp != NULL)
    {
        /*
         * We have a brute force time zone per SQL99? Then use it without
         * change since we have already rotated to the time zone.
         */
        if (HasCTZSet)
        {
            *tzp = CTimeZone;
            tm->tm_gmtoff = CTimeZone;
            tm->tm_isdst = 0;
            tm->tm_zone = NULL;
            if (tzn != NULL)
                *tzn = NULL;
        }
        else
        {
            *tzp = -tm->tm_gmtoff;      /* tm_gmtoff is Sun/DEC-ism */

            /*
             * XXX FreeBSD man pages indicate that this should work - tgl
             * 97/04/23
             */
            if (tzn != NULL)
            {
                /*
                 * Copy no more than MAXTZLEN bytes of timezone to tzn, in
                 * case it contains an error message, which doesn't fit in the
                 * buffer
                 */
                StrNCpy(*tzn, tm->tm_zone, MAXTZLEN + 1);
                if (strlen(tm->tm_zone) > MAXTZLEN)
                    ereport(WARNING,
                            (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                             errmsg("invalid time zone name: \"%s\"",
                                    tm->tm_zone)));
            }
        }
    }
    else
        tm->tm_isdst = -1;
}

static int abstime_cmp_internal ( AbsoluteTime  a,
AbsoluteTime  b 
) [static]

Definition at line 365 of file nabstime.c.

References INVALID_ABSTIME.

Referenced by abstimeeq(), abstimege(), abstimegt(), abstimele(), abstimelt(), abstimene(), and btabstimecmp().

{
    /*
     * We consider all INVALIDs to be equal and larger than any non-INVALID.
     * This is somewhat arbitrary; the important thing is to have a consistent
     * sort order.
     */
    if (a == INVALID_ABSTIME)
    {
        if (b == INVALID_ABSTIME)
            return 0;           /* INVALID = INVALID */
        else
            return 1;           /* INVALID > non-INVALID */
    }

    if (b == INVALID_ABSTIME)
        return -1;              /* non-INVALID < INVALID */

    if (a > b)
        return 1;
    else if (a == b)
        return 0;
    else
        return -1;
}

Datum abstime_finite ( PG_FUNCTION_ARGS   ) 
Datum abstime_timestamp ( PG_FUNCTION_ARGS   ) 

Definition at line 492 of file nabstime.c.

References abstime2tm(), ereport, errcode(), errmsg(), ERROR, INVALID_ABSTIME, MAXDATELEN, NOEND_ABSTIME, NOSTART_ABSTIME, NULL, PG_GETARG_ABSOLUTETIME, PG_RETURN_TIMESTAMP, TIMESTAMP_NOBEGIN, TIMESTAMP_NOEND, tm, and tm2timestamp().

Referenced by convert_timevalue_to_scalar().

{
    AbsoluteTime abstime = PG_GETARG_ABSOLUTETIME(0);
    Timestamp   result;
    struct pg_tm tt,
               *tm = &tt;
    int         tz;
    char        zone[MAXDATELEN + 1],
               *tzn = zone;

    switch (abstime)
    {
        case INVALID_ABSTIME:
            ereport(ERROR,
                    (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                 errmsg("cannot convert abstime \"invalid\" to timestamp")));
            TIMESTAMP_NOBEGIN(result);
            break;

        case NOSTART_ABSTIME:
            TIMESTAMP_NOBEGIN(result);
            break;

        case NOEND_ABSTIME:
            TIMESTAMP_NOEND(result);
            break;

        default:
            abstime2tm(abstime, &tz, tm, &tzn);
            if (tm2timestamp(tm, 0, NULL, &result) != 0)
                ereport(ERROR,
                        (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
                         errmsg("timestamp out of range")));
            break;
    };

    PG_RETURN_TIMESTAMP(result);
}

Datum abstime_timestamptz ( PG_FUNCTION_ARGS   ) 

Definition at line 565 of file nabstime.c.

References abstime2tm(), ereport, errcode(), errmsg(), ERROR, INVALID_ABSTIME, MAXDATELEN, NOEND_ABSTIME, NOSTART_ABSTIME, PG_GETARG_ABSOLUTETIME, PG_RETURN_TIMESTAMP, TIMESTAMP_NOBEGIN, TIMESTAMP_NOEND, tm, and tm2timestamp().

{
    AbsoluteTime abstime = PG_GETARG_ABSOLUTETIME(0);
    TimestampTz result;
    struct pg_tm tt,
               *tm = &tt;
    int         tz;
    char        zone[MAXDATELEN + 1],
               *tzn = zone;

    switch (abstime)
    {
        case INVALID_ABSTIME:
            ereport(ERROR,
                    (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                 errmsg("cannot convert abstime \"invalid\" to timestamp")));
            TIMESTAMP_NOBEGIN(result);
            break;

        case NOSTART_ABSTIME:
            TIMESTAMP_NOBEGIN(result);
            break;

        case NOEND_ABSTIME:
            TIMESTAMP_NOEND(result);
            break;

        default:
            abstime2tm(abstime, &tz, tm, &tzn);
            if (tm2timestamp(tm, 0, &tz, &result) != 0)
                ereport(ERROR,
                        (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
                         errmsg("timestamp out of range")));
            break;
    };

    PG_RETURN_TIMESTAMP(result);
}

Datum abstimeeq ( PG_FUNCTION_ARGS   ) 
Datum abstimege ( PG_FUNCTION_ARGS   ) 
Datum abstimegt ( PG_FUNCTION_ARGS   ) 
Datum abstimein ( PG_FUNCTION_ARGS   ) 

Definition at line 221 of file nabstime.c.

References DateTimeParseError(), DecodeDateTime(), DTK_DATE, DTK_EARLY, DTK_EPOCH, DTK_INVALID, DTK_LATE, elog, ERROR, MAXDATEFIELDS, MAXDATELEN, ParseDateTime(), PG_GETARG_CSTRING, PG_RETURN_ABSOLUTETIME, tm, and tm2abstime().

Referenced by parsetinterval().

{
    char       *str = PG_GETARG_CSTRING(0);
    AbsoluteTime result;
    fsec_t      fsec;
    int         tz = 0;
    struct pg_tm date,
               *tm = &date;
    int         dterr;
    char       *field[MAXDATEFIELDS];
    char        workbuf[MAXDATELEN + 1];
    int         dtype;
    int         nf,
                ftype[MAXDATEFIELDS];

    dterr = ParseDateTime(str, workbuf, sizeof(workbuf),
                          field, ftype, MAXDATEFIELDS, &nf);
    if (dterr == 0)
        dterr = DecodeDateTime(field, ftype, nf, &dtype, tm, &fsec, &tz);
    if (dterr != 0)
        DateTimeParseError(dterr, str, "abstime");

    switch (dtype)
    {
        case DTK_DATE:
            result = tm2abstime(tm, tz);
            break;

        case DTK_EPOCH:

            /*
             * Don't bother retaining this as a reserved value, but instead
             * just set to the actual epoch time (1970-01-01)
             */
            result = 0;
            break;

        case DTK_LATE:
            result = NOEND_ABSTIME;
            break;

        case DTK_EARLY:
            result = NOSTART_ABSTIME;
            break;

        case DTK_INVALID:
            result = INVALID_ABSTIME;
            break;

        default:
            elog(ERROR, "unexpected dtype %d while parsing abstime \"%s\"",
                 dtype, str);
            result = INVALID_ABSTIME;
            break;
    };

    PG_RETURN_ABSOLUTETIME(result);
}

Datum abstimele ( PG_FUNCTION_ARGS   ) 
Datum abstimelt ( PG_FUNCTION_ARGS   ) 
Datum abstimene ( PG_FUNCTION_ARGS   ) 
Datum abstimeout ( PG_FUNCTION_ARGS   ) 

Definition at line 285 of file nabstime.c.

References abstime2tm(), buf, DateStyle, EARLY, EncodeDateTime(), INVALID, INVALID_ABSTIME, LATE, MAXDATELEN, NOEND_ABSTIME, NOSTART_ABSTIME, PG_GETARG_ABSOLUTETIME, PG_RETURN_CSTRING, pstrdup(), and tm.

Referenced by tintervalout().

{
    AbsoluteTime time = PG_GETARG_ABSOLUTETIME(0);
    char       *result;
    int         tz;
    double      fsec = 0;
    struct pg_tm tt,
               *tm = &tt;
    char        buf[MAXDATELEN + 1];
    char        zone[MAXDATELEN + 1],
               *tzn = zone;

    switch (time)
    {
            /*
             * Note that timestamp no longer supports 'invalid'. Retain
             * 'invalid' for abstime for now, but dump it someday.
             */
        case INVALID_ABSTIME:
            strcpy(buf, INVALID);
            break;
        case NOEND_ABSTIME:
            strcpy(buf, LATE);
            break;
        case NOSTART_ABSTIME:
            strcpy(buf, EARLY);
            break;
        default:
            abstime2tm(time, &tz, tm, &tzn);
            EncodeDateTime(tm, fsec, true, tz, tzn, DateStyle, buf);
            break;
    }

    result = pstrdup(buf);
    PG_RETURN_CSTRING(result);
}

Datum abstimerecv ( PG_FUNCTION_ARGS   ) 
Datum abstimesend ( PG_FUNCTION_ARGS   ) 
Datum btabstimecmp ( PG_FUNCTION_ARGS   ) 
Datum btreltimecmp ( PG_FUNCTION_ARGS   ) 
Datum bttintervalcmp ( PG_FUNCTION_ARGS   ) 
AbsoluteTime GetCurrentAbsoluteTime ( void   ) 

Definition at line 88 of file nabstime.c.

References NULL.

Referenced by timenow(), and timetravel().

{
    time_t      now;

    now = time(NULL);
    return (AbsoluteTime) now;
}

Datum interval_reltime ( PG_FUNCTION_ARGS   ) 

Definition at line 834 of file nabstime.c.

References Interval::day, DAYS_PER_MONTH, DAYS_PER_YEAR, INT64CONST, Interval::month, PG_GETARG_INTERVAL_P, PG_RETURN_RELATIVETIME, SECS_PER_DAY, Interval::time, and USECS_PER_SEC.

{
    Interval   *interval = PG_GETARG_INTERVAL_P(0);
    RelativeTime time;
    int         year,
                month,
                day;
    TimeOffset  span;

    year = interval->month / MONTHS_PER_YEAR;
    month = interval->month % MONTHS_PER_YEAR;
    day = interval->day;

#ifdef HAVE_INT64_TIMESTAMP
    span = ((INT64CONST(365250000) * year + INT64CONST(30000000) * month +
             INT64CONST(1000000) * day) * INT64CONST(86400)) +
        interval->time;
    span /= USECS_PER_SEC;
#else
    span = (DAYS_PER_YEAR * year + (double) DAYS_PER_MONTH * month + day) * SECS_PER_DAY + interval->time;
#endif

    if (span < INT_MIN || span > INT_MAX)
        time = INVALID_RELTIME;
    else
        time = span;

    PG_RETURN_RELATIVETIME(time);
}

Datum intinterval ( PG_FUNCTION_ARGS   ) 
Datum mktinterval ( PG_FUNCTION_ARGS   ) 

Definition at line 917 of file nabstime.c.

References ABSTIMEMAX, ABSTIMEMIN, TimeIntervalData::data, INVALID_ABSTIME, palloc(), PG_GETARG_ABSOLUTETIME, PG_RETURN_TIMEINTERVAL, and TimeIntervalData::status.

{
    AbsoluteTime t1 = PG_GETARG_ABSOLUTETIME(0);
    AbsoluteTime t2 = PG_GETARG_ABSOLUTETIME(1);
    AbsoluteTime tstart = ABSTIMEMIN(t1, t2);
    AbsoluteTime tend = ABSTIMEMAX(t1, t2);
    TimeInterval tinterval;

    tinterval = (TimeInterval) palloc(sizeof(TimeIntervalData));

    if (t1 == INVALID_ABSTIME || t2 == INVALID_ABSTIME)
        tinterval->status = T_INTERVAL_INVAL;

    else
    {
        tinterval->status = T_INTERVAL_VALID;
        tinterval->data[0] = tstart;
        tinterval->data[1] = tend;
    }

    PG_RETURN_TIMEINTERVAL(tinterval);
}

static void parsetinterval ( char *  i_string,
AbsoluteTime i_start,
AbsoluteTime i_end 
) [static]

Definition at line 1469 of file nabstime.c.

References abstimein(), CStringGetDatum, DatumGetAbsoluteTime, DirectFunctionCall1, ereport, errcode(), errmsg(), ERROR, INVALID_INTERVAL_STR, and IsSpace.

Referenced by tintervalin().

{
    char       *p,
               *p1;
    char        c;

    p = i_string;
    /* skip leading blanks up to '[' */
    while ((c = *p) != '\0')
    {
        if (IsSpace(c))
            p++;
        else if (c != '[')
            goto bogus;         /* syntax error */
        else
            break;
    }
    if (c == '\0')
        goto bogus;             /* syntax error */
    p++;
    /* skip leading blanks up to '"' */
    while ((c = *p) != '\0')
    {
        if (IsSpace(c))
            p++;
        else if (c != '"')
            goto bogus;         /* syntax error */
        else
            break;
    }
    if (c == '\0')
        goto bogus;             /* syntax error */
    p++;
    if (strncmp(INVALID_INTERVAL_STR, p, strlen(INVALID_INTERVAL_STR)) == 0)
        goto bogus;             /* undefined range, handled like a syntax err. */
    /* search for the end of the first date and change it to a \0 */
    p1 = p;
    while ((c = *p1) != '\0')
    {
        if (c == '"')
            break;
        p1++;
    }
    if (c == '\0')
        goto bogus;             /* syntax error */
    *p1 = '\0';
    /* get the first date */
    *i_start = DatumGetAbsoluteTime(DirectFunctionCall1(abstimein,
                                                        CStringGetDatum(p)));
    /* undo change to \0 */
    *p1 = c;
    p = ++p1;
    /* skip blanks up to '"', beginning of second date */
    while ((c = *p) != '\0')
    {
        if (IsSpace(c))
            p++;
        else if (c != '"')
            goto bogus;         /* syntax error */
        else
            break;
    }
    if (c == '\0')
        goto bogus;             /* syntax error */
    p++;
    /* search for the end of the second date and change it to a \0 */
    p1 = p;
    while ((c = *p1) != '\0')
    {
        if (c == '"')
            break;
        p1++;
    }
    if (c == '\0')
        goto bogus;             /* syntax error */
    *p1 = '\0';
    /* get the second date */
    *i_end = DatumGetAbsoluteTime(DirectFunctionCall1(abstimein,
                                                      CStringGetDatum(p)));
    /* undo change to \0 */
    *p1 = c;
    p = ++p1;
    /* skip blanks up to ']' */
    while ((c = *p) != '\0')
    {
        if (IsSpace(c))
            p++;
        else if (c != ']')
            goto bogus;         /* syntax error */
        else
            break;
    }
    if (c == '\0')
        goto bogus;             /* syntax error */
    p++;
    c = *p;
    if (c != '\0')
        goto bogus;             /* syntax error */

    /* it seems to be a valid tinterval */
    return;

bogus:
    ereport(ERROR,
            (errcode(ERRCODE_INVALID_DATETIME_FORMAT),
             errmsg("invalid input syntax for type tinterval: \"%s\"",
                    i_string)));
    *i_start = *i_end = INVALID_ABSTIME;        /* keep compiler quiet */
}

static void reltime2tm ( RelativeTime  time,
struct pg_tm tm 
) [static]

Definition at line 708 of file nabstime.c.

References FMODULO, SECS_PER_DAY, SECS_PER_HOUR, SECS_PER_MINUTE, pg_tm::tm_hour, pg_tm::tm_mday, pg_tm::tm_min, pg_tm::tm_mon, pg_tm::tm_sec, and pg_tm::tm_year.

Referenced by reltimeout().

{
    double      dtime = time;

    FMODULO(dtime, tm->tm_year, 31557600);
    FMODULO(dtime, tm->tm_mon, 2592000);
    FMODULO(dtime, tm->tm_mday, SECS_PER_DAY);
    FMODULO(dtime, tm->tm_hour, SECS_PER_HOUR);
    FMODULO(dtime, tm->tm_min, SECS_PER_MINUTE);
    FMODULO(dtime, tm->tm_sec, 1);
}

static int reltime_cmp_internal ( RelativeTime  a,
RelativeTime  b 
) [static]

Definition at line 1043 of file nabstime.c.

References INVALID_RELTIME.

Referenced by btreltimecmp(), reltimeeq(), reltimege(), reltimegt(), reltimele(), reltimelt(), and reltimene().

{
    /*
     * We consider all INVALIDs to be equal and larger than any non-INVALID.
     * This is somewhat arbitrary; the important thing is to have a consistent
     * sort order.
     */
    if (a == INVALID_RELTIME)
    {
        if (b == INVALID_RELTIME)
            return 0;           /* INVALID = INVALID */
        else
            return 1;           /* INVALID > non-INVALID */
    }

    if (b == INVALID_RELTIME)
        return -1;              /* non-INVALID < INVALID */

    if (a > b)
        return 1;
    else if (a == b)
        return 0;
    else
        return -1;
}

Datum reltime_interval ( PG_FUNCTION_ARGS   ) 

Definition at line 866 of file nabstime.c.

References Interval::day, DAYS_PER_MONTH, ereport, errcode(), errmsg(), ERROR, INVALID_RELTIME, Interval::month, MONTHS_PER_YEAR, palloc(), PG_GETARG_RELATIVETIME, PG_RETURN_INTERVAL_P, Interval::time, and TMODULO.

{
    RelativeTime reltime = PG_GETARG_RELATIVETIME(0);
    Interval   *result;
    int         year,
                month,
                day;

    result = (Interval *) palloc(sizeof(Interval));

    switch (reltime)
    {
        case INVALID_RELTIME:
            ereport(ERROR,
                    (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                  errmsg("cannot convert reltime \"invalid\" to interval")));
            result->time = 0;
            result->day = 0;
            result->month = 0;
            break;

        default:
#ifdef HAVE_INT64_TIMESTAMP
            year = reltime / SECS_PER_YEAR;
            reltime -= year * SECS_PER_YEAR;
            month = reltime / (DAYS_PER_MONTH * SECS_PER_DAY);
            reltime -= month * (DAYS_PER_MONTH * SECS_PER_DAY);
            day = reltime / SECS_PER_DAY;
            reltime -= day * SECS_PER_DAY;

            result->time = (reltime * USECS_PER_SEC);
#else
            TMODULO(reltime, year, SECS_PER_YEAR);
            TMODULO(reltime, month, DAYS_PER_MONTH * SECS_PER_DAY);
            TMODULO(reltime, day, SECS_PER_DAY);

            result->time = reltime;
#endif
            result->month = MONTHS_PER_YEAR * year + month;
            result->day = day;
            break;
    }

    PG_RETURN_INTERVAL_P(result);
}

Datum reltimeeq ( PG_FUNCTION_ARGS   ) 
Datum reltimege ( PG_FUNCTION_ARGS   ) 
Datum reltimegt ( PG_FUNCTION_ARGS   ) 
Datum reltimein ( PG_FUNCTION_ARGS   ) 

Definition at line 613 of file nabstime.c.

References DateTimeParseError(), DecodeInterval(), DecodeISO8601Interval(), DTERR_BAD_FORMAT, DTERR_FIELD_OVERFLOW, DTK_DELTA, elog, ERROR, INTERVAL_FULL_RANGE, MAXDATEFIELDS, MAXDATELEN, MINS_PER_HOUR, ParseDateTime(), PG_GETARG_CSTRING, PG_RETURN_RELATIVETIME, SECS_PER_MINUTE, SECS_PER_YEAR, tm, pg_tm::tm_hour, pg_tm::tm_mday, pg_tm::tm_min, pg_tm::tm_mon, pg_tm::tm_sec, and pg_tm::tm_year.

{
    char       *str = PG_GETARG_CSTRING(0);
    RelativeTime result;
    struct pg_tm tt,
               *tm = &tt;
    fsec_t      fsec;
    int         dtype;
    int         dterr;
    char       *field[MAXDATEFIELDS];
    int         nf,
                ftype[MAXDATEFIELDS];
    char        workbuf[MAXDATELEN + 1];

    dterr = ParseDateTime(str, workbuf, sizeof(workbuf),
                          field, ftype, MAXDATEFIELDS, &nf);
    if (dterr == 0)
        dterr = DecodeInterval(field, ftype, nf, INTERVAL_FULL_RANGE,
                               &dtype, tm, &fsec);

    /* if those functions think it's a bad format, try ISO8601 style */
    if (dterr == DTERR_BAD_FORMAT)
        dterr = DecodeISO8601Interval(str,
                                      &dtype, tm, &fsec);

    if (dterr != 0)
    {
        if (dterr == DTERR_FIELD_OVERFLOW)
            dterr = DTERR_INTERVAL_OVERFLOW;
        DateTimeParseError(dterr, str, "reltime");
    }

    switch (dtype)
    {
        case DTK_DELTA:
            result = ((tm->tm_hour * MINS_PER_HOUR + tm->tm_min) * SECS_PER_MINUTE) + tm->tm_sec;
            result += tm->tm_year * SECS_PER_YEAR + ((tm->tm_mon * DAYS_PER_MONTH) + tm->tm_mday) * SECS_PER_DAY;
            break;

        default:
            elog(ERROR, "unexpected dtype %d while parsing reltime \"%s\"",
                 dtype, str);
            result = INVALID_RELTIME;
            break;
    }

    PG_RETURN_RELATIVETIME(result);
}

Datum reltimele ( PG_FUNCTION_ARGS   ) 
Datum reltimelt ( PG_FUNCTION_ARGS   ) 
Datum reltimene ( PG_FUNCTION_ARGS   ) 
Datum reltimeout ( PG_FUNCTION_ARGS   ) 

Definition at line 666 of file nabstime.c.

References buf, EncodeInterval(), IntervalStyle, MAXDATELEN, PG_GETARG_RELATIVETIME, PG_RETURN_CSTRING, pstrdup(), reltime2tm(), and tm.

{
    RelativeTime time = PG_GETARG_RELATIVETIME(0);
    char       *result;
    struct pg_tm tt,
               *tm = &tt;
    char        buf[MAXDATELEN + 1];

    reltime2tm(time, tm);
    EncodeInterval(tm, 0, IntervalStyle, buf);

    result = pstrdup(buf);
    PG_RETURN_CSTRING(result);
}

Datum reltimerecv ( PG_FUNCTION_ARGS   ) 
Datum reltimesend ( PG_FUNCTION_ARGS   ) 
Datum timemi ( PG_FUNCTION_ARGS   ) 
Datum timenow ( PG_FUNCTION_ARGS   ) 
Datum timeofday ( PG_FUNCTION_ARGS   ) 

Definition at line 1594 of file nabstime.c.

References buf, cstring_to_text(), gettimeofday(), NULL, pg_localtime(), PG_RETURN_TEXT_P, pg_strftime(), session_timezone, and snprintf().

{
    struct timeval tp;
    char        templ[128];
    char        buf[128];
    pg_time_t   tt;

    gettimeofday(&tp, NULL);
    tt = (pg_time_t) tp.tv_sec;
    pg_strftime(templ, sizeof(templ), "%a %b %d %H:%M:%S.%%06d %Y %Z",
                pg_localtime(&tt, session_timezone));
    snprintf(buf, sizeof(buf), templ, tp.tv_usec);

    PG_RETURN_TEXT_P(cstring_to_text(buf));
}

Datum timepl ( PG_FUNCTION_ARGS   ) 
Datum timestamp_abstime ( PG_FUNCTION_ARGS   ) 

Definition at line 459 of file nabstime.c.

References DetermineTimeZoneOffset(), ereport, errcode(), errmsg(), ERROR, NULL, PG_GETARG_TIMESTAMP, PG_RETURN_ABSOLUTETIME, session_timezone, timestamp2tm(), TIMESTAMP_IS_NOBEGIN, TIMESTAMP_IS_NOEND, tm, and tm2abstime().

{
    Timestamp   timestamp = PG_GETARG_TIMESTAMP(0);
    AbsoluteTime result;
    fsec_t      fsec;
    int         tz;
    struct pg_tm tt,
               *tm = &tt;

    if (TIMESTAMP_IS_NOBEGIN(timestamp))
        result = NOSTART_ABSTIME;
    else if (TIMESTAMP_IS_NOEND(timestamp))
        result = NOEND_ABSTIME;
    else if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) == 0)
    {
        tz = DetermineTimeZoneOffset(tm, session_timezone);
        result = tm2abstime(tm, tz);
    }
    else
    {
        ereport(ERROR,
                (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
                 errmsg("timestamp out of range")));
        result = INVALID_ABSTIME;
    }

    PG_RETURN_ABSOLUTETIME(result);
}

Datum timestamptz_abstime ( PG_FUNCTION_ARGS   ) 

Definition at line 536 of file nabstime.c.

References ereport, errcode(), errmsg(), ERROR, NULL, PG_GETARG_TIMESTAMP, PG_RETURN_ABSOLUTETIME, timestamp2tm(), TIMESTAMP_IS_NOBEGIN, TIMESTAMP_IS_NOEND, tm, and tm2abstime().

{
    TimestampTz timestamp = PG_GETARG_TIMESTAMP(0);
    AbsoluteTime result;
    fsec_t      fsec;
    struct pg_tm tt,
               *tm = &tt;

    if (TIMESTAMP_IS_NOBEGIN(timestamp))
        result = NOSTART_ABSTIME;
    else if (TIMESTAMP_IS_NOEND(timestamp))
        result = NOEND_ABSTIME;
    else if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) == 0)
        result = tm2abstime(tm, 0);
    else
    {
        ereport(ERROR,
                (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
                 errmsg("timestamp out of range")));
        result = INVALID_ABSTIME;
    }

    PG_RETURN_ABSOLUTETIME(result);
}

static int tinterval_cmp_internal ( TimeInterval  a,
TimeInterval  b 
) [static]

Definition at line 1177 of file nabstime.c.

References TimeIntervalData::data, INVALID_ABSTIME, TimeIntervalData::status, and T_INTERVAL_INVAL.

Referenced by bttintervalcmp(), tintervaleq(), tintervalge(), tintervalgt(), tintervalle(), tintervallt(), and tintervalne().

{
    bool        a_invalid;
    bool        b_invalid;
    AbsoluteTime a_len;
    AbsoluteTime b_len;

    /*
     * We consider all INVALIDs to be equal and larger than any non-INVALID.
     * This is somewhat arbitrary; the important thing is to have a consistent
     * sort order.
     */
    a_invalid = a->status == T_INTERVAL_INVAL ||
        a->data[0] == INVALID_ABSTIME ||
        a->data[1] == INVALID_ABSTIME;
    b_invalid = b->status == T_INTERVAL_INVAL ||
        b->data[0] == INVALID_ABSTIME ||
        b->data[1] == INVALID_ABSTIME;

    if (a_invalid)
    {
        if (b_invalid)
            return 0;           /* INVALID = INVALID */
        else
            return 1;           /* INVALID > non-INVALID */
    }

    if (b_invalid)
        return -1;              /* non-INVALID < INVALID */

    a_len = a->data[1] - a->data[0];
    b_len = b->data[1] - b->data[0];

    if (a_len > b_len)
        return 1;
    else if (a_len == b_len)
        return 0;
    else
        return -1;
}

Datum tintervalct ( PG_FUNCTION_ARGS   ) 
Datum tintervalend ( PG_FUNCTION_ARGS   ) 
Datum tintervaleq ( PG_FUNCTION_ARGS   ) 
Datum tintervalge ( PG_FUNCTION_ARGS   ) 
Datum tintervalgt ( PG_FUNCTION_ARGS   ) 
Datum tintervalin ( PG_FUNCTION_ARGS   ) 

Definition at line 725 of file nabstime.c.

References ABSTIMEMAX, ABSTIMEMIN, TimeIntervalData::data, INVALID_ABSTIME, palloc(), parsetinterval(), PG_GETARG_CSTRING, PG_RETURN_TIMEINTERVAL, and TimeIntervalData::status.

{
    char       *tintervalstr = PG_GETARG_CSTRING(0);
    TimeInterval tinterval;
    AbsoluteTime i_start,
                i_end,
                t1,
                t2;

    parsetinterval(tintervalstr, &t1, &t2);

    tinterval = (TimeInterval) palloc(sizeof(TimeIntervalData));

    if (t1 == INVALID_ABSTIME || t2 == INVALID_ABSTIME)
        tinterval->status = T_INTERVAL_INVAL;   /* undefined  */
    else
        tinterval->status = T_INTERVAL_VALID;

    i_start = ABSTIMEMIN(t1, t2);
    i_end = ABSTIMEMAX(t1, t2);
    tinterval->data[0] = i_start;
    tinterval->data[1] = i_end;

    PG_RETURN_TIMEINTERVAL(tinterval);
}

Datum tintervalle ( PG_FUNCTION_ARGS   ) 
Datum tintervalleneq ( PG_FUNCTION_ARGS   ) 
Datum tintervallenge ( PG_FUNCTION_ARGS   ) 
Datum tintervallengt ( PG_FUNCTION_ARGS   ) 
Datum tintervallenle ( PG_FUNCTION_ARGS   ) 
Datum tintervallenlt ( PG_FUNCTION_ARGS   ) 
Datum tintervallenne ( PG_FUNCTION_ARGS   ) 
Datum tintervallt ( PG_FUNCTION_ARGS   ) 
Datum tintervalne ( PG_FUNCTION_ARGS   ) 
Datum tintervalout ( PG_FUNCTION_ARGS   ) 

Definition at line 756 of file nabstime.c.

References AbsoluteTimeGetDatum, abstimeout(), TimeIntervalData::data, DatumGetCString, DirectFunctionCall1, INVALID_INTERVAL_STR, palloc(), pfree(), PG_GETARG_TIMEINTERVAL, PG_RETURN_CSTRING, TimeIntervalData::status, T_INTERVAL_INVAL, and T_INTERVAL_LEN.

{
    TimeInterval tinterval = PG_GETARG_TIMEINTERVAL(0);
    char       *i_str,
               *p;

    i_str = (char *) palloc(T_INTERVAL_LEN);    /* ["..." "..."] */
    strcpy(i_str, "[\"");
    if (tinterval->status == T_INTERVAL_INVAL)
        strcat(i_str, INVALID_INTERVAL_STR);
    else
    {
        p = DatumGetCString(DirectFunctionCall1(abstimeout,
                                  AbsoluteTimeGetDatum(tinterval->data[0])));
        strcat(i_str, p);
        pfree(p);
        strcat(i_str, "\" \"");
        p = DatumGetCString(DirectFunctionCall1(abstimeout,
                                  AbsoluteTimeGetDatum(tinterval->data[1])));
        strcat(i_str, p);
        pfree(p);
    }
    strcat(i_str, "\"]");
    PG_RETURN_CSTRING(i_str);
}

Datum tintervalov ( PG_FUNCTION_ARGS   ) 
Datum tintervalrecv ( PG_FUNCTION_ARGS   ) 

Definition at line 786 of file nabstime.c.

References buf, TimeIntervalData::data, ereport, errcode(), errmsg(), ERROR, INVALID_ABSTIME, palloc(), PG_GETARG_POINTER, PG_RETURN_TIMEINTERVAL, pq_getmsgint(), and TimeIntervalData::status.

{
    StringInfo  buf = (StringInfo) PG_GETARG_POINTER(0);
    TimeInterval tinterval;
    int32       status;

    tinterval = (TimeInterval) palloc(sizeof(TimeIntervalData));

    tinterval->status = pq_getmsgint(buf, sizeof(tinterval->status));
    tinterval->data[0] = pq_getmsgint(buf, sizeof(tinterval->data[0]));
    tinterval->data[1] = pq_getmsgint(buf, sizeof(tinterval->data[1]));

    if (tinterval->data[0] == INVALID_ABSTIME ||
        tinterval->data[1] == INVALID_ABSTIME)
        status = T_INTERVAL_INVAL;      /* undefined  */
    else
        status = T_INTERVAL_VALID;

    if (status != tinterval->status)
        ereport(ERROR,
                (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
                 errmsg("invalid status in external \"tinterval\" value")));

    PG_RETURN_TIMEINTERVAL(tinterval);
}

Datum tintervalrel ( PG_FUNCTION_ARGS   ) 
Datum tintervalsame ( PG_FUNCTION_ARGS   ) 
Datum tintervalsend ( PG_FUNCTION_ARGS   ) 

Definition at line 816 of file nabstime.c.

References buf, TimeIntervalData::data, PG_GETARG_TIMEINTERVAL, PG_RETURN_BYTEA_P, pq_begintypsend(), pq_endtypsend(), pq_sendint(), and TimeIntervalData::status.

{
    TimeInterval tinterval = PG_GETARG_TIMEINTERVAL(0);
    StringInfoData buf;

    pq_begintypsend(&buf);
    pq_sendint(&buf, tinterval->status, sizeof(tinterval->status));
    pq_sendint(&buf, tinterval->data[0], sizeof(tinterval->data[0]));
    pq_sendint(&buf, tinterval->data[1], sizeof(tinterval->data[1]));
    PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
}

Datum tintervalstart ( PG_FUNCTION_ARGS   ) 
static AbsoluteTime tm2abstime ( struct pg_tm tm,
int  tz 
) [static]

Definition at line 176 of file nabstime.c.

References AbsoluteTimeIsReal, date2j(), HOURS_PER_DAY, INVALID_ABSTIME, MAX_DAYNUM, MINS_PER_HOUR, MONTHS_PER_YEAR, SECS_PER_MINUTE, pg_tm::tm_hour, pg_tm::tm_mday, pg_tm::tm_min, pg_tm::tm_mon, pg_tm::tm_sec, and pg_tm::tm_year.

Referenced by abstimein(), timestamp_abstime(), and timestamptz_abstime().

{
    int         day;
    AbsoluteTime sec;

    /* validate, before going out of range on some members */
    if (tm->tm_year < 1901 || tm->tm_year > 2038 ||
        tm->tm_mon < 1 || tm->tm_mon > MONTHS_PER_YEAR ||
        tm->tm_mday < 1 || tm->tm_mday > 31 ||
        tm->tm_hour < 0 ||
        tm->tm_hour > HOURS_PER_DAY ||  /* test for > 24:00:00 */
      (tm->tm_hour == HOURS_PER_DAY && (tm->tm_min > 0 || tm->tm_sec > 0)) ||
        tm->tm_min < 0 || tm->tm_min > MINS_PER_HOUR - 1 ||
        tm->tm_sec < 0 || tm->tm_sec > SECS_PER_MINUTE)
        return INVALID_ABSTIME;

    day = date2j(tm->tm_year, tm->tm_mon, tm->tm_mday) - UNIX_EPOCH_JDATE;

    /* check for time out of range */
    if (day < MIN_DAYNUM || day > MAX_DAYNUM)
        return INVALID_ABSTIME;

    /* convert to seconds */
    sec = tm->tm_sec + tz + (tm->tm_min + (day * HOURS_PER_DAY + tm->tm_hour) * MINS_PER_HOUR) * SECS_PER_MINUTE;

    /*
     * check for overflow.  We need a little slop here because the H/M/S plus
     * TZ offset could add up to more than 1 day.
     */
    if ((day >= MAX_DAYNUM - 10 && sec < 0) ||
        (day <= MIN_DAYNUM + 10 && sec > 0))
        return INVALID_ABSTIME;

    /* check for reserved values (e.g. "current" on edge of usual range */
    if (!AbsoluteTimeIsReal(sec))
        return INVALID_ABSTIME;

    return sec;
}