在程序中,经常需要输出系统的当前时间、计算程序的执行时间、使用计时器等。

一、时间的类型

1.格林威治标准时间

coordinated universal time(UTC)是世界标准时间,即常说的格林威治标准时间(greenwich mean time,GMT).

2.日历时间

日历时间(calendar time)是用"一个标准时间点(如1970年1月1日0点)到此时经过的秒数"来表示的时间.

二、时间函数的API

时间函数的API均属于系统调用函数.。

1.获取日历时间

#include <time.h>

time_t time(time_t *tloc)

函数功能:获取日历时间,即从1970年1月1日0点到现在所经历的秒数.

参数:通常设置为NULL

(time_t在time.h中定义:typedef long int time_t)

例:

#include <time.h>

int main(int argc,char *argv[])

{

int seconds=0;

seconds = time(NULL);

printf("seconds=%d\n",seconds);

}

执行结果:

[root@localhost Time]# ./time

seconds=1294908511

通常用户得到日历时间的秒数没有实际的意义,但可以为时间转化做一些铺垫性质的工作.为了更好的利用时间,用户需要将这些秒数转化为更容易接受的时间表示方式,这些表示时间的方式有格林威治时间、本地时间等.

2.将日历时间转换为格林威治标准时间

struct tm *gmtime(const time_t *timep)

函数功能:将日历时间转化为格林威治标准时间,并保存在tm结构

参数:日历时间的返回值

3.将日历时间转化为本地时间

struct tm* localtime(const time_t *timep)

函数功能:将日历时间转化为本地时间,并保存至tm结构

参数:日历时间的返回值

由上面两个函数可以看出,这两个函数的返回值均存放在tm结构中,具体的tm结构如下:

struct tm

{

int tm_sec;   //秒值

int tm_min;   //分钟值

int tm_hour;  //小时值

int tm_mday;  //本月第几日

int tm_mon;   //本年第几月

int tm_year;  //tm_year+1900=哪一年

int tm_wday;  //本周第几日

int tm_yday;  //本年第几日

int tm_isdst; //日光节约时间

}

建立time.c

#include <stdio.h>

#include <time.h>

int main(int argc,char *argv[])

{

 struct tm *local;

time_t t;

t = time(null);   //获取日历时间

local = localtime(&t);  //将日历时间转化为本地时间,并保存在struct tm结构中

printf("local hour is :%d\n",local->tm_hour);

local = gmtime(&t);  //将日历时间转化为格林威治时间,并保存在struct tm结构中

printf("utc hour is :%d\n",local->tm_hour);

return 0;

}

执行结果:

[root@localhost Time]# gcc –o time time.c

[root@localhost Time]# ./time

Local hour is: 0

UTC hour is: 8

[root@localhost Time]# date

Thu Jan 13 00:52:44 PST 2011

利用函数gmtime()、localtime()可以将日历时间转化为格林威治时间和本地时间,虽然用户可通过结构体tm来获取

这些时间值,但看起来还不方便,最好是将所有的信息,如年、月、日、星期、时、分、秒以字符串的形式显示出来,

这样就加直观.

4.时间显示

char *asctime(const struct tm *tm)

函数功能:将tm格式的时间转化为字符串

参数:日历时间的返回值

例如: SAT Jul 30 08:43:03 2005

该函数较ctime()使用起来更加的复杂.必须按照下面3个步骤来进行.

<1>使用函数time()来获取日历时间

<2>使用函数gmtime()将日历时间转化为格林威治标准时间

<3>使用函数asctime()将tm格式的时间转化为字符串

例程:

#include <time.h>

#include <stdio.h>

int main(void)

{

struct tm *ptr;

time_t lt;

lt=time(null);

ptr=gmtime(&lt);

printf(asctime(ptr));

printf(ctime(&lt));

return 0;

}

char *ctime(const time_t *timep)

函数功能:将日历时间转化为本地时间的字符串形式

参数:日历时间的返回值

该函数较asctime()使用起来更加简单.必须按照下面2个步骤来进行.

<1>使用函数time()来获取日历时间

<2>使用函数ctime()将日历时间直接转化为字符串

5.获取从今日凌晨到现在的时间差

int gettimeofday(struct timeval *tv,struct timezone *tz)

函数功能:获取从今日凌晨(0:0:0)到现在的时间差,常用于计算事件耗时

参数1:存放从今日凌晨(0:0:0)到现在的时间差,时间差以秒或微秒为单位,以结构体形式存放

struct timeval

{

int tv_sec;    //秒数

int tv_usec;   //微秒数

}

参数2:常设置为null

函数用法:可以在做某件事情之前调用gettimeofday(),在做完该件事情之后调用gettimeofday(),两个函数的参数1

的差就是做该事情所消耗的时间.

例程:计算函数function()的耗时

time.c

#include <sys/time.h>

#include <stdio.h>

#include <stdlib.h>

#include <math.h>

void function()

{

unsigned int i,j;

double y;

for(i=0;i<1000;i++)

for(j=0;j<1000;j++)

y++;

}

void main()

{

struct timeval tpstart,tpend;

float timeuse;

gettimeofday(&tpstart,null); // 开始时间

function();

gettimeofday(&tpend,null);   // 结束时间

// 计算执行时间,以微秒为单位进行计算

timeuse=1000000*(tpend.tv_sec-tpstart.tv_sec)+tpend.tv_usec-tpstart.tv_usec;

timeuse/=1000000;

printf("used time:%f\n",timeuse);

exit(0);

}

