-
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
Open
dmcghan
wants to merge
2
commits into
OraOpenSource:master
Choose a base branch
from
dmcghan:epoch-functions
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Epoch functions #135
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,17 +25,23 @@ as | |
* @return Unix Epoch time | ||
*/ | ||
function date2epoch( | ||
p_date in date) | ||
p_date timestamp, | ||
p_date_in_tzr in varchar2 default sessiontimezone) | ||
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'); | ||
begin | ||
return | ||
round( | ||
(p_date - to_date ('19700101', 'yyyymmdd')) * 86400 | ||
- (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 commentThe reason will be displayed to describe this comment to others. Learn more. Define constants for the magic numbers. |
||
+ (extract (hour from l_tswtz - l_start) * 3600000) | ||
+ (extract (minute from l_tswtz - l_start) * 60000) | ||
+ (extract (second from l_tswtz - l_start) * 1000)); | ||
end date2epoch; | ||
|
||
|
||
|
@@ -59,48 +65,15 @@ as | |
* @return date | ||
*/ | ||
function epoch2date( | ||
p_epoch in number) | ||
return date | ||
p_epoch in number, | ||
p_date_out_tzr in varchar2 default sessiontimezone) | ||
return timestamp with time zone | ||
as | ||
|
||
l_tswtz timestamp with time zone; | ||
begin | ||
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 commentThe reason will be displayed to describe this comment to others. Learn more. Use the constant:
And define a constant for magic number 86400000. |
||
return l_tswtz at time zone (p_date_out_tzr); | ||
end epoch2date; | ||
|
||
|
||
/*! | ||
* Coverts timestamp to Unix Epoch time | ||
* | ||
* @private Currently used for crypto. Needs more testing to make puplically available. | ||
* | ||
* @example | ||
* | ||
* select oos_util_date.timestamp2epoch(current_timestamp) | ||
* from dual; | ||
* | ||
* OOS_UTIL_DATE.TIMESTAMP2EPOCH(CURRENT_TIMESTAMP) | ||
* --------------------------------- | ||
* 1474277938 | ||
* | ||
* @author Adrian Png | ||
* @created 22-Sep-2016 | ||
* @param p_timestamp Timestamp to convert to Epoch format | ||
* @return Unix Epoch time | ||
*/ | ||
function timestamp2epoch( | ||
p_timestamp in timestamp) | ||
return pls_integer | ||
as | ||
c_start_time constant timestamp with time zone := timestamp '1970-01-01 00:00:00 +00:00'; | ||
begin | ||
return extract(day from (p_timestamp - c_start_time)) * 86400 | ||
+ extract(hour from (p_timestamp - c_start_time)) * 3600 | ||
+ extract(minute from (p_timestamp - c_start_time)) * 60 | ||
+ extract(second from (p_timestamp - c_start_time)) | ||
; | ||
end timestamp2epoch; | ||
|
||
|
||
end oos_util_date; | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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:
instead of
l_start
.