nanog mailing list archives

Re: Unix Timestamp


From: Pat Myrto <pat () rwing ORG>
Date: Mon, 22 Oct 2001 12:22:24 -0700 (PDT)


Brandon Handeland has declared that:


Does anyone have a formula to convert a unix time stamp into the current time?

I know it is January 1st, 1970, in UTC format.  Just need some example code.

I'm trying to export some data from a HP/UX box into a MSSQL database and 
need to use the timestamp feature of MSSQL.

Not sure if I got your needs understood, but here is a try...

WARNING.  ELEGANCE IS NOT A FEATURE HERE.

If you want to convert a unix clock count into an ascii format,
you can use time() and ctime() as per an example below.  If you
want to go from an ascii represntation to unix timestamp, perhaps
use strptime() or friends, to convert to struct tm, and timelocal()
or timegm() to convert to unix clock form.
If ascii form may vary in format use something like getdate.y
or parsedate.y to parse it...

/* very quick-n-dirty to get ascii time from unix clock (no
 * sanity checks and such nice things).
 */

#include <sys/types.h>
#include <sys/time.h>
#include <stdio.h>

time_t time();

main()
{
    time_t clk;
    char *tstring;

    clk = time(NULL);

    tstring = ctime(&clk);
    printf("%s\n", tstring);
}

Pat M/HW

Thanks

----------------------------------------------------------------------
Brandon Handeland             Brandon () wyoming com
Senior Network Engineer
wyoming.com NOC       (307) 857-1092
----------------------------------------------------------------------



-- 
#include <std.disclaimer.h>    Pat Myrto (pat at rwing dot ORG)     Seattle WA
"Buy things that increase in value, Lease things that do not."
        --Rockefeller


Current thread: