About game time display in cocos2dx-lua versions, cocos2dxlua

Source: Internet
Author: User
Tags time zones

About game time display in cocos2dx-lua versions, cocos2dxlua
The time display problem is the time difference problem (the following code snippet is written in lua script ).
Generally, the time in the game is based on the time on the game server. When a game logs on, it receives a timestamp from the server. The common solution is to receive a timestamp, and the client maintains the timestamp on its own. Sometimes, the client displays the time:
Local ServerTimeStamp = XXXXXXXX -- this is the timestamp that the server sends to the client for maintenance.
Local ServerTimeZone = XXXX -- this is the time zone difference of the server time (also sent to the client by the server)
Local CurrentDateTime = OS. date ("* t", ServerTimeStamp)
Print ("year ="... CurrentDateTime. year)
Print ("month ="... CurrentDateTime. month)
Print ("day ="... CurrentDateTime. day)
Print ("hour ="... CurrentDateTime. hour)
Print ("min ="... CurrentDateTime. min)

Print ("sec ="... CurrentDateTime. sec)

If the time zone of your game server is GMT + and the time zone of the machine running the code above is GMT +, you may be very happy to see that, the printed time is the same as the server time. However, the time zone of the machine used to run the preceding code is not UTC + 8, if a Japanese uses his machine to run your program (the time zone of his machine is dongjiu), the printed time is one hour faster than that of the game server, this is definitely wrong.

Why?
The Time Zone of the current device is also associated when OS. date () is called above. Therefore, it is correct to finish running in dongba district. However, you cannot specify a machine that uses a fixed time zone to run your program. How can this problem be solved?
-- Compute the difference in seconds between local time and UTC.
Local function get_timezone ()
Local now = OS. time ()
Return OS. difftime (now, OS. time (OS. date ("! * T ", now )))
End
Local localTimeZone = get_timezone ()
Local timeZoneD = ServerTimeZone-localTimeZone -- calculates the difference between the time zone of the server and the client.
Local CurrentDateTime = OS. date ("* t", ServerTimeStamp + timeZoneD)
At this time, you will find that whether you use a host in dongba, dongjiu, or other time zones to run, it correctly prints the time of this server.
By extension, the client can correctly display the Time of the server no matter which time zone of the server or the time zone of the client.
For another extension, no matter which version of cocos2dx is used, no matter which game engine framework is used, it should be processed in a time zone similar to the above, allows the client to correctly display the Time of the server.

Related Article

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.