下面叙述两方面的东西。第一部分,介绍自己写的两个获取时间的示例,这是比较常用的用法;第二部分,各种时间函数的用法汇总,来自网上,已经无法追溯出处了。搜索了下,在以下博客《c语言中时间的获取》中有类似内容。

http://hi.baidu.com/greatren518/blog/item/5c5faf5413b13352d00906e2.html

第一部分

示例1:使用time()和localtime()来统计某一段代码的执行时间,这里只要求精确到分钟。其实使用这两个函数可以精确到秒。

/*get_time0.c*/

#include <stdio.h>
#include <time.h>

#define SLEEPING_TIME 65

void function_B()
{
    printf("sleeping %ds\r\n", SLEEPING_TIME);
    sleep(SLEEPING_TIME);
}

int main()
{
    time_t time_current;
    time_t time_start;
    struct tm *ptime_start;
    struct tm *ptime_current;
    int interval=0;

time(&time_start);
    ptime_start = localtime(&time_start);
    
    function_B();
     
    time(&time_current);
    ptime_current = localtime(&time_current);

interval = ((ptime_current->tm_hour) - (ptime_start->tm_hour)) * 60 + ((ptime_current->tm_min) - (ptime_start->tm_min));
    printf("interval =%d m\r\n", interval);
    return 0;

}
编译:

# gcc -o get_time0 get_time0.c

执行结果:

# ./get_time0
sleeping 65s
interval =0 m

发现时间间隔interval 居然为0. 怎么回事呢?看看加了以下打印的get_time1.c

示例2:在 get_time0.c 上 加了些打印。

/*get_time1.c*/

#include <stdio.h>
#include <time.h>

#define SLEEPING_TIME 65

void function_B()
{
    printf("sleeping %ds\r\n", SLEEPING_TIME);
    sleep(SLEEPING_TIME);
}

int main()
{
    time_t time_current;
    time_t time_start;
    struct tm *ptime_start;
    struct tm *ptime_current;
    int interval=0;

time(&time_start);
    ptime_start = localtime(&time_start);
    printf("start_hour =%dh, start_minute=%dm\r\n", ptime_start->tm_hour, ptime_start->tm_min);    
    
    function_B();
     
    time(&time_current);
    ptime_current = localtime(&time_current);
    printf("current_hour =%dh, current_minute=%dm\r\n", ptime_current->tm_hour, ptime_current->tm_min);

printf("t_h_start=%d\r\n", ptime_start->tm_hour);
    printf("t_h_current=%d\r\n", ptime_current->tm_hour);
    printf("t_m_start=%d\r\n", ptime_start->tm_min);
    printf("t_m_current=%d\r\n", ptime_current->tm_min);

interval = ((ptime_current->tm_hour) - (ptime_start->tm_hour)) * 60 + ((ptime_current->tm_min) - (ptime_start->tm_min));
    printf("interval =%d m\r\n", interval);
    return 0;

}

编译:

# gcc -o get_time1 get_time1.c

执行结果:

# ./get_time1
start_hour =17h, start_minute=21m
sleeping 65s
current_hour =17h, current_minute=22m
t_h_start=17
t_h_current=17
t_m_start=22
t_m_current=22
interval =0 m
从结果可以看出,在第二次执行localtime()后,指针ptime_start 和 ptime_current所指向的区域的值变得一样了。为什么会这样呢?这是由于localtime()完成对输入时间(s)的格式转换后,返回的是一个指针,这个指针的内存是在localtime()内部分配的。我猜测是一个静态局部变量,两次使用localtime()返回的指针都指向这个静态局部变量,而第二使用后的值把第一次的覆盖了。

其实,这里如果真的是想统计时间间隔的话,直接将time_start 和 time_current 相减就可以了。因为使用time()获得时间本身就是从公元1970年1月1日的UTC时间从0时0分0秒算起到现在所经过的秒数。

interval  = time_current - time_start; /*second*/

之所以大费周章,是想说明localtime()的用法和使用localtime()转换时间格式的时候要当心。

使用time()获取时间 和localtime()转换时间格式,只能精确到秒。如果需要更精确一点,可以使用gettimeofday(),这个时间函数可以精确到us。见示例3.

示例3:使用gettimeofday()获取时间间隔,精确到ms(可以精确到us)。

/*get_time2.c*/

#include <stdio.h>
#include <sys/time.h>

#define SLEEPING_TIME 2

