免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4155 | 回复: 9
打印 上一主题 下一主题

C 日期处理函数? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-03-22 17:41 |只看该作者 |倒序浏览
各位仁兄:
请问:
C 中有没有将“2010-12-10 10:01:25”这个日期字符串转化成日历时间的函数?

论坛徽章:
0
2 [报告]
发表于 2011-03-22 17:46 |只看该作者

  1.        
  2. mktime(将时间结构数据转换成经过的秒数)
  3. 相关函数
  4.         time,asctime,gmtime,localtime
  5. 表头文件
  6.         #include<time.h>
  7. 定义函数
  8.         time_t mktime(strcut tm * timeptr);
  9. 函数说明
  10.         mktime()用来将参数timeptr所指的tm结构数据转换成从公元1970年1月1日0时0分0 秒算起至今的UTC时间所经过的秒数。
  11. 返回值
  12.         返回经过的秒数。
  13. 范例
  14.         /* 用time()取得时间(秒数),利用localtime()
  15. 转换成struct tm 再利用mktine()将struct tm转换成原来的秒数*/
  16. #include<time.h>
  17. main()
  18. {
  19. time_t timep;
  20. strcut tm *p;
  21. time(&timep);
  22. printf(“time() : %d \n”,timep);
  23. p=localtime(&timep);
  24. timep = mktime(p);
  25. printf(“time()->localtime()->mktime():%d\n”,timep);
  26. }
  27. 执行
  28.         time():974943297
  29. time()->localtime()->mktime():974943297
  30.  
复制代码
http://man.chinaunix.net/develop/c&c++/linux_c/function/04.html#linuxc37

论坛徽章:
0
3 [报告]
发表于 2011-03-22 17:50 |只看该作者
strptime ()这个函数将字符串转换日期
win系统下好像没有这个函数

论坛徽章:
0
4 [报告]
发表于 2011-03-22 17:51 |只看该作者
看来需要自己把字符串拆分出来呀,本来以为库中直接由函数可以用的

论坛徽章:
0
5 [报告]
发表于 2011-03-22 18:02 |只看该作者
mktime的时候,要注意时区之差吧?
要减去时区差的。。

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
6 [报告]
发表于 2011-03-22 18:59 |只看该作者
不行。你的字符串太没有规律了。

论坛徽章:
0
7 [报告]
发表于 2011-03-22 19:04 |只看该作者
需要自己去解析了 感觉自己写也不费什么事 这个轮子还是可以造的...

论坛徽章:
1
2015年迎新春徽章
日期:2015-03-04 09:56:11
8 [报告]
发表于 2011-03-22 20:00 |只看该作者
自己搞。

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
9 [报告]
发表于 2011-03-22 21:46 |只看该作者
各位仁兄:
请问:
C 中有没有将“2010-12-10 10:01:25”这个日期字符串转化成日历时间的函数?
wisage 发表于 2011-03-22 17:41
随便写了一个有点笨拙的。
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <string.h>

  5. int main(void)
  6. {
  7.         struct tm t_struct;
  8.         char buf[256];
  9.         char t_str[] = "2010-12-10 10:01:25";
  10.         time_t cal_time;
  11.         strncpy(buf, t_str, 4);
  12.         t_struct.tm_year = atoi(buf) - 1900;
  13.         memset(buf, 0, 4);

  14.         strncpy(buf, t_str + 5, 2);
  15.         t_struct.tm_mon = atoi(buf);

  16.         strncpy(buf, t_str + 8, 2);
  17.         t_struct.tm_mday = atoi(buf);

  18.         strncpy(buf, t_str + 11, 2);
  19.         t_struct.tm_hour = atoi(buf);

  20.         strncpy(buf, t_str + 14, 2);
  21.         t_struct.tm_min = atoi(buf);

  22.         strncpy(buf, t_str + 17, 2);
  23.         t_struct.tm_sec = atoi(buf);

  24.         cal_time = mktime(&t_struct);
  25.         printf("The result is: %ld\n", (long int)cal_time);
  26.         exit(0);
  27. }
复制代码
验证了一下结果:
  1. $ ./time_convert
  2. The result is: 1294624885
复制代码
不知道为什么有偏差,和下面的比较:
  1. $ echo | awk '{print mktime("2010 12 10 10 01 25")}'
  2. 1291946485
复制代码

论坛徽章:
15
射手座
日期:2014-11-29 19:22:4915-16赛季CBA联赛之青岛
日期:2017-11-17 13:20:09黑曼巴
日期:2017-07-13 19:13:4715-16赛季CBA联赛之四川
日期:2017-02-07 21:08:572015年亚冠纪念徽章
日期:2015-11-06 12:31:58每日论坛发贴之星
日期:2015-08-04 06:20:00程序设计版块每日发帖之星
日期:2015-08-04 06:20:00程序设计版块每日发帖之星
日期:2015-07-12 22:20:002015亚冠之浦和红钻
日期:2015-07-08 10:10:132015亚冠之大阪钢巴
日期:2015-06-29 11:21:122015亚冠之广州恒大
日期:2015-05-22 21:55:412015年亚洲杯之伊朗
日期:2015-04-10 16:28:25
10 [报告]
发表于 2011-03-24 12:12 |只看该作者
各位仁兄:
请问:
C 中有没有将“2010-12-10 10:01:25”这个日期字符串转化成日历时间的函数?
wisage 发表于 2011-03-22 17:41



    http://bbs.chinaunix.net/thread-2287078-2-3.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP