我编写了一个函数来将日期转换为 Unix 时间戳。无论当前 DST 状态是什么(例如 EST 或 EDT),该函数都可以正常工作。这是功能:
function unix_time_from_date(in_date in date) return number
as
ut number := 0;
tz varchar2(8) := '';
begin
-- Get the local timezone from the passed in date
-- Assuming the date supplied is for the local time zone
select
extract(
timezone_abbr from cast(in_date as timestamp with local time zone)
)
into tz
from dual;
-- Get the Unix timestamp
select
(new_time(in_date, tz, 'GMT') - to_date('01-JAN-1970', 'DD-MM-YYYY')) * (
86400)
into ut
from dual;
return ut;
end unix_time_from_date;
当我从 JDeveloper 之类的客户端执行此功能时,它的效果很好。据我所知,这是因为客户端正在向第一个查询提供时区信息。但是,如果我在从 mod_plsql 页面调用的过程中使用该函数,则会收到错误消息ORA-01857: not a valid time zone
。这个错误是从new_time
函数中抛出的,因为tz
设置为'UNK'
.
所以,我为这个问题实现了一个解决方法,如下所示:
function unix_time_from_date(in_date in date) return number
as
ut number := 0;
tz varchar2(8) := '';
begin
-- Get the local timezone from the passed in date
-- Assuming the date supplied is for the local time zone
select
extract(
timezone_abbr from cast(in_date as timestamp with local time zone)
)
into tz
from dual;
if tz = 'UNK' then
select
extract(
timezone_abbr from cast(sysdate as timestamp with local time zone)
)
into tz
from dual;
end if;
-- Get the Unix timestamp
select
(new_time(in_date, tz, 'GMT') - to_date('01-JAN-1970', 'DD-MM-YYYY')) * (
86400)
into ut
from dual;
return ut;
end unix_time_from_date;
除了,这仍然失败并tz
设置为'UNK'
. 有谁知道这里会发生什么?从 Oracle 应用服务器进程调用函数时,为什么我无法获得本地时区缩写?