The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > OS Specific Forums > HP-UX
Google UNIX.COM


HP-UX HP-UX (Hewlett Packard UniX) is Hewlett-Packard's proprietary implementation of the Unix operating system, based on System V.

More UNIX and Linux Forum Topics You Might Find Helpful
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

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-25-2008
Registered User
 

Join Date: Feb 2008
Posts: 7
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.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 03-25-2008
Cameron's Avatar
Registered User
 

Join Date: Nov 2001
Location: Brisbane, Australia
Posts: 486
If 'date +%s' provides you the result you need, why search for an alternative ??

Sorry - curious to know why you'd seek an alternative. BTW - What OS are you using ??
Reply With Quote
  #3 (permalink)  
Old 03-26-2008
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,453
See: HP-UX & need help with time
Reply With Quote
  #4 (permalink)  
Old 03-26-2008
Registered User
 

Join Date: Apr 2002
Location: Dallas, TX
Posts: 53
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.
Reply With Quote
  #5 (permalink)  
Old 03-26-2008
Registered User
 

Join Date: Feb 2008
Posts: 7
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.
Reply With Quote
  #6 (permalink)  
Old 03-31-2008
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 3,859
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
output:
Code:
csadev:/home/jmcnama> ddiff.shl
63
Reply With Quote
  #7 (permalink)  
Old 03-31-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
Install the GNU core utilities, you will thank yourself a million times for that.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 01:34 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0