How to obtain the current system time in win32, iOS, and android cocos2d-x

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.