![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| HP-UX HP-UX (Hewlett Packard UniX) is Hewlett-Packard's proprietary implementation of the Unix operating system, based on System V. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| help with while loop or any other alternative? | filthymonk | Shell Programming and Scripting | 3 | 04-17-2008 11:19 PM |
| Alternative of format date in HP unix | epall | UNIX for Dummies Questions & Answers | 2 | 09-29-2007 10:56 PM |
| Using awk (or an alternative) | michaeltravisuk | Shell Programming and Scripting | 5 | 03-08-2007 07:37 PM |
| .NET Alternative | goon12 | UNIX for Dummies Questions & Answers | 3 | 04-06-2005 09:07 AM |
| cut -f (or awk alternative) | gefa | Shell Programming and Scripting | 6 | 03-02-2005 09:26 AM |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
|||
|
Alternative to date +%s
Hi,
My version of HP-UX does not support "date +%s" to find the number of seconds elapsed since 01/01/1970. It would be great if anyone can give some simpler ideas for achieving the same. Thanks. |
| Forum Sponsor | ||
|
|
|
|||
|
You could try installing GNU's date command. It has a %s option and some other really cool features that once you install you'll want to keep forever. To get it, install GNU's coreutils on a test server and then copy just the date command over. I usually rename it to gdate so that HP's date program is still intact.
|
|
|||
|
I use HP-UX and it does not support "date +%s".
My basic requirement is that I need to find the difference (number of days) between two dates. My idea was to convert the dates to seconds. date +%s -d <date> gives the number of seconds elapsed since 01/01/1970 to the current date. Thus I can get both the dates in seconds & then find difference and then convert it back to days. But the problem here that as I said earlier, HP-UX does not support date +%s. And I need this to be strictly in ksh and not in perl. Any help on this will be highly appreciated. Thanks. |
|
|||
|
You really should look in the FAQ forum here under Date Arithmetic - see the datecalc script
try something like this: Code:
#!/bin/ksh
# returns 1 if leap year, Gregorian
# usage: isleapyr <year>
isleapyr()
{
year=$1
if [[ $(( $year % 4 )) -eq 0 && $(( $year % 100 )) -ne 0 \
|| $(( $year % 400 )) -eq 0 ]] ; then
echo 1
else
echo 0
fi
}
# days_in_year number of days in full years
# since Dec 31 1970 midnight
#
# usage: days_in_years <year>
days_in_years()
{
start=1970
total=0
year=$1
while [[ $start -lt $year ]]
do
days=365
if [[ $(( $start % 4 )) -eq 0 ]] ; then
if [[ $( isleapyr $start ) -eq 1 ]] ; then
days=366
fi
fi
start=$(( $start + 1 ))
total=$(( $total + $days ))
done
echo $total
}
# days_in_months: return days in months
# usage: <month_number> <year>
days_in_months()
{
month=$(( $1 - 1 ))
start=0
result=0
if [[ $( isleapyr $2 ) -eq 1 ]] ; then
set -A arr 31 29 31 30 31 30 31 31 30 31 30 31
else
set -A arr 31 28 31 30 31 30 31 31 30 31 30 31
fi
while [[ $start -lt $month ]]
do
result=$(( $result + ${arr[start]} ))
start=$(( $start + 1 ))
done
echo $result
}
# y_total total days after epoch start midnight Dec 31 1969
# usage <day of month> <month> <year>
y_total()
{
totaly=$( days_in_years $3 )
totalm=$( days_in_months $2 $3 )
echo $(( $totaly + $totalm + $1 ))
}
# date_diff
#
# usage: date_diff <dd/mm/yyyy> <dd/mm/yyyy>
# difference in days: date range from 01/01/1970 -> on
# return +/- value or zero
date_diff()
{
if [[ "$1" = "$2" ]] ; then
echo 0
return
fi
echo "$1" | awk -F'/' '{print $1 +0, $2 +0, $3} ' | read d1 m1 y1
days1=$( y_total $d1 $m1 $y1 )
echo "$2" | awk -F'/' '{print $1 +0, $2 +0, $3} ' | read d1 m1 y1
days2=$( y_total $d1 $m1 $y1 )
echo "$(( $days2 - $days1))"
}
# example
date_diff 31/12/2007 03/03/2008
Code:
csadev:/home/jmcnama> ddiff.shl 63 |