Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Determine Daylight Saving or Standard Time for Unix Timestamp

Status
Not open for further replies.

kvang

Programmer
Oct 7, 2005
129
US
If given a unix timestamp, how do I determine which timezone to use (EST or EDT)? I'm trying to edit this function to automatically return the right time zone with the value passed.

Code:
CREATE OR REPLACE FUNCTION Unixts_To_Date(unixts IN PLS_INTEGER) RETURN DATE IS
        /**
         * Converts a UNIX timestamp into an Oracle DATE 
         */
        unix_epoch DATE := TO_DATE('19700101000000','YYYYMMDDHH24MISS');
        max_ts PLS_INTEGER := 2145916799; -- 2938-12-31 23:59:59
        min_ts PLS_INTEGER := -2114380800; -- 1903-01-01 00:00:00
        oracle_date DATE;
        
        BEGIN
            
            IF unixts> max_ts THEN
                RAISE_APPLICATION_ERROR(
                    -20901,
                    'UNIX timestamp too large for 32 bit limit'
                );
            ELSIF unixts <min_ts THEN
                RAISE_APPLICATION_ERROR(
                    -20901,
                    'UNIX timestamp too small for 32 bit limit' );
            ELSE
				oracle_date := unix_epoch + (unixts/86400);
            END IF;
            
            RETURN NEW_TIME(oracle_date, 'GMT', 'EDT');
        
END;
/
 
BKou,

How are you obtaining the Unix timestamp? (I know that when I issue the o/s command "date", it returns current date, time, and time zone.)

[santa]Mufasa
(aka Dave of Sandy, Utah, USA)
[I provide low-cost, remote Database Administration services: www.dasages.com]
“Beware of those that seek to protect you from harm. The cost will be your freedoms and your liberty.”
 
I'm reading a time stamp in stored seconds from a field in an event log table. I want to find out if the date is during daylight saving time.
 
I could figure it out if I can find the dates when DST starts and ends. Any tip on that is helpful.
 
BKou,

In the U.S. (except for Hawaii and the parts of Arizona not under control of Indian Reservations), Daylight Savings Time currently:[ul][li]begins at 1:00 A.M. on the 2nd Sunday in March[/li][li]ends at 2:00 A.M. on the 1st Sunday in November.[/li][/ul]Let us know if this resolves your question.

[santa]Mufasa
(aka Dave of Sandy, Utah, USA)
[I provide low-cost, remote Database Administration services: www.dasages.com]
“Beware of those that seek to protect you from harm. The cost will be your freedoms and your liberty.”
 
Actually, I was able to figure it out by interpreting someone's VB code. Here's what I came up with:

Code:
CREATE OR REPLACE FUNCTION Isdst(dDate IN DATE) RETURN INTEGER IS 

  dStartDST DATE;
  dEndDST DATE;
  DaysTilSun INTEGER;

BEGIN
   
   --* Figure out how many days after Mar 1, [Supplied Year] that the second Sunday occurs
   --  Then set when DST starts
  DaysTilSun := 15 - TO_CHAR(TO_DATE('03/01/' || TO_CHAR(dDate,'YYYY'), 'MM/DD/YYYY'), 'D');
  dStartDST := TO_DATE('03/01/' || TO_CHAR(dDate,'YYYY'), 'MM/DD/YYYY') + DaysTilSun;
  
   --* Figure out how many days before Nov 1, [Supplied Year] that Sunday occurs
   --  Then set when DST starts
   DaysTilSun := 8 - TO_CHAR(TO_DATE('11/01/' || TO_CHAR(dDate,'YYYY'), 'MM/DD/YYYY'), 'D');
   IF DaysTilSun = 7 THEN 
   	  DaysTilSun := 0; 
   END IF;
   dEndDST := TO_DATE('11/01/' || TO_CHAR(dDate,'YYYY'), 'MM/DD/YYYY') + DaysTilSun;
  
   --* Note that Anytime on the Sunday that DST starts is considered DST and
   --  ANYtime on the day it ends is considered DST
   IF dDate >= dStartDST AND dDate < dEndDST THEN
  	  RETURN 1;
   ELSE
  	  RETURN 0;
   END IF; 

END;
/
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top