您的位置:首页 > 移动开发 > Cocos引擎

cocos2d-x 系统时间获取和格式化

2014-03-25 10:37 399 查看
//1、获取系统时间毫秒数

double HelloWorld::getTimeDouble()

{

    time_t timep; //注:time_t 实际上是长整形

#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)

    time(&timep);

#else

    struct cc_timeval now;

    CCTime::gettimeofdayCocos2d(&now, NULL);

    timep = now.tv_sec;

#endif

    return timep;

}

//从格式化时间

string HelloWorld::getTimeStringByMills(

    double timemills)

{

    struct tm* tm;

    time_t timep = NULL;

    timep = timemills;

    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;

    char result[100]= {""};

    sprintf(result,"%d-%d-%d--%d:%d.%d",year,

            month,

            day,hour,min,second);

    CCLog(result);

    return result;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息