void function_A()
{
    printf("sleeping %ds\r\n", SLEEPING_TIME);
    sleep(SLEEPING_TIME);
}

int main()

{
    struct timeval start, end;
    int interval;

gettimeofday(&start, NULL);
    
    function_A();

gettimeofday(&end, NULL);
    
    interval = 1000000*(end.tv_sec - start.tv_sec) + (end.tv_usec - start.tv_usec);/*us*/
    printf("interval = %fms\n", interval/1000.0);
}

编译:

# gcc -o get_time2 get_time2.c

执行结果:

# ./get_time2
sleeping 2s

interval = 2000.627000ms

第二部分:时间函数的用法汇总

来自网上,已经无法追溯出处了。搜索了以下,在以下博客《c语言中时间的获取》中有类似内容。

http://hi.baidu.com/greatren518/blog/item/5c5faf5413b13352d00906e2.html

 

asctime(将时间和日期以字符串格式表示)
相关函数
time,ctime,gmtime,localtime
表头文件
#include<time.h>
定义函数
char * asctime(const struct tm * timeptr);
函数说明
asctime()将参数timeptr所指的tm结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果以字符串形态返回。此函数已经由时区转换成当地时间,字符串格式为:“Wed Jun 30 21:49:08 1993\n”
返回值
若再调用相关的时间日期函数,此字符串可能会被破坏。此函数与ctime不同处在于传入的参数是不同的结构。
附加说明
返回一字符串表示目前当地的时间日期。
范例
#include <time.h>
main()
{
time_t timep;
time (&timep);
printf(“%s”,asctime(gmtime(&timep)));
}
执行
Sat Oct 28 02:10:06 2000
 

ctime(将时间和日期以字符串格式表示)
相关函数
time,asctime,gmtime,localtime
表头文件
#include<time.h>
定义函数
char *ctime(const time_t *timep);
函数说明
ctime()将参数timep所指的time_t结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果以字符串形态返回。此函数已经由时区转换成当地时间,字符串格式为“Wed Jun 30 21 :49 :08 1993\n”。若再调用相关的时间日期函数,此字符串可能会被破坏。
返回值
返回一字符串表示目前当地的时间日期。
范例
#include<time.h>
main()
{
time_t timep;
time (&timep);
printf(“%s”,ctime(&timep));
}
执行
Sat Oct 28 10 : 12 : 05 2000
 

gettimeofday(取得目前的时间)
相关函数
time,ctime,ftime,settimeofday
表头文件
#include <sys/time.h>
#include <unistd.h>
定义函数
int gettimeofday ( struct timeval * tv , struct timezone * tz )
函数说明
gettimeofday()会把目前的时间有tv所指的结构返回,当地时区的信息则放到tz所指的结构中。
timeval结构定义为:
struct timeval{
long tv_sec; /*秒*/
long tv_usec; /*微秒*/
};
timezone 结构定义为:
struct timezone{
int tz_minuteswest; /*和Greenwich 时间差了多少分钟*/
int tz_dsttime; /*日光节约时间的状态*/
};
上述两个结构都定义在/usr/include/sys/time.h。tz_dsttime 所代表的状态如下
DST_NONE /*不使用*/
DST_USA /*美国*/
DST_AUST /*澳洲*/
DST_WET /*西欧*/
DST_MET /*中欧*/
DST_EET /*东欧*/
DST_CAN /*加拿大*/
DST_GB /*大不列颠*/
DST_RUM /*罗马尼亚*/
DST_TUR /*土耳其*/
DST_AUSTALT /*澳洲(1986年以后)*/
返回值
成功则返回0,失败返回-1,错误代码存于errno。附加说明EFAULT指针tv和tz所指的内存空间超出存取权限。
范例
#include<sys/time.h>
#include<unistd.h>
main(){
struct timeval tv;
struct timezone tz;
gettimeofday (&tv , &tz);
printf(“tv_sec; %d\n”, tv,.tv_sec) ;
printf(“tv_usec; %d\n”,tv.tv_usec);
printf(“tz_minuteswest; %d\n”, tz.tz_minuteswest);
printf(“tz_dsttime, %d\n”,tz.tz_dsttime);
}
执行
tv_sec: 974857339
tv_usec:136996
tz_minuteswest:-540
tz_dsttime:0
 

