Summary of several methods for obtaining the time difference and calculating the time difference.
Reprinted from: http://blog.csdn.net/coder_xia/article/details/6566708
I. Standard C and C ++ are available
1. Use time_t to obtain the timeTime(Time_t * timer), calculate the time difference using double difftime (time_t timer1, time_t timer0 ). Accurate to seconds.
The test procedure is as follows:
1 #include <time.h> 2 #include <stdio.h> 3 int main() 4 { 5 time_t start ,end ; 6 double cost; 7 time(&start); 8 sleep(1); 9 time(&end);10 cost=difftime(end,start);11 printf("%f/n",cost);12 return 0;13 }
This program passed the test in fedora9.
For the sleep function in the code, note the following:
1) In windows, it is a Sleep function and contains windows. h.
2) for sleep, 1000 in Windows and Linux means different meanings. In Windows, 1000 is represented in milliseconds, that is, 1 second. in Linux, 1000 is represented in seconds, in Linux, usleep can be used for millisecond-level functions.
2. clock_tClock (), Clock ()
The time interval after the computer is started is obtained, and the CPU time is accurate to 1/CLOCKS_PER_SEC.
The test procedure is as follows:
1 #include <time.h> 2 #include <stdio.h> 3 int main() 4 { 5 double start,end,cost; 6 start=clock(); 7 sleep(1); 8 end=clock(); 9 cost=end-start;10 printf("%f/n",cost);11 return 0;12 }
II. C ++ (for windows, both linux and windows are supported in Standard c)
1,GetTickCount ()
The call function must contain windows. h. The system runs in milliseconds. The test procedure is as follows:
1 #include <iostream> 2 #include <windows.h> 3 using namespace std; 4 int main() 5 { 6 double start = GetTickCount(); 7 Sleep(1000); 8 double end=GetTickCount(); 9 cout << "GetTickCount:" << end-start << endl;10 return 0;11 }
2,GetLocalTime ()
The year, month, and other information stored by the struct are obtained. In C language, the time function is obtained from 00:00:00 on January 1, January 1, 1970 to the number of seconds at this time. The gmtime function is required to convert to a commonly used calendar (the returned result is world time. To display the commonly used time, it is the localtime function ).
In the C language, the structure for storing commonly used calendars is struct tm, which is contained in time. in h, the c ++ language is the SYSTEMTIME struct, which is contained in winbase. h (programming includes windows. h ). Of course, the precision must be seconds.
The test procedure is as follows:
1 # include <iostream> 2 # include <windows. h> 3 using namespace std; 4 int main () 5 {6 SYSTEMTIME start; // windows. in h, 7 GetLocalTime (& start); // time. the same effect of the tm struct of h is 8 cout <start. year <endl; 9}
The example code of the gmtime method in C language is as follows:
#include <time.h>#include <stdio.h>#include <stdlib.h>int main(){ struct tm *tm_ptr; time_t the_time; (void) time(&the_time); tm_ptr = gmtime(&the_time); printf("Raw time is %ld/n", the_time); printf("gmtime gives:/n"); printf("date: %02d/%02d/%02d/n", tm_ptr->tm_year, tm_ptr->tm_mon+1, tm_ptr->tm_mday); printf("time: %02d:%02d:%02d/n", tm_ptr->tm_hour, tm_ptr->tm_min, tm_ptr->tm_sec); exit(0);}
In addition, the C language has a function ctime () similar to the GetLocalTime method ().
For localtime (), the prototype is: struct tm * localtime (const time_t * timep); change the gmtime of the test program to localtime, you can see that the output time is the time and date for obtaining. To get a more friendly time and date, you can use the asctime or ctime function for output like date. The prototype is char * ctime (const time_t * timeval). The test code is as follows:
1 #include <time.h> 2 #include <stdio.h> 3 #include <stdlib.h> 4 int main() 5 { 6 time_t the_time; 7 time(&the_time); 8 printf("The date is : %s /n" , ctime(&the_time)); 9 exit(0);10 }
3. You can use
BOOL QueryPerformanceFrequency (LARGE_INTEGER * lpFrequency)
BOOL QueryPerformanceCounter (LARGE_INTEGER * lpPerformanceCount) gets the counter value
Then, divide the difference between two counters by Frequency to get the time.
The test procedure is as follows:
1 # include <iostream> 2 # include <windows. h> 3 using namespace std; 4 int main () 5 {6 LARGE_INTEGER m_nFreq; 7 LARGE_INTEGER m_nBeginTime; 8 LARGE_INTEGER nEndTime; 9 QueryPerformanceFrequency (& m_nFreq ); // obtain the clock cycle 10 QueryPerformanceCounter (& m_nBeginTime); // obtain the clock count 11 Sleep (100); 12 QueryPerformanceCounter (& nEndTime); 13 cout <(double) (nEndTime. quadPart-m_nBeginTime.QuadPart) * 1000/m_nFreq.QuadPart <endl; 14}
Note that the result must be forcibly converted to double, or the following error is returned: <is ambiguous.
4,TimeGetTime().
Precision: milliseconds, equivalent to GetTickCount. Windows. h is required for use, and Winmm. lib is added. (although the information is found to include mmsystem. h, it is verified that it does not need to be included ). The test code is as follows:
#include <iostream>#include <windows.h>//GetTickCount//#include <mmsystem.h>using namespace std;int main(){ DWORD start = timeGetTime();// Sleep(1000); DWORD end= timeGetTime();// cout << timeGetTime() << endl; return 0;}
5. In MFC,CTime: GetCurrentTime () is accurate to seconds and test code is not listed.
I will not summarize the usage of timer or something.
Reference URL:
1. http://blog.csdn.net/wallaceli1981/archive/2009/10/24/4723218.aspx
2. http://wenku.baidu.com/view/beb3c9eef8c75fbfc77db2b5.html
EXCEL computing time difference
The two cells are directly subtracted.
For example, A1 and A2 use time to subtract the time difference from each other. However, EXCEL uses days by default. to convert them to hours or minutes, You need to multiply them. For example
Days of difference
= A1-A2
Hours of difference
= (A1-A2) * 24
Minutes
= (A1-A2) * 24*60
The time difference is seconds. I believe you can also export it ..
How to calculate the time difference ??
The later time plus (24 hours × the number of days of the Difference), then minus the previous time. Pay special attention to the carry of time.
The problem specific to the landlord is:
(12:23:34 + 24:00:00)-13:34:32
= (12 + 24) :( 23 + 00) :( 34 + 00)-13:34:32
= 36: 23: 34-13:34:32
= (36-13) :( 23-34) :( 34-32)
= 23 :(-11): 02
= 22 :( 60-11): 02
= 22:49:02
The time difference is 22 hours, 49 minutes, and 2 seconds.