| CodeGuru Home | VC++ / MFC / C++ | .NET / C# | Visual Basic | Newsletters | VB Forums | Developer.com |
|
|||||||
| C++ (Non Visual C++ Issues) Ask or answer C and C++ questions not related to Visual C++. This includes Console programming, Linux programming, or general ANSI C++. |
![]() |
|
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Hello I am using the following code to find the seconds passed since 1 Feb 1970
It is working fine with 1 Feb. If I try to do it for 1 Jan then I get a big 0 in output. Why is it so ?? What to do ?? Currently I am manually adding 2678400 seconds to what I get from 1 Feb. #include <time.h> #include <ctime> #include <iostream> using namespace std; double dateDifferenceInSeconds(std::time_t date1, std::time_t date2) { if ( date1 != (std::time_t)(-1) && date2 != (std::time_t)(-1) ) { return std::difftime(date2, date1) ;/// (60 * 60 * 24); } return NULL; } int main() { struct std::tm a = {0,0,0,2,0,70}; /* Feb 1, 1970 */ time_t currentTime; time ( ¤tTime ); time_t x = mktime(&a); double timeInSeconds = dateDifferenceInSeconds(x, currentTime); printf("\n%.0lf\n",timeInSeconds + 2678400); // Adding seconds of 1 month return 0; } If I use {0,0,0,1,0,70} in place of {0,0,0,2,0,70} for starting it from 1 jan then I get 0 as output |
|
#2
|
|||
|
|||
|
Re: Why My App is crazy with 1 Jan 1970
What ever time function returns itself is number of seconds elapsed from 1 st jan 1970.
Why do you want to calculate it separately? |
|
#3
|
|||
|
|||
|
Actually I was making a function that will give the difference between two dates/time
It was working fine with all but when I tested with 1970; it didn't worked. Instead of time function you can assume another date in that structure like {0,0,0,1,0,100} for 1 Jan 2000. Forget about time function; tell me for difftime |
![]() |
| Bookmarks |
|
||||||
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|