Manipulating dates in a POSIX shell (/usr/bin/sh) (179 Views)
Reply
Regular Advisor
PatRoy
Posts: 160
Registered: ‎08-15-2006
Message 1 of 5 (179 Views)

Manipulating dates in a POSIX shell (/usr/bin/sh)

Hi.

I have a shell script which needs to get today's date (i.e. 20061215) and add 1 day to this date, so I'd end up having 20061216 in a variable...

In Linux's gnu date command, I have `date -d "+1 day" +"%Y%m%d"` which works perfectly. Installing GNU Date on HPUX isn't an option for me... and I've already got my hole script in shell... so Perl isn't a friendly approach...

Has anyone done this?

Thanks, Pat
Regular Advisor
PatRoy
Posts: 160
Registered: ‎08-15-2006
Message 2 of 5 (179 Views)

Re: Manipulating dates in a POSIX shell (/usr/bin/sh)

So far, the closest I got was to issue :

date +%s

which gives me the epoch time.. so I'd end up with something like "1166123240". I could just add up "86400", which totals a day's worth, but then, how do I convert that to a date string?

thx again! Pat
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 3 of 5 (179 Views)

Re: Manipulating dates in a POSIX shell (/usr/bin/sh)

Hi PAt:

Well, if you don't want Perl, I'd use Clay Stephenson's 'caljd' date manipulation utility!

You can download it from Merijn's website:

http://mirrors.develooper.com/hpux/

Regards!

...JRF...

Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 4 of 5 (179 Views)

Re: Manipulating dates in a POSIX shell (/usr/bin/sh)

Hi Pat:

I have to add that Perl *is* such a straightforward approach for your task, though:

# perl -MPOSIX=strftime -le 'print strftime("%Y%m%d",localtime(time+(86400)))'
20061216

Plus, as you can see, the syntax mimics the GNU 'date' since both use the 'strftime' C library notation.

Regards!

...JRF...
Regular Advisor
PatRoy
Posts: 160
Registered: ‎08-15-2006
Message 5 of 5 (179 Views)

Re: Manipulating dates in a POSIX shell (/usr/bin/sh)

Yep.. just found an exact same solution... I had:

perl -MPOSIX -le 'print for strftime( "%Y%m%d", localtime(time + 86400) )'

which does the same.

Thanks!
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.