c++怎么实现string与time互相转换

c++
878
2023/7/9 16:03:59
栏目: 编程语言
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

#include 
#include 
#include 
using namespace std;

time_t string2time_t(const string string_time);
string time_t2string(const time_t time_t_time);
int main()
{
    string strTime = "2014/03/28 18:25:26";
    time_t time1 = string2time_t(strTime);
    cout<tm_year += 1900;
    pTm->tm_mon +=1;

    sprintf_s(szTime, "%04d/%02d/%02d %02d:%02d:%02d",
        pTm->tm_year,
        pTm->tm_mon,
        pTm->tm_mday,
        pTm->tm_hour,
        pTm->tm_min,
        pTm->tm_sec);

    string strTime = szTime;

    delete pTm;
    pTm = NULL;

    return strTime;
}
string get_time()

{ string?timeStr; //time_t?t?=?time(0); time_t?t?=?time(NULL); char?tmp[64]; strftime(?tmp,?sizeof(tmp),?"%Y-%m-%d?%X?",localtime(&t)?);//?2017-05-16?17:00:00 ? timeStr?=?tmp; return?timeStr; }

time_t?convert_string_to_time_t(const?std::string?&?time_string)
{
struct?tm?tm1;
time_t?time1;
sscanf(time_string.c_str(),?“%d-%d-%d?%d:%d:%d”?,
&(tm1.tm_year),
&(tm1.tm_mon),
&(tm1.tm_mday),
&(tm1.tm_hour),
&(tm1.tm_min),
&(tm1.tm_sec));
?
tm1.tm_year?-=?1900;
tm1.tm_mon?–;
tm1.tm_isdst=-1;
time1?=?mktime(&tm1);
?
return?time1;
}

辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读: C++中delete关键字的使用场景有哪些