【转】cocos2d-x获取系统时间——2013-08-25 10
欢迎转载,本帖地址: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的更多相关文章
- Android获取系统时间方法的总结
Android获取系统时间方法的方法有很多种,常用的有Calendar.Date.currentTimeMills等方法. (1)Calendar Calendar获取系统时间首先要用Calendar ...
- 用PHP获取系统时间时,时间比当前时间少8个小时
自PHP5.0开始,用PHP获取系统时间时,时间比当前时间少8个小时.原因是PHP.ini中没有设置timezone时,PHP是使用的UTC时间,所以在中国时间要少8小时. 解决办法: 1.在PHP. ...
- C/C++获取系统时间
C/C++获取系统时间需要使用Windows API,包含头文件"windows.h". 系统时间的数据类型为SYSTEMTIME,可以在winbase.h中查询到如下定义: ty ...
- VC++编程中获取系统时间
<span style="white-space:pre"> </span>总结了在程序中如何获得系统时间的方法 void CGetSystenTimeDl ...
- cocos2d-x 获取系统时间
转自:http://blog.csdn.net/jinjian2009/article/details/9449585 之前使用过cocos2d-x获取系统时间,毫秒级的 long getCurren ...
- C++11新特性,利用std::chrono精简传统获取系统时间的方法
一.传统的获取系统时间的方法 传统的C++获取时间的方法须要分平台来定义. 相信百度代码也不少. 我自己写了下,例如以下. const std::string getCurrentSystemTime ...
- c++ 怎样获取系统时间
c++ 怎样获取系统时间 2008-04-28 15:34 //方案— 长处:仅使用C标准库:缺点:仅仅能精确到秒级 #include <time.h> #include <stdi ...
- Linux C 语言 获取系统时间信息
比如获取当前年份: /* 获取当前系统时间 暂时不使用 int iyear = 0; int sysyear = 0; time_t now; ...
- cocos2d-x获取系统时间
欢迎转载,本帖地址:http://blog.csdn.net/jinjian2009/article/details/9449585 之前使用过cocos2d-x获取系统时间,毫秒级的 long ge ...
随机推荐
- 【Lucene3.6.2入门系列】第03节_简述Lucene中常见的搜索功能
package com.jadyer.lucene; import java.io.File; import java.io.IOException; import java.text.SimpleD ...
- 公司估值(贴现现金流量法DCF)
创业公司总会遇到并购或者入股等情况,CEO需要了解一些公司估值的方法,本文主要介绍贴现现金流量估值方法,供大家参考: 中国资产评估协会要求:在对企业价值进行评估时,应分析收益法.市场法和资产基础法三种 ...
- Android开发UI之去掉title bar
去掉屏幕上的title bar有3个方法: 1.java代码实现: @Override publicvoid onCreate(Bundle savedInstanceState) { super.o ...
- MVC——数据库增删改查(aspx)
MVC: V(View) :视图→就是页面的模板 C(Control): 控制器→客户主要面对的就是控制器, M(Model):模板→在模板里面主要就是写关于数据库的各种增删改查的方法 它们之间的关系 ...
- bzoj1497
这道题让我涨姿势了 对于这类问题,我们称作最大权闭合图问题 就是每个点都有一个点权,要求选择一个点集,其中每个点的指向的点也在点集中,使这样一个点权和最大 对于这种问题,我们添加源点s,汇点t 对于点 ...
- BZOJ_2179_FFT快速傅立叶_(FFT)
描述 http://www.lydsy.com/JudgeOnline/problem.php?id=2179 超大整数乘法 分析 FFT模板题. 把数字看成是多项式,x是10.然后用FFT做多项式乘 ...
- Luogu_1565_牛宫_(最大子矩阵)
描述 http://www.luogu.org/problem/show?pid=1565 给出一个n*m的矩阵,求最大的且和值为正的子矩阵. 分析 很容易想到的是用前缀和维护,暴力枚举左上角和右下角 ...
- GAC的理解及其作用
转:http://www.cnblogs.com/smallstone/archive/2010/06/29/1767508.html 一.GAC的作用 全称是Global Assembly Cach ...
- BrnMall多店版网上商城正式发布
前些日子一直忙于多店版网上商城系统BrnMall的开发,工作比较多,所以博客断了.这几天项目完成了,时间比较自由,所以把这段时间总结的一些关于单店版BrnShop和多店版BrnMall区别写下来,希望 ...
- CentOS6.x安装配置nginx [转]
博文来源:http://leyewen.blog.163.com/ nginx安装 nginx的官网:http://nginx.org/ 相应下载页面:http://nginx.org/en/ ...