Link to home
Start Free TrialLog in
Avatar of raza
razaFlag for United States of America

asked on

date calculation


I am trying to get diffrance between in two dates.

Start Date/Time :  yyyy-mm-dd hh:mm:ss.t

End Date/Time :   yyyy-mm-dd hh:mm:ss.t


What is the best way to calculate diffrance in seconds or minutes?.

Avatar of jcaldwel
jcaldwel


In time.h...
    extern double       difftime(time_t, time_t);
Avatar of raza

ASKER


I have string value in yyyy-mm-dd hh:mm:ss.t format

how do I use time_t for the input of difftime?
Fill your secs, mins, hours, days etc into a tm struct (also in time.h), then call mktime on it, which returns the time_t you want for difftime...
Avatar of raza

ASKER


The following is a code I am trying to get it work. I am always getting 0 for difftime;



static int DiffDateTime(
     char *EndDateTime,
     char *StartDateTime
     )
{
     /*
      * Input DateTime format
      * y y y y - m m - d d   h h : m m : s s.t
      * 1 2 3 4 5 6 7 8 9 0 1 1 2 3 4 5 6 7 8
      */

     char StringVal[32];

     struct tm Start;
     struct tm End;

     PARS_mid(StartDateTime,0,4,StringVal);
     Start.tm_year = atoi(StringVal);
     
     PARS_mid(StartDateTime,5,2,StringVal);
     Start.tm_mon  = atoi(StringVal);
     
     PARS_mid(StartDateTime,8,2,StringVal);
     Start.tm_mday  = atoi(StringVal);

     PARS_mid(StartDateTime,11,2,StringVal);
     Start.tm_hour  = atoi(StringVal);
     
     PARS_mid(StartDateTime,14,2,StringVal);
     Start.tm_min  = atoi(StringVal);

     PARS_mid(StartDateTime,17,2,StringVal);
     Start.tm_sec  = atoi(StringVal);
     

     PARS_mid(EndDateTime,0,4,StringVal);
     End.tm_year  = atoi(StringVal);
     
     PARS_mid(EndDateTime,5,2,StringVal);
     End.tm_mon  = atoi(StringVal);
     
     PARS_mid(EndDateTime,8,2,StringVal);
     End.tm_mday  = atoi(StringVal);

     PARS_mid(EndDateTime,11,2,StringVal);
     End.tm_hour  = atoi(StringVal);
     
     PARS_mid(EndDateTime,14,2,StringVal);
     End.tm_min  = atoi(StringVal);

     PARS_mid(EndDateTime,17,2,StringVal);
     End.tm_sec  = atoi(StringVal);
     
     return difftime(mktime(&End),mktime(&Start));

}




static char *PARS_mid (
                            const char *chString, /* String             */
                            int start,      /* Starting Possition */
                            int len,           /* lenth of string    */
                         char       *result
                            )
{
     int i;
     int k;
     int nActualEnd;
     int nActualStart;
     int nIndex;


     if(!strlen(chString))                         /* If There Is No The Length                            */
          return NULL;                              /* Return NULL                                                */

     if ( (unsigned int) start > strlen(chString))               /* Make Sure The Start Is Not More Than The Length */
          return NULL;                              /* If It Is Return Null                                      */
     if (start < 0)                                   /* If The Start Is Less Than 0                            */
          nActualStart = 0;                         /* Set The nActualStart To 0                            */
     else          
          nActualStart = start;                    /* Else Set The nActualStart                            */

     if ( (unsigned int) len > strlen(chString))                    /* If The Length Is Longer Than The String             */
          nActualEnd = strlen(chString);          /* Set The nActualEnd To The Length Of The String  */
     else          
          nActualEnd = len;                         /* Else nActualEnd To The Length                       */

     /* Copy The Part We Want From The Orginal String To The Result */
     
     for (i = 0, nIndex = nActualStart, k = 0; i < nActualEnd; i++)    
     {          
          if (chString[nIndex] != '\0')               /* If Not A NULL Char */
               result[k++] = chString[nIndex++];
          else
               break;
     }

     result[k] = '\0';                              /* Add A NULL Char To The End Of The String */

    return result;                                   /* Return The New String */

}
ASKER CERTIFIED SOLUTION
Avatar of constructor
constructor

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ooops, I just did the opposite mix-up of you. The function head should of course read:

static int DiffDateTime(const char *EndDateTime,const char *StartDateTime)

or you should remove the cast to int again.
Avatar of raza

ASKER

Thank you very much... you have been very helpful.