Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between two date in C

Tags:

c

linux

time.h

I am trying to get the difference between two date by using below C code.

but code always giving difference 0. Help me to where i am making mistake.

I am using gcc compiler under linux.

#include <stdio.h>  
#include <time.h>       
int main ()
{
  struct tm start_date;
  struct tm end_date;
  time_t start_time, end_time;
  double seconds;

  start_date.tm_hour = 0;  start_date.tm_min = 0;  start_date.tm_sec = 0;
  start_date.tm_mon = 10; start_date.tm_mday = 15; start_date.tm_year = 2013;

  end_date.tm_hour = 0;  end_date.tm_min = 0;  end_date.tm_sec = 0;
  end_date.tm_mon = 10; end_date.tm_mday = 20; end_date.tm_year = 2013;

  start_time = mktime(&start_date);
  end_time = mktime(&end_date);

  seconds = difftime(end_time, start_time);

  printf ("%.f seconds difference\n", seconds);

  return 0;
}

EDIT : @qchen answer helped lot to solve my problem. one more doubt is there. Below was my update. From the answer

  start_date.tm_hour = 0;  start_date.tm_min = 0;  start_date.tm_sec = 0;
  start_date.tm_mon = 10-1; start_date.tm_mday = 18; start_date.tm_year = 2013-1876;

  end_date.tm_hour = 0;  end_date.tm_min = 0;  end_date.tm_sec = 0;
  end_date.tm_mon = 10-1; end_date.tm_mday = 20; end_date.tm_year = 2013-1876;

tm_year is the year since 1900, then why i getting correct output if i replace 1876 with year between 1876 to 2012.

like image 364
sujin Avatar asked Sep 26 '13 18:09

sujin


People also ask

How can I compare two dates in C?

Consider the problem of comparison of two valid dates d1 and d2. There are three possible outcomes of this comparison: d1 == d2 (dates are equal), d1 > d2 (date d1 is greater, i.e., occurs after d2) and d1 < d2(date d1 is smaller, i.e., occurs before d2).

How do you find the difference between two dates?

Use the DATEDIF function when you want to calculate the difference between two dates. First put a start date in a cell, and an end date in another. Then type a formula like one of the following. Warning: If the Start_date is greater than the End_date, the result will be #NUM!.

How do I find the difference between two dates in C++?

#include "boost/date_time/gregorian/gregorian_types. hpp" using namespace boost::gregorian; date date1(2012, Apr, 2); date date2(2003, Feb, 2); long difference = (date1 - date2). days();

How do I get the number of days between two dates in C#?

Use DateTime. Subtract to get the difference between two dates in C#.


2 Answers

The problem is that tm_year is the year since 1900, so 2013 would be 113 http://en.cppreference.com/w/cpp/chrono/c/tm

  start_date.tm_hour = 0;  start_date.tm_min = 0;  start_date.tm_sec = 0;
  start_date.tm_mon = 10; start_date.tm_mday = 15; start_date.tm_year = 113;

  end_date.tm_hour = 0;  end_date.tm_min = 0;  end_date.tm_sec = 0;
  end_date.tm_mon = 10; end_date.tm_mday = 20; end_date.tm_year = 113;

Given 2013, mktime will return -1 as the calendar time cannot be represented. You would think that the year 3913 would be a valid calendar time and the reason is related to the year 2038 problem, as pointed out by Joni

like image 149
qxixp Avatar answered Oct 04 '22 11:10

qxixp


OP did not check mktime()` result.

As @Joni mentions, set the tm_isdst field. Use 0 or 1 if you know if how DST is applied, else use '-1' and let the OS make the determination.

@qchen mentioned the year 1900 offset as you likely want .tm_year = 2013-1900.

I assert the underlying issue is using mktime() without checcking if it is (time_t) -1. With robust code, this return value should be tested and missing that opened OP code to unexpected results.

like image 45
chux - Reinstate Monica Avatar answered Oct 04 '22 09:10

chux - Reinstate Monica