-
Notifications
You must be signed in to change notification settings - Fork 73
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
base: master
Are you sure you want to change the base?
Epoch functions #135
Conversation
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'); |
There was a problem hiding this comment.
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'); |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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( |
There was a problem hiding this comment.
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( |
There was a problem hiding this comment.
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( |
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing in
.
Thank you ! This contribution is welcome, but unfortunately @martindsouza has not have time for the project recently. Please be patient ! |
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.