linux下c/c++ 常用时间和字符串互相转化介绍
阅读(680) | 评论(0) | 转发(0) |
<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>
c/c++ 中经常会遇到时间和字符串互相转化的情形
用以下2个函数来转就很方便了
1、时间转字符串函数
size_t strftime( char *strDest, size_t maxsize, const char *format, const struct tm *timeptr );
2、字符串转时间函数
char *strptime(const char *s, const char *format, struct tm *tm);
- #include
- #include
- int main()
- {
- struct tm tm_time;
- strptime("2010-11-15 10:39:30", "%Y-%m-%d %H:%M:%S", &tm_time);
- printf("%ld/n", mktime(&tm_time));
- printf("-------------------------------------/n");
- char szBuf[256] = {0};
- time_t timer = time(NULL);
- strftime(szBuf, sizeof(szBuf), "%Y-%m-%d %H:%M:%S", localtime(&timer));
- printf("%s/n", szBuf);
- return 0;
- }
给主人留下些什么吧!~~
评论热议