开发者

oracle systimestamp (sysdate) to milliseconds

开发者 https://www.devze.com 2023-03-30 08:06 出处:网络
Could you provide impl开发者_如何学Pythonementation of stored function to get current systimestamp as milliseconds.

Could you provide impl开发者_如何学Pythonementation of stored function to get current systimestamp as milliseconds.

Something I can use like

select current_time_ms from dual;

and get the difference, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC.

Thanks.


  • DB timezone agnostic
  • with milliseconds
  • works in XE
    function current_time_ms
        return number
    is
        out_result number;
    begin
        select extract(day from(sys_extract_utc(systimestamp) - to_timestamp('1970-01-01', 'YYYY-MM-DD'))) * 86400000 
            + to_number(to_char(sys_extract_utc(systimestamp), 'SSSSSFF3'))
        into out_result
        from dual;
        return out_result;
    end current_time_ms;


The best thing I know of is:

select extract(day    from (systimestamp - timestamp '1970-01-01 00:00:00')) * 86400000
     + extract(hour   from (systimestamp - timestamp '1970-01-01 00:00:00')) * 3600000
     + extract(minute from (systimestamp - timestamp '1970-01-01 00:00:00')) * 60000
     + extract(second from (systimestamp - timestamp '1970-01-01 00:00:00')) * 1000 unix_time
from dual;

I'm not quite sure what requirements you have regarding time zone. You might need to make minor adjustments for that.


Adding to @Mykhaylo Adamovych answer (which looks correct!) here goes a more straightforward approach using oracle Java support (i.e. not in XE and not in AWS RDS). Less portable (in case you care), but seemed faster in my testing.

CREATE or replace FUNCTION current_java_timestamp RETURN number
AS LANGUAGE JAVA NAME 'java.lang.System.currentTimeMillis() return java.lang.Long';
/


SELECT to_char(sysdate, 'HH24:MI:SS'), to_char(systimestamp, 'HH24:MI:SS.FF6') FROM dual


AFAIK, there is no direct way for achieving this (other than manually writing a long-winded SQL function).

Why do you need this specifically?

You could use a stored Java function and then use the System.getCurrentMillis() that Java provides to return you a value in Milliseconds from 1.1.1970 to now.


Below code gives the difference in milliseconds:

with t as (select systimestamp - to_timestamp(sysdate ) diff from dual)
select extract(day from diff) * 24 * 3600000+
   extract(hour from diff) * 3600000+
   extract(minute from diff) * 60000 +
   extract(second from diff) * 1000  
    dif
from t

For conversion of milliseconds to Hours, Minutes, seconds, modify and use below query as appropriate:

with t as (select systimestamp - to_timestamp(sysdate ) diff from dual)
select extract(day from diff) * 24 * 3600000+
   extract(hour from diff) * 3600000+
   extract(minute from diff) * 60000 +
   extract(second from diff) * 1000  
    dif,
    (to_char (to_date(round(( extract(day from diff) * 24 * 3600000+
   extract(hour from diff) * 3600000+
   extract(minute from diff) * 60000 +
   extract(second from diff) * 1000)/1000), 'SSSSS' ), 'HH24"Hrs" MI"Min" SS"Sec"'))  timeval
from t


I've posted here some methods to convert timestamp to nanoseconds and nanoseconds to timestamp. These methods are not affected by time zones and have a nanosecond precision.

You just need to adjust it to get milliseconds instead of nanoseconds.

SELECT (EXTRACT(DAY FROM (
    SYSTIMESTAMP --Replace line with desired timestamp --Maximum value: TIMESTAMP '3871-04-29 10:39:59.999999999 UTC'
- TIMESTAMP '1970-01-01 00:00:00 UTC') * 24 * 60) * 60 + EXTRACT(SECOND FROM
    SYSTIMESTAMP --Replace line with desired timestamp
)) *  1000 AS MILLIS FROM DUAL;

MILLIS
1598434427263.027
0

精彩评论

暂无评论...
验证码 换一张
取 消