执行结果:

[root@localhost lishuai]# gcc time.c -o time -wall

[root@localhost lishuai]# ./time

use time:0.006288

6.延时函数

<1>使程序睡眠seconds秒

unsigned int sleep(unsigned int seconds)

函数功能:使程序睡眠seconds秒

参数:需要休眠的秒数

<2>使程序睡眠usec微秒

void usleep(unsigned long usec)

函数功能:使程序睡眠usec微秒

参数:需要休眠的秒数

转载于:https://www.cnblogs.com/Charles-Zhang-Blog/archive/2013/05/10/3071229.html

linux c语言获取时间相关推荐

  1. Linux C语言获取时间 gettimeofday timeval

    timeval 结构体源码,gettimeofday() 的第一个参数 #ifndef __timeval_defined #define __timeval_defined 1#include &l ...

  2. linux 字符串时间转换,Linux C/C++时间字符串与time_t之间的转换方法(转)

    js 获取小数点位数方法及 字符串与数字之间相互转换方法 1.获取小数点位数方法 a. 使用 js 中 subsrting,indexOf,parseFloat三个函数,代码如下: var s = & ...

  3. linux使用世界时间,linux世界里的时间

    通常,操作系统可以使用三种方法来表示系统的当前时间与日期: ①最简单的一种方法就是直接用一个64位的计数器来对时钟滴答进行计数. ②第二种方法就是用一个32位计数器来对秒进行计数,同时还用一个32位的 ...

  4. 使用ntpdate校正linux系统的时间

    当Linux服务器的时间不对的时候,可以使用ntpdate工具来校正时间. 安装:yum install ntpdate ntpdate简单用法: # ntpdate ip # ntpdate 210 ...

  5. Linux中表示“时间”的结构体和相关函数

    转载于:http://blog.chinaunix.net/uid-25909722-id-2827364.html Linux中表示"时间"的结构体和相关函数 2011-09-1 ...

  6. linux centos 网络 时间 日期 同步

    1.安装ntp和ntpdate工具 yum -y install ntp ntpdate 2.设置系统时间与网络时间同步 ntpdate ntp1.aliyun.com 这里主要就是通过时间服务器对系 ...

  7. Linux内核之时间系统

    Linux内核之时间系统 1.Linux时间系统 (1)CMOS时钟 (2)系统时钟 (3)节拍数(jiffies) (4)墙上时间(xtime) 2.重要数据结构 (1)struct tk_read ...

  8. Linux系统之时间管理

    Linux系统之时间管理 一.date命令介绍 1.date帮助 2.查询系统时间 ①查询电脑硬件时间 ②查询UTC时间 ③查询系统时间 ④特定时间格式输出系统时间 ⑤查询电脑所有时间信息 三.修改系 ...

  9. Linux 应用层的时间编程【转】

    转自:https://blog.csdn.net/chinalj2009/article/details/21223681 浅析 Linux 中的时间编程和实现原理,第 1 部分: Linux 应用层 ...

最新文章

  1. 深度强化学习(Deep Reinforcement Learning)的资源
  2. Load Runner测试脚本(tuxedo服务)的编写指南
  3. PowerMock 简介--转载
  4. 使用u-boot的USB下载功能烧写程序到Nand Flash ——韦东山嵌入式Linux学习笔记06
  5. 江西财经计算机应用杨教授,江西财经大学信息管理学院研究生导师介绍:刘德喜...
  6. Base64 + 变为 空格 问题分析
  7. JasperReport生成PDF中文不显示处理
  8. IDEA 中创建多级目录
  9. linux7删除网卡文件,CentOS 7下删除virbr0网卡信息
  10. word给表头和图题按章节编号
  11. android 编译 libjpeg-turbo,编译Android环境的libjpeg-turbo
  12. STM32L452CCU6 STM32L432KCU6 GD32F407VKT6 嵌入式技术数据手册 32位ARM
  13. iOS 苹果企业账号申请流程
  14. 计算机心理测试题,心理测验丨测试你隐藏的懒人指数?
  15. Mockplus Cloud自动生成规格,Mockplus Cloud交互式动画原型
  16. 80老翁谈人生(198):老翁谈人生系列短文目录索引
  17. 【烈日炎炎战后端】JAVA虚拟机(3.6万字)
  18. [讲座论坛] 应对气候变化的中国视角
  19. 【Linux工具】-vim介绍
  20. 华硕M5A78L-MLX3PLUS羿龙IIX4(3424元)家用学习型装机配置

热门文章

  1. ODB——基于c++的ORM映射框架尝试(安装)
  2. wndows系统命令总结
  3. 利用XML生成Excel
  4. nlp gpt论文_GPT-3:NLP镇的最新动态
  5. 在Python中有效使用JSON的4个技巧
  6. 软件工程方法学要素含义_日期时间数据的要素工程
  7. idea 搜索不到gsonformat_Idea中GsonFormat插件安装
  8. Docker初学者指南-如何创建您的第一个Docker应用程序
  9. k均值算法 二分k均值算法_使用K均值对加勒比珊瑚礁进行分类
  10. 【OCR技术系列之八】端到端不定长文本识别CRNN代码实现