Does anyone know of a quick way to convert the Unix time (in seconds since Jan 1, 1970) to a readable date via bash and standard commands?
Been trying to do this one for ages... Best I can come up with uses a perl one liner:
perl -e 'print scalar localtime 1102434671'
Sean
On Tue, 7 Dec 2004, Kevin McGregor wrote:
Does anyone know of a quick way to convert the Unix time (in seconds since Jan 1, 1970) to a readable date via bash and standard commands?
Roundtable mailing list Roundtable@muug.mb.ca http://www.muug.mb.ca/mailman/listinfo/roundtable
Cool. I had to do this just last week. After wasting half an hour digging through man pages and Google searches, I gave up and wrote a q&d C program. It seems there ought to be a way to do this with "date -d", but I can't find a way to get it to take seconds since epoch as an input. For that matter, I can't figure out how to get it to output the date as such either.
According to Sean A. Walberg:
Been trying to do this one for ages... Best I can come up with uses a perl one liner:
perl -e 'print scalar localtime 1102434671'
Sean
On Tue, 7 Dec 2004, Kevin McGregor wrote:
Does anyone know of a quick way to convert the Unix time (in seconds since Jan 1, 1970) to a readable date via bash and standard commands?
I don't have a gnu/linux box (or gnu date) on hand.. but on freebsd:
$ date -j -r 1102434671 Tue Dec 7 09:51:11 CST 2004
on gnu date I believe it needs to be done with the + option, but I don't have a machine to test that on right now.
Theo
On Tue, December 7, 2004 9:48, Kevin McGregor said:
Does anyone know of a quick way to convert the Unix time (in seconds since Jan 1, 1970) to a readable date via bash and standard commands?
You know, that is strange.
The date command is amazingly flexible on what input it will accept but I can't get it to convert a simple unixtime to a string.
Here are some examples:
$ date --date yesterday Mon Dec 6 11:11:38 CST 2004 $ date --date "2 days ago" Sun Dec 5 11:11:38 CST 2004 $ date --date "2 days" Thu Dec 9 11:11:38 CST 2004 $ date --date "last year" Sun Dec 7 11:11:38 CST 2003
$ date --date "1102439535" date: invalid date `1102439535'
Weird eh?
Ok, but there is a solution:
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.
Hope that helps.
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.
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 when the CVS version is released and becomes part of most distributions.