Solved

Korn Shell Newbie Scripter: Date arithmetic

Posted on 1999-01-05
9
429 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
Comment Utility
#!/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
Comment Utility
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
Comment Utility
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
 
LVL 84

Expert Comment

by:ozo
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 51

Expert Comment

by:ahoffmann
Comment Utility
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
Comment Utility
#!/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
Comment Utility
> #   1900 < Y < 2100
1900 was not a leap year
0
 
LVL 3

Expert Comment

by:mliberi
Comment Utility
in fact I wrote 1900 < Y (not 1900 <= Y)

0
 

Author Comment

by:calliope
Comment Utility
Close enough.  Thanks!
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

763 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now