Solved

Convert sybase datetime to time in seconds since unix epoch

Posted on 2007-03-27
6
7,484 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
  • 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Employees depend heavily on their PCs, and new threats like ransomware make it even more critical to protect their important data.
Having trouble getting your hands on Dynamics 365 Field Service or Project Service trial? Worry No More!!!
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

773 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