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;
}