常见的时间函数有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<time.h>
#include<unistd.h>
#include<stdio.h>
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]#

“`

参考文章传送门http://blog.csdn.net/water_cow/article/details/7521567

常用C语言time时间函数的更多相关文章

  1. [转帖]C语言计算时间函数 & 理解linux time命令的输出中“real”“user”“sys”的真正含义

    C语言计算时间函数 & 理解linux time命令的输出中“real”“user”“sys”的真正含义 https://blog.csdn.net/willyang519/article/d ...

  2. C语言的时间函数

    下面是C语言的获取本地时间和构造时间进行格式化时间显示输出的相关函数:This page is part of release 3.35 of the Linux man-pages project. ...

  3. c语言随机函数&&时间函数

    c语言中的随机函数为rand(),但是rand生成的值得大小主要相对一个变量才产生的一定有含义的数,这个相对的变量我们可以再srand()函数中进行设置,srand函数是void类型,内部含一个无符号 ...

  4. linux下常用的几个时间函数:time,gettimeofday,clock_gettime,_ftime

    time()提供了秒级的精确度 1.头文件 <time.h> 2.函数原型 time_t time(time_t * timer) 函数返回从TC1970-1-1 0:0:0开始到现在的秒 ...

  5. R语言日期时间函数

    Sys.Date( ) returns today's date. date() returns the current date and time.# print today's datetoday ...

  6. sqlserver常用日期、时间函数和格式

    Sql Server中常用的日期与时间函数1.  当前系统日期.时间    select getdate() 2. dateadd  在向指定日期加上一段时间的基础上,返回新的 datetime 值  ...

  7. windows时间函数

    介绍        我们在衡量一个函数运行时间,或者判断一个算法的时间效率,或者在程序中我们需要一个定时器,定时执 行一个特定的操作,比如在多媒体中,比如在游戏中等,都会用到时间函数.还比如我们通过记 ...

  8. python之时间函数

    import time print(time.clock())print(time.process_time())print(time.time()) #返回当前系统时间戳print(time.cti ...

  9. PHP中日期时间函数date()用法总结

    date()是我们常用的一个日期时间函数,下面我来总结一下关于date()函数的各种形式的用法,有需要学习的朋友可参考. 格式化日期date() 函数的第一个参数规定了如何格式化日期/时间.它使用字母 ...

随机推荐

  1. JSP动态网页

    01.什么是服务器 02.什么是动态网页  动态网页是指在服务器端运行的,使用程序语言设计的交互式网页,它们会根据某种条件的变化,返回不同的网页内容.可以让用户和服务器交互的网站 动态网站可以实现交互 ...

  2. LeetCode难度与出现频率

    转载自:LeetCode Question Difficulty Distribution               1 Two Sum 2 5 array sort         set Two ...

  3. Cracking The Coding Interview 4.1

    //Implement a function to check if a tree is balanced. For the purposes of this question, a balanced ...

  4. JavaWeb基础-Session和Cookie

    JSP状态管理 http的无状态性,服务器不会记得发送请求的浏览器是哪一个 保存用户状态的两大机制:session和cookie Cookie:是web服务器保存在客户端的一系列文本信息 作用:对特定 ...

  5. jmeter中操作数据库

    jmeter是如何操作数据库的? 步骤一:导入jdbc的jar包,jmeter本身不能直接连接mysql,所以需要导入第三方的jar包来连接mysql 点击测试计划,添加classpath,选择mys ...

  6. L314 单音节词读音规则(二)-元音字母发音规则

    1 单个元音发音尽量拖音一下(2S),发音会饱满一些. 2开音节: 辅音(辅组)(没有)+元音+辅音+e 的单词其中:元音发字母本身音,辅音字母不为r , 字母e不发音. 相对开音节:第一个元音都发字 ...

  7. JavaWeb:c3p0配置问题-----java.sql.SQLException: Connections could not be acquired from the underlying database!

    错误原因 c3p0的配置错误 错误显示 -classpath "D:\Program\Software\IntelliJIDEA\IntelliJ IDEA 2018.2.5\lib\ide ...

  8. ob 函数的使用

    ob 函数的使用1. 页面静态化 $id = isset($_GET['id'])?$_GET['id']-0:0; $filename = "html/".date(" ...

  9. 网络协议理论,http协议,数据结构,常用返回码

    一.网络协议理论 先是DNS协议 将域名转化成IP地址 这个你要知道 域名只是人记着方便 计算机记的是IP 然后是TCPIP协议 数据在传输过程中可能要经过陆游器 涉及到的是ARP协议 将IP地址转换 ...

  10. 推荐系统-07-lambda架构

    Lambda 架构 Lambda 架构由Storm的作者Nathan Marz提出,其设计目的在于提供一个能满足大数据系统关键特性的架构,包括高容错.低延迟.可扩展等.其整合离线计算与实时计算,融合不 ...