c语言时间等待函数linux,常用C语言time时间函数
常见的时间函数有time( )、ctime( )、gmtime( )、localtime( )、mktime( )、asctime( )、difftime( )、gettimeofday( )、settimeofday( )
其中,gmtime和localtime函数差不多,只是localtime函数会按照时区输出,而gmtime是用于输出0时区的
常见的时间类型有
time_t
struct timeval(设置时间函数settimeofday( )与获取时间函数gettimeofday( )均使用该事件类型作为传参。)
struct tm,
struct timespec
使用gmtime( )和localtime( )可将time_t时间类型转换为tm结构体;
使用mktime( )将tm结构体转换为time_t时间类型;
使用asctime( )将struct tm转换为字符串形式。
//各个结构体的定义
struct tm{
int tm_sec; /*秒 - 取值区间为[0, 59]*/
int tm_min; /*分 - 取值区间为[0, 59]*/
int tm_hour; /*时 - 取值区间为[0, 23]*/
int tm_mday; /*日 - 取值区间为[1, 31]*/
int tm_mon; /*月份 - 取值区间为[0, 11]*/
int tm_year; /*年份 - 其值为1900年至今年数*/
int tm_wday; /*星期 - 取值区间[0, 6],0代表星期天,1代表星期1,以此类推*/
int tm_yday; /*从每年的1月1日开始的天数-取值区间为[0, 365],0代表1月1日*/
int tm_isdst; /*夏令时标识符,使用夏令时,tm_isdst为正,不使用夏令时,tm_isdst为0,不了解情况时,tm_isdst为负*/
};
Struct tmieval{
time_t tv_sec; /*秒s*/
suseconds_t tv_usec; /*微秒us*/
};
struct timespec{
time_t tv_sec; /*秒s*/
long tv_nsec; /*纳秒ns*/
};
现在我们来看一下使用这些函数的程序
首先是time()函数的使用
[root@bogon time]# cat time.c
#include
#include
#include
int main()
{
time_t seconds,sec,time1,time2;
struct tm *mytm,gettm;
seconds=time(NULL);
mytm=localtime(&seconds);//localtime的参数为time_t类型
sec=mktime(mytm);//mktime参数为结构体tm类型
time1=time(NULL);//time参数类型为time_t类型,或者为NULL也可以
sleep(1);//因为要difftime,所以让time1和time2不同
time2=time(NULL);
printf("use time: %ld\n",seconds);
printf("use ctime: %s",ctime(&seconds));//ctime的类型也为time_t类型
printf("use gmtime: %d-%d-%d\n",(mytm->tm_year)+1900,(mytm->tm_mon)+1,mytm->tm_mday);
printf("use mktime :%ld\n",sec);
printf("use asctime: %s",asctime(mytm));//跟ctime功能差不多,只是它的参数是结构体tm类型的
printf("use difftime: %lf\n",difftime(time1,time2));//计算time1-time2
return 0;
}
[root@bogon time]# gcc time.c
[root@bogon time]# ./a.out
use time: 1495946001
use ctime: Sat May 27 21:33:21 2017
use gmtime: 2017-5-27
use mktime :1495946001
use asctime: Sat May 27 21:33:21 2017
use difftime: -1.000000
[root@bogon time]#
c语言时间等待函数linux,常用C语言time时间函数相关推荐
- linux环境c语言编程 蔡晋,Linux环境C语言编程
Linux环境C语言编程第1讲linux系统环境介绍 Linux环境C语言编程第2讲命令行解析+环境变量+gcc基本参数 Linux环境C语言编程第3讲共享库.gdb的使用 Linux环境C语言编程第 ...
- linux c数字转字符串函数,Linux常用C函数—字符串转换篇
Linux 常用C 函数-字符串转换篇 atof (将字符串转换成浮点型数) 相关函数 atoi ,atol ,strtod ,strtol ,strtoul 定义函数 double atof(con ...
- asp系统转换php系统时间函数,window_Asp常用通用函数之生成时间的整数,'函数名:GetMyTimeNumber()' - phpStudy...
Asp常用通用函数之生成时间的整数 '函数名:GetMyTimeNumber() '作 用:生成时间的整数 '参 数:lx ---- 时间整数的类型 ' lx=0 到分钟 lx=1 到小时 lx=2 ...
- linux使用模板函数,Linux常用C函数
Linux常用C函数Tag内容描述: 1.Linux常用C函数 接口处理篇 accept(接受socket连线) 相关函数 socket,bind,listen,connect 表头文件 #inclu ...
- linux 单调时间实现原理,Linux系统下的单调时间函数
欢迎转载,转载请注明出处:http://forever.blog.chinaunix.net 一.编写linux下应用程序的时候,有时候会用到高精度相对时间的概念,比如间隔100ms.那么应该使用哪个 ...
- linux io函数,Linux下普通IO文件操作函数---C语言
普通文件io总结 file结构体 typedef struct { int level; /*填充/清空一级缓存*/ unsigned flag; /*文件状态指针*/ char fd; /*文件描述 ...
- linux拷贝文件函数,linux下文件操作的各个函数
作者:HoytEmail:hoytluo@21cn.com 前言: 我们在这一节将要讨论linux下文件操作的各个函数. 文件的创建和读写 文件的各个属性 目录文件的操作 管道文件 --------- ...
- linux设置多语言环境,怎么为Linux系统配置多语言环境?
这篇文章主要讲述了怎么为Linux系统配置多语言环境的基本方法,其中还包括编码的修改方法,我们就拿用户使用最多的Ubuntu和CentOS这两个为例子吧.需要的朋友可以参考下. 修改 /etc/sys ...
- linux脚本语言求累加和,Linux Shell脚本语言与数学表达式
当你理解了Shell脚本,每当需要时都能流畅编写时,那种感觉很爽的.本章中,我们将教你用脚本语言进行比较复杂的数学运算. 让我们从斐波那契数列开始吧. 斐波那契数列,又称黄金分割数列,指的是这样一个数 ...
最新文章
- 团体——L1-003 个位数统计 (c)
- WPF 3D和光照学习1
- open with code 报错没有项目_Python开发:解决Ubuntu安装tesserocr报错
- [HDU 1430] 魔板
- tomcat 加载js 中文乱码
- MacBook如何配置环境变量
- 【ValueError: data type <class ‘numpy.object_‘> not inexact】
- 给new comer程序员的建议
- 达梦数据库DM8启动与关闭
- 单节点Elasticsearch健康状态为 yellow
- ppt to html c,PPTC参数.pdf
- JavaScript点名器
- windows测试linux端口,windows、Linux 测试服务器、电脑的某些个端口是否打开
- MySql now函数
- 2019写给对象的话_2019@对象的暖心说说简短的 适合艾特对象的说说一句话超甜...
- PTA_Python3_人民币与美元汇率兑换程序
- macOS如何查看pdm文件
- 计算机专业对于女生来说怎么样,计算机专业好不好女生学计算机有前途吗
- VScode代码格式自动修正,自动排版
- 二次贝塞尔曲线转换为三次贝塞尔曲线