Need to convert an epoch date to MMDDYYHHmm format

System: HP-UX
Kornshell
Perl is installed, but not POSIX

Hello,

I am calculating a future date/time. To do this I take the system date in epoch format and add to it. I now need to take the new epoch date and convert it to MMDDYYHHmm format.

Any help with this is greatly appreciated.

One way (adapted from a similar post about dates in the past).

Calculate date 28 days in the future and display in MMDDYYhhmm format, compensating for Perl numbering months from zero and years from 1900.

perl -w  -e '$d=28*86400;@t=localtime (time +$d); printf "%.2d%.2d%.2d%.2d%.2d", $t[4] +1,$t[3],$t[5] -100,$t[2],$t[1];'

BTW. HP-UX default shell has been Posix for over 10 years. See "man sh".

I am not experiencing the problem with Perl numbering the months correctly. I need to find the date and time 10 minutes ahead, so I just modified the code you posted to:

perl -w  -e '@t=localtime (time +600); printf "%.2d%.2d%.2d%.2d%.2d", $t[4] +1,$t[3],$t[5] -100,$t[2],$t[1];'

At the time of 13:21, I received output of 0708101331

This appears to be correct . Do you see any potential problems doing it this way?

Good catch on POSIX. What I meant to say was that strftime for awk was not installed. strftime was used in another possible solution I found. Sorry for the confusion.

No issues I can see.

Note: The code fragment within the "printf"
$t[4] +1
is adding one to the Month to compensate for Perl's month numbering.
1 Like

Ok, I understand. Thanks for your help methyl.