Cross-platform approach method one: manual violence law
#include <iostream> using namespace Std; #include <time.h> time_t t = time (NULL); struct tm* stime=localtime (&t); char tmp[32 ]={null} ; sprintf (TMP, "%04d -%02d -%02d %02d : %02d : %02d ", 1900 +stime->tm_year,1 +stime->tm_mon, Stime->tm_mday, Stime->tm_hour, stime->tm_min,stime->tm_sec); Cout<<tmp<<endl;
Output: 2015-04-02 23:12:56
Method Two: Opportunistic method
#include <iostream>using namespace std;#include <time.h> time(0); char tmp[32]={NULL}; "%Y-%m-%d %H:%M:%S",localtime(&t)); cout<<tmp<<endl;
Output: 2015-04-02 23:12:56
Method Three: Simply obtain the calendar time method
#include <iostream>usingnamespacestd;#include <time.h>#include <string>time_t tm;time(&tm);char tmp[128]={NULL};strcpy(tmp,ctime(&tm));//或者//struct tm* stime=localtime(&tm);//strcpy(tmp,asctime(stime));cout<<tmp<<endl;
Output result: Fri 14 23:19:42 2015
Windows Platform Acquisition Time
#include <iostream>usingnamespacestd;#include <time.h>#include <windows.h>SYSTEMTIME sys; GetLocalTime( &sys );char tmp[64]={NULL};sprintf(tmp,"%4d-%02d-%02d %02d:%02d:%02d ms:%03d",sys.wYear,sys.wMonth,sys.wDay,sys.wHour,sys.wMinute,sys.wSecond,sys.wMilliseconds); cout<<tmp<<endl;
Output result: 2015-08-14 23:41:56 ms:354
UNIX Platform Acquisition Time
#include <sys/time.h> #include <unistd.h> struct Timeval now_time;gettimeofday (&now_time, NULL); time_t tt = Now_time.tv_sec;tm *temp = localtime (&TT); char time_str[32 ]={null} ; Span class= "Hljs-keyword" >sprintf (Time_str, "%04d -%02d -%02d %02d : %02d : %0 2d ", temp->tm_year+ 1900 , temp->tm_mon+ 1 , Temp->tm_mday,temp->tm_hour,temp->tm_min, temp->tm_sec);cout< <tmp<<endl;
Output time: 2015-08-14-23:41:56
Need to know the knowledge point
(1) UTC (coordinated Universal time): Coordinate the world, also known as the World standard times. It was provided by an atomic clock in Greenwich Mean Time (Greenwich Mean time,gmt). For example, the time difference between the Chinese mainland and UTC is +8. That's utc+8. The United States is UTC-5.
(2) Calendar time: The time that is represented by the "number of seconds elapsed from one standard point to the time", which is obtained by The Times () function. This standard point of time will vary for different compilers, but for a compiled system. This standard time point is constant, the time corresponding calendar time in the compilation system is measured by the standard time point, so it is possible to say that the calendar time is "relative time", but no matter what time zone you are in. At the same time, the calendar time is the same for the same standard point.
(3) Epoch refers to a specific point in time: 1970-01-01 00:00:00 UTC, or Unix timestamp.
(4) Clock tick: clocking unit (without calling it a clock tick), the length of time of a clock unit is controlled by the CPU.
A clock tick is not a cycle of the CPU, but rather a basic unit of time in C + +.
In the VC + + time.h file. We are able to find relevant definitions:
#ifndef _CLOCK_T_DEFINEDlong clock_t;#define _CLOCK_T_DEFINED#endif#define CLOCKS_PER_SEC ((clock_t)1000)void );
This function returns the CPU clock meter between "turn on this program process" to "call clock () function in program"
The number of clock ticks, called the Wall Clock Time (Wal-clock), in MSDN.
//获取逝去时间 start, finish; start=clock(); … finish=clock();//逝去多少秒 start)/ CLOCKS_PER_SEC ;
(5) Time.h also provides two different functions to convert the calendar time (an integer represented by time_t) to the time format TM that we normally see when the date is divided by minutes and seconds:
struct tm * gmtime(const time_t *timer); struct tm * localtime(const time_t * timer);
The Gmtime () function converts the calendar time to world standard Time (GMT). and returns a TM structure to hold this time. The localtime () function converts the calendar time to local time. For example, the world standard Time now obtained with the gmtime () function is July 30, 2005 7:18 20 seconds, so the local time I get in China using the localtime () function will be 8 hours later than the world standard Time.
(6) Decomposition time is the year, month, day, hour, minute, seconds and other components of the time structure of the preservation, is the TM structure in C + +. We can use the Mktime () function to convert the time represented by the TM structure into a calendar time. Its function prototypes are as follows:
time_t mktime (struct TM * timeptr);
This function has the opposite effect to the gmtime and localtime functions.
References
[1] http://blog.sina.com.cn/s/blog_48f3622d010008zx.html
Common ways to get local time in C + +