1 #include<stdio.h>
2 #include<time.h>
3 #include <iostream>
4 #include <time.h>
5 #include <cstdio>
6 using namespace std;
7 time_t convert_string_to_time_t(const std::string & time_string)
8 {
9 struct tm tm1;
10 time_t time1;
11 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));
12
13 tm1.tm_year -= 1900;
14 tm1.tm_mon --;
15 tm1.tm_isdst=-1;
16 time1 = mktime(&tm1);
17
18 return time1;
19 }
20
21 int main(int argc, char *argv[])
22 {
23 std::string date_string("2010-11-20 18:08:01");
24 time_t tmp_time;
25 tmp_time = convert_string_to_time_t(date_string);
26 cout<<tmp_time<<endl;
27 struct tm *p;
28 p = localtime(&tmp_time);
29 p->tm_year = p->tm_year + 1900;
30 p->tm_mon = p->tm_mon + 1;
31 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);
32 }