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脚本,每当需要时都能流畅编写时,那种感觉很爽的.本章中,我们将教你用脚本语言进行比较复杂的数学运算. 让我们从斐波那契数列开始吧. 斐波那契数列,又称黄金分割数列,指的是这样一个数 ...
最新文章
- AI回溯过去解决复杂任务 |《自然》论文
- sgmllib Introduction
- 前端基础-html-表格的结构标签(了解)
- AngularJS 指令实践指南(二)
- otl_stream查询返回数据_Hive查询的18种方式,你都学会了吗?
- 孙鑫-MFC笔记十--修改应用程序外观
- 【论文翻译】CenterNet: Objects as Points
- 安装activex手机控件_86/BRZ 免“油饼”安装 Defi 机油压力表
- java微信支付详解_java微信支付接入流程详解
- php 外包 上海,== | php外包与php技术服务商
- 软件测试用例的实际结果,软件测试结果归纳与分析
- 还在肝阴阳师?别错过千年一遇的高冷妹纸
- utf8编码和utf8mb4编码(其它编码简介)
- 老宇哥带你玩转 ESP32:04 串口玩起来是真方便
- loadrunner11.0 安装 破解
- java支持多继承吗
- MT【35】用复数得到的两组恒等式
- Timer.cancel()
- Spring Boot入门教程(四十六): @Async
- 小米5aosp android p,为小米5刷入Lineage OS 15.1