时间和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;
        }
    }
}

 

 

原创粉丝点击