Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert Epoch time to Date 2

Status
Not open for further replies.

Mag0007

MIS
Feb 15, 2005
829
US
How do I convert epoch time to regular date in awk? Is it possible?

I have something like this:
1144172897
 
Hi

If you have [tt]gawk[/tt], then with the [tt]strftime()[/tt] function :
Code:
print strftime("%c",1144172897)
Probably [tt]nawk[/tt] also has something usefull.

If you have only standard [tt]awk[/tt] I suggest to use an external command to do the conversion.

Feherke.
 
Hmm.

I only have awk and nawk on my system :-(

 
not natively within awk, but....
on Solaris:
Code:
currentEpoch='1144172897'
echo "0t${currentEpoch}=Y" | /usr/bin/adb

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
You may consider perl:
#!/bin/ksh
epoch=$1
eval $(perl -e '
($ss,$nn,$hh,$dd,$mm,$yy,$wd,$yd,$dst)=localtime('$epoch');
printf("yy=%04d mm=%02d dd=%02d hh=%02d nn=%02d ss=%02d\n",$yy+1900,$mm+1,$dd,$hh,$nn,$ss);
printf("DATE=%04d%02d%02d\n",$yy+1900,$mm+1,$dd);
printf("TIME=%02d%02d%02d\n",$hh,$nn,$ss)')
echo "$epoch:"$DATE,$TIME,$yy,$mm,$dd,$hh,$nn,$ss

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
I was able to get gawk and the strftime() to work. however %c only prints the current date. How do I convert a epoch number to a regular date?

TIA
 
Hi

Maybe I am confused, but as far as I know, that 1144172897 is called Unix time.

Would be better to show us exactly what you want to get as the conversion's result.

And [tt]strftime()[/tt] will format its second parameter, if exist, not only the current date. And [tt]%c[/tt] is for current system-wide date format, not for current date. For more on format specifiers, see man strftime.

Feherke.
 
Sorry for being unclear.

On AIX:
lsuser -a time_last_login ALL

gives me something like this:
user1 time_last_login=1144279759

It seems time_last_login is a UNIX epoch time.

I want to convert 1144279759 to regular date

:)

In awk preferably....

Perl it would be something like this:
Code:
perl -we 'print (my $time= localtime 1144279759, "\n")'
 
Hi

And what is the difference between the [tt]perl[/tt] and [tt]awk[/tt] results ?
Code:
[blue]master #[/blue] perl -e 'print "".localtime 1144279759'
Thu Apr  6 01:29:19 2006

[blue]master #[/blue] awk 'BEGIN{print strftime("%c",1144279759)}'
Thu Apr  6 01:29:19 2006

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top