当前位置:编程学习 > wap >>

cocos2d-x在win32和iOS、android下获取当前系统时间的方法

最近在游戏里要显示当前系统时间的功能,网上一搜很多写着获取的方法,大都是如下
 
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;  

 

 
以上的方法后来验证确实没错,但是只在unix和linux下才是正常的,在win32得到的时间是1970年1月1日。
后用另外方法在win32下获取到正确时间,如下
 
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;  

 

 
此方法可在win32获取到正确的当前时间。
补充:移动开发 , 其他 ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,