cocos2d-x 获取系统时间 年月日时分秒还有毫秒


1秒(s) = 1000 毫秒(ms) = 1,000,000 微秒(μs) = 1,000,000,000 纳秒(ns) = 1,000,000,000,000 皮秒(ps)

本函数主要实现获取系统年月日时分秒还有毫秒。

对网上获取系统毫秒时,本人觉得写的有点问题(1秒(s) = 1000 毫秒(ms) )数据应该是0~1000之间的数据,可是获取出来的不是负数,就是很大的一个数据。

下面红颜色是网上说的获取系统毫秒的方法,注释的地方有说明。

在游戏中这些参数是够用了


typedef struct{

            int _nian;//

            int _yue;//

            int _ri;//

            int _si;//小时

            int _fen;//分钟

            int _miao;//

            long _haomiao;//毫秒

            void log() {

                CCLOG(" %d",_nian);

                CCLOG(" %d",_yue);

                CCLOG(" %d",_ri);

                CCLOG(" %d",_si);

                CCLOG(" %d",_fen);

                CCLOG(" %d",_miao);

                CCLOG("毫秒 %ld",_haomiao);

            }

 }GameSystemTime;


GameSystemTime  GameCommonMethod::getSystemTime(){

    struct timeval now;  // 3.0一下版本是这样 struct cc_timeval now;

    gettimeofday(&now, nullptr);// 3.0一下版本是这样 CCTime::gettimeofdayCocos2d(&now, NULL);

    struct tm* ptm;

    ptm = localtime(&now.tv_sec);

    

    int nian = ptm->tm_year;//

    int yue = ptm->tm_mon;//

    int day = ptm->tm_mday;//

    int hour = ptm->tm_hour;//小时

    int sec = ptm->tm_min;//分钟

    int second=ptm->tm_sec;//

    long time= now.tv_usec;//微秒  在除以1000就是毫

//   long long  timeA = ((long long)now.tv_sec) *1000+ now.tv_usec /1000;//这个获取出来的数据可达13位数据 (now.tv_sec 秒(0~59) now.tv_usec 微秒(0~1,000,000 )  )我觉网上这样算有点复杂 应该是  now.tv_sec秒+毫秒,在不放心的话,可以加上分,应该是 now.tv_sec+now.tv_usec /1000 这样的话是 0~1059)之间

//    long aa= now.tv_sec;

    GameSystemTime m_time;

    m_time._nian=1900+nian;

    m_time._yue=yue;

    m_time._ri=(day+1);

    m_time._si=hour;

    m_time._fen=sec;

    m_time._miao=second;

    m_time._haomiao=(time/1000);


    return m_time;

}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值