免费注册 查看新帖 |

Chinaunix

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

linux 下时间函数.... [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-03-15 15:09 |只看该作者 |倒序浏览
linux c下有现成的将时间转换成秒的函数吗...
例如: 2010-10-15 18:34:35  转换成秒.........

多谢............

论坛徽章:
0
2 [报告]
发表于 2010-03-15 15:10 |只看该作者
asctime

论坛徽章:
0
3 [报告]
发表于 2010-03-15 15:16 |只看该作者
mktime

论坛徽章:
0
4 [报告]
发表于 2010-03-15 15:18 |只看该作者
#include <time.h>

       char *asctime(const struct tm *tm);
       char *asctime_r(const struct tm *tm, char *buf);

       char *ctime(const time_t *timep);
       char *ctime_r(const time_t *timep, char *buf);

       struct tm *gmtime(const time_t *timep);
       struct tm *gmtime_r(const time_t *timep, struct tm *result);

       struct tm *localtime(const time_t *timep);
       struct tm *localtime_r(const time_t *timep, struct tm *result);

       time_t mktime(struct tm *tm);

论坛徽章:
0
5 [报告]
发表于 2010-03-15 15:18 |只看该作者
我只要转换, 当前系统时间不能改变........

论坛徽章:
0
6 [报告]
发表于 2010-03-15 15:24 |只看该作者
我只要转换, 当前系统时间不能改变........
ruchong 发表于 2010-03-15 15:18



    楼上给你贴的都是转换用的。不过Ctime系列函数已经过时,strftime使用格式化的方法,是Ctime系列函数的替代。
    man strftime

论坛徽章:
4
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:56:11IT运维版块每日发帖之星
日期:2016-08-11 06:20:00IT运维版块每日发帖之星
日期:2016-08-15 06:20:00
7 [报告]
发表于 2010-03-15 15:33 |只看该作者
回复 1# ruchong

没有现成的函数,需要自己实现。把年月日等各个字段拆分开,然后使用mktime这个函数。

论坛徽章:
0
8 [报告]
发表于 2010-03-15 15:50 |只看该作者
谢谢大家. 大家帮忙看一下哪个地方出问题了......
int main()
{
    struct tm t;
    t.tm_sec = 1;
    t.tm_min = 30;
    t.tm_hour = 9;
    t.tm_mday = 22;
    t.tm_mon = 11;
    t.tm_year = 2010;
    time_t ti = mktime(&t);
    if(-1 == ti)
    {
        printf("error = %s\n", strerror(errno));
        printf("ti = %ld\n", ti);
    }
}

输出:
localhost test # ./a.out
error = Success
ti = -1

论坛徽章:
0
9 [报告]
发表于 2010-03-15 16:14 |只看该作者
tm_year        //指1900年开始经过的时间
所以应该用2010-1900吧

论坛徽章:
4
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:56:11IT运维版块每日发帖之星
日期:2016-08-11 06:20:00IT运维版块每日发帖之星
日期:2016-08-15 06:20:00
10 [报告]
发表于 2010-03-15 16:24 |只看该作者
tm_year        //指1900年开始经过的时间
所以应该用2010-1900吧
empty141 发表于 2010-03-15 16:14


楼上正解。
tm_year中的年份等于实际年份 - 1900,月份tm_mon等于实际月份 - 1
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP