C++实现日期转换类DateTime

概述

工作中我们在网络传输时使用time_t来传输时间,在显示时使用字符串来显示,下面是一个日期转换类的实现,方便以后使用:

// DateTime.hpp
#ifndef _DATETIME_H
#define _DATETIME_H

#include <ctime>
#include <string>
#include <type_traits>

class DateTime
{
public:
    template<typename T>
    static typename std::enable_if<std::is_same<std::string, T>::value, std::string>::type convert(time_t t)
    {
        return time2string(t);
    }

    template<typename T>
    static typename std::enable_if<std::is_same<time_t, T>::value, time_t>::type convert(const std::string& timeStr)
    {
        return string2time(timeStr);
    }

    static std::string currentTime()
    {
        return time2string(time(nullptr));
    }

private:
    static std::string time2string(time_t t)
    {
        struct tm* tmNow = localtime(&t);
        char timeStr[sizeof("yyyy-mm-dd hh:mm:ss")] = {''};
        std::strftime(timeStr, sizeof(timeStr), "%Y-%m-%d %H:%M:%S", tmNow);
        return timeStr;
    }
    
    static time_t string2time(const std::string& timeStr)
    {
        struct tm stTm;
        sscanf(timeStr.c_str(), "%d-%d-%d %d:%d:%d",
               &(stTm.tm_year),
               &(stTm.tm_mon),
               &(stTm.tm_mday),
               &(stTm.tm_hour),
               &(stTm.tm_min),
               &(stTm.tm_sec));

        stTm.tm_year -= 1900;
        stTm.tm_mon--;
        stTm.tm_isdst = -1;

        return mktime(&stTm);
    }
};

#endif

下面是DateTime的具体使用例子:

// main.cpp
#include <iostream>
#include "DateTime.hpp"

int main()
{
    std::string timeStr = DateTime::convert<std::string>(time(nullptr));
    std::cout << timeStr << std::endl;
    std::cout << DateTime::convert<time_t>(timeStr) << std::endl;
    std::cout << DateTime::currentTime() << std::endl << std::endl;

    return 0;
}

该例子的github地址:https://github.com/chxuan/samples/tree/master/DateTime

原文地址:https://www.cnblogs.com/highway-9/p/5757113.html