Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7458007
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T13:23:39+00:00 2026-05-29T13:23:39+00:00

I have written a function to convert a date into a Unix time stamp.

  • 0

I have written a function to convert a date into a Unix time stamp. The function is written to work no matter what the current DST status is (e.g. EST or EDT). This is the function:

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;

This function works great when I execute it from a client like JDeveloper. From what I gather, this is because the client is supplying time zone information to the first query. However, if I use the function from within a procedure that is called from a mod_plsql page, then I get the error ORA-01857: not a valid time zone. This error is being thrown from the new_time function because tz is set to 'UNK'.

So, I implemented a work-around for this problem like so:

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;

Except, this still fails with tz being set to 'UNK'. Does anyone know what could be happening here? Why can’t I get the local time zone abbreviation when the function is called from a Oracle Application Server process?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T13:23:40+00:00Added an answer on May 29, 2026 at 1:23 pm

    The function as written does not work when the session calling it does not have the time zone information set. Therefore, you need to explicitly specify the source time zone. The following function solves this problem (and corrects the return type):

    function unix_time_from_date
        (
          in_date   in date,
          in_src_tz in varchar2 default 'America/New_York'
        )
      return integer
    as
      ut      integer       := 0;
      tz      varchar2(8)   := '';
      tz_date timestamp with time zone;
      tz_stmt varchar2(255);
    begin
      -- Get the local time zone abbreviation from the passed in date
      tz_stmt := 'select systimestamp at time zone ''' || in_src_tz || ''' from dual';
      execute immediate tz_stmt into tz_date;
      select
        extract(timezone_abbr from tz_date)
      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;
    

    Note the addition of a second parameter to the function. This parameter, in_src_tz, is used to indicate what time zone the in_date parameter is in. The value of in_src_tz should be one of the timezone listed in the tzname column of the v$timezone_names table.

    Also, you cannot simply select the value of the tzabbrev column in the v$timezone_names table due to time zone having multiple abbreviations. By using the extract, you will get the current abbreviation with DST factored in.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have written a code which converts date and time into timezone , now
I have written a function to convert string to integer if ( data !=
I have written some general functions to convert between decimal and any other base-n
I have written a function that gets a given number of random records from
I have written a function that positions a tooltip just above a textbox. The
I have written a function below: void trans(double x,double y,double theta,double m,double n) {
I have written a function for a multilevel wordpress menu, but I'd like it
I have written a function to print database table to an array like this
I have written a function: function selectWithPaging($where){ $pg = (int) (!isset($_GET[pg]) ? 1 :
I have written a javascript function that uses setInterval to manipulate a string every

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.