C++时间管理

在编写程序的时候我们经常需要获取系统当前的时间,在之后我们免不了与系统API打交道。C++11添加了一个时间管理模块,从而使我们能够从语言层面获取到系统时间(C++11引入的东西确实方便了很多)。这里我们对其进行简单的封装,能够有效的获取系统当前的时间:

#ifndef HEADER_CTIME
#define HEADER_CTIME

#include <mutex>

class CMemaryPool;
class CTime
{
public:
    CTime();
    ~CTime();
    CTime(CTime const& t);

    void Now();
    std::string GetDateStr();
    int GetDate();
    int GetYearDay();
    int GetMonthDay();
    int GetWeekDay();
    int GetMonth();
    int GetYear();
    int GetHour();
    int GetMin();
    int GetSec();
    int GetMsec();

    //return xxxx xx xx-xx:xx:xx
    std::string GetFormatTime();
    //return xxxx xx xx-xx:xx:xx
    bool GetFormatTime(char* res, int len);

    bool operator==(CTime const& t);
    bool operator>(CTime const& t);
    bool operator>=(CTime const& t);
    bool operator<(CTime const& t);
    bool operator<=(CTime const& t);

private:
    time_t      _time;
    std::tm     _tm;
    std::mutex  _mutex;
};

#endif
#include <ctime>
#include <chrono>

#include "Time.h"

CTime::CTime() {
    Now();
}

CTime::~CTime() {

}

CTime::CTime(CTime const& t) {
    memcpy(this, &t, sizeof(*this));
}

std::string CTime::GetDateStr() {
    char tmp[32];
    sprintf_s(tmp, "%04d%02d%02d", (_tm.tm_year + 1900), _tm.tm_mon + 1, _tm.tm_mday);
    return tmp;
}

int CTime::GetDate() {
    char tmp[32];
    sprintf_s(tmp, "%04d%02d%02d", (_tm.tm_year + 1900), _tm.tm_mon + 1, _tm.tm_mday);
    return atoi(tmp);
}

int CTime::GetYearDay() {
    return _tm.tm_yday;
}

int CTime::GetMonthDay() {
    return _tm.tm_mday;
}

int CTime::GetWeekDay() {
    return _tm.tm_wday;
}

int CTime::GetMonth() {
    return _tm.tm_mon;
}

int CTime::GetYear() {
    return _tm.tm_year + 1900;
}

int CTime::GetHour() {
    return _tm.tm_hour;
}

int CTime::GetMin() {
    return _tm.tm_min;
}

int CTime::GetSec() {
    return _tm.tm_sec;
}

int CTime::GetMsec() {
    return _time;
}

std::string CTime::GetFormatTime() {
    char res[32] = { 0 };
    sprintf_s(res, "%04d %02d %02d-%02d:%02d:%02d", (_tm.tm_year + 1900), _tm.tm_mon + 1, _tm.tm_mday, _tm.tm_hour, _tm.tm_min, _tm.tm_sec);
    return std::move(std::string(res));
}

bool CTime::GetFormatTime(char* res, int len) {
    if (len < 32) {
        return false;
    }
    snprintf(res, 32, "%04d %02d %02d-%02d:%02d:%02d", (_tm.tm_year + 1900), _tm.tm_mon + 1, _tm.tm_mday, _tm.tm_hour, _tm.tm_min, _tm.tm_sec);
    return true;
}

void CTime::Now() {
    _time = std::chrono::time_point_cast<std::chrono::milliseconds>(std::chrono::system_clock::now()).time_since_epoch().count();
    time_t it = _time / 1000;
    {
        std::unique_lock<std::mutex> lock(_mutex);
        _tm = *std::gmtime(&it);
    }
}

bool CTime::operator==(CTime const& t) {
    return _time == t._time;
}

bool CTime::operator>(CTime const& t) {
    return _time > t._time;
}

bool CTime::operator>=(CTime const& t) {
    return _time >= t._time;
}

bool CTime::operator<(CTime const& t) {
    return _time < t._time;
}

bool CTime::operator<=(CTime const& t) {
    return _time <= t._time;
}

这个好像没有什么需要说明的…不贴源码都不知道要写什么。
GitHub:https://github.com/caozhiyi/Base

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值