gmtime(取得目前时间和日期)
相关函数
time,asctime,ctime,localtime
表头文件
#include<time.h>
定义函数
struct tm*gmtime(const time_t*timep);
函数说明
gmtime()将参数timep 所指的time_t 结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果由结构tm返回。
结构tm的定义为
struct tm
{
int tm_sec;
int tm_min;
int tm_hour;
int tm_mday;
int tm_mon;
int tm_year;
int tm_wday;
int tm_yday;
int tm_isdst;
};
int tm_sec 代表目前秒数,正常范围为0-59,但允许至61秒
int tm_min 代表目前分数,范围0-59
int tm_hour 从午夜算起的时数,范围为0-23
int tm_mday 目前月份的日数,范围01-31
int tm_mon 代表目前月份,从一月算起,范围从0-11
int tm_year 从1900 年算起至今的年数
int tm_wday 一星期的日数,从星期一算起,范围为0-6
int tm_yday 从今年1月1日算起至今的天数,范围为0-365
int tm_isdst 日光节约时间的旗标
此函数返回的时间日期未经时区转换,而是UTC时间。
返回值
返回结构tm代表目前UTC 时间
范例
#include <time.h>
main(){
char *wday[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
time_t timep;
struct tm *p;
time(&timep);
p=gmtime(&timep);
printf(“%d%d%d”,(1900+p->tm_year), (1+p->tm_mon),p->tm_mday);
printf(“%s%d;%d;%d\n”, wday[p->tm_wday], p->tm_hour, p->tm_min, p->tm_sec);
}
执行
2000/10/28 Sat 8:15:38
 

localtime(取得当地目前时间和日期)
相关函数
time, asctime, ctime, gmtime
表头文件
#include<time.h>
定义函数
struct tm *localtime(const time_t * timep);
函数说明
localtime()将参数timep所指的time_t结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果由结构tm返回。结构tm的定义请参考gmtime()。此函数返回的时间日期已经转换成当地时区。
返回值
返回结构tm代表目前的当地时间。
范例
#include<time.h>
main(){
char *wday[]={“Sun”,”Mon”,”Tue”,”Wed”,”Thu”,”Fri”,”Sat”};
time_t timep;
struct tm *p;
time(&timep);
p=localtime(&timep); /*取得当地时间*/
printf (“%d%d%d ”, (1900+p->tm_year),( l+p->tm_mon), p->tm_mday);
printf(“%s%d:%d:%d\n”, wday[p->tm_wday],p->tm_hour, p->tm_min, p->tm_sec);
}
执行
2000/10/28 Sat 11:12:22
 

mktime(将时间结构数据转换成经过的秒数)
相关函数
time,asctime,gmtime,localtime
表头文件
#include<time.h>
定义函数
time_t mktime(strcut tm * timeptr);
函数说明
mktime()用来将参数timeptr所指的tm结构数据转换成从公元1970年1月1日0时0分0 秒算起至今的UTC时间所经过的秒数。
返回值
返回经过的秒数。
范例
/* 用time()取得时间(秒数),利用localtime()
转换成struct tm 再利用mktine()将struct tm转换成原来的秒数*/
#include<time.h>
main()
{
time_t timep;
strcut tm *p;
time(&timep);
printf(“time() : %d \n”,timep);
p=localtime(&timep);
timep = mktime(p);
printf(“time()->localtime()->mktime():%d\n”,timep);
}
执行
time():974943297
time()->localtime()->mktime():974943297
 

settimeofday(设置目前时间)
相关函数
time,ctime,ftime,gettimeofday
表头文件
#include<sys/time.h>
#include<unistd.h>
定义函数
int settimeofday ( const struct timeval *tv,const struct timezone *tz);
函数说明
settimeofday()会把目前时间设成由tv所指的结构信息,当地时区信息则设成tz所指的结构。详细的说明请参考gettimeofday()。注意,只有root权限才能使用此函数修改时间。
返回值
成功则返回0,失败返回-1,错误代码存于errno。
错误代码
EPERM 并非由root权限调用settimeofday(),权限不够。
EINVAL 时区或某个数据是不正确的,无法正确设置时间。
 

time(取得目前的时间)
相关函数
ctime,ftime,gettimeofday
表头文件
#include<time.h>
定义函数
time_t time(time_t *t);
函数说明
此函数会返回从公元1970年1月1日的UTC时间从0时0分0秒算起到现在所经过的秒数。如果t 并非空指针的话,此函数也会将返回值存到t指针所指的内存。
返回值
成功则返回秒数,失败则返回((time_t)-1)值,错误原因存于errno中。
范例
#include<time.h>
mian()
{
int seconds= time((time_t*)NULL);
printf(“%d\n”,seconds);
}
执行
9.73E+08
 

Linux 时间函数小结相关推荐

  1. Linux时间函数札记

    关于gmtime.gmtime_r.localtime.localtime_r 测试环境:vmware 7 + Redhat5.5,系统时间使用UTC,时区为上海. 1.函数功能介绍 使用man gm ...

  2. linux时间函数详解

    一.时间相关说明 格林威治时间表示0时区的标准时间.其他时区的时间和此标准时间均有时间差.UTC(Universal Time Coordinated)是世界协调时间,是格林威治时间在互联网中的表示方 ...

  3. linux 时间函数

    下面讲解的是linux中 时间相关的函数和将时间转换相关函数 1.获取时间相关函数 1.1 获取秒级时间差函数 #include <time.h> time_t time(time_t * ...

  4. oracle和MySQL的日期函数_mysql与oracle的日期/时间函数小结

    前言 本文的日期/时间全部格式化为"2016-01-01 01:01:01"形式: MONITOR_TIME为数据库表字段: 字符串与日期/时间相互转换函数 Oracle 日期/时 ...

  5. mysql日期存到oracle_mysql与oracle的日期/时间函数小结

    前言 本文的日期/时间全部格式化为"2016-01-01 01:01:01"形式: MONITOR_TIME为数据库表字段: 字符串与日期/时间相互转换函数 Oracle 日期/时 ...

  6. oracle 选时间到五月,oracle日期时间函数小结

    oracle日期时间函数总结 经常写 sql 的同学应该会接触到一些 oracle 的日期时间函数, 例如: 财务软件或者人力资源软件需要按照每年, 每季度, 每月, 甚至每个星期来进行统计. 今天闲 ...

  7. Linux时间函数time()、ctime()、ctime_r()、localtime()、localtime_r()、asctime()、strftime()的转换关系

    上面这个图就是它们所有函数的关系转换,比较清晰,不需要太多文字描述. 下面是它们的代码实现过程: #include<stdio.h> #include<unistd.h> #i ...

  8. Linux 时间函数的使用

    头文件 #include <chrono> #include <functional>namespace hsm { namespace common {class Timer ...

  9. linux几种时间函数总结

    一.linux时间函数总结 最近的工作中用到的时间函数比较频繁,今天抽时间总结一下,在linux下,常用的获取时间的函数有如下几个:  asctime,  ctime, gmtime, localti ...

最新文章

  1. Fibonacci(斐波纳契)数列各种优化解法
  2. 解决 Mysql下使用EF Code First 指定表Engine无效的思路
  3. Ubuntu18.04安装qt后的问题
  4. Python之io概念
  5. chrome开发总结(交互/权限/存储)-爬虫
  6. member selection 运算符是什么
  7. android 扩展textview,Android可收缩/扩展的TextView【1】
  8. LAMP架构简介与概述 及服务安装
  9. 使用Python写登录京东商城购物,加入购物车的脚本
  10. Deep Homography Estimation - Pytorch实现
  11. RFID-Si24R1芯片基于Android 4.4平台的kernel驱动
  12. 在HTML中什么表示水平线,HTML中加入水平线的标签是( )
  13. Linux 重置root密码
  14. 计算机组成原理学习笔记(四)指令系统(学习王道)
  15. FPGA学习:Verilog基本语法
  16. 云南农职《JavaScript交互式网页设计》 综合机试试卷⑥——简易旅游网
  17. 花火之声不闻于耳 [线段树]
  18. 各行各业数据及分析研究报告网站参考
  19. 台式计算机wifi老掉线,电脑总掉线是什么原因_电脑总掉线wifi正常
  20. 成功解决wps中引用尾注生成参考文献之后,无法插入致谢和附录等章节

热门文章

  1. Update和FixedUpdate的区别
  2. Mac电脑上矢量绘图设计软件推荐:Sketch一款不逊色PS设计应用
  3. linux查看zookeeper版本
  4. 雷军布局智能家居 A轮投资丽维家过千万
  5. 2020年 第十一届蓝桥杯第一场省赛题解(C++B组版)
  6. 厉害了,不能错过,大神关于TCP/IP 协议的理解分享(网络协议篇一)
  7. 静态绑定IP与MAC
  8. 职称计算机考试太难了,全国职称计算机考试到底难不难考
  9. WPF TreeView 控件样式
  10. IEDA超实用插件,开启这些功能,提高工作效率、提高code容错 ❤️【建议收藏】