【转】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 ...
随机推荐
- Fast UI Draw (Intel出品)
Fast UI Draw in a library that provides a higher performance Canvas interface. It is designed so tha ...
- gcc编译常用选项
我的博客:www.while0.com GDB调试: -g 生成的可执行文件才可以用gdb调试 (建议在发行版中用strip filename 来把这些调试信息去除) 开始调试. 以下是基础调试命令: ...
- rails3 Bundle简介
Rails 3开始使用bundle来管理项目的gem依赖.该命令只在一个含有Gemfile的目录下执行. Gemfile: Rails 项目所有的依赖包都在这里配置,不像以前通过require来查找 ...
- msdn上wcf的介绍
https://msdn.microsoft.com/zh-cn/library/dd456779(v=vs.110).aspx Windows Communication Foundation Ar ...
- Android开发UI之ViewPager及PagerAdapter
ViewPager,官网链接--http://developer.android.com/reference/android/support/v4/view/ViewPager.html ViewPa ...
- Redis 如何导出数据
Redis是一款支持多种数据类型的Key-Value数据库. 这里介绍下如何从Redis中导出数据. 数据是如何存储的?. Redis中是把数据保存到内存中的,但是它也会定期的把数据写会到硬盘中. R ...
- bzoj1492
好题+神题,首先肯定是dp,我们设f[i]为到第i天能获得的最多的B卷(设获得的钱数亦可)由题目hint可知,要么全买要么全卖,我们有f[i]=max(maxmoney,f[j]*b[i]+f[j]* ...
- bzoj2756
看到相邻格子都+1的操作一下就想到黑白染色了相邻格子都+1说明不管怎么弄,黑格子的总和和白格子总和的差总是定值这里首先要注意,最后不一定变成的是所有元素中的最大值,可能比它大比如 1 2 2 ...
- Centos 6.5中使用yum安装jdk
默认会有open-jdk的安装 我们需要卸载 卸载过程: 查找rpm安装包 rpm –aq|grep java; 卸载 rpm –e –nodeps 安装包 查看目前是否通过yum进行安装jdk yu ...
- 【转】【iOS知识学习】_视图控制对象生命周期-init、viewDidLoad、viewWillAppear、viewDidAppear、viewWillDisappear等的区别及用途
原文网址:http://blog.csdn.net/weasleyqi/article/details/8090373 iOS视图控制对象生命周期-init.viewDidLoad.viewWillA ...