cocos2d-x获取当前系统时间,实现跨平台
在unix和linux下的获取时间和在win32获得的时间是不同的,所以要分开来写。代码如下:
1 std::string Tools::get_date_now()
2 {
3 #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
4 struct cc_timeval now;
5 CCTime::gettimeofdayCocos2d(&now, NULL);
6 struct tm *tm;
7 time_t tp = now.tv_sec;
8 tm = localtime(&tp);
9 char date[32] = { 0 };
10 sprintf(date, "%d-%02d-%02d", (int) tm->tm_year + 1900,
11 (int) tm->tm_mon + 1, (int) tm->tm_mday);
12 return string(date);
13 #endif
14 #if ( CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 )
15 struct tm *tm;
16 time_t timep;
17 time(&timep);
18 tm = localtime(&timep);
19 //int year = tm->tm_year + 1900;
20 //int month = tm->tm_mon + 1;
21 //int day = tm->tm_mday;
22 //int hour=tm->tm_hour;
23 //int minute=tm->tm_min;
24 //int second=tm->tm_sec;
25 char time[64] = { 0 };
26 sprintf(time, "%d-%02d-%02d %02d:%02d:%02d", (int) tm->tm_year + 1900,
27 (int) tm->tm_mon + 1, (int) tm->tm_mday, (int) tm->tm_hour,
28 (int) tm->tm_min, (int) tm->tm_sec);
29 return string(time);
30 #endif
31 }