KSH script Not working (calculate days since 1/1/2000 given day 4444)

I am unable to get this KSH script to work. Can someone help. I've been told this should work with KSH93. Which I think I have on Solaris 10.

If I do a grep -i version /usr/dt/bin/dtksh I get
@(#)Version M-12/28/93d
@(#)Version 12/28/93
@(#)Version M-12/28/93

This is correct for ksh93...right?

Script that doesn't work:

#!/usr/dt/bin/dtksh

printf "%(%d %b %Y)T\n" "Jan 1 2000 + 4444 days"

Output (this is all I get):
(0

I should get 02 Mar 2012

Also, can someone tell me how to add a variable in place of 4444? Can I just do '$num_days'

Umm, I have Version M-12/28/93e and I get:

$ printf "%T\n" now
ksh93: printf: T: unknown format specifier

So I'm guessing you need a later version for this feature.

Your probably going to have to resort to perl for this sort of thing, eg from_epoch.pl:

#!/usr/bin/perl
use Time::Local;
use POSIX qw(strftime);
print POSIX::strftime("%d %b %Y\n", localtime(timelocal(0,0,0,1,0,2000) + $ARGV[0]*24*3600));
$ ./from_epoch.pl 4444
02 Mar 2012
1 Like

Thanks for info...I'll give it a try tomorrow. Will this perl code work with any version...I am using Solaris 10. Just curious because someone else gave me some perl code and I couldn't get it to work. Also, could someone tell me how to save this output to variable? Is it possible to run your code first and then run some csh code I already have written (can I just enter #!/usr/bin/csh and append my code to your perl code...in other words have one script that has both perl and csh)? If this isn't possible how do you write the output of the perl code to a file?

I don't have Solaris 10 to test this on but considering it's only using
Time and POSIX there is a very good change it will work fine.

From a csh script you should be able to do.

set num_days=4444
set mydate=`perl -e 'use Time::Local;use POSIX qw(strftime);print POSIX::strftime("%d %b %Y\n", localtime(timelocal(0,0,0,1,0,2000) + $ARGV[0]*24*3600));' $num_days`

That's great...thanks so much. Just curious do I need set num_days=4444 or $num_days at the end of the perl code? Won't $ARGV[0] will be my number of days from the user?

I just set num_days in the example to show you how a shell variable is passed into the perl program. Typically you would be doing some sort of calculation or assignment from the script's arguments into num_days.

We then put $num_days on the end of the perl command line and this can be accessed as ARGV[0] from within perl. ARGV[0] here isn't a parameter to the csh script but rather the first parameter passed on the perl command line.