subtracting Tue, Feb 26, 2008 01:38:25 AM from Mon, Feb 25, 2008 09:30:03 PM


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting subtracting Tue, Feb 26, 2008 01:38:25 AM from Mon, Feb 25, 2008 09:30:03 PM
# 8  
Old 02-27-2008
Hi Jaduks,

I am greatfull for your reply.

But to my unluck the first command
Quote:
date +%s -d "Feb 26, 2008 01:38:25 AM"
is throwing me the following error:
Quote:
date: bad format character - s
Sreejith_VK
# 9  
Old 02-27-2008
Try +%H:%M:%S. See the man pages for more representation formats.
# 10  
Old 02-27-2008
Thanks vino for solving that error.

I am using this modified script and its giving me an unexpected output which is mentioned futher below:

Quote:
#!/bin/sh

D1=`date +%H:%M:%S -d "Feb 26, 2008 01:38:25 AM"`
D2=`date +%H:%M:%S -d "Feb 25, 2008 09:30:03 PM"`
#((diff_sec= expr$D1-$D2))
diff_sec=$D1-$D2
echo - | awk '{printf "%d:%d:%d","'"$diff_sec"'"/(60*60),"'"$diff_sec"'"%(60*60)/60,"'"$diff_sec"'"%
60}'
Output:
Quote:
0:0:15
I was expecting an out put like this:
Quote:
04:08:22
Any clue?
Sreejith_VK
# 11  
Old 02-27-2008
I don't think using the following format, you can get the difference between the two dates.

date +%H:%M:%S -d "Feb 26, 2008 01:38:25 AM"

It just tells the time in hh:mm:ss format, does not care about the date (25th or 26th)

The reason I used %s was to get the epoch seconds of both the dates and then taking the difference in seconds and then converting the seconds to hh:mm:ss format using awk.

Last edited by jaduks; 02-27-2008 at 09:38 AM..
# 12  
Old 02-27-2008
Here is a reasonably general purpose solution based on a small C program.

Code:
/*
** DIFFDATE     F.P.Murphy Feb 26th, 2008
**
** USAGE: diffdate [-d] -f dateformat startdate finishdate
**
**        -d turn on debugging
**        -f format strings per strptime(2) to match inputted start and finish dates
**
** OUTPUT:  Prints difference between startdate and finishdate as HH:MM:SS
**          Handles up to 99 hrs difference as currently written
**
** EXAMPLE: diffdate -f "%a, %b %d, %Y %T %p" "Tue, Feb 19, 2008 08:00:02 PM" "Wed, Feb 20, 2008 02:19:09 AM"
*/

#include <stdio.h>
#include <time.h>
#include <unistd.h>


int
main(int argc, char* argv[])
{
   struct tm tm1, tm2;

   char buf[50];
   char format[50];
   char startdate[80];
   char finishdate[80];

   time_t t1, t2;
   long diff;
   int secs;
   int mins;
   int hours;

   int c;
   extern char *optarg;
   extern int optind, optopt;
   int debug = 0;
   int errflg = 0;

   while ((c = getopt(argc, argv, "df:h")) != -1) {
        switch(c) {
        case 'd':
            debug = 1;
            break;
        case 'f':
            strcpy(format, optarg);
            break;
        case 'h':
            errflg++;
            break;
        case '?':
            fprintf(stderr, "Unknown option: -%c\n", optopt);
            errflg++;
        }
   }
   if (errflg || (argc - optind != 2)) {
       fprintf(stderr, "Usage: diffdate [-d] -f format startdate finishdate\n");
       exit(2);
   }

   strcpy(startdate, argv[optind]);
   optind++;
   strcpy(finishdate, argv[optind]);

   if (debug) {
       fprintf(stderr, "Format: %s\n", format);
       fprintf(stderr, "Start Date: %s\n", startdate);
       fprintf(stderr, "Finish Date: %s\n", finishdate);
   }

   if (!strptime(startdate, format, &tm1)) {
       fprintf(stderr, "strptime() error\n");
       exit(1);
   }
   strftime(buf, 50, "%a %Y %H:%M:%S", &tm1);

   if (!strptime(finishdate, format, &tm2)) {
      fprintf(stderr, "strptime() error\n");
      exit(1);
   }
   strftime(buf, 50, "%a %Y %H:%M:%S", &tm2);

   if (((t1 = mktime(&tm1)) == -1) || ((t2 = mktime(&tm2)) == -1)) {
      fprintf(stderr, "mktime() error\n");
      exit(1);
   }

   diff = t2 - t1;
   if (diff < 0)
      diff = t1 - t2;

   if (debug) {
      printf("Start Date: seconds since Epoch:  %ld\n", (long) t1);
      printf("Finish Date: seconds since Epoch:  %ld\n", (long) t2);
      printf("Difference in seconds: %ld\n", (long) diff);
   }

   secs  = diff % 60;
   mins  = diff % (60 * 60) / 60;
   hours = diff / (60 * 60);

   printf("%02d:%02d:%02d\n", hours, mins, secs);
}

Here is sample output:
Code:
$./diffdate -f "%a, %b %d, %Y %T %p" "Tue, Feb 19, 2008 08:00:02 PM" "Wed, Feb 20, 2008 02:19:09 AM"
06:19:07
$

# 13  
Old 02-27-2008
The problem was there are different implmentations of date. GNU & usually Linux have GNU date which was what version the suggestions using date had. The OP does not seem to have GNU date.

The only real portable solutions are to use perl or C. Since you already have a C solution, you are set.

Last edited by jim mcnamara; 02-27-2008 at 11:54 AM..
# 14  
Old 02-28-2008
Hi Murphy,

I am greatfull to you.
The code written by you is working perfectly.
I could call your c program from my script and get the output pass to another file.

Thanks a lot for solving my problem permanently.
Sreejith_VK
Login or Register to Ask a Question

Previous Thread | Next Thread

2 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk mktime(strftime(format,"6-FEB-2013 08:50:03.841")

I'm trying to use AWK to filter on some dates in a field by converting them to Unix Time. mktime(strftime(format,"6-FEB-2013 08:50:03.841")What is the proper format for my date strings as they appear in my database? My first thought is %d-%b-%Y %H:%M:%Sbut I see the following issues: %d is... (3 Replies)
Discussion started by: Michael Stora
3 Replies

2. UNIX for Dummies Questions & Answers

converting date format: "May 31 2008" to "2008-05-31"

I have the following script to find out the last day of the last month .... and the output of this script is in the following format ... Script goes like this .... #!/bin/ksh cur_month=`date +%m` cur_year=`date +%Y` prev_month=$(($cur_month-1)) # Check to see if this is January if ... (8 Replies)
Discussion started by: santosham
8 Replies
Login or Register to Ask a Question