Shell Scripts and Date Question

Shell Scripts and Date Question

Post by Joe Bottom » Sat, 05 Jun 1999 04:00:00



Hi All,

I have been tasked twice in several months to write sh or ksh scripts
to get ranges of dates.

The first dealt with all dates in a previous month, the second with
dates in either the current month, or a previous month...

In each I ended up having to handle Jan 1, 19XX, or SomeMonth 1, 19XX
with hacks like parsing out returns from cal, using expr or ksh math
built-ins and etc...

Is there a better way!?

Looking for a utility, bundled with Solaris 2.3 => 7 that given a day
(1-end_of_month), month (1-12) year (NNNN) and a value (example 7 for
one week), would return the correct day month year.

Any input would be appreciated...

 
 
 

Shell Scripts and Date Question

Post by JGM_van_Ra » Sat, 05 Jun 1999 04:00:00


Hi

You mean this

       date '+%d %h %Y Day=%w Week=%W'

Output is      04 Jun 1999 Day=5 Week=22

Use man date . It will give you more data outputs for you.
Good luck
          Jos van Ras

*** Philips Medical Systems Netherlands
*** System Dev. Cardio/Vascular
*** Hardware, Librarian & Support Unix
*** The Netherlands

*** Homepage:http://www.jgmvanras.demon.nl/

Quote:> Looking for a utility, bundled with Solaris 2.3 => 7 that given a day
> (1-end_of_month), month (1-12) year (NNNN) and a value (example 7 for
> one week), would return the correct day month year.

> Any input would be appreciated...


 
 
 

Shell Scripts and Date Question

Post by Joe Bottom » Sat, 05 Jun 1999 04:00:00


No like this...  Say I have script that uses a date range for
auditing or some other purpose.

Say today is March 1 2000.  I invoke the utility asking for
the last day of the previous month and it returns February 29 2000.

You see my problem.  I know the current date.  How do I get
the previous date which is in the former month?  How do I do
it so that no matter what I get the correct date, even if it's
a leap year, like above?

Like I said, I have already done this by passing cal a month
year value, parsing the return, and acting on the values I capture
accordingly.  It's one big HACK...  

I need something that operates from sh or ksh only, PERL's date module
which would be perfect for such a task can't be used.  I can't write
a compiled program either...


> Hi

> You mean this

>        date '+%d %h %Y Day=%w Week=%W'

> Output is      04 Jun 1999 Day=5 Week=22

> Use man date . It will give you more data outputs for you.
> Good luck
>           Jos van Ras

> *** Philips Medical Systems Netherlands
> *** System Dev. Cardio/Vascular
> *** Hardware, Librarian & Support Unix
> *** The Netherlands

> *** Homepage:http://www.jgmvanras.demon.nl/

> > Looking for a utility, bundled with Solaris 2.3 => 7 that given a day
> > (1-end_of_month), month (1-12) year (NNNN) and a value (example 7 for
> > one week), would return the correct day month year.

> > Any input would be appreciated...

 
 
 

Shell Scripts and Date Question

Post by justi » Sun, 06 Jun 1999 04:00:00


hey joe,
i had a similar problem re date arithmetic which was instantly solved by d/l
gnu's date (in sh_utils) - it lets you specify date -- 2 days ago or date --
1 month etc (syntax not precise)
so now my script works out todays day and then subtracts that to get the date
of last sunday, for instance

hth
jc


> No like this...  Say I have script that uses a date range for
> auditing or some other purpose.

> Say today is March 1 2000.  I invoke the utility asking for
> the last day of the previous month and it returns February 29 2000.
> ....

 
 
 

Shell Scripts and Date Question

Post by Peter Sundstro » Wed, 09 Jun 1999 04:00:00



>No like this...  Say I have script that uses a date range for
>auditing or some other purpose.

>Say today is March 1 2000.  I invoke the utility asking for
>the last day of the previous month and it returns February 29 2000.

>You see my problem.  I know the current date.  How do I get
>the previous date which is in the former month?  How do I do
>it so that no matter what I get the correct date, even if it's
>a leap year, like above?

>Like I said, I have already done this by passing cal a month
>year value, parsing the return, and acting on the values I capture
>accordingly.  It's one big HACK...

>I need something that operates from sh or ksh only, PERL's date module
>which would be perfect for such a task can't be used.  I can't write
>a compiled program either...

Do you mean you can't use a binary?  Pity if that's the case because what
you need is GNU date.  It allows you to all sorts of date/time
manipulations, eg:

date --date '2 days ago' "+%Y%m%d"

 
 
 

Shell Scripts and Date Question

Post by Hwang Gwo-Ju » Wed, 09 Jun 1999 04:00:00



> Hi All,

> I have been tasked twice in several months to write sh or ksh scripts
> to get ranges of dates.

> The first dealt with all dates in a previous month, the second with
> dates in either the current month, or a previous month...

> In each I ended up having to handle Jan 1, 19XX, or SomeMonth 1, 19XX
> with hacks like parsing out returns from cal, using expr or ksh math
> built-ins and etc...

> Is there a better way!?

> Looking for a utility, bundled with Solaris 2.3 => 7 that given a day
> (1-end_of_month), month (1-12) year (NNNN) and a value (example 7 for
> one week), would return the correct day month year.

