时间和game后台
来源:互联网 发布:ubuntu 光盘安装 编辑:程序博客网 时间:2024/05/16 17:45
1.计算机"时间"的概念,世界时间和本地时间
世界时间:简单的时候就是不同国家的日出、日落时间必定有所偏差,这些偏差就是所谓的时差。所以需要一
个统一的时间作为各国的换算标准,根据世界时间和时差换算成本地时间。
整个地球分为二十四时区(一天24小时嘛),每个时区都有自己的本地时间,北京时间是UCT时间加上8小时
而UCT就是世界时间,UCT+8就是我们的本地时间
2."时间"在游戏后台里的重要性
游戏后台里很多系统都跟时间有关(这个时间一般使用本地时间)如果有些任务一天只能做一次,奖励是1个在
线小时领取一次,技能的buffer/debuffer持续30s等。
localtime函数获得1970-1-1零点零分到当前时间系统所偏移的秒数时间转换为日历时间
struct tm *localtime(const time_t *clock); 函数返回struct tm结构体,该结构体存储时间的
各个分量年月日时分秒
struct tm
{
int tm_sec; /* seconds after the minute - [0,59] */
int tm_min; /* minutes after the hour - [0,59] */
int tm_hour; /* hours since midnight - [0,23] */
int tm_mday; /* day of the month - [1,31] */
int tm_mon; /* months since January - [0,11] */
int tm_year; /* years since 1900 */
int tm_wday; /* days since Sunday - [0,6] */
int tm_yday; /* days since January 1 - [0,365] */
int tm_isdst; /* daylight savings time flag */
};
这里要注意
tm_year是从1900为基数的,比如tm_yea=113时间的年为113+1900=2013
tm_mon不是1-12,是0-11,所以0表示的是1月,1是2月这样推算
mktime函数
time_t mktime(struct tm * timeptr);
mktime()用来将参数timeptr所指的tm结构数据转换成从公元1970年1月1日8时0分0 秒算起至今的UTC时间所
经过的秒数
3.一个时间class的实现
有了上面的基本概念和相关函数,下面我们来实现一个Time有关的类
该类的基本需求
1.能把time_t和struct tm相关转换
2.能获得当前的年月日时分秒
3.能比较2个时间类大小和做+,- 基本运算
class CLibTime
{
public:
//构造函数
CLibTime() {}
CLibTime(time_t time) { m_time = time; }
CLibTime(struct tm t)
{
m_time = mktime(&t);
}
time_t GetTime() { return m_time; }
CLibTime(int nYear, int nMonth, int nDay, int nHour, int nMin, int nSec, int nDST = -1)
{
struct tm atm;
atm.tm_sec = nSec;
atm.tm_min = nMin;
atm.tm_hour = nHour;
atm.tm_mday = nDay;
atm.tm_mon = nMonth - 1;
atm.tm_year = nYear - 1900; //这里基数是1900开始,所以要减去1900
atm.tm_isdst = nDST;
m_time = mktime(&atm); //mktime()用来将参数timeptr所指的tm结构数据转换成从公元1970年1月1日8时0分0 秒算起至今的UTC时间所经过的秒数。
}
CLibTime(const string& strDateTime)
{
struct tm stTm;
if (strDateTime.length() == 19) // YYYY-mm-dd HH:MM:SS
{
stTm.tm_year = strtol(strDateTime.substr(0, 4).c_str(), NULL, 10) - 1900;
stTm.tm_mon = strtol(strDateTime.substr(5, 2).c_str(), NULL, 10) - 1;
stTm.tm_mday = strtol(strDateTime.substr(8, 2).c_str(), NULL, 10);
stTm.tm_hour = strtol(strDateTime.substr(11, 2).c_str(), NULL, 10);
stTm.tm_min = strtol(strDateTime.substr(14, 2).c_str(), NULL, 10);
stTm.tm_sec = strtol(strDateTime.substr(17, 2).c_str(), NULL, 10);
m_time = mktime(&stTm);
if (m_time == -1)
{
m_time = 0;
}
}
}
CLibTime(const CLibTime& OtherTime)
{
m_time = OtherTime.m_time;
}
const CLibTime& operator=(const CLibTime& timeSrc)
{
m_time = timeSrc.m_time;
return *this;
}
const CLibTime& operator=(time_t t)
{
m_time = t; return *this;
}
//返回年月日时分秒接口
struct tm* GetLocalTm(struct tm* ptm = NULL) const
{
if (ptm != NULL)
{
struct tm* ptmTemp = localtime(&m_time);
if (ptmTemp == NULL)
{
return NULL;
}
*ptm = *ptmTemp;
return ptm;
}
else
{
return localtime(&m_time);
}
}
time_t GetTime() const { return m_time; }
int GetYear() const { return (GetLocalTm(NULL)->tm_year) + 1900; }
int GetMonth() const { return GetLocalTm(NULL)->tm_mon + 1; }
int GetDay() const { return GetLocalTm(NULL)->tm_mday; }
int GetHour() const { return GetLocalTm(NULL)->tm_hour; }
int GetMinute() const { return GetLocalTm(NULL)->tm_min; }
int GetSecond() const { return GetLocalTm(NULL)->tm_sec; }
/**
* 返回星期几.
* @return 0=星期天 1=星期一
*/
int GetDayOfWeek() const
{
return GetLocalTm(NULL)->tm_wday;
}
bool operator==(const CLibTime& time) const { return m_time == time.m_time; }
bool operator!=(const CLibTime& time) const { return m_time != time.m_time; }
bool operator<(const CLibTime& time) const { return m_time < time.m_time; }
bool operator>(const CLibTime& time) const { return m_time > time.m_time; }
bool operator<=(const CLibTime& time) const { return m_time <= time.m_time; }
bool operator>=(const CLibTime& time) const { return m_time >= time.m_time; }
//格式化
std::string Format(const char* pszFormat) const
{
time_t time = m_time;
struct tm* ptmTemp = localtime(&time);
char szBuffer[256];
if (ptmTemp == NULL || !strftime(szBuffer,sizeof(szBuffer), pszFormat, ptmTemp))
{
return "";
}
return std::string(szBuffer);
}
private:
time_t m_time;
};
4.如何运用
//使用CLibTime
class CTTimeUtils
{
public:
/**
* 比较两个时间是否是同一天
* @return true/false
*/
static bool IsSameDay(time_t tTime1, time_t tTime2)
{
CLibTime tmTime1(tTime1);
CLibTime tmTime2(tTime2);
return (tmTime1.GetYear() == tmTime2.GetYear()
&& tmTime1.GetMonth() == tmTime2.GetMonth()
&& tmTime1.GetDay() == tmTime2.GetDay());
}
/**
* 计算两个时间的差
* @return 返回差值, 单位微妙
*/
static int Diff(const struct timeval& tv1, const struct timeval& tv2)
{
return (tv1.tv_sec - tv2.tv_sec) * 1000000 +
(tv1.tv_usec - tv2.tv_usec);
}
//判断是否为星期天
static bool IsSundays(time_t time)
{
CLibTime tmTime1(tTime1);
if (tmTime1.GetDayOfWeek() == 0)
{
return true;
}
else
{
return false;
}
}
}
- 时间和game后台
- iOS当前时间和后台返回时间做比较大小
- WordPress: 后台添加 用户注册时间 和登录IP 显示
- Java后台时间转换
- iOS4 请求更多后台时间
- iOS4 请求更多后台时间
- iOS4 请求更多后台时间
- ios 后台长短时间运行
- app后台任务挂起时间
- (最新)iOS后台运行时间
- iOS后台运行延长时间
- 在前后台传输时间数据
- magento 后台模块开发之设定时间 显示小时和分钟
- web开发Java后台和jsp页面时间格式的转换
- 定时请求php 后台 AJAX 请求,在页面中倒计时,和显示刷新时间
- Jump Game 和Jump Game II---LeetCode
- Game 和Screen分析
- Jump Game低时间复杂度版
- node.js下express的AJAX通讯:jsonp,json
- cocos2d-x学习笔记-CCAction(动作)
- 静态局部变量求阶乘
- Android SDK各版本与API level对应关系
- 今天又写了个鼠标划过显示不同图片的JS
- 时间和game后台
- emacs info 使用方法
- 快速排序法学习心得
- php url 重写 静态化
- 一个标准课程
- 计算日期是一年中的第几天
- {嵌入式}之TQ2440(天嵌)小记
- 计算日期是一年中的第几天(不用函数)
- Acronis Disk Director Suite(windows7分区工具)