The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Operating Systems > 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-18-2008 02:19 AM
Alternative of format date in HP unix epall UNIX for Dummies Questions & Answers 2 09-30-2007 01:56 AM
Using awk (or an alternative) michaeltravisuk Shell Programming and Scripting 5 03-08-2007 11:37 PM
.NET Alternative goon12 UNIX for Dummies Questions & Answers 3 04-06-2005 12:07 PM
cut -f (or awk alternative) gefa Shell Programming and Scripting 6 03-02-2005 01:26 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-26-2008
Data469 Data469 is offline
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.
  #2 (permalink)  
Old 03-26-2008
Cameron's Avatar
Cameron Cameron is offline Forum Advisor  
Registered User
  
 

Join Date: Nov 2001
Location: Brisbane, Australia
Posts: 499
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 ??
  #3 (permalink)  
Old 03-26-2008
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,111
See: HP-UX & need help with time
  #4 (permalink)  
Old 03-26-2008
keelba keelba is offline
Registered User
  
 

Join Date: Apr 2002
Location: Dallas, TX
Posts: 57
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.
  #5 (permalink)  
Old 03-26-2008
Data469 Data469 is offline
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.
  #6 (permalink)  
Old 03-31-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,717
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
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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 12:29 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-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0