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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert unix timestamp 4

Status
Not open for further replies.

frangac

Technical User
Feb 8, 2004
163
ZA
Hi All or PHV,

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

Can AWK do something like this?

Many Thanks
Chris
 
[tt]
BEGIN{
$0 = "2004 Mar 24 18:24:19"
OFS="/" ## ouput field separator
$2 = (index("JanFebMarAprMayJunJulAugSepOctNovDec",
$2) - 1) / 3 + 1
if ($2<10) $2 = "0" $2
if ($3<10) $3 = "0" $3
print $0
}
[/tt]
If you have nawk, use it instead of awk because
on some systems awk is very old and lacks many useful features.

Let me know whether or not this helps.


 
Hi futurelet,

Thanks for the reply,

It works well but what I am looking for is how to convert the unix timestamp "1080145459" into a date format like "20040324182419" or "2004/03/24/18:24:19"

Thanks Once Again
Chris
 
What futurelet suggested you was something like this:
echo "od1080145459=Y|adb | tr -d '\011' | nawk '
BEGIN{OFS="/"}
{$2=(index("JanFebMarAprMayJunJulAugSepOctNovDec",$2)-1)/3+1
if($2<10)$2="0"$2
if($3<10)$3="0"$3
print
}'

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Hi PHV,futurelet

Great stuff..
Do u sleep, eat,drink AWK or WHAT!

Anyway Please explain the following
{$2=(index("JanFebMarAprMayJunJulAugSepOctNovDec",$2)-1)/3+1
if($2<10)$2="0"$2

Thanks
Chris
 
Yes, converting the number of seconds since the epoch to the current date would be a rather intricate operation; it would involve determining leap years, etc. Use whatever you can to get to "2004 Mar 24 18:24:19", and then use awk.
 
{$2=(index("JanFebMarAprMayJunJulAugSepOctNovDec",$2)-1)/3+1
the main goal is to get the month number from the month name.
the index function returns the position of $2 (month name) in the list, so in the example 7 (position of "Mar")
as each month name is fixed length (3) and the list is in month order, we can deduce the month number from the given position, so in the example (7-1)/3 + 1 = 3
another way: (index("...",$2)+2)/3
if($2<10)$2="0"$2
if $2 is a single digit, padd it with a leading zero
another way: $2=sprintf("%02d",$2)

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Hey - it's been awhile since thread, but I need to get it going and it ain't working for me. I'm on Solaris box [both 5.7 and 5.8].

Trying:
echo "od1080145459=Y|adb
gives me back: 'symbol not found'

I assume it's a problem with the double quote.

Tried:
echo 'od1080145459=Y'|adb
gives me back the same 'symbol not found'

Tried:
echo '1080145459=Y'|adb'
gave me back incorrect date: '2038 Feb 3 08:19:21'
when should have been: '2004 Mar 24 11:24:19'

Anybody [futurelet?] care shed some lite - I'm not really good with 'adb'.

mucho gracias.

vlad

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
ah, never mind - I figured it out:
echo '0t1080145459=Y'|adb

Google does wonders

thanks - you can go back to sleep [wink]

vlad

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top