> Any input would be appreciated...

Hi,

I had a similar problem before, so I worte the following script to solve my
problem.
First, save the script as a file, says "Dday".
Then, you can run the script in four ways:
1. no argument, to show yesterday
        $ Dday
         1999 06 07
2. 3 arguments, to show the date before the designate date.
        $ Dday 2000 1 1
        1999 12 31
3. 1 argument(either positive or negative value), to show the date of n
days
    difference from the current date
        $ Dday -500
        1998 01 24
        $Dday 207
        2000 01 01
4. 4 argument, to show the date of n days difference from the designate
date
        $Dday 2000 3 1 -1
        2000 02 29

Quote:> I need something that operates from sh or ksh only, PERL's date module
> which would be perfect for such a task can't be used.  I can't write
> a compiled program either...

I use a AWK script inside the 'Dday'. But, it doesn't matter, you can
easily
run it in Solaris. If you do need a Perl version, please send E-mail to me.


--
Gwo-Juh

================Cut Here=================

# Usage: Dday  [yyyy mm dd] [{+|-}ndays]
if [ $# -eq 0 ]
then
    Y=`date "+%Y"`
    M=`date "+%m"`
    D=`date "+%d"`
    nb=-1
elif [ $# -eq 1 ]
then
    Y=`date "+%Y"`;
    M=`date "+%m"`;
    D=`date "+%d"`;
    nb=$1;
elif [ $# -eq 3 ]
then
    Y=$1; M=$2; D=$3; nb=-1;
else
    Y=$1; M=$2; D=$3; nb=$4;
fi
nawk -v TYear=$Y -v TMnth=$M -v TDay=$D -v NBefore=$nb '
function IsLeap(yy)
{
    if ((yy%400)==0) {return 1;}
    if ((yy%100)==0) {return 0;}
    if ((yy%4)==0)   {return 1;}
    return 0;

Quote:}

BEGIN {
    Mds="31:28:31:30:31:30:31:31:30:31:30:31";
    split(Mds,MDA,":");
    # Find out the day of yesterday.
    Year = TYear+0;
    Mnth = TMnth+0;
    Day = TDay+0;
    Day += NBefore;
    while( (Day <= 0) || (Day > MDA[Mnth]) ) {
        if(Day <= 0) {
            Mnth -= 1;
            if(Mnth == 0) {
                Mnth = 12;
                Year--;
                MDA[2] = IsLeap(Year) ? 29:28;
            } else if( Mnth == 2 && IsLeap(Year)) {
                MDA[2]=29;
            }
            Day += MDA[Mnth];
            } else if(Day > MDA[Mnth] ) {
            if( Mnth == 2 && IsLeap(Year)) {
                MDA[2]=29;
            }
            Day -= MDA[Mnth];
            Mnth += 1;
            if(Mnth == 13 ) {
                Mnth = 1;
                Year++;
                MDA[2] = IsLeap(Year) ? 29:28;
            }
        }
    }
    #print Mnth,Day,Hour
    printf "%04d %02d %02d \n",Year,Mnth,Day;

Quote:} ' -

===================Cut Here============

  gwojuh.vcf
< 1K Download
 
 
 

Shell Scripts and Date Question

Post by brian hile » Thu, 10 Jun 1999 04:00:00



Quote:> I have been tasked twice in several months to write sh or ksh scripts
> to get ranges of dates.

Does it have to be k/sh? The advantage is a variant of the KISS
principle ("keep it simple, stupid!") -- what you don't need to
code costs you nothing, is code you don't need to debug or maintain,
and works NOW.

I have written a function library of time-date manipulation functions using
well known if extremely arcane algorithms developed by mathematicians
much better than I (they involve finding the Modified Julian Numbers of
two dates and subtracting.) Unfortuneately ksh93 is required with it's
implementation of floating point calculations, which I'll bet you don't
have.

http://www.magnet.ch/serendipity/hermetic/cal_stud/jdn.htm
http://www.pip.dknet.dk/~pip10160/calendar.html

Why don't you try external tools that are freely available?

http://www.interlog.com/~john/technogeek/source/mktime/

-Brian

 
 
 

1. Date conversion in Bourne shell script question

Hi,

I am writing an account audit script which will need to calculate
number of days between today and some date in the past represented by
standards date string, something like: Wed Feb  4 13:17:52 EST 2004.
I need a function to convert in to UNIX epoch time so I can calculate
a difference and convert it back to number of days.
I am limiting myself to Bourne shell and awk, because I want the same
script to run on HP-UX, Solaris and AIX.

I am sure it is not a new problem, but I cannot find any references on
that.
I would be greatfull if you can point me in the right direction.

Thanks in advance

2. Solaris 2.6 rlogin replacement

3. shell script within shell script (general question)

4. bash scripting question

5. Shell script for future 'date'?

6. Multiple AIX boot configurations on AIX 4.1.5

7. Date setting problem on shell script?

8. slightly advanced tput terminal cursor movement question

9. A Solaris 8 (Bourne Shell) Scripting question, regarding a log rotation script

10. Date arithmetic in C-Shell scripts?

11. Using DATE in shell script ??????

12. extract part of date and use in a shell script

13. Get file date from c shell scripts