Solved

Convert sybase datetime to time in seconds since unix epoch

Posted on 2007-03-27
6
7,578 Views
Last Modified: 2012-06-22
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
Comment
Question by:Grant Rogers
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 10

Accepted Solution

by:
bret earned 63 total points
ID: 18800557
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
 
LVL 3

Assisted Solution

by:knel1234
knel1234 earned 62 total points
ID: 18804017
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
 
LVL 3

Expert Comment

by:knel1234
ID: 18804052


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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:Grant Rogers
ID: 18806572
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
 
LVL 10

Expert Comment

by:bret
ID: 18812035
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
 

Author Comment

by:Grant Rogers
ID: 18814862
Well I got it working thanks for you help guys
0

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Resolving an irritating Remote Desktop connection that stops your saved credentials from being used.
We take a look at some of the most common obstacles that IT teams run into as they work relentlessly to keep all the alarms and sirens from going off at once.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question