• Status: Solved
• Priority: Medium
• Security: Public
• Views: 271

# 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?.

0
raza
• 3
• 3
1 Solution

Commented:

In time.h...
extern double       difftime(time_t, time_t);
0

Author Commented:

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

how do I use time_t for the input of difftime?
0

Commented:
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...
0

Author Commented:

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 */

}
0

Commented:
It is good practice to zero out structures before use (since you only specify some of the fields). And the year field in struct tm is counted from 1900, so you have to subtract this number later. Moreover, difftime returns a double, so you have to cast explicitly, or change your function to return a double. Here is how it probably should look:

static double DiffDateTime(const char *EndDateTime,const char *StartDateTime)
{
/* Input format: 'yyyy-mm-dd hh:mm:ss' */
char StringVal[32];
struct tm Start,End;
/* Make start time: */
memset(&Start,0,sizeof(struct tm));
PARS_mid(StartDateTime,0,4,StringVal);
Start.tm_year=atoi(StringVal)-1900;
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);
/* Make end time: */
memset(&End,0,sizeof(struct tm));
PARS_mid(EndDateTime,0,4,StringVal);
End.tm_year=atoi(StringVal)-1900;
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 */
return (int)difftime(mktime(&End),mktime(&Start));
}

Here's a test program of mine, if you can't get the other to work:

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

/* Format: 'yyyy-mm-dd hh:mm:ss' */
const char datetime[]="2003-02-11 11:28:34";

time_t StringToTime(const char *s)
{
struct tm t;

/*     if(ValidateString(s)==0) return -1; */
memset(&t,0,sizeof(struct tm));
sscanf(s,"%d",&t.tm_year);
sscanf(s+5,"%d",&t.tm_mon);
sscanf(s+8,"%d",&t.tm_mday);
sscanf(s+11,"%d",&t.tm_hour);
sscanf(s+14,"%d",&t.tm_min);
sscanf(s+17,"%d",&t.tm_sec);
t.tm_year-=1900;
return mktime(&t);
}

int main()
{
time_t thetime,curtime;

thetime=StringToTime(datetime);
time(&curtime);
printf("\nInitial time: %s",ctime(&thetime));
printf("Current time: %s",ctime(&curtime));
printf("Difference:   %.0lf s\n",difftime(curtime,thetime));
return 0;
}

You can make your own validation function if you need it...
0

Commented:
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.
0

Author Commented:
Thank you very much... you have been very helpful.
0

## Featured Post

• 3
• 3
Tackle projects and never again get stuck behind a technical roadblock.