欢迎转载,本帖地址:http://blog.csdn.net/jinjian2009/article/details/9449585

之前使用过cocos2d-x获取系统时间,毫秒级的

long getCurrentTime()
{
struct timeval tv;
gettimeofday(&tv,NULL);
return tv.tv_sec * 1000 + tv.tv_usec / 1000;
}

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

或者这样写

long getCurrentTime()
{
struct cc_timeval tv;
CCTime::gettimeofdayCocos2d(&tv, NULL);
return tv.tv_sec * 1000 + tv.tv_usec / 1000;
//tv_sec:秒   tv_usec:微妙
}

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

上面两种实现应该都是没有问题的~~~之前获取时间的主要作用是给随机函数做种子,或者计算FPS,或者作为自己的定时器使用~这些都没有问题

后来有项目需要获取年月日等时间,这些怎么来计算呢?

void GetTime()
{
struct cc_timeval now;
CCTime::gettimeofdayCocos2d(&now, NULL); struct tm *tm;
time_t timep = now.tv_sec;
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;
}

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

这样可以获取年月日时分秒

但是本人在win32上获取的年月日是1970-1-1,时分秒也是不对的,本人使用的是cocos2d-x2.1.3版本

debug发现CCTime::gettimeofdayCocos2d(&now, NULL); 该方法获取的now.tv_sec数据很短,转换下也就几个小时,

找到CCTime::gettimeofdayCocos2d方法的实现,发现也是调用了gettimeofday这个方法

继续找,发现win32平台下的该方法的实现

int gettimeofday(struct timeval * val, struct timezone *)
{
if (val)
{
LARGE_INTEGER liTime, liFreq;
QueryPerformanceFrequency( &liFreq );
QueryPerformanceCounter( &liTime );
val->tv_sec = (long)( liTime.QuadPart / liFreq.QuadPart );
val->tv_usec = (long)( liTime.QuadPart * 1000000.0 / liFreq.QuadPart - val->tv_sec * 1000000.0 );
}
return 0;
}

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

网上搜了下,原来是cocos2d-x官方论坛里有人修改过该方法,帖子地址:http://www.cocos2d-x.org/boards/6/topics/8191

代码如下

@@ -30,20 +30,11 @@ int CC_DLL gettimeofday(struct timeval * val, struct timezone *)
{
if (val)
{
- SYSTEMTIME wtm;
- GetLocalTime(&wtm);
-
- struct tm tTm;
- tTm.tm_year = wtm.wYear - 1900;
- tTm.tm_mon = wtm.wMonth - 1;
- tTm.tm_mday = wtm.wDay;
- tTm.tm_hour = wtm.wHour;
- tTm.tm_min = wtm.wMinute;
- tTm.tm_sec = wtm.wSecond;
- tTm.tm_isdst = -1;
-
- val->tv_sec = (long)mktime(&tTm); // time_t is 64-bit on win32
- val->tv_usec = wtm.wMilliseconds * 1000;
+ LARGE_INTEGER liTime, liFreq;
+ QueryPerformanceFrequency( &liFreq );
+ QueryPerformanceCounter( &liTime );
+ val->tv_sec = (long)( liTime.QuadPart / liFreq.QuadPart );
+ val->tv_usec = (long)( liTime.QuadPart * 1000000.0 / liFreq.QuadPart - val->tv_sec * 1000000.0 );
}
return 0;
}

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

可以发现以前该方法的实现是使用了GetLocalTime来获取本地时间,然后进行计算的,如果按照这样的实现方法,本人之前写的GetTime()方法应该能获取到正确的年月日时分秒,但是上述代码为了使获取的时间更为精确,使用了

QueryPerformanceFrequency( &liFreq );

QueryPerformanceCounter( &liTime );

两个方法,QueryPerformanceCounter( &liTime );这个方法,是计算CPU运行到现在的时间,所以很明显本人获取的时间是开机到现在的时间,本人计算了下,还真是差不多~

所以可以理解,win32下gettimeofday方法的实现被官方这么修改过之后是不能获取到准确的年月日时分秒的

如果要获取准确的数据,可以使用以下方法

void GetTime(int level)
{
struct tm *tm;
time_t timep;
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
time(&timep);
#else
struct cc_timeval now;
CCTime::gettimeofdayCocos2d(&now, NULL);
timep = now.tv_sec;
#endif 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;
}

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

这回win32平台上OK了,至于其他平台上有没有问题,要测试下~

总结了一下上面的转载:

