From 186ed34ee54eb43a36d433078dc6ef8a768e9bb7 Mon Sep 17 00:00:00 2001 From: Guido Fioravantti Date: Mon, 5 Oct 2015 13:32:03 +0200 Subject: [PATCH] Overloads CDB_DateToNumber() and adds test --- scripts-available/CDB_DateToNumber.sql | 16 ++++++++++++++++ test/CDB_DateToNumberTest.sql | 2 ++ test/CDB_DateToNumberTest_expect | 2 ++ 3 files changed, 20 insertions(+) create mode 100644 test/CDB_DateToNumberTest.sql create mode 100644 test/CDB_DateToNumberTest_expect diff --git a/scripts-available/CDB_DateToNumber.sql b/scripts-available/CDB_DateToNumber.sql index 84c3395..651d429 100644 --- a/scripts-available/CDB_DateToNumber.sql +++ b/scripts-available/CDB_DateToNumber.sql @@ -1,5 +1,21 @@ -- Convert timestamp to double precision -- +CREATE OR REPLACE FUNCTION CDB_DateToNumber(input timestamp) +RETURNS double precision AS $$ +DECLARE output double precision; +BEGIN + BEGIN + SELECT extract (EPOCH FROM input) INTO output; + EXCEPTION WHEN OTHERS THEN + RETURN NULL; + END; +RETURN output; +END; +$$ +LANGUAGE 'plpgsql' STABLE STRICT; + +-- Convert timestamp with time zone to double precision +-- CREATE OR REPLACE FUNCTION CDB_DateToNumber(input timestamp with time zone) RETURNS double precision AS $$ DECLARE output double precision; diff --git a/test/CDB_DateToNumberTest.sql b/test/CDB_DateToNumberTest.sql new file mode 100644 index 0000000..3821bd1 --- /dev/null +++ b/test/CDB_DateToNumberTest.sql @@ -0,0 +1,2 @@ +SELECT * FROM CDB_DateToNumber('1999-01-08 00:00:00+00'::timestamp); +SELECT * FROM CDB_DateToNumber('1999-01-08 00:00:00+00'::timestamp with time zone); diff --git a/test/CDB_DateToNumberTest_expect b/test/CDB_DateToNumberTest_expect new file mode 100644 index 0000000..8445360 --- /dev/null +++ b/test/CDB_DateToNumberTest_expect @@ -0,0 +1,2 @@ +915753600 +915753600