Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
.
google site



UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD.

Closed Thread
English Japanese Spanish French German Portuguese Italian Powered by Powered by Google
 
Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-12-2005
Registered User
 

Join Date: Apr 2005
Posts: 1
Unhappy date difference

Hi...I need some help with a date script. I need to allow the user to enter the month (alpha) day (int) and year (YYYY) and count the difference in number of days since Jan 1, 1952 to the users date. I've been messing with this for about 10 hours and I think I'm just making the script worse =(

Please help

#!/bin/bash
# grpdif: Number of days between two dates.
# Usage: ./grpdif 01/01/2001
# [M]M/[D]D/YYYY
ARGS=1 # Two command line parameters expected.
E_PARAM_ERR=65 # Param error.
REFYR=1940 # Reference year.
REFMO=1 # Reference month.
REFDY=1 # Reference day.
CENTURY=100
DIY=365
ADJ_DIY=367 # Adjusted for leap year + fraction.
MIY=12
DIM=31
LEAPCYCLE=4
diff= # Declare global variable for date difference.
value= # Declare global variable for absolute value.
day= # Declare globals for day, month, year.
month=
year=
MAXRETVAL=255 # Largest permissable
#+ positive return value from a function.

Param_Error () # Command line parameters wrong.
{
echo "Usage: `basename $0` [M]M/[D]D/YYYY"
exit $E_PARAM_ERR
}


Parse_Date () # Parse date from command line params.
{
month=${1%%/**}
dm=${1%/**} # Day and month.
day=${dm#*/}
let "year = `basename $1`"
}

check_date () # Checks for invalid date(s) passed.
{
[ "$day" -gt "$DIM" ] || [ "$month" -gt "$MIY" ] || [ "$year" -lt "$REFYR" ] && Param_Error
# Exit script on bad value(s).
# Uses "or-list / and-list".
#
let "year=$REFYR"
let “month=$REFMO”
let “day=$REFDY”
let "indexyr = $year / $CENTURY"
let "Days = $DIY*$year + $year/$LEAPCYCLE - $indexyr + $indexyr/ $LEAPCYCLE + $ADJ_DIY*$month/$MIY + $day - $DIM"
echo $Days
}

day_index ()
{ # Days from Jan. 1, 1952 to date passed as param.

day=$1
month=$2
year=$3

let "month = $month - 2"
if [ "$month" -le 0 ]
then
let "month += 12"
let "year -= 1"
fi

let "year -= $REFYR"
let "month -=$REFMO"
let "day -=$REFDY"
let "indexyr = $year / $CENTURY"
let "Days = $DIY*$year + $year/$LEAPCYCLE - $indexyr + $indexyr/$LEAPCYCLE + $ADJ_DIY*$month/$MIY + $day - $DIM"
echo $Days
}

calculate_difference () # Difference between to day indices.
{
let "diff = $1 - $2" # Global variable.
}

if [ $# -ne "$ARGS" ] # Require two command line params.
then
Param_Error
fi

Parse_Date $1
check_date $day $month $year # See if valid date.
day=$? # on day and/or month.
month=$?
let "date1 = `day_index $day $month $year`"


Parse_Date $2
check_date $day $month $year
day=$?
month=$?
let "date2 = $(day_index $day $month $year)"


calculate_difference $date1 $date2

diff=$value
echo $diff
exit 0
Sponsored Links
  #2 (permalink)  
Old 04-12-2005
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,150
See Yesterdays Date/Date Arithmetic
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Date Difference in PERL usshell Shell Programming and Scripting 1 05-02-2008 11:57 AM
date difference muay_tb Linux 14 03-18-2008 10:08 AM
Weird date difference problem meeraKh Shell Programming and Scripting 22 03-12-2008 11:50 PM
date difference piyush_movadiya Shell Programming and Scripting 2 08-11-2005 05:10 AM
date difference rajuMBT UNIX for Dummies Questions & Answers 1 08-10-2005 06:11 AM



All times are GMT -4. The time now is 06:01 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2010. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0