Solved

Korn Shell Newbie Scripter: Date arithmetic

Posted on 1999-01-05
9
440 Views
Last Modified: 2013-12-26
I am writing a korn shell script that takes in the date in the form of YYYY MM DD as parameters.  I need to do a check so that the date entered is no more than three days before the current date, or no later than the current date.  

The minimum information I need is how to calculate the date of three days before today, and then format it to '*%Y %m %d' and compare it to the input.

My ideal situation would be for someone out there to write a test that takes the input, makes sure it falls within the boundaries, and ensures that the input is formatted to YYYY MM DD.

Thanks!
0
Comment
Question by:calliope
  • 3
  • 2
  • 2
  • +1
9 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 1295170
#!/bin/ksh
threedaysago=`perl -e '($y,$m,$d)=(localtime(time-3*24*60*60))[5,4,3];$y+=1900;$m+=1;printf"%04d %02d %02d",$y,$m,$d'`
today=`date +"%Y %m %d`
if [[ "$1" < "$threedaysago" || "$today" < "$1" ]] ;then
  echo bad date $1
else
  echo good date $1
fi
0
 

Author Comment

by:calliope
ID: 1295171
That works -- thanks ozo!

I'm just a little bit uncomfortable about introducing perl into the script, because if the script gets migrated somewhere where perl is not installed, it will break.  I'm increasing the value
0
 
LVL 84

Expert Comment

by:ozo
ID: 1295172
A way that works wihout perl on some systems is to set your TZ offset 72 hour ahead,
but that's not portable to systems that don't allow large timezone offsets
Or, there's a big messy numerical computation you could do purely in ksh, which I can post if you're interested
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 84

Expert Comment

by:ozo
ID: 1295173
this was a Bourne shell script from Q.10029753, but it works in ksh too:

dy=`date +%j`
yy=`date +%Y`
y1=`expr $yy - 1`
dd=`expr $dy + $y1 \* 365 + $y1 / 4 - $y1 / 100 + $y1 / 400`
d=`expr $dd - 3`
y1=`expr $d \* 400 / 146097`
y=`expr $y1 + 1`
d=`expr $d - $y1 \* 365 - $y1 / 4 + $y1 / 100 - $y1 / 400`
l=`expr \( $y % 4 \| $y % 100 = 0 \& $y % 400 \) = 0`
d=`expr $d + \( $d \> 59 + $l \) \* \( 2 - $l \)`
m=`expr \( $d + 183 \) \* 12 / 367`
d=`expr $d + 183 - $m \* 367 / 12`
m=`expr \( $m + 6 \) % 12 + 1`
echo $y $m $d
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 1295174
you may fiddle around with:

   env TZ=GMT+3 date
   env TZ=GMT-3 date

But , as ozo said, it may not work on all UNIX flaviours; most will reject to do it switching for or back over new year (which will be handled by ozo's script).
Short solution may be worth a try ..
0
 
LVL 3

Accepted Solution

by:
mliberi earned 80 total points
ID: 1295175
#!/bin/ksh

function days
# number of days in a given month
# usage days Y M
#   1900 < Y < 2100
#      1 <= M <= 12
{
  integer A=$1 M=$2
  case $2 in
    4|6|9|11)        echo 30;;
    1|3|5|7|8|10|12) echo 31;;
    2) if ((A%4))
         then
           echo 28
         else
           echo 29
       fi
  esac
}

function Ndays
# the number of days beetween two dates
# usage Ndays Y1 M1 D1 Y2 M2 D2
{
  integer Y1=$1 M1=$2 D1=$3 Y2=$4 M2=$5 D2=$6 N=0 I

  if ((Y1>Y2||Y1==Y2&&M1>M2||Y1==Y2&&M1==M2&&D1>D2))
    then
      let I=Y1; let Y1=Y2; let Y2=I
      let I=M1; let M1=M2; let M2=I
      let I=D1; let D1=D2; let D2=I
      echo "-\c"
  fi

  let I=Y1
  while ((I<Y2))
  do
    let N+=337+$(days $I 2)
    let I+=1
  done
  let I=1
  while ((I<M1))
  do
    let D1+=$(days $Y1 $I)
    let I+=1
  done
  let I=1
  while ((I<M2))
  do
    let D2+=$(days $Y2 $I)
    let I+=1
  done
  echo $((N+D2-D1))
}

# ------------------------------------------------- main
integer nowYYYY nowMM nowDD YYYY=$1 MM=$2 DD=$3 delta
date +"%Y %m %d" | read nowYYYY nowMM nowDD

let delta=$(Ndays $YYYY $MM $DD $nowYYYY $nowMM $nowDD)
if ((delta<3 || delta>0))
  then
    echo bad date
  else
    echo good
fi
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 1295176
> #   1900 < Y < 2100
1900 was not a leap year
0
 
LVL 3

Expert Comment

by:mliberi
ID: 1295177
in fact I wrote 1900 < Y (not 1900 <= Y)

0
 

Author Comment

by:calliope
ID: 1295178
Close enough.  Thanks!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question