Gosh, I don't think I've ever been compelling before!
I perhaps could have provided some context around my question. I have several log and data files (e.g. Squid's access.log; arp.dat) which store the timestamp as the usual seconds since the epoch.
I was looking for something quick and dirty which would let me list the files with the timestamp converted to something readable for a quick look as to what happened on which day.
I ended up doing this for the Squid log:
perl -nale 'print scalar localtime @F[0], substr($_,14)' <access.log
Thanks to Sean W. for the perl pointer and everyone who contributed!
Kevin
----- Original Message ----- From: John Lange john.lange@open-it.ca Date: Tuesday, December 7, 2004 2:08 pm Subject: Re: [RndTbl] Convert time in seconds to date in bash?
I was so compelled by this question that I emailed the maintainer of date who replied:
On Tue, 2004-12-07 at 12:01, Jim Meyering wrote:
If you use the coreutils from CVS, this new notation works:
date --date=@1102439250
So there you go, there is no way to enter seconds from epoc directly into the date command... BUT there will be some time in the future whenthe CVS version is released and becomes part of most distributions. -- John Lange OpenIT ltd. (204) 885 0872
On Tue, 2004-12-07 at 12:12, Gilbert E. Detillieux wrote:
According to John Lange:
Here is how you print epoc:
$ date -u --date "Jan 1, 1970 00:00:00" +%s 0
Now in our time zone: $ date -u --date "Jan 1, 1970 00:00:00 +0600" +%s -21600
So,
$ date -u --date "Jan 1, 1970 00:00:00 +0600 + 1102439250 seconds" Tue Dec 7 11:07:30 UTC 2004
Ya, that seems crazy complicated but its the only way I could
get it to
work using strictly the bash command line.
Using further formating options would clean up the output
including> > correcting the timezone indicator.
Simplifying for local time zone...
% date ; date --date "Jan 1, 1970 00:00:00 +0000 + `date +%s`
seconds"> Tue Dec 7 12:09:35 CST 2004
Tue Dec 7 12:09:35 CST 2004 %
I wish the GNU date(1) man page would explain the input formats
better.> The simply use the keyword "STRING" without ever defining what a valid
"STRING" would be. There's a lot of power there, once you know
what's> allowed.
Roundtable mailing list Roundtable@muug.mb.ca http://www.muug.mb.ca/mailman/listinfo/roundtable
When dealing with text log files (or even database log files) I generally resort to perl.
Writing an effective "program" with just bash scripting is just to challenging.
Perl was originally designed for exactly this purpose and with its extensive regular expression library it is probably still the best.
I will give a nod to PHP though. Recent versions of PHP compile both as a module for Apache AND as a stand alone interpreter (just like perl).
PHP also has full regular expression support and its "Basic like" code makes it a snap to code in. Recently I've been using it much more than perl even for simple scripts.
Just for the record, here is the PHP code to convert unixtime to a date string ;)
<? date("r", 1102439250); ?>
John
On Tue, 2004-12-07 at 14:28, Kevin McGregor wrote:
Gosh, I don't think I've ever been compelling before!
I perhaps could have provided some context around my question. I have several log and data files (e.g. Squid's access.log; arp.dat) which store the timestamp as the usual seconds since the epoch.
I was looking for something quick and dirty which would let me list the files with the timestamp converted to something readable for a quick look as to what happened on which day.
I ended up doing this for the Squid log:
perl -nale 'print scalar localtime @F[0], substr($_,14)' <access.log
Thanks to Sean W. for the perl pointer and everyone who contributed!
Kevin
----- Original Message ----- From: John Lange john.lange@open-it.ca Date: Tuesday, December 7, 2004 2:08 pm Subject: Re: [RndTbl] Convert time in seconds to date in bash?
I was so compelled by this question that I emailed the maintainer of date who replied:
On Tue, 2004-12-07 at 12:01, Jim Meyering wrote:
If you use the coreutils from CVS, this new notation works:
date --date=@1102439250
So there you go, there is no way to enter seconds from epoc directly into the date command... BUT there will be some time in the future whenthe CVS version is released and becomes part of most distributions. -- John Lange OpenIT ltd. (204) 885 0872
On Tue, 2004-12-07 at 12:12, Gilbert E. Detillieux wrote:
According to John Lange:
Here is how you print epoc:
$ date -u --date "Jan 1, 1970 00:00:00" +%s 0
Now in our time zone: $ date -u --date "Jan 1, 1970 00:00:00 +0600" +%s -21600
So,
$ date -u --date "Jan 1, 1970 00:00:00 +0600 + 1102439250 seconds" Tue Dec 7 11:07:30 UTC 2004
Ya, that seems crazy complicated but its the only way I could
get it to
work using strictly the bash command line.
Using further formating options would clean up the output
including> > correcting the timezone indicator.
Simplifying for local time zone...
% date ; date --date "Jan 1, 1970 00:00:00 +0000 + `date +%s`
seconds"> Tue Dec 7 12:09:35 CST 2004
Tue Dec 7 12:09:35 CST 2004 %
I wish the GNU date(1) man page would explain the input formats
better.> The simply use the keyword "STRING" without ever defining what a valid
"STRING" would be. There's a lot of power there, once you know
what's> allowed.
Roundtable mailing list Roundtable@muug.mb.ca http://www.muug.mb.ca/mailman/listinfo/roundtable