c语言字符串转为时间戳,C语言实现字符转UNIX时间戳

strptime函数:https://zhidao.baidu.com/question/235245979.html

char *strptime(const char *restrict buf, const char *restrict format, struct tm *restrict tm);

buf指向一个字符串格式的时间,函数将这个时间用format表示的格式解析,存放到tm中去

例子:

strptime(“6 Dec 2001 12:33:45”, “%d %b %Y %H:%M:%S”, &tm);

返回值:

解析成功返回最后解析字符的下一个字符的地址,失败返回NULL

mktime函数:

time_t mktime(strcut tm * timeptr);

mktime()函数用来将timeptr所指的tm数据结构转换成从公元1970年1月1日0时0分0秒算起至今的UTC时间所经过的秒数

返回值:

经过秒数

将时间字符串转换为UNIX时间戳

int64_t str_to_time(const char *time_str)

{

struct tm tm_time;

int64_t unixtime;

strptime(time_str, “%Y-%m-%d %H:%M:%S”, &tm_time);

unixtime=mktime(&tm_time);

return unixtime;

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值