linux c语言获取时间
在程序中,經(jīng)常需要輸出系統(tǒng)的當前時間、計算程序的執(zhí)行時間、使用計時器等。
一、時間的類型
1.格林威治標準時間
coordinated universal time(UTC)是世界標準時間,即常說的格林威治標準時間(greenwich mean time,GMT).
2.日歷時間
日歷時間(calendar time)是用"一個標準時間點(如1970年1月1日0點)到此時經(jīng)過的秒數(shù)"來表示的時間.
二、時間函數(shù)的API
時間函數(shù)的API均屬于系統(tǒng)調(diào)用函數(shù).。
1.獲取日歷時間
??#include <time.h>
??time_t time(time_t *tloc)
??函數(shù)功能:獲取日歷時間,即從1970年1月1日0點到現(xiàn)在所經(jīng)歷的秒數(shù).
??參數(shù):通常設(shè)置為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);
?}
執(zhí)行結(jié)果:
[root@localhost Time]# ./time
seconds=1294908511
通常用戶得到日歷時間的秒數(shù)沒有實際的意義,但可以為時間轉(zhuǎn)化做一些鋪墊性質(zhì)的工作.為了更好的利用時間,用戶需要將這些秒數(shù)轉(zhuǎn)化為更容易接受的時間表示方式,這些表示時間的方式有格林威治時間、本地時間等.
2.將日歷時間轉(zhuǎn)換為格林威治標準時間
???struct tm *gmtime(const time_t *timep)
???函數(shù)功能:將日歷時間轉(zhuǎn)化為格林威治標準時間,并保存在tm結(jié)構(gòu)
???參數(shù):日歷時間的返回值
3.將日歷時間轉(zhuǎn)化為本地時間
???struct tm* localtime(const time_t *timep)
???函數(shù)功能:將日歷時間轉(zhuǎn)化為本地時間,并保存至tm結(jié)構(gòu)
???參數(shù):日歷時間的返回值
??由上面兩個函數(shù)可以看出,這兩個函數(shù)的返回值均存放在tm結(jié)構(gòu)中,具體的tm結(jié)構(gòu)如下:
???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; //日光節(jié)約時間
???}
???建立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);??//將日歷時間轉(zhuǎn)化為本地時間,并保存在struct tm結(jié)構(gòu)中
?????printf("local hour is :%d\n",local->tm_hour);
?????local = gmtime(&t);??//將日歷時間轉(zhuǎn)化為格林威治時間,并保存在struct tm結(jié)構(gòu)中
?????printf("utc hour is :%d\n",local->tm_hour);
?????return 0;
???}
???執(zhí)行結(jié)果:
[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
?利用函數(shù)gmtime()、localtime()可以將日歷時間轉(zhuǎn)化為格林威治時間和本地時間,雖然用戶可通過結(jié)構(gòu)體tm來獲取
這些時間值,但看起來還不方便,最好是將所有的信息,如年、月、日、星期、時、分、秒以字符串的形式顯示出來,
這樣就加直觀.
4.時間顯示
???char *asctime(const struct tm *tm)
???函數(shù)功能:將tm格式的時間轉(zhuǎn)化為字符串
???參數(shù):日歷時間的返回值
???例如:?SAT Jul 30 08:43:03 2005
該函數(shù)較ctime()使用起來更加的復(fù)雜.必須按照下面3個步驟來進行.
???<1>使用函數(shù)time()來獲取日歷時間
???<2>使用函數(shù)gmtime()將日歷時間轉(zhuǎn)化為格林威治標準時間
???<3>使用函數(shù)asctime()將tm格式的時間轉(zhuǎn)化為字符串
???例程:
????#include <time.h>
????#include <stdio.h>
????int main(void)
???{
??????struct tm *ptr;
??????time_t lt;
??????lt=time(null);?
??????ptr=gmtime(<);
??????printf(asctime(ptr));
??????printf(ctime(<));
??????return 0;
???}
???char *ctime(const time_t *timep)
???函數(shù)功能:將日歷時間轉(zhuǎn)化為本地時間的字符串形式
???參數(shù):日歷時間的返回值
???該函數(shù)較asctime()使用起來更加簡單.必須按照下面2個步驟來進行.
???<1>使用函數(shù)time()來獲取日歷時間
???<2>使用函數(shù)ctime()將日歷時間直接轉(zhuǎn)化為字符串
??5.獲取從今日凌晨到現(xiàn)在的時間差
???int gettimeofday(struct timeval *tv,struct timezone *tz)
???函數(shù)功能:獲取從今日凌晨(0:0:0)到現(xiàn)在的時間差,常用于計算事件耗時
???參數(shù)1:存放從今日凌晨(0:0:0)到現(xiàn)在的時間差,時間差以秒或微秒為單位,以結(jié)構(gòu)體形式存放
?????????struct timeval
????????{
???????????int tv_sec;????//秒數(shù)
???????????int tv_usec;???//微秒數(shù)
????????}
???參數(shù)2:常設(shè)置為null
???函數(shù)用法:可以在做某件事情之前調(diào)用gettimeofday(),在做完該件事情之后調(diào)用gettimeofday(),兩個函數(shù)的參數(shù)1
的差就是做該事情所消耗的時間.
???例程:計算函數(shù)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);???// 結(jié)束時間
??// 計算執(zhí)行時間,以微秒為單位進行計算
??????timeuse=1000000*(tpend.tv_sec-tpstart.tv_sec)+tpend.tv_usec-tpstart.tv_usec;
??????timeuse/=1000000;
??????printf("used time:%f\n",timeuse);
??????exit(0);
??}
?執(zhí)行結(jié)果:
?[root@localhost lishuai]# gcc time.c -o time -wall
?[root@localhost lishuai]# ./time
????use time:0.006288
6.延時函數(shù)
???<1>使程序睡眠seconds秒
??????unsigned int sleep(unsigned int seconds)
???函數(shù)功能:使程序睡眠seconds秒
???參數(shù):需要休眠的秒數(shù)
???<2>使程序睡眠usec微秒
??????void usleep(unsigned long usec)
???函數(shù)功能:使程序睡眠usec微秒
???參數(shù):需要休眠的秒數(shù)
轉(zhuǎn)載于:https://www.cnblogs.com/Charles-Zhang-Blog/archive/2013/05/10/3071229.html
總結(jié)
以上是生活随笔為你收集整理的linux c语言获取时间的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CentOS6最小化安装默认启动的服务说
- 下一篇: C/C++ 读取16进制文件