Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Epoch functions #135

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Epoch functions #135

wants to merge 2 commits into from

Conversation

dmcghan
Copy link

@dmcghan dmcghan commented Nov 3, 2016

Alternative implementation. Removed timestamp2epoch in favor of implicit data conversion. Here, date2epoch uses the timestamp data type so if a date is passed it doesn't lose precision but any timestamp data type can be passed without losing milliseconds.

Also, both functions now have an additional tzr parameter which allows for additional flexibility when needed. In the case of date2epoch, the parameter can be used to specify which timezone the date or timestamp being passed in is from. For epoch2date, the tzr parameter allows the user to get a date back in a specific timezone.

The tzr parameter defaults to the sessiontimezone for convenience.

Alternative implementation. Removed timestamp2epoch in favor of implicit data conversion. Here, date2epoch uses the timestamp data type so if a date is passed it doesn't lose precision but any timestamp data type can be passed without losing milliseconds.

Also, both functions now have an additional tzr parameter which allows for additional flexibility when needed. In the case of date2epoch, the parameter can be used to specify which timezone the date or timestamp being passed in is from. For epoch2date, the tzr parameter allows the user to get a date back in a specific timezone.

The tzr parameter defaults to the sessiontimezone for convenience.
Updated implementation of epoch functions.
return number
as
$if dbms_db_version.version >= 12 $then
pragma udf;
$end

l_tswtz timestamp with time zone;
l_start timestamp with time zone := to_timestamp_tz('19700101 utc', 'yyyymmdd tzr');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use a package level constant:

epoch constant timestamp with time zone := timestamp '1970-01-01 00:00:00 +00:00';

instead of l_start.

return
to_date ('19700101', 'yyyymmdd')
+ ((p_epoch + ((to_number(substr(tz_offset(sessiontimezone), 1, 3))+0) * 3600)) / 86400); -- Note: Was +1 but was causing 1 hour ahead (#123)
l_tswtz := to_timestamp_tz('19700101 utc', 'yyyymmdd tzr') + numtodsinterval(p_epoch/86400000, 'day');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the constant:

l_tswtz := epoch + numtodsinterval(p_epoch/86400000, 'day');

And define a constant for magic number 86400000.

- (to_number(substr (tz_offset (sessiontimezone), 1, 3))+0) * 3600); -- Note: Was +1 but was causing 1 hour behind (#123)
l_tswtz := from_tz(p_date, p_date_in_tzr) at time zone 'utc';

return round((extract (day from l_tswtz - l_start) * 86400000)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Define constants for the magic numbers.

@@ -1,17 +1,14 @@
create or replace package oos_util_date
as

function date2epoch(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make this a deterministic function.

function date2epoch(
p_date in date)
p_date timestamp,
p_date_in_tzr in varchar2 default sessiontimezone)
return number;

function epoch2date(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make this a deterministic function.

@@ -1,17 +1,14 @@
create or replace package oos_util_date
as

function date2epoch(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we also have a p_date in timestamp with time zone version ?

function date2epoch(
p_date in date)
p_date timestamp,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing in.

@janihur
Copy link
Contributor

janihur commented Jan 5, 2017

Thank you ! This contribution is welcome, but unfortunately @martindsouza has not have time for the project recently. Please be patient !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants