字符串转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); }
点击查看代码
相关文章推荐
- 如何在android studio上加入OpenCV库
- SpringMVC 常见异常处理
- Android Studio使用9patch图片报错
- JavaScript OOP
- KeilMDK配置项中Use MicroLIB是干什么的
- oracle如何查看当前有哪些用户连接到数据库
- win7 ubuntu默认启动配置
- poj 1161 Floyd+枚举
- 手动创建image
- 利用vbs 运行程序 并使窗口隐藏
- [leetcode][math] Factorial Trailing Zeroes
- 第十三周(动物这样叫)
- 第13周项目2-形状类族的中的纯虚函数
- 关于layer-list在xml和代码中的用法ProgressBar
- Mac Error Create Android Project - “Errors running builder 'Android Resource Manager' on project”
- VMware workstation的理解【功能和作用以及原理】
- Android项目导出为jar包后调用外部资源的问题
- Android Studio导入gradle项目问题解决
- C++ goto 在g++ 编译时出现 crosses initialization 和 jump to label xxx [-fpermissive] 错误
- 列出排列组合2^n种可能的方法