[RndTbl] Convert time in seconds to date in bash?

Kevin McGregor kmcgregor at shaw.ca
Tue Dec 7 14:28:33 CST 2004


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 at 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 at muug.mb.ca
> http://www.muug.mb.ca/mailman/listinfo/roundtable
> 




More information about the Roundtable mailing list