Solved

# date calculation

Posted on 2003-03-10
Medium Priority
266 Views

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
Question by:raza
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 3
• 3

LVL 1

Expert Comment

ID: 8107436

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

Author Comment

ID: 8109239

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

LVL 2

Expert Comment

ID: 8109544
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 Comment

ID: 8109886

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

LVL 2

Accepted Solution

constructor earned 200 total points
ID: 8110307
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

LVL 2

Expert Comment

ID: 8110312
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 Comment

ID: 8110479
Thank you very much... you have been very helpful.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were smallâ€¦
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address.Â This address might be address of another variable/address of devices/address of fuâ€¦
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
###### Suggested Courses
Course of the Month10 days, 10 hours left to enroll