Operating System - HP-UX
1821800 Members
3340 Online
109637 Solutions
New Discussion юеВ

Working with date using shell scripts

 
SOLVED
Go to solution
Anoop P_2
Regular Advisor

Working with date using shell scripts

Hi there,

This is about date calculations. The question is,

Suppose today is 25 Feb 2004. How do I correctly find out the date after 10 days? (Consider month change, leap year etc.)

Is there any function which I could use straigt away?

I do give points to responses.

Thanks in advance!
Anoop
13 REPLIES 13
RAC_1
Honored Contributor

Re: Working with date using shell scripts

One of forum members AC Clay has written one script called date hammer. Search forums for it. It serves many such purposes.
There is no substitute to HARDWORK
Peter Godron
Honored Contributor

Re: Working with date using shell scripts

Anoop,
get a copy of the excellent caljd package by A. Clay Stephenson.

One version at:
http://mirrors.develooper.com/hpux/
right at the bottom of the page.
Arunvijai_4
Honored Contributor

Re: Working with date using shell scripts

Hi Anoop,

You can use A.Clay's script (Caljd)

http://mirrors.develooper.com/hpux/caljd-2.25.sh
http://mirrors.develooper.com/hpux/caljd-2.2.pl

-Arun
"A ship in the harbor is safe, but that is not what ships are built for"
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Working with date using shell scripts

And once you find this mysterious script then to find the date 10 days from now in MM/DD/YYYY format you would:

TODAYPLUS10=$(caljd.sh -S "/" $(caljd.sh -n 10))
echo "10 days from now is ${TODAYPLUS10}"

And, yes, it works across year boundaries, leap years. It can also calculate n days from a given date in either direction and can even optionally skip days in the week so that "10 days from now unless that is a weekend" also works. In that case, the next non-excluded day is output.

Invoke as caljd.sh -u for usage and many examples.
If it ain't broke, I can fix that.
Doug O'Leary
Honored Contributor

Re: Working with date using shell scripts

Hey;

No disrespect to A.Clay's script, but you could also download gnu date and use it thusly:

$ date --date="10 days"
Sat Jul 15 09:24:42 CDT 2006
$ date
Wed Jul 5 09:24:43 CDT 2006
$ date --date="3 days ago"
Sun Jul 2 09:29:02 CDT 2006

Other, more standard formatting strings can be used as well...

gnu date, and other gnu utilies can be found at: http://hpux.cs.utah.edu/hppd/hpux/Gnu/sh_utils-2.0/

HTH;

Doug

------
Senior UNIX Admin
O'Leary Computers Inc
linkedin: http://www.linkedin.com/dkoleary
Resume: http://www.olearycomputers.com/resume.html
inventsekar_1
Respected Contributor

Re: Working with date using shell scripts

Clay,
i remember that, in a particular year(around 1800 i think), they suddenly changed to a new year format.. so some 10 days missed in that particular month..
is ur script considers that also..?
Be Tomorrow, Today.
A. Clay Stephenson
Acclaimed Contributor

Re: Working with date using shell scripts

The year was 1752 (at least for some countries and their colonies). Do a "cal 9 1752". and no, caljd.sh, does not take that into account. It calculates proleptic dates -- meaning it always calculates dates in Gregorian even for dates before the Gregorian calendar was adopted. Note that the calendar wasn't even universally adopted for many years so the proleptic model makes as much sense as any other.

One of the contributory reasons for Napoleon's victory at Austerlitz in 1805 was that the Russians were still using the Julian calendar while the Austrians and English were using the Gregorian calendar so that when the allies agreed to converge their forces they agreed on a date that was nevertheless a different day.

The "short" September in 1752 also caused riots (particularly in Ireland) when the landlords showed up to collect the rent. The landlords were justly saying the a new month was here and the tenets were justly saying that they were cheated out of two weeks --- and both were right and wrong.
If it ain't broke, I can fix that.
Anoop P_2
Regular Advisor

Re: Working with date using shell scripts

Hi.

Thanks for all who responded. A.Clay's solution helped me through, but I had problems to get the date in the format "DD-MMM-YY" Any ways, I wrote some lines on my own to accomplish that.

Clay, The script is good, but I have one suggestion to standardize it like many other HPUX commands. The script returns the Julian day if executed with out any option, but I expected it to return the usage. To know how to use it, I had to give lots of wrong arguments :) until today when I saw that it had the -u option. Thats just a suggestion.

Great script; hats off to you.

Have assigned points as usual.

Cheers!
Anoop
Steven E. Protter
Exalted Contributor

Re: Working with date using shell scripts

Shalom Anoop,

I've ported A. Clay's script to Linux (1 line fix, change the shell). I had to do a fair amount of testing on it because its a production script on all my Linux/HP-UX server.

I think there is a way to use it to exactly get your date format. Otherwise maybe he'll upgrade it.

What do you mean by standardize like other HP-UX commands? Its not part of the OS, its an add in. Maybe A. Clay should sell it to HP as part of the OS?

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Anoop P_2
Regular Advisor

Re: Working with date using shell scripts

Hi SEP,

I will reframe it this way. "Standardize like other Unix commands"

Unix commands which need an argument to function normally, output the usage syntax, when ever no argument is passed with the command. A usual behavour.

I was suggesting to have this feature here too. I dont insist.

Thanks!
Anoop
Arturo Galbiati
Esteemed Contributor

Re: Working with date using shell scripts

I'm using the mktime SW and it's great!
http://fresh.t-systems-sfr.com/unix/src/privat2/mktime-2.3.tar.gz/

To obtain your date:
mktime -F %y%m%d -d+10

HTH,
Art
A. Clay Stephenson
Acclaimed Contributor

Re: Working with date using shell scripts

First to address your suggestion for an improvement. The "standard" UNIX command behavior is to output and error message when INCORRECT arguments are used NOT when no arguments are supplied -- as long as that is a legal condition for a given command. Do ls, cat, bc, or date output a usage message when supplied with no arguments? Caljd.sh's behavior changes with the number of arguments left after all options have been stripped: if 0, output today's Julian Day; if 1) convert a Julian Day into a Gregorian date; if 3) convert a Gregorian Date into a Julian Day. The very behavior you are complaining about is what makes the command so flexible and reversible. It's also fairly standard across most flavors of UNIX to use command -? or command -u or command -h to output help on stderr. -h is already a meaningful argument (it says skip past holidays) but either -u or -? will generate a usage message. Your request for an "improvement" is rejected but since it is a script feel free to "fix" it as much as you like.

Now there was a subtle bug when combining outputting month names with a non-whitespace separator but this is fixed in the version, 2.27, attached here.

To get 10 days from today in DD-MMM-YYYY format the solution is:

TODAYPLUS10=$(caljd.sh -e -S "-" -o $(caljd.sh -n 10))
echo "10 days from now is ${TODAYPLUS10}"




If it ain't broke, I can fix that.
Anoop P_2
Regular Advisor

Re: Working with date using shell scripts

Clay,

I think you are right. Thank you for the script and the explanation.

Anoop