Using awk or nawk to convert epoch time to date format

Looking for some help and usually when I do a search this site comes up. Hopefully someone can give me a little direction as to how to use one of these two commands to achieve what I'm trying to do.

What am I trying to do?

I need to take the time value in epoch format returned from the system (Solaris) and use A command to convert it to a standard date format of my choosing.

Why not just use date '+%format'?

One main reason is that I need the date 12 hours in the future as a variable for a command that will be executed at a given point in time. If there is a way to do this using just date, I'm all ears. I know with GNU date you can pull epoch with %s, format the time and even add time to the value returned by date using options like "+12 hours" or "-1 day", etc. These options do not work with date on Solaris.

Why not just use Perl or another program language?

Right now there are no options to deploy additional functionality, not even updates to the packages like gawk (which works, btw....GRRRRR!!!!), so we must work within the confines of awk or nawk or whatever else will work. Perl is not installed on the Solaris machines and unfortunately is not an option. :frowning:

What is needed.

So far I can get the epoch value, but for my variable I need to have the date 12 hours in the future in the following format:

MMDDYYYYTHHMMSS (the 'T' is a delimiter for the program reading the value)

I can achieve this with the current date and time, but not with future date and time. Because I am using Solaris, I cannot use gawk (on this build) and am limited to either awk or nawk as available commands. Again, this is with my limited knowledge. If there is another command that can take epoch time and convert to a date format, please educate me. [FONT=Arial][SIZE=2]

What works, what doesn't.

So far I have tried the following but with syntax errors that are vague at best. This seems like it would be the best solution if I could figure out what Solaris doesn't like about the syntax. I can copy this text and input it directly into a Linux command line and I get what I want, so it's not a matter of typing. :slight_smile:

$ awk 'BEGIN{print strftime("%m%d%YT%H%M%S", '1154020897')}'
awk: syntax error near line 1
awk: illegal statement near line 1
$

This works in Linux (Fedora 14), but not in Solaris 10.

I'm using nawk this way to get the epoch time.

nawk 'BEGIN{print srand()}')

For the date, I am using this for my start date, but so far I haven't seen a method to calculate the time 12 hours in the future.

date '+%m%d%YT%H%M%S'

So is there any hope? Does anyone have any suggestions that may prove effective?

Thanks in advance for the help!

perl -e '($sec,$min,$hour,$mday,$mon,$year,$wday,$yday)=gmtime(time);$year+=1900;$mon="0$mon" if length$mon==1;$mday="0$mday" if length$mday==1;$hour-=12 if $hour>12;print"$mon-$mday-$year | $hour:$min:$sec\n"'

or if you need script:

#!/usr/bin/perl -w

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday)=gmtime(time);
$year+=1900;
$mon="0$mon" if length$mon==1;
$mday="0$mday" if length$mday==1;
$hour-=12 if $hour>12;
print"$mon-$mday-$year | $hour:$min:$sec\n";

---------- Post updated at 08:38 PM ---------- Previous update was at 08:34 PM ----------

actualy you will need to do the same for $sec $min $hour as i did for $mon and $mday
they may have single digit too

you can change 'gmtime' for 'localtime' if you prefer

Thanks for the quick reply, but Perl is not an option. If it were, this would have been resolved before it was an issue. :smiley:

i didn't get the "additional functionality" thing.. perl exists on any platform and this simple script doesn't require any "additional functionality"

Gotcha. The least common denominator is what we have to go by and some of the Solaris machines do not have Perl installed. Not sure if this company policy or what, but that is the limiting factor.

That's a good point though. I'll update my original post to reflect this limitation.

#!/bin/ksh

# here's my current EPOCH time
currentEpoch=$(/usr/bin/truss /usr/bin/date 2>&1 | /usr/bin/awk '/^time/ {print $NF}')
future12=$(($currentEpoch + 43200))

# here we convert EPOCH back to human readable format
back2Human=$(echo "0t${currentEpoch}=Y" | /usr/bin/adb)

Or look through the FAQs