Recently, the function of displaying the current system time is required in the game. Many methods of searching on the Internet are described as follows:
Struct cc_timeval now; CCTime: gettimeofdayCocos2d (& now, NULL); struct tm * tm; tm = localtime (& now. TV _sec); int year = tm-> tm_year + 1900; int month = tm-> tm_mon + 1; int day = tm-> tm_mday; int hour = tm-> tm_hour; int minute = tm-> tm_min; int second = tm-> tm_sec; long millSecond = now. TV _sec * 1000 + now. TV _usec/1000;
The above method was proved to be correct later, but it is normal only in unix and linux, and the time obtained in win32 is January 1, 1970. Then, use another method to get the correct time in win32, as shown below:
Struct tm * tm; time_t timep; time (& timep); tm = localtime (& timep); int year = tm-> tm_year + 1900; int month = tm-> tm_mon + 1; int day = tm-> tm_mday; int hour = tm-> tm_hour; int minute = tm-> tm_min; int second = tm-> tm_sec;
This method can obtain the correct current time in win32.