Difference in date

Dear all,

I fancy that I'm pretty competent in ksh, but I have someone on HP-UX wanting me to script up a simple interface to handle user alterations rather than giving them high privileges to run up SAM. This is all fairly straightforward, but I'm stuck on an epoch date issue.

When we have a short-term contractor, we have to set an expiry date on the account. Looking and the manual pages for usermod it gives me the syntax and I have tested it all out just fine, but the parameters it requires is for number of days in the future that the account will expire, yet my operative wants to key in a date.

I need to be able to get the difference in days between today and the date given - a simple current_date minus target_date in format yyyymmdd will tell me if the target date is in the past, but how can I do this to count the days in between?

Preference is for ksh, but I guess perl may be acceptable, especially as I found a lovely routine on here for going the other way that I have embedded in a ksh script:-

perl -e 'print scalar localtime $ARGV[0],"\n" ' $seconds_since_epoch

I've seen various suggestions, but all for some wild perl or C but I'd prefer to understand what the code is saying because someone will no doubt ask me later on! There is a neat suggestion of

perl -e 'use Time::Local; print timelocal("00","00","00","01","01","2000"),"\n";'

which I can get to work just fine on AIX, but HP-UX gives me the response:-

I think that this means that there is a library not installed or in the path. I also don't really know perl at all, but need to tweak it to accept a variable as in the first example.

Of course, the plan is to have a little stub of code that I can shovel my date into (time will just be midnight to make it easy) and get "seconds-from-epoch". After that, I can take one from the other and convert back with the perl above.

It's a shame that date +%s is not an option.

I had considered moving the timezone forwards and counting how many days to step, but it's hardly good code to do it like that, kicking up stacks more processes and possibly taking a while to run if the date in question is perhaps 18 months away.

Any suggestions?

Many thanks, in advance,
Robin

If you can install GNU date, you'll get a date utility with +%s.

The "Can't locate Time/Local.pm" error is fairly straightforward: You don't have it. You could install it. (perl considers it Time::Local)

Think you should find your answer, and more, in this rather good faq article...

http://www.unix.com/answers-frequently-asked-questions/13785-yesterdays-date-date-arithmetic.html

:slight_smile:

Yes,
as already suggested, use the wonderful script by Perderabo:

datecalc -a year month day - year month day

Thanks for responding Corona688,

I had considered this approach, but dismissed them because I wouldn't know where to get them. Searching for "GNU date" get zillions of hits :eek: but that's because of all the pages with the words GNU & date anywhere on them. Doh! :rolleyes:

I might have to just write a utility ksh program to basically count my way along, but it won't really be that concise I suppose.

Any other suggestions?

OS reports as

whatever that translates back to.

Thanks again,
Robin

Without concise tools, there's not a concise way. Check radouluv's post.

looks powerful.