//cocos2d-x获取系统时间,毫秒级的;
long MenuLayer::currentTimeInMS() {
struct cc_timeval now;
CCTime::gettimeofdayCocos2d(&now, NULL);
return (now.tv_sec * 1000 + now.tv_usec /1000.0)/10;
}
//cocos2d-x获取系统时间,包括年月日
void MenuLayer::GetTime() {
struct tm *tm;
time_t timep;
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
time(&timep);
#else
struct cc_timeval now;
CCTime::gettimeofdayCocos2d(&now, NULL);
timep = now.tv_sec;
#endif
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;
}

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

【转】cocos2d-x获取系统时间——2013-08-25 10的更多相关文章

  1. Android获取系统时间方法的总结

    Android获取系统时间方法的方法有很多种,常用的有Calendar.Date.currentTimeMills等方法. (1)Calendar Calendar获取系统时间首先要用Calendar ...

  2. 用PHP获取系统时间时,时间比当前时间少8个小时

    自PHP5.0开始,用PHP获取系统时间时,时间比当前时间少8个小时.原因是PHP.ini中没有设置timezone时,PHP是使用的UTC时间,所以在中国时间要少8小时. 解决办法: 1.在PHP. ...

  3. C/C++获取系统时间

    C/C++获取系统时间需要使用Windows API,包含头文件"windows.h". 系统时间的数据类型为SYSTEMTIME,可以在winbase.h中查询到如下定义: ty ...

  4. VC++编程中获取系统时间

    <span style="white-space:pre"> </span>总结了在程序中如何获得系统时间的方法 void CGetSystenTimeDl ...

  5. cocos2d-x 获取系统时间

    转自:http://blog.csdn.net/jinjian2009/article/details/9449585 之前使用过cocos2d-x获取系统时间,毫秒级的 long getCurren ...

  6. C++11新特性,利用std::chrono精简传统获取系统时间的方法

    一.传统的获取系统时间的方法 传统的C++获取时间的方法须要分平台来定义. 相信百度代码也不少. 我自己写了下,例如以下. const std::string getCurrentSystemTime ...

  7. c++ 怎样获取系统时间

    c++ 怎样获取系统时间 2008-04-28 15:34 //方案— 长处:仅使用C标准库:缺点:仅仅能精确到秒级 #include <time.h> #include <stdi ...

  8. Linux C 语言 获取系统时间信息

    比如获取当前年份:        /* 获取当前系统时间 暂时不使用        int iyear = 0;        int sysyear = 0;        time_t now;  ...

  9. cocos2d-x获取系统时间

    欢迎转载,本帖地址:http://blog.csdn.net/jinjian2009/article/details/9449585 之前使用过cocos2d-x获取系统时间,毫秒级的 long ge ...

随机推荐

  1. 158. Read N Characters Given Read4 II - Call multiple times

    题目: The API: int read4(char *buf) reads 4 characters at a time from a file. The return value is the ...

  2. noproguard.classes-with-local.dex

    make: *** [out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/noproguard.classes-with-loca ...

  3. Getting Started Tutorial

    Getting Started Tutorial The topics contained in this section are intended to give you quick exposur ...

  4. poj2686 Traveling by Stagecoach

                    http://poj.org/problem?id=2686                                                  Trav ...

  5. 读取编码为utf-16le的文件并打印

    import codecs data = open('test.txt').read()if data[:2] == codecs.BOM_UTF16_LE:    data = data[2:]  ...

  6. debian下安装AMD驱动

    参考:http://blog.sciencenet.cn/blog-296919-464464.html 去AMD官网下载对应的驱动: amd-driver-installer-catalyst-13 ...

  7. MFC之RTTI与动态创建

    本人能力.精力有限,所言所感都基于自身的实践和有限的阅读.查阅,如有错误,欢迎拍砖,敬请赐教——博客园:钱智慧. 在说RTTI之前需要明白c++中类静态成员的初始化特点:类的静态数据成员需要在类体外显 ...

  8. Jlink烧写图文教程

    ,点击安装          , 插上jlink,安装驱动后,红灯一直亮                                                                 ...

  9. arm linux kernel启动之start_kernel

    了解完kernel启动以前的汇编之后我们来看看正式的c语言启动代码,也就是我们的start_kernel函数了.start_kernel相当大,里面每一个调用到的函数都足够我们伤脑筋了,我这里只是浅尝 ...

  10. BestCoder Round #81 (div.2) B Matrix

    B题...水题,记录当前行是由原矩阵哪行变来的. #include<cstdio> #include<cstring> #include<cstdlib> #inc ...