常见的时间函数有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. gitblit系列七:使用Jenkins配置自动化持续集成构建

    1.安装 方法一: 下载jenkin.exe安装文件 下载地址:https://jenkins.io/content/thank-you-downloading-windows-installer/ ...

  2. (C/C++学习笔记) 六. 表达式

    六. 表达式 ● 表达式 表达式 expression An expression consists of a combination of operators and operands. (An o ...

  3. 深入理解java虚拟机---java虚拟机内存管理(七)

    本地方法栈.java堆.方法区 本地方法栈在HotSpot版本内与java虚拟机栈是合二为一的.不单独区分本地方法栈.但是java虚拟机中是有这样一块区域的. 作用: 1.本地方法栈为虚拟机栈执行ja ...

  4. web项目与jsp有关的三个jar的依赖

    <!-- jsp --> <dependency> <groupId>javax.servlet</groupId> <artifactId> ...

  5. 从今天开始 每天记录HTML,CSS 部分的学习笔记

    从今天开始 每天记录HTML,CSS 部分的学习笔记

  6. AOP 实现请求参数打印

    1.编写打印方法 import java.util.Enumeration; import javax.servlet.http.HttpServletRequest; import org.aspe ...

  7. synchronized(一)

    /** * 线程安全概念:当多个线程访问某一个类(对象或方法)时,这个对象始终都能表现出正确的行为,那么这个类(对象或方法)就是线程安全的. * synchronized:可以在任意对象及方法上加锁, ...

  8. Linux:软件包安装

    软件包安装 一.rpm安装 1.挂载系统:mount /dev/cdrom/ /mnt/cdrom/ 2.进入相应的目录(Centos7 为Packages,有一些是Server):cd /mnt/c ...

  9. apache rewrite 规则

    啥是虚拟主机呢?就是说把你自己的本地的开发的机子变成一个虚拟域名,比如:你在开发pptv下面的一个项目 127.0.0.1/pptv_trunk,你想把自己的机器域名变成www.pptv.com.那么 ...

  10. phpexcel 的使用

    首先到phpexcel官网上下载最新的phpexcel类,下周解压缩一个classes文件夹,里面包含了PHPExcel.php和PHPExcel的文件夹,这个类文件和文件夹是我们需要的,把class ...