免费注册 查看新帖 |

Chinaunix

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

在linux中,怎样用c/c++将指定字符串转换成时间格式 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-08-08 20:49 |只看该作者 |倒序浏览
比如:将“Sat Oct 28 10 : 12 : 05 2000” 转换成time_t类型
我只知道将time_t类型转换为string类型,反过来就不知道了,
路过的帮忙顶一下,回帖有分

论坛徽章:
0
2 [报告]
发表于 2006-08-08 21:49 |只看该作者
原帖由 lovesu 于 2006-8-8 20:49 发表
比如:将“Sat Oct 28 10 : 12 : 05 2000” 转换成time_t类型
我只知道将time_t类型转换为string类型,反过来就不知道了,
路过的帮忙顶一下,回帖有分

>>
>>
>>如果你的字符格式固定,那就容易多了。可以使用scanf等函数赋值给时间参数
>>就可以了。如果字符格式不固定,那就不好处理了。
>>
>>

论坛徽章:
0
3 [报告]
发表于 2006-08-09 15:23 |只看该作者
字符串固定,怎么赋值,请问高手

论坛徽章:
0
4 [报告]
发表于 2006-08-09 19:21 |只看该作者
原帖由 lovesu 于 2006-8-9 15:23 发表
字符串固定,怎么赋值,请问高手

>>
>>
>>大致就像:
>>
>>
//Sat Oct 28 10 : 12 : 05 2000
#include <stdio.h>

int
mian(){
    char t_string[] = "Sat Oct 28 10 : 12 : 05 2000";
    char t_week[3];
    char t_mon[3];
    unsigned int t_day;
    unsigned int t_hour;
    unsigned int t_min;
    unsigned int t_sec;
    unsigned int t_year;
   
    sscanf(t_string, "%s %s %d %d : %d : %d %d",
           t_week, t_mon, &t_day, &t_hour, &t_min, &t_sec, &t_year);
           
    printf("%s %s %d %d : %d : %d %d\n",
           t_week, t_mon, t_day, t_hour, t_min, t_sec, t_year);
    return 1;
}

论坛徽章:
0
5 [报告]
发表于 2006-08-09 19:22 |只看该作者
原帖由 Bayweb 于 2006-8-9 19:21 发表

>>
>>
>>大致就像:
>>
>>
//Sat Oct 28 10 : 12 : 05 2000
#include <stdio.h>

int
mian(){
    char t_string[] = "Sat Oct 28 10 : 12 : 05 2000" ...

>>
>>
>>之后将各个数据转换为你要的类型就可以了.
>>
>>
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP