1. 程式人生 > >cocos2d-x 獲取系統時間 年月日時分秒還有毫秒

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之間的資料,可是獲取出來的不是負數,就是很大的一個數據。

下面紅顏色是網上說的獲取系統毫秒的方法,註釋的地方有說明。

在遊戲中這些引數是夠用了

typedefstruct{

            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;


GameSystemTimeGameCommonMethod::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;

}