首页 > 代码库 > cocos2dx 3.x(获取当前系统时间)

cocos2dx 3.x(获取当前系统时间)

 1 //
 2 //  MainScene.cpp
 3 //  helloworld
 4 //
 5 //  Created by apple on 16/10/21.
 6 //
 7 //
 8 
 9 #include "MainScene.hpp"
10 USING_NS_CC;
11 Scene * MainScene::createScene()
12 {
13      auto scene = Scene::create();
14 //    CCScene * scene = CCScene::create();// 创建场景
15     //创建层
16     MainScene *layer = MainScene::create();
17     scene->addChild(layer);
18     return scene;
19 }
20 bool MainScene::init(){
21     if (!Layer::init()) {
22         return false;
23     }
24     //获取屏幕大小
25     size = Director::getInstance()->getVisibleSize();
26     //auto size = Director::getInstance()->getWinSize();
27     
28     //获取系统时间
29     struct timeval now;
30     struct tm *time;
31     
32     gettimeofday(&now, NULL);
33     
34     time = localtime(&now.tv_sec);      //microseconds: 微秒
35     int year = time->tm_year +1900;
36     log("year = %d", year);         //显示年份
37     
38     char date1[32] = {0};
39     sprintf(date1, "%d %02d %02d", (int)time->tm_year + 1900, (int)time->tm_mon + 1, (int)time->tm_mday);
40     log("%s", date1);        //显示年月日
41     
42     char date2[50] = {0};
43     sprintf(date2, "%02d %02d %02d", (int)time->tm_hour, (int)time->tm_min, (int)time->tm_sec);
44     log("%s", date2);       //显示时分秒
45     
46     //return StringUtils::format("%s", date);       std::string getcurrTime(){}
47     
48     return true;
49 }

 

cocos2dx 3.x(获取当前系统时间)