1832251 Members
2616 Online
110041 Solutions
New Discussion

Re: Converting TimeStamp

 
Chris Frangandonis
Regular Advisor

Converting TimeStamp

Hi All,

Is there a way to covert a unix timestamp to '%Y%m%d %H%M'
for eg 1080145459 must result to "20040324182419" or "2004/03/24/18:24:19"

I have tried the following and works well, but cannot get the date format that I require.

echo "od1080145459=Y|adb | tr -d '\011' and the result is
2004 Mar 24 18:24:19

Many Thanks
Chris

6 REPLIES 6
Kent Ostby
Honored Contributor

Re: Converting TimeStamp

well its ugly, but you could create an awk script called date.awk with the following in it:

BEGIN{dadate["Jan"]="01";dadate["Feb"]="02"; dadate["Mar"]="03";dadate["Apr"]="0
4";dadate["May"]="05";dadate["Jun"]="06";dadate["Jul"]="07";dadate["Aug"]="08";
dadate["Sep"]="09";dadate["Oct"]="10";dadate["Nov"]="11";dadate["Dec"]="12";}
{print $1"/"dadate[$2]"/"$3"/"$4 }

Then pipe what you have so far (the 2004 Mar 24 18:24:19 ) through the script using | awk -f date.awk

That would do it.

Oz

"Well, actually, she is a rocket scientist" -- Steve Martin in "Roxanne"
Jeroen Peereboom
Honored Contributor

Re: Converting TimeStamp

Chris,

interesting adb use. It must have something to do with localization.

Anyway, I wrote a little C program. May need some adjustment for your output format.

See attachment.

JP.
Hein van den Heuvel
Honored Contributor

Re: Converting TimeStamp

That adb command does nor work for me.

If the remaining challenge is translate month names to number, might I suggest the awk index function? for example:

echo "2004 Mar 24 18:24:19" | awk '{m=index("xxJanFebMarAprMayJunJulSepOctNovDec",$2)/3; print m"/"$3"/"$1}'


Hein.
A. Clay Stephenson
Acclaimed Contributor

Re: Converting TimeStamp

Probably the cleanest way to do this is Perl.

timestamp.pl
---------------------------------
#!/usr/bin/perl -w

use strict;

sub do_1_timestamp
{
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($_[0]);
printf("%04d%02d%02d%02d%02d\n",$year + 1900,$mon + 1,$mday,$hour,$min);
}

while ($#ARGV >= 0)
{
do_1_timestamp($ARGV[0]);
shift;
}
------------------------------------------
TS=1080145459
TM=$(timestamp.pl ${TS})
echo "Timestamp for ${TS} -> ${TM}"

The above script uses the localtime() function which takes into account timezone and daylight saving time. You might choose to use the gmtime() function which report UTC time. I intentionally grabbed all the values returned from localtime() so that you can modify the printf to as complicated an output as you like. Note: $year is number of years since 1900 and $mon ranges from 0 to 11 -- just like the C counterparts.

If it ain't broke, I can fix that.
Nicolas Dumeige
Esteemed Contributor

Re: Converting TimeStamp

perl -e '$epochtime = 1058850045; print scalar localtime($epochtime);'
Tue Jul 22 07:00:45 2003
All different, all Unix
Hein van den Heuvel
Honored Contributor

Re: Converting TimeStamp


Clay, right. Perl is proabbly the ultimate solution.

btw... 'shift' byy default operates on ARGV so the main loop in you example can be simplified from:

while ($#ARGV >= 0)
{
do_1_timestamp($ARGV[0]);
shift;
}

to (a less readbale perhaps):

do_1_timestamp($ts) while ($ts=shift);

Cheers,
Hein.


perl -e 'while ($#ARGV>=0) { print $ARGV[0],"\n"; shift }' aap noot mies

vs

perl -e 'while ($_=shift) { print $_,"\n"}' aap noot mies