加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C语言实现字符转unix时间戳的简单实例

发布时间:2020-12-16 05:24:03 所属栏目:百科 来源:网络整理
导读:C语言实现字符转unix时间戳,需要先转成tm类型,再得到它的Unix时间戳。附上实现代码: #include stdio.h#include time.hint strtotime(char datetime[]){struct tm tm_time;int unixtime;strptime(datetime,"%Y-%m-%d %H:%M:%S",unixtime = mktime(return u

C语言实现字符转unix时间戳,需要先转成tm类型,再得到它的Unix时间戳。附上实现代码:

#include <stdio.h>
#include <time.h>
int strtotime(char datetime[])
{
struct tm tm_time;
int unixtime;
strptime(datetime,"%Y-%m-%d %H:%M:%S",&tm_time);

unixtime = mktime(&tm_time);
return unixtime;
}


另附上几个时间相关的函数,做个笔记:

//当前时间


char* get_curr_time()
{
char* strtime = (char *)malloc(sizeof(char)*20);
memset(strtime,sizeof(char)*20);
time_t now;
time(&now);
strftime(strtime,20,gmtime(&now));
printf("Info: current time %sn",strtime);
return strtime;
}


//当前时间的unix时间戳


int get_curr_unixtime(void)
{
time_t now;
int unixtime = time(&now);
return unixtime;
}


//字符转unix时间戳


int strtotime(char datetime[])
{
struct tm tm_time;
int unixtime;
strptime(datetime,“%Y-%m-%d %H:%M:%S”,&tm_time);

unixtime = mktime(&tm_time);
return unixtime;
}

以上这篇C语言实现字符转unix时间戳的简单实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持编程小技巧。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读