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. Thanks ---------------------------------------------------------------------- Brandon Handeland Brandon@wyoming.com Senior Network Engineer wyoming.com NOC (307) 857-1092 ----------------------------------------------------------------------
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
% perl5 -e '$x = localtime(1003723200); print "$x\n" -- -Barry Shein Software Tool & Die | bzs@TheWorld.com | http://www.TheWorld.com Purveyors to the Trade | Voice: 617-739-0202 | Login: 617-739-WRLD The World | Public Access Internet | Since 1989 *oo*
[ On Monday, October 22, 2001 at 15:37:46 (-0400), Barry Shein wrote: ]
Subject: Re: Unix Timestamp
% perl5 -e '$x = localtime(1003723200); print "$x\n"
PERL!?!?!? What the heck's the matter with "date"!?!?!?!? 0.0 :-) $ date -r 1003723200 Mon Oct 22 00:00:00 EDT 2001 Just to clarify what I mean by my surprise above: $ time ksh -c 'i=0; while [ $i -lt 100 ] ; do date -r 1003723200 ; : $((i++)) ; done > /dev/null' 5.24s real 0.65s user 4.43s system $ time ksh -c 'i=0; while [ $i -lt 100 ] ; do perl -e '\''$x = localtime(1003723200); print "$x\n"'\'' ; : $((i++)) ; done > /dev/null' 26.72s real 7.48s user 12.64s system (no it's not a fast system, just an old SS2, but it's a great demo of the abuses some people will do with perl) Heck even GNU Awk, one of the usually slower and more gargantuan awks (but the only one with strftime()), does as well or better than 'date': $ time ksh -c 'i=0; while [ $i -lt 10 ] ; do gawk '\''END{print strftime("%c",1003723200)}'\'' < /dev/null ; : $((i++)) ; done > /dev/null' 1.91s real 0.62s user 1.15s system (oddly gawk does consistently better than date in the system and real time, though almost always about the same in the user time -- and one of the reasons this is odd is 'date' is static linked and about 1/2 the size of the dynamic-linked gawk!) -- Greg A. Woods +1 416 218-0098 VE3TCP <gwoods@acm.org> <woods@robohack.ca> Planix, Inc. <woods@planix.com>; Secrets of the Weird <woods@weird.com>
Greg A. Woods wrote (on Oct 22):
$ time ksh -c 'i=0; while [ $i -lt 100 ] ; do date -r 1003723200 ; : $((i++)) ; done > /dev/null' 5.24s real 0.65s user 4.43s system [...] $ time ksh -c 'i=0; while [ $i -lt 10 ] ; do gawk '\''END{print strftime("%c",1003723200)}'\'' < /dev/null ; : $((i++)) ; done > /dev/null' 1.91s real 0.62s user 1.15s system
You only run awk 10 times according to that, not 100 like in the previous two examples (only one quoted). But this is getting OT. Chris. -- == chris@easynet.net T: +44 845 333 0122 == Global IP Network Engineering, Easynet Group PLC F: +44 845 333 0122
Once upon a time, Greg A. Woods <woods@weird.com> said:
PERL!?!?!? What the heck's the matter with "date"!?!?!?!? 0.0 :-)
$ date -r 1003723200 Mon Oct 22 00:00:00 EDT 2001
On Solaris 7: $ date -r 1003723200 date: illegal option -- r usage: date [-u] mmddHHMM[[cc]yy][.SS] date [-u] [+format] date -a [-]sss[.fff] On Digital Unix 4.0F: $ date -r 1003723200 date: illegal option -- r Usage: date [-u] [+field descriptors] On Red Hat Linux 7.1: $ date -r 1003723200 date: 1003723200: No such file or directory But perl works on all three. :-) -- Chris Adams <cmadams@hiwaay.net> Systems and Network Administrator - HiWAAY Internet Services I don't speak for anybody but myself - that's enough trouble.
[ On Monday, October 22, 2001 at 18:18:41 (-0500), Chris Adams wrote: ]
Subject: Re: Unix Timestamp
On Red Hat Linux 7.1: $ date -r 1003723200 date: 1003723200: No such file or directory
The first two were kinda sad examples of the state of commercial Unix (unfortunately even SuSv2 lacks this now ancient feature!), but this last one (i.e. GNU date) surprises the heck out of me -- especially since there's not even an equivalent option with a different name....
But perl works on all three. :-)
So does the gawk variant, and so would a silly one-line C program. No doubt python and ruby variants would be equally portable. I really hate perl. It is the worst of all the bad interpreted languages. Well maybe not as bad as VB.... But enough of this nonsense -- the best answer is the one from Michael Barrow to just use FROM_UNIXTIME() in the MySQL code directly. -- Greg A. Woods +1 416 218-0098 VE3TCP <gwoods@acm.org> <woods@robohack.ca> Planix, Inc. <woods@planix.com>; Secrets of the Weird <woods@weird.com>
But enough of this nonsense -- the best answer is the one from Michael Barrow to just use FROM_UNIXTIME() in the MySQL code directly.
That Barrow guy can't read! =) His solution would work best if the guy used MySQL, but he said that he's using *MS*SQL. Oh well.... -- Michael L. Barrow P: 805.965.1314 F:781.240.5836
On Mon, Oct 22, 2001 at 08:45:25AM -0400, Greg A. Woods wrote:
[ On Monday, October 22, 2001 at 18:18:41 (-0500), Chris Adams wrote: ]
On Red Hat Linux 7.1: $ date -r 1003723200 date: 1003723200: No such file or directory
The first two were kinda sad examples of the state of commercial Unix (unfortunately even SuSv2 lacks this now ancient feature!), but this
sounds like a BSD-ism (for purists?); NetBSD 1.5.2 has the "-r" option, and it works as expected. Linux 6.2 and up switched to sh-utils-2.0, which uses "-r" as "refer to date of file", so there's now a clear divergence.
last one (i.e. GNU date) surprises the heck out of me -- especially since there's not even an equivalent option with a different name....
yes, especially, as you can go forward: bash$ date "+%s" 1003819816 however, a quick reading of the doc indicated that one can do: bash$ date --date="01/01/1970 UTC 1003819816 seconds" Tue Oct 23 02:50:16 EDT 2001 unfortunately, the timezone (EDT here) doesn't work right, except on a old slackware box (which is probably a bug, either way). -- Henry Yen Aegis Information Systems, Inc. Senior Systems Programmer Hicksville, New York
"Greg A. Woods" wrote:
[ On Monday, October 22, 2001 at 18:18:41 (-0500), Chris Adams wrote: ]
Subject: Re: Unix Timestamp
On Red Hat Linux 7.1: $ date -r 1003723200 date: 1003723200: No such file or directory
The first two were kinda sad examples of the state of commercial Unix (unfortunately even SuSv2 lacks this now ancient feature!), but this last one (i.e. GNU date) surprises the heck out of me -- especially since there's not even an equivalent option with a different name....
But perl works on all three. :-)
So does the gawk variant, and so would a silly one-line C program. No doubt python and ruby variants would be equally portable. I really hate perl. It is the worst of all the bad interpreted languages. Well maybe not as bad as VB....
But enough of this nonsense -- the best answer is the one from Michael Barrow to just use FROM_UNIXTIME() in the MySQL code directly.
-- Greg A. Woods
+1 416 218-0098 VE3TCP <gwoods@acm.org> <woods@robohack.ca> Planix, Inc. <woods@planix.com>; Secrets of the Weird <woods@weird.com>
Hmmm... How exactly does one use a MySQL builtin inside an M$ SQL database? Owen -- *********************************************************************** "Every time you turn on your new car, you're turning on 20 microprocessors. Every time you use an ATM, you're using a computer. Every time I use a settop box or game machine, I'm using a computer. The only computer you don't know how to work is your Microsoft computer, right?" - Scott McNealy, CEO of Sun Microsystems, Inc., from an April 1997 interview in Upside Magazine *********************************************************************** "One World, One Web, One Program." - Microsoft Promotional Ad "Ein Volk, Ein Reich, Ein Führer" - Adolf Hitler
On Mon, 22 Oct 2001, Barry Shein wrote:
% perl5 -e '$x = localtime(1003723200); print "$x\n"
Forgot the ' at the end: perl5 -e '$x = localtime(1003723200); print "$x\n"'
<> Nathan Stratton CTO, Exario Networks, Inc. nathan@robotics.net nathan@exario.net http://www.robotics.net http://www.exario.net
If you are using perl, localtime() If you are using C, strftime() uses a timezone struct provided by the c function localtime(). strftime takes printf style arguments, and builds strings that represent the time for you. mike -------------------------------------------------------- UNIX was not designed to stop you from doing stupid things, because that would also stop you from doing clever things. -- Doug Gwyn Mike Wiacek Systems Administrator Magpage Internet Services 800-250-2990 Ext 226 On Mon, 22 Oct 2001, Brandon Handeland wrote:
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.
Thanks
---------------------------------------------------------------------- Brandon Handeland Brandon@wyoming.com Senior Network Engineer wyoming.com NOC (307) 857-1092 ----------------------------------------------------------------------
participants (11)
-
Barry Shein
-
Brandon Handeland
-
Chris Adams
-
Chrisy Luke
-
Henry Yen
-
Michael L. Barrow
-
Mike Wiacek
-
Nathan Stratton
-
Owen DeLong
-
Pat Myrto
-
woods@weird.com