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
int _fen;//分鐘
int _miao;//秒
long _haomiao;//毫秒
void log() {
CCLOG("年 %d",_nian);
CCLOG("月 %d",_yue);
CCLOG("日 %d",_ri);
CCLOG("時 %d",_si);
CCLOG("分 %d",_fen);
CCLOG("
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
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;
}