欢迎大家来到IT世界,在知识的湖畔探索吧!
linux获取时间api
1:gettimeofday();
2:time()
3:localtime_r()
4:settimeofday()
对应接口定义可以使用man查看
头文件include<sys/time>
或者include<time>
例子:
int main()
{
struct timeval tv;
gettimeofday(&tv,NULL);
printf(“sec:%ld,usec:%ld”,tv.tv_sec,tv.tv_usec);//微秒
}
例子设置时间
void setsystime(unsigned char hour, unsigned char min, unsigned char sec)
{
time_t timep;
struct tm stTime;
struct timeval tv;
time(&timep);
localtime_r(&timep;&stTime);
stTime.tm_hour = hour;
stTime.tm_min = min;
stTime.tm_sec = sec;
tv.tv_sec = (long)mktime(&stTime);
tv.tv_usec = 0;
settimeofday(&tv, (struct timezone*)0);
}
获取系统时间
int getsystime(unsigned char *hour, unsigned char *min, unsigned char *sec)
{
if((NULL == hour)||(NULL == min)||(NULL == sec))
{
return -1;
}
time_t timep;
struct tm tmnow;
time(&timep);
localtime_r(&timep,&tmnow);
*sec = (unsigned char)tmnow.tm_sec;
*min = (unsigned char)tmnow.tm_min;
*hour = (unsigned char)tmnow.tm_hour;
return 0;
}
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://itzsg.com/34615.html