Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Korn Shell Newbie Scripter: Date arithmetic

Posted on 1999-01-05
9
Medium Priority
?
444 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

 
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 240 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

Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

Question has a verified solution.

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

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
In this post we will learn different types of Android Layout and some basics of an Android App.
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.
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

719 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