4、Linux时间编程

来源:互联网 发布:windows软件 编辑:程序博客网 时间:2024/06/01 12:23

1、格林威治标准时间

定义:               Coordinated Universal Time(UTC),世界标准时间

函数原型:       struct tm *gmtime(const time_t *timep);

函数功能:        将timep指定的日历时间转换成标准时间

头文件:            #include<time.h>

返回值:            成功返回标准时间参数

参数:                待转化的日历时间


2、日历时间

定义:                是用“从一个标准时间点(如:1970年1月1日0点)到此时经过的秒数”来表示的时间。

函数原型:        time_t time(time_t *t);

函数功能:        返回日历时间

头文件:            #include<time.h>

返回值:           1970.01.01至今的秒数

参数:               不为空时保存返回值


3、获取本地时间

定义:                  本机的时间
函数原型:          struct tm *localtime(const time_t *timep);
函数功能:          将日历时间换为本地时间
头文件:              #include<time.h>
返回值:              成功返回本地时间,失败返回-1
参数:                  待转化的日历时间

4、以字符串的方式显示时间

函数原型: char *asctime(const struct tm *tm);
函数功能: 将struct tm格式的时间转换成字符串格式的时间
头文件: #include<time.h>
返回值:              字符串方式显示的时间
参数:                    带转换的tm格式的时间

5、获取高精度时间

定义: 获取微秒级的时间
函数原型: int gettimeofday(struct  timeval  *restrict tp,  void *restrict tzp);
函数功能: 获取今天开始到现在走了多少微秒
头文件: #include<sys/time.h>
返回值:              成功则返回0,失败返回-1
参数:                    tp为获取到的时间,tzp合法值为NULL
struct timval{time_t  tv_sec;    //秒数long tv_usec; //微秒数};struct tm{int tm_sec;inttm_min;int tm_hour;inttm_mday;inttm_mon;inttm_year;//year since 1900inttm_wday;      //day since sunday:[0-6]inttm_yday;//days since Januaryinttm_isdst;}



0 0
原创粉丝点击