Solved

Korn Shell Newbie Scripter: Date arithmetic

Posted on 1999-01-05
9
438 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MFC Dialog 9 49
Turn a spreadsheet into a vba executable. 2 85
matchUp  challenge 6 60
JQuery serialize and unserialize 8 78
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

776 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