Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Number of days since ..

Status
Not open for further replies.

stephnane

Technical User
Jun 25, 2004
34
FR
Hi.

I need an AIX (ksh) script or command to know the number of days since a date(1th og 970 for example)? This date has no importance for me.

I just need to know haw many days there are between two dates.

Tks.

Stephnane
 
Not as easy.

Convert from start date from Gregorian Time to Julian time.
Convert the end date from Gregorian Time to Julian time.
Get the difference between end and start (end - start)
Convert result to Gregorian Time again.


Good luck :)
 
stephnane,

Two solutions. Check out the site for date routines, a single line PERL solution.

use Date::Calc qw(Add_Delta_DHMS);
($year2, $month2, $day2, $h2, $m2, $s2) =
Add_Delta_DHMS( $year, $month, $day, $hour, $minute, $second,
$days_offset, $hour_offset, $minute_offset, $second_offset );

or use epoch seconds, again thru perl or a simple C program.

#include <stdio.h>
#include <time.h>
#include <sys/time.h>

/* converting epoch seconds to a date */

main (int argc, char *argv[])
{
int epoch;
epoch = atoi(argv[1]);

printf("%d %s",epoch,ctime(&epoch));
}
gvasrcsm01(lucm02a):/home/lucm02a/C $ cat epochtimenow.c
#include <stdio.h>
#include <time.h>
#include <sys/time.h>

/* giving you todays date in epoch seconds */

main (int argc, char *argv[])
{
int epoch;
time((int *)&epoch);
printf("%d\n",epoch);
}
 
Hi,
try the function ToJul below

!/bin/ksh
# KornShell implementation of Julian Day functions

# ToJul and FromJul functions based on C routines presented by
# David Burki, The C Users Journal, Vol. 11 , No 2, February, 1993,
# page 30.
#
# Burki's algorithm was an adaptation of the FORTRAN code used to
# implement the algorithm presented by H. Fliegl and T. Van Flanders,
# Communications of the ACM, Vol. 11, No. 10, October, 1968, page 657.


function ToJul #month day year
{
# ToJul prints the Julian Day value for the date passed

integer lmonth=${1}
integer lday=${2}
integer lyear=${3}

# set to defaults if null
if [[ ${lmonth}"" -eq "" ]]
then
lmonth=$(date +"%m")
fi

if [[ ${lday}"" -eq "" ]]
then
lday=$(date +"%d")
fi

if [[ ${lyear}"" -eq "" ]]
then
lyear=$(date +"%Y")
fi


jul_day=$(( lday - 32075 + 1461 *
(lyear + 4800 + (lmonth - 14) /12) /
4 + 367 * (lmonth - 2 - (lmonth -14) /
12 * 12 ) / 12 - 3 * ((lyear + 4900 +
(lmonth - 14) / 12 ) / 100) /4 ))

echo ${jul_day}
}


function FromJul #julian_day
{
# FromJul prints the date represented by the passed Julian Day value in the
# form "month day year"

integer t1
integer t2
integer yr
integer mo

integer jul_date=${1}
integer month
integer day
integer year

t1=$((jul_date + 68569))
t2=$(( 4 * t1 / 146097 ))
t1=$(( t1 - (146097 * t2 + 3)/4))
yr=$(( 4000 * ( t1 + 1) /1461001 ))
t1=$(( t1 - 1461 * yr /4 + 31 ))
mo=$(( 80 * t1 / 2447 ))
day=$(( t1 - 2447 * mo / 80 ))
t1=$(( mo / 11 ))
month=$(( mo + 2 - 12 * t1 ))
year=$(( 100 * (t2 - 49) + yr + t1 ))
echo "${month} ${day} ${year}"
}

function DayOfWeek #julian_day
{
integer julian_day=${1}
echo $(( julian_day % 7 ))
}

#-------------------
today=$(ToJul `date +"%m %e %Y"`)
(( yesterday = today -1 ))
set `FromJul $yesterday`
month=$1
day=$2
year=$3
echo "$day/$month/$year"


Ali
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top