Sunday, August 2, 2009

How do i Find Difference between two date and Time in C programming?

I have Date in String format. The Date is of format YYYYMMDDHH24MISS


Eg :- 20071102121007





I need to find the difference in Minutes.


I also need to Validate if this date and time is of Right Format Eg: - Date cannot be more than 31 and Hour Time cannot be more than 24 Hours and Etc.


Do you know any Pre-Defined functions for this ???


Or Any Good Logic that can Help me ??? I think this is possible in Unix Shell Scripting


But I need it in C. Plz Help

How do i Find Difference between two date and Time in C programming?
#include %26lt;iostream%26gt;


#include %26lt;ctime%26gt;





int main()


{


struct std::tm a = {0,0,0,24,5,104}; /* June 24, 2004 */


struct std::tm b = {0,0,0,5,6,104}; /* July 5, 2004 */


std::time_t x = std::mktime(%26amp;a);


std::time_t y = std::mktime(%26amp;b);


if ( x != (std::time_t)(-1) %26amp;%26amp; y != (std::time_t)(-1) )


{


double difference = std::difftime(y, x) / (60 * 60 * 24);


std::cout %26lt;%26lt; std::ctime(%26amp;x);


std::cout %26lt;%26lt; std::ctime(%26amp;y);


std::cout %26lt;%26lt; "difference = " %26lt;%26lt; difference %26lt;%26lt; " days" %26lt;%26lt; std::endl;


}


return 0;


}


No comments:

Post a Comment