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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date difference in HP-UX shell

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Is there any way of finding the differemce of two dates in number of days or Hours.

Basically I will have a date in ascii format and need to find the difference from current date.

 
This would be a difficult parsing task in a shell script - I would suggest doing it in C, which has a number of routines to deal with dates. It would be a fairly small program...

#include <time.h>

int main(int argc, char *argv[])
{
struct tm dateIn;
time_t now = time((time_t *)NULL);
time_t then;
double diff;
char * OK;

/* formats: %a day
%b month
%d day of month
%T hour:min:sec
%Z time zone
%Y full year
*/
OK = strptime(argv[1], &quot;%a %b %d %T %Z %Y&quot;, &dateIn);
if(OK)
{
then = mktime(&dateIn);
diff = difftime(now, then);
printf(&quot;difference between now and then = %f seconds\n&quot;, diff);
}
else
{
printf(&quot;error...\n&quot;);
}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top