From 204a47a5eef2694c4e8105ad68e667446bbf36a0 Mon Sep 17 00:00:00 2001 From: Sebastian Harl Date: Thu, 8 Nov 2012 07:57:57 +0100 Subject: [PATCH] RRTimeslice: Added cast from timestamptz to rrtimeslice. --- src/postrr.h.in | 2 ++ src/postrr.sql.in | 9 +++++++++ src/rrtimeslice.c | 29 +++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+) diff --git a/src/postrr.h.in b/src/postrr.h.in index d9df4af..bf93551 100644 --- a/src/postrr.h.in +++ b/src/postrr.h.in @@ -79,6 +79,8 @@ rrtimeslice_typmodout(PG_FUNCTION_ARGS); Datum rrtimeslice_to_rrtimeslice(PG_FUNCTION_ARGS); Datum +timestamptz_to_rrtimeslice(PG_FUNCTION_ARGS); +Datum rrtimeslice_to_timestamptz(PG_FUNCTION_ARGS); /* comparison operators */ diff --git a/src/postrr.sql.in b/src/postrr.sql.in index 9f34f22..eb19745 100644 --- a/src/postrr.sql.in +++ b/src/postrr.sql.in @@ -108,6 +108,15 @@ CREATE CAST (rrtimeslice AS rrtimeslice) WITH FUNCTION RRTimeslice(rrtimeslice, integer, boolean) AS IMPLICIT; +CREATE OR REPLACE FUNCTION RRTimeslice(timestamptz, integer, boolean) + RETURNS rrtimeslice + AS 'postrr-@POSTRR_MAJOR_VERSION@.@POSTRR_MINOR_VERSION@', 'timestamptz_to_rrtimeslice' + LANGUAGE 'C' IMMUTABLE STRICT; + +CREATE CAST (timestamptz AS rrtimeslice) + WITH FUNCTION RRTimeslice(timestamptz, integer, boolean) + AS IMPLICIT; + CREATE OR REPLACE FUNCTION Tstamptz(rrtimeslice) RETURNS timestamptz AS 'postrr-@POSTRR_MAJOR_VERSION@.@POSTRR_MINOR_VERSION@', 'rrtimeslice_to_timestamptz' diff --git a/src/rrtimeslice.c b/src/rrtimeslice.c index 7de9ed7..1e8283c 100644 --- a/src/rrtimeslice.c +++ b/src/rrtimeslice.c @@ -244,6 +244,7 @@ PG_FUNCTION_INFO_V1(rrtimeslice_typmodin); PG_FUNCTION_INFO_V1(rrtimeslice_typmodout); PG_FUNCTION_INFO_V1(rrtimeslice_to_rrtimeslice); +PG_FUNCTION_INFO_V1(timestamptz_to_rrtimeslice); PG_FUNCTION_INFO_V1(rrtimeslice_to_timestamptz); PG_FUNCTION_INFO_V1(rrtimeslice_cmp); @@ -511,6 +512,34 @@ rrtimeslice_to_rrtimeslice(PG_FUNCTION_ARGS) PG_RETURN_RRTIMESLICE_P(tslice); } /* rrtimeslice_to_rrtimeslice */ +Datum +timestamptz_to_rrtimeslice(PG_FUNCTION_ARGS) +{ + TimestampTz tstamp; + int32 typmod; + + rrtimeslice_t *tslice; + + if (PG_NARGS() != 3) + ereport(ERROR, ( + errmsg("timestamptz_to_rrtimeslice() " + "expects three arguments"), + errhint("Usage: timestamptz_to_rrtimeslice" + "(timestamptz, typmod, is_explicit)") + )); + + tstamp = PG_GETARG_TIMESTAMPTZ(0); + typmod = PG_GETARG_INT32(1); + + tslice = (rrtimeslice_t *)palloc0(sizeof(*tslice)); + + tslice->tstamp = tstamp; + if (typmod >= 0) + rrtimeslice_apply_typmod(tslice, typmod); + + PG_RETURN_RRTIMESLICE_P(tslice); +} /* timestamptz_to_rrtimeslice */ + Datum rrtimeslice_to_timestamptz(PG_FUNCTION_ARGS) { -- 2.30.2