Cocos2d-x gets system time

Source: Internet
Author: User

Previous use of cocos2d-x to get system time, in milliseconds


[Cpp]
Long getCurrentTime ()
{
Struct timeval TV;
Gettimeofday (& TV, NULL );
Return TV. TV _sec * 1000 + TV. TV _usec/1000;
}

Long getCurrentTime ()
{
Struct timeval TV;
Gettimeofday (& TV, NULL );
Return TV. TV _sec * 1000 + TV. TV _usec/1000;
}


Or write it like this.


[Cpp]
Long getCurrentTime ()
{
Struct cc_timeval TV;
CCTime: gettimeofdayCocos2d (& TV, NULL );
Return TV. TV _sec * 1000 + TV. TV _usec/1000;
}

Long getCurrentTime ()
{
Struct cc_timeval TV;
CCTime: gettimeofdayCocos2d (& TV, NULL );
Return TV. TV _sec * 1000 + TV. TV _usec/1000;
}

 

The above two implementations should be correct ~~~ The main function of obtaining the time is to seed the random function, calculate the FPS, or use it as a timer ~ None of these problems

How can we calculate the time required for projects such as year, month, and day?


[Cpp]
Void GetTime ()
{
Struct cc_timeval now;
CCTime: gettimeofdayCocos2d (& now, NULL );
 
Struct tm * tm;
Time_t timep = now. TV _sec;
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 min = tm-> tm_min;
Int second = tm-> tm_sec;
}

Void GetTime ()
{
Struct cc_timeval now;
CCTime: gettimeofdayCocos2d (& now, NULL );

Struct tm * tm;
Time_t timep = now. TV _sec;
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 min = tm-> tm_min;
Int second = tm-> tm_sec;
}

 

In this way, you can obtain

But I in win32 get the year, month, day is, the time is not correct, I use the cocos2d-x2.1.3 version

Debug found that CCTime: gettimeofdayCocos2d (& now, NULL); The now. TV _sec data obtained by this method is very short, and the conversion will take several hours,

Find the implementation of the CCTime: gettimeofdayCocos2d method and find that the gettimeofday method is also called.

Continue searching and find the implementation of this method on the win32 platform


[Cpp]
Int gettimeofday (struct timeval * val, struct timezone *)
{
If (val)
{
LARGE_INTEGER liTime, liFreq;
QueryPerformanceFrequency (& liFreq );
QueryPerformanceCounter (& liTime );
Val-> TV _sec = (long) (liTime. QuadPart/liFreq. QuadPart );
Val-> TV _usec = (long) (liTime. QuadPart * 1000000.0/liFreq. QuadPart-val-> TV _sec * 1000000.0 );
}
Return 0;
}

Int gettimeofday (struct timeval * val, struct timezone *)
{
If (val)
{
LARGE_INTEGER liTime, liFreq;
QueryPerformanceFrequency (& liFreq );
QueryPerformanceCounter (& liTime );
Val-> TV _sec = (long) (liTime. QuadPart/liFreq. QuadPart );
Val-> TV _usec = (long) (liTime. QuadPart * 1000000.0/liFreq. QuadPart-val-> TV _sec * 1000000.0 );
}
Return 0;
}

 

The Code is as follows:


@-30,20 + 30,11 @ int CC_DLL gettimeofday (struct timeval * val, struct timezone *)
{
If (val)
{
-SYSTEMTIME wtm;
-GetLocalTime (& wtm );
-
-Struct tm tTm;
-TTm. tm_year = wtm. wYear-1900;
-TTm. tm_mon = wtm. wMonth-1;
-TTm. tm_mday = wtm. wDay;
-TTm. tm_hour = wtm. wHour;
-TTm. tm_min = wtm. wMinute;
-TTm. tm_sec = wtm. wSecond;
-TTm. tm_isdst =-1;
-
-Val-> TV _sec = (long) mktime (& tTm); // time_t is 64-bit on win32
-Val-> TV _usec = wtm. wMilliseconds * 1000;
+ LARGE_INTEGER liTime, liFreq;
+ QueryPerformanceFrequency (& liFreq );
+ QueryPerformanceCounter (& liTime );
+ Val-> TV _sec = (long) (liTime. QuadPart/liFreq. QuadPart );
+ Val-> TV _usec = (long) (liTime. QuadPart * 1000000.0/liFreq. QuadPart-val-> TV _sec * 1000000.0 );
}
Return 0;
}


We can find that the previous implementation of this method uses GetLocalTime to get the local time and then calculate it. If you follow this implementation method, I wrote the GetTime () the method should be able to get the correct year, month, day, hour, minute, second, but the above code is used to make the acquisition time more accurate

QueryPerformanceFrequency (& liFreq );
QueryPerformanceCounter (& liTime );


Two Methods: QueryPerformanceCounter (& liTime); this method is used to calculate the current CPU running time, so it is obvious that the time I get is the time from the boot to the present, I calculated it, it's almost the same ~

So we can understand that after the implementation of the gettimeofday method in win32 is officially modified, it cannot obtain accurate year, month, day, hour, minute, and second information.

To obtain accurate data, use the following methods:


[Cpp]
Void GetTime (int level)
{
Struct tm * tm;
Time_t timep;
# If (CC_TARGET_PLATFORM = CC_PLATFORM_WIN32)
Time (& timep );
# Else
Struct cc_timeval now;
CCTime: gettimeofdayCocos2d (& now, NULL );
Timep = now. TV _sec;
# Endif
 
 
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 min = tm-> tm_min;
Int second = tm-> tm_sec;
}

Void GetTime (int level)
{
Struct tm * tm;
Time_t timep;
# If (CC_TARGET_PLATFORM = CC_PLATFORM_WIN32)
Time (& timep );
# Else
Struct cc_timeval now;
CCTime: gettimeofdayCocos2d (& now, NULL );
Timep = now. TV _sec;
# Endif


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 min = tm-> tm_min;
Int second = tm-> tm_sec;
}

 

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.