?
Solved

finding difference between two datetime values in C

Posted on 2010-11-22
6
Medium Priority
?
450 Views
Last Modified: 2012-05-10
Hi,

I have the following problem. I have to find the elapsed time in seconds between two given dates in the following format.

yyyyMMddhhmmss example: 20101118190541

Now i have two of these as a char [14] array and i need to find the difference in seconds between them.

Is there any C library which is portable or some algorithm i can use to calculate it ?
0
Comment
Question by:MarioC82
[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
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 34189245
You can easily calculate the difference between two timestamps in time_t format using difftime :

        http://www.cplusplus.com/reference/clibrary/ctime/difftime/

It's just a matter of getting the corresponding time_t for your timestamp. And that can be done by parsing the timestamp, filling a struct tm, and then calling mktime :

        http://www.cplusplus.com/reference/clibrary/ctime/mktime/
0
 

Author Comment

by:MarioC82
ID: 34190010
I will have a look at this and respond again.
0
 

Author Comment

by:MarioC82
ID: 34190364
Hi i have a quick question, with the above date format would something like this work to populate the tm struct.

fromDate is a char[14] array containing the date.

scanf(fromDate, "%.4s%.2s%.2s%.2s%.2%.2", t->tm_year, t->tm_mon, t->tm_mday, t->tm_hour, t->tm_min, t->tm_wday);

However i dont seem to get the values to pass i only get current date still.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Expert Comment

by:masheik
ID: 34190489
You are missing %s in sscanf
0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 2000 total points
ID: 34190557
The idea is good, but there are a few problems with that statement :

1) a 14 character string is a char[15] array, because you need an extra character for the terminating '\0'.

2) when parsing a string, you use sscanf, not scanf.

3) the struct tm holds ints, so you need to use %d, not %s when parsing.

4) %.4d is not supported for sscanf. You need to use %4d.

5) you need to pass the addressed of the ints as additional parameters of sscanf.

6) tm_sec indicates the seconds, not tm_wday.

7) you need to adjust tm_year and tm_mon after parsing.

Here's some sample code :
#include <stdio.h>
#include <time.h>

int main(void) {
    char fromDate[15] = "20101118190541";
    struct tm tt;

    sscanf(fromDate, "%4d%2d%2d%2d%2d%2d", &(tt.tm_year), &(tt.tm_mon), &(tt.tm_mday), &(tt.tm_hour), &(tt.tm_min), &(tt.tm_sec));
    tt.tm_year -= 1900;
    tt.tm_mon -= 1;

    time_t tim = mktime(&tt);
    printf("%s\n", ctime(&tim));

    return 0;
}

Open in new window

0
 

Author Comment

by:MarioC82
ID: 34190690
Thanks Infinity08.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question