歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Linux下關於時間概念的C語言編程

Linux下關於時間概念的C語言編程

日期:2017/3/1 9:46:02   编辑:Linux編程

Abstract

在程序中,經常需要輸出系統的當前時間、計算程序的執行時間、使用計時器等。

Introduction

一、時間的類型
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>
void main()
{
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; //日光節約時間
}
建立time1.c
#include <stdio.h>
#include <time.h>
int main(void)
{
  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 time1.c -o time1
[root@localhost Time]# ./time1
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微秒
參數:需要休眠的秒數

Copyright © Linux教程網 All Rights Reserved