• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 7715
  • Last Modified:

Convert sybase datetime to time in seconds since unix epoch

Hi Guys

I am looking for a way to convert master..sysdatabases columns: crdate and dumptrdate into seconds since the UNIX epoch.  Perferably I would like the databases to do this but failing that a C/C++ function that does it

Thanks
0
Grant Rogers
Asked:
Grant Rogers
  • 2
  • 2
  • 2
2 Solutions
 
bretCommented:
The TSQL datediff function is probably the place to start, you can get the difference between any two dates in various units, including seconds.  You will  then need to make adjustments for the difference between the time zone the date is from and UTC.  And adjustments for whether the date was affected by daylight savings time or not.  Note that ASE doesn't store any information about time zone or daylight savings time, and the environment ASE ran under may have changed over time (i.e. from one time zone to another if corporate headquarters moved, etc.), so you may need to gather some historical metaknowledge about the server's history to get a correct result.  I don't think the datediff function makes any adjustments for leap seconds, either.

(I recommend running ASE servers in a UTC environment, making clients responsible for any conversions to their local timezone).
0
 
knel1234Commented:
select datediff(ss, sd.crdate, sd.dumptrdate)
    from sysdatabases sd

I am able to retrieve the seconds between the 2 timestamps.
36495950
36407822
11106342
Command has been aborted.

Unfortunately, the problem is that the crdate will often default to 1/1/1900.
This causes a 535 error and the Command has aborted message above.

It is important to remember that datediff produces results of datatype int, and causes errors if the result is greater than 2,147,483,647.
1)For seconds, this is 68 years, 19 days, 3:14:07 hours.
2)For milliseconds, this is approximately 24 days, 20:31.846 hours.

cheers
knel
0
 
knel1234Commented:


FYI

I added my thoughts just for another point of view.  However, you should be mindful of Brets comments.  
In addition, this year (2007) is perfect example of potential issues relating to daylight savings time.

You might want to rethink the question.  What question(s) are you really trying to answer?

cheers
knel
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
Grant RogersMonitoring ConsultantAuthor Commented:
Thanks guys that information is very usful, it also explains why they take the epoch from 1900 in this function:

(Taken from)
http://cvs.zope.org/Products/ZSybaseDA/src/ctsybase.c?rev=1.3

#define EPOCH_DAYS_SINCE_1900     25567
#define SYBASE_TICKS_PER_SECOND     300
#define SECONDS_PER_DAY           86400

/*****************************************************************************
 *                                                                           *
 * CS_DATETIMEToTimestamp : convert a time in CS_DATETIME format to a        *
 *                          unix timestamp                                   *
 *                                                                           *
 * Arguments:                                                                *
 *        date - the date in CS_DATETIME format                              *
 *                                                                           *
 * Remarks:                                                                  *
 *                                                                           *
 ****************************************************************************/

static time_t
CS_DATETIMEToTimestamp (CS_DATETIME date)
{
  return ((date.dtdays - EPOCH_DAYS_SINCE_1900) * SECONDS_PER_DAY) +
    (date.dttime / SYBASE_TICKS_PER_SECOND);
}

Can anyone confirm this function is correct?
0
 
bretCommented:
Well, assuming that the CS_DATETIME is a UTC value, it is certainly close, but I'd be careful making that assumption.   It doesn't make any adjustments for leap seconds, though (but then, I don't know if UNIX time does either).
0
 
Grant RogersMonitoring ConsultantAuthor Commented:
Well I got it working thanks for you help guys
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 2
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now