If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Date function to return UNIX timestamp?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-19-08, 14:09
db2user db2user is offline
Registered User
 
Join Date: Dec 2002
Posts: 123
Date function to return UNIX timestamp?

Hi,

Is there any DB2 date function that returns a timestamp that is a long integer containing the number of seconds between January 1 1970 00:00:00 GMT and a date specified? Thanks!
Reply With Quote
  #2 (permalink)  
Old 12-19-08, 14:22
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
Try timestampdiff.

Andy
Reply With Quote
  #3 (permalink)  
Old 12-19-08, 16:49
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Timestampdiff is an approximation, which uses 30 days' long months.

This approach works way better:
Code:
values(
  (days(current_timestamp-current_timezone) - days('1970-01-01') )*86400 + 
  midnight_seconds(current_timestamp - current_timezone)
)
Reply With Quote
  #4 (permalink)  
Old 12-21-08, 08:06
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
@n_i: your approach is also only an approximation because you ignore leap years as well as leap seconds.

@db2user: what is the underlying requirement to have seconds since 1970-01-01? Could you elaborate on that? Maybe there is a completely different solution that is different from your approach.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #5 (permalink)  
Old 12-21-08, 10:32
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Quote:
Originally Posted by stolze
@n_i: your approach is also only an approximation because you ignore leap years as well as leap seconds.
Even if it were true, it would not be me but rather the DAYS function

However, the formula produces the same result as the date utilitiy on AIX, so I'm thinking it does account for leap years and seconds as it should.
Reply With Quote
  #6 (permalink)  
Old 12-21-08, 12:18
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
You are right on the leap years - my fault. But you do not account for leap seconds because you assume 86400 seconds for every day. There are a few days which actually have 86401 seconds, however.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #7 (permalink)  
Old 12-21-08, 14:49
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,196
Quote:
Originally Posted by stolze
You are right on the leap years - my fault. But you do not account for leap seconds because you assume 86400 seconds for every day. There are a few days which actually have 86401 seconds, however.
Knut, when is the last time you took a day of vacation?
__________________
M. A. Feldman
IBM Certified DBA on DB2 for Linux, UNIX, and Windows
IBM Certified DBA on DB2 for z/OS and OS/390
Reply With Quote
  #8 (permalink)  
Old 12-21-08, 15:25
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Quote:
Originally Posted by stolze
Y But you do not account for leap seconds because you assume 86400 seconds for every day. There are a few days which actually have 86401 seconds, however.
I think that is only partially true, because the leap seconds don't accumulate over time. When the leap second is added to adjust for the slowing Earth rotation, the time is paused for 1 second, so there's a "hole in time" at midnight. In other words, as an example, there are 86400 seconds on December 31 and 86400 seconds on January 1st, with 1-second pause in between. Therefore, the formula will return a possibly incorrect result only during that leap 1 second long pause, which I think is an acceptable precision.
Reply With Quote
  #9 (permalink)  
Old 12-21-08, 15:56
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,079
Quote:
Originally Posted by stolze
what is the underlying requirement to have seconds since 1970-01-01?
That's the standard way a timestamp is stored on a UNIX (or Linux) system.
E.g., the last modification time of a file is stored as a 4-byte integer (with the above meaning) as an attribute of the inode.
So a useful reason for obtaining such a value within DB2 is to match timestamps of DB2 with timestamps of the operating system.
Be careful with daylight-saving-time jumps, though! UNIX really does what it says (# seconds since 1970-01-01-00:00:00 UTC), while DB2 uses local time, i.e., will typically report 3601 seconds time difference while in reality only one second elapsed, if you do the timediff around the spring DST switch moment.
This justifies the current_timezone element in Nick's answer, which effectively converts the current_timestamp to UTC.
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/
Reply With Quote
  #10 (permalink)  
Old 12-22-08, 03:23
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
That is well understood. My question was what this timestamp will be compared against, i.e. what is the application doing with it. Maybe it is trivial to change working ISO timestamps, i.e. yyyy-mm-dd-HH.MM.SS?

Regarding leap seconds in UNIX/Linux systems, you will find such a definition of "struct tm" in /usr/include/time.h:
Code:
struct tm
{
  int tm_sec;                   /* Seconds.     [0-60] (1 leap second) */
  int tm_min;                   /* Minutes.     [0-59] */
  int tm_hour;                  /* Hours.       [0-23] */
  int tm_mday;                  /* Day.         [1-31] */
  int tm_mon;                   /* Month.       [0-11] */
  int tm_year;                  /* Year - 1900.  */
  int tm_wday;                  /* Day of week. [0-6] */
  int tm_yday;                  /* Days in year.[0-365] */
  int tm_isdst;                 /* DST.         [-1/0/1]*/
Whether the OP has to care about it and needs the accuracy here or can be a bit sloppy, I do not know...
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development

Last edited by stolze; 12-22-08 at 03:27.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On