Myk*_*ych 13 database oracle stored-procedures
你能提供存储函数的实现来获得当前systimestamp的毫秒数.
我能用的东西就像
select current_time_ms from dual;
Run Code Online (Sandbox Code Playgroud)
并获得当前时间与UTC时间1970年1月1日午夜之间的差异(以毫秒为单位).
谢谢.
Myk*_*ych 18
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;
我所知道的最好的事情是:
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;
Run Code Online (Sandbox Code Playgroud)
我不太确定你对时区有什么要求.您可能需要对此进行微调.
添加到@Mykhaylo Adamovych 的答案(看起来是正确的!)这里是使用 oracle Java 支持(即不在 XE 中也不在 AWS RDS 中)的更直接的方法。便携性较差(以防万一),但在我的测试中似乎更快。
CREATE or replace FUNCTION current_java_timestamp RETURN number
AS LANGUAGE JAVA NAME 'java.lang.System.currentTimeMillis() return java.lang.Long';
/
Run Code Online (Sandbox Code Playgroud)
小智 5
SELECT to_char(sysdate, 'HH24:MI:SS'), to_char(systimestamp, 'HH24:MI:SS.FF6') FROM dual
Run Code Online (Sandbox Code Playgroud)