1

I have two date and time strings separately in variables. I need to calculate the difference between these 2 date and time values in milliseconds. How to get that in C. The solution should work across platforms(at least windows and unix).

char date1[] = {"26/11/2015"};
char time1[] = {"20:22:19"};
char date2[] = {"26/11/2015"};
char time2[] = {"20:23:19"};

First I need to save this into some time structure and then compare 2 time structures to get the difference. What is the time structure which is available in C Library to do this.

Arun
  • 2,247
  • 3
  • 28
  • 51
  • 2
    Just as a note: You don't need a library to do this. You could do with some simple math. Secondly to get milliseconds you just add 3 zeros to your times because they aren't in millisecond precision to begin with. – Jite Nov 26 '15 at 15:05
  • does your date and time have to be in this format? if you can have it in `type time_t` that will save you a lot of work. – Thanushan Nov 26 '15 at 15:50
  • @ Jite Disagree about "do with some simple math". What is the range of valid years? Are only valid dates submitted? Should code consider daylight saving lime? Leap year rules, Far easier to call `mktime()`. – chux - Reinstate Monica Nov 26 '15 at 17:01

2 Answers2

2

Use mktime() and difftime()

The mktime function returns the specified calendar time encoded as a value of type time_t. If the calendar time cannot be represented, the function returns the value (time_t)(-1). C11dr §7.27.2.3 4

The difftime function returns the difference expressed in seconds as a double §7.27.2.2 2

#include <time.h>
#include <stdlib.h>
#include <string.h>

time_t parse_dt(const char *mdy, const char *hms) {
  struct tm tm;
  memset(&tm, 0, sizeof tm);
  if (3 != sscanf(mdy, "%d/%d/%d", &tm.tm_mon, &tm.tm_mday, &tm.tm_year)) return -1;
  tm.tm_year -= 1900;
  tm.tm_mday++;
  if (3 != sscanf(hms, "%d:%d:%d", &tm.tm_hour, &tm.tm_min, &tm.tm_sec)) return -1;
  tm.tm_isdst = -1;  // Assume local time
  return mktime(&tm);
}

int main() {
  // application
  char date1[] = { "26/11/2015" };
  char time1[] = { "20:22:19" };
  char date2[] = { "26/11/2015" };
  char time2[] = { "20:23:19" };
  time_t t1 = parse_dt(date1, time1);
  time_t t2 = parse_dt(date2, time2);
  if (t1 == -1 || t2 == -1) return 1;
  printf("Time difference %.3f\n", difftime(t2, t1) * 1000.0);
  return 0;
}

Output

Time difference 60000.000
Community
  • 1
  • 1
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
1

from the man page for mktime()

this is the prototype for mktime()

time_t mktime(struct tm *tm);

this is the description of the function mktime()

  The mktime() function takes an argument representing
   broken-down time which is a representation separated into year,  month,
   day, and so on.

   Broken-down  time  is  stored  in  the structure tm which is defined in
   <time.h> as follows:

       struct tm {
           int tm_sec;         /* seconds */
           int tm_min;         /* minutes */
           int tm_hour;        /* hours */
           int tm_mday;        /* day of the month */
           int tm_mon;         /* month */
           int tm_year;        /* year */
           int tm_wday;        /* day of the week */
           int tm_yday;        /* day in the year */
           int tm_isdst;       /* daylight saving time */
       };

   The members of the tm structure are:

   tm_sec    The number of seconds after the minute, normally in the range
             0 to 59, but can be up to 60 to allow for leap seconds.

   tm_min    The number of minutes after the hour, in the range 0 to 59.

   tm_hour   The number of hours past midnight, in the range 0 to 23.

   tm_mday   The day of the month, in the range 1 to 31.

   tm_mon    The number of months since January, in the range 0 to 11.

   tm_year   The number of years since 1900.

   tm_wday   The number of days since Sunday, in the range 0 to 6.

   tm_yday   The number of days since January 1, in the range 0 to 365.

   tm_isdst  A  flag  that  indicates  whether  daylight saving time is in
             effect at the time described.  The value is positive if  day‐
             light  saving time is in effect, zero if it is not, and nega‐
             tive if the information is not available.


   The  mktime() function converts a broken-down time structure, expressed
   as local time, to calendar time representation.  The  function  ignores
   the  values  supplied  by the caller in the tm_wday and tm_yday fields.
   The value specified in the tm_isdst field informs mktime()  whether  or
   not  daylight  saving  time (DST) is in effect for the time supplied in
   the tm structure: a positive value means DST is in effect;  zero  means
   that  DST  is  not  in effect; and a negative value means that mktime()
   should (use timezone information and system databases  to)  attempt  to
   determine whether DST is in effect at the specified time.

   The  mktime()  function modifies the fields of the tm structure as fol‐
   lows: tm_wday and tm_yday are set to values determined  from  the  con‐
   tents of the other fields; if structure members are outside their valid
   interval, they will be normalized (so that, for example, 40 October  is
   changed  into  9  November); tm_isdst is set (regardless of its initial
   value) to a positive value or to 0, respectively, to  indicate  whether
   DST  is  or  is  not in effect at the specified time.  Calling mktime()
   also sets the external variable tzname with information about the  cur‐
   rent timezone.

   If  the  specified  broken-down  time cannot be represented as calendar
   time (seconds since the Epoch), mktime() returns (time_t) -1  and  does
   not alter the members of the broken-down time structure.

==========================================

this is from the man page for difftime()

this is the prototype:

double difftime(time_t time1, time_t time0);

This is the description:

   The  difftime()  function returns the number of seconds elapsed between
   time time1 and time time0, represented as a double.  Each of the  times
   is  specified  in calendar time, which means its value is a measurement
   (in seconds) relative to the Epoch, 1970-01-01 00:00:00 +0000 (UTC).

to get the result in seconds. To get the results in milliseconds, multiply by 1000.0

user3629249
  • 16,402
  • 1
  • 16
  • 17
  • using the above information, parse the strings, adjusting for offset from 0 for month, etc. set the fields in the `tm` structure, call `mktime()`. Do this for both the sets of data. then call `difftime()` to get the difference in seconds (as a double value), multiply the difference by 1000.0 to get the milliseconds – user3629249 Nov 26 '15 at 22:03
  • the functions are part of the standard 'c' libraries, so are available in windows. Why ask if it will work in windows. Just code it, the compile it, then run it. That will enable you to learn more and give you proof that it works in windows. (notice that the standard 'c' library is not the same as the MSDN library nor what is to be found in the windows.h header file – user3629249 Nov 27 '15 at 16:06