C language reading time

method one:

 1 #include<windows.h>
 2 #include<iostream>
 3 #include<iomanip>
 4 #include<cstdio>
 5 #include<time.h>
 6 using namespace std;
 7  
 8 int main(int argc, char **argv)
 9 {
10     cout<<time(NULL)<<endl;
11     time_t tmpcal_ptr;
12     struct tm *tmp_ptr = NULL;
13     
14     time(&tmpcal_ptr);
15     //tmpcal_ptr = time(NULL);   两种取值方法均可以
16     printf("tmpcal_ptr=%d\n", tmpcal_ptr);
17     
18     tmp_ptr = gmtime(&tmpcal_ptr);
19     printf("after gmtime, the time is:%d:%d:%d\n", tmp_ptr->tm_hour, tmp_ptr->tm_min, tmp_ptr->tm_sec);
20     
21     tmp_ptr = localtime(&tmpcal_ptr);
22     printf ("after localtime, the time is:%d.%d.%d ", (1900+tmp_ptr->tm_year), (1+tmp_ptr->tm_mon), tmp_ptr->tm_mday);
23     printf("%d:%d:%d\n", tmp_ptr->tm_hour, tmp_ptr->tm_min, tmp_ptr->tm_sec);
24     
25     return 0;
26     }    

Second way to accurately :( milliseconds)

 1 #include<windows.h>
 2 #include<iostream>
 3 #include<iomanip>
 4 #include<cstdio>
 5 #include<time.h>
 6 using namespace std;
 7 
 8 int main()
 9 {
10        SYSTEMTIME sys; 
11        GetLocalTime( &sys ); 
12        printf( "%4d/%02d/%02d %02d:%02d:%02d.%03d 星期%1d\n",sys.wYear,sys.wMonth,sys.wDay,sys.wHour,sys.wMinute, sys.wSecond,sys.wMilliseconds,sys.wDayOfWeek); 
13  }

 

Guess you like

Origin www.cnblogs.com/xiongchongwen/p/12661462.html