Search notes:

Oracle: PL/SQL package TIM for DATE and INTERVAL related functionality

Functions

rfc_3339_sec modelled after the --rfc-3339=sec option of the date shell command.
iso_8601 Initial implementation of an attempt to create ISO 8601 formatted strings for dates
s_ago Returns the difference, in seconds, between now (sysdate or systimestamp) and the parameter (whose datatype is date or timestamp)
to_s Converts an interval day to second to a number that corresponds to the amount of seconds of that interval.

Source code

Specification

create or replace package tim as
 --
 -- Version 0.4
 --

    function s_ago(dt  date                  ) return number;
    function s_ago(ts  timestamp             ) return number;
    function to_s (int interval day to second) return number;

    function iso_8601(dt date                    ) return varchar2;
    function iso_8601(ts timestamp               ) return varchar2;
    function iso_8601(ts timestamp with time zone) return varchar2;

    function rfc_3339_sec(dt date) return varchar2;

end tim;
/

show errors
Github repository PL-SQL-pkg-tim, path: /spec.sql

Body

create or replace package body tim as
 --
 -- Version 0.4
 --

    function s_ago(dt date) return number is -- {
    begin
       return round((sysdate - dt) * 60*60*24);
    end s_ago; -- }

    function s_ago(ts timestamp) return number is -- {
    begin
       return round(to_s(systimestamp - ts));
    end s_ago; -- }

    function to_s(int interval day to second) return number is -- {
    begin
       return
             extract( second from int ) +
             extract( minute from int ) * 60 +
             extract( hour   from int ) * 60 * 60 +
             extract( day    from int ) * 60 * 60 * 24;

    end to_s; -- }

    function iso_8601(dt date) return varchar2 is -- {
    begin
        return to_char(dt,'YYYY-MM-DD"T"HH24:MI:SS"Z"');
    end iso_8601; -- }

    function iso_8601(ts timestamp) return varchar2 is -- {
    begin
        return to_char(ts,'YYYY-MM-DD"T"HH24:MI:SS.FF3"Z"');

    end iso_8601; -- }

    function iso_8601(ts timestamp with time zone) return varchar2 is -- {
    begin
        return to_char(ts,'YYYY-MM-DD"T"HH24:MI:SS.FF3TZR');
    end iso_8601; -- }

    function rfc_3339_sec(dt date) return varchar2 is
    begin
        return to_char(dt,'YYYY-MM-DD HH24:MI:SS');
    end rfc_3339_sec; -- }

end tim;
/

show errors
Github repository PL-SQL-pkg-tim, path: /body.sql

Test

History

0.1 Initial release functions to_s and s_ago
0.2 Initial attempt for function iso_8601 (2021-06-26)
0.3 Add rfc_3999_sec (2021-07-03)
0.4 It's RFC 3339, not 3999!

See also

Other simple PL/SQL packages I've written over time.

Index