日付を 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 Application Server プロセスから呼び出されたときに、ローカル タイム ゾーンの省略形を取得できないのはなぜですか?