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
% perl5 -e '$x = localtime(1003723200); print "$x\n"
[ 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!)
% 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
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.
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. 
[ 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.
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....
[ 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).
"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
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