您的位置:首页 > 其它

字符串转time_t

2015-06-02 12:09 267 查看
#include<stdio.h>
#include<time.h>
#include <iostream>
#include <time.h>
#include <cstdio>
using namespace std;
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;
}

int main(int argc, char *argv[])
{
std::string date_string("2010-11-20 18:08:01");
time_t tmp_time;
tmp_time = convert_string_to_time_t(date_string);
cout<<tmp_time<<endl;
struct tm *p;
p = localtime(&tmp_time);
p->tm_year = p->tm_year + 1900;
p->tm_mon = p->tm_mon + 1;
printf("date is %04d-%02d-%02d %02d:%02d:%02d\n", p->tm_year, p->tm_mon, p->tm_mday,p->tm_hour,p->tm_min,p->tm_sec);
}


点击查看代码
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: