常见的时间函数有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. TensorFlow学习笔记——节点(constant、placeholder、Variable)

    一. constant(常量) constant是TensorFlow的常量节点,通过constant方法创建,其是计算图(Computational Graph)中的起始节点,是传入数据. 创建方式 ...

  2. [Leetcode 101]判断对称树 Symmetric Tree

    [题目] Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). ...

  3. 安装ubuntu gnome桌面

    注意: ubuntu 14.04.5默认的为unity桌面,有多点触发,没有自带Tweak Tool工具. 安装gnome桌面 sudo apt-get install ubuntu-gnome-de ...

  4. CentOS7下cratedb备份及恢复(快照)

    一:创建存储库 1.1 概要 CREATE REPOSITORY repository_name TYPE type [ WITH (repository_parameter [= value], [ ...

  5. python中的循环以及,continue和break的使用

    循环 目标 程序的三大流程 while 循环基本使用 break 和 continue while 循环嵌套 01. 程序的三大流程 在程序开发中,一共有三种流程方式: 顺序 —— 从上向下,顺序执行 ...

  6. shell脚本实例-安装httpd,安装yum源

    1.安装httpd #!/usr/bin/bash getway=192.168.1.1 ping -c1 www.baidu.com &>/dev/null if [ $? -eq 0 ...

  7. restful接口设计规范总结

    这篇 文章主要是借鉴他人,但是自己很想总结出一套规范,以供向我这样的新手使用,用来规范代码,如果有什么好的提议,请不吝赐教,本篇文章长期更新! 一.重要概念: REST,即Representation ...

  8. C#窗体布局技巧

    using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...

  9. vue--http请求的封装--session

    export function Fecth (url, data, file, _method) { if (file) { // 需要上传文件 return new Promise((resolve ...

  10. C# 曲线控件 曲线绘制 实时曲线 多曲线控件 开发

    Prepare 本文将使用一个NuGet公开的组件来实现曲线的显示,包含了多种显示的模式和配置来满足各种不同的应用场景,方便大家进行快速的开发系统. 在Visual Studio 中的NuGet管理器 ...