1、格林威治時間 英語:Greenwich MeanTime,GMT)是指位於英國倫敦郊區的皇家格林尼治天文臺的標準時間,因爲本初子午線被定義在通過那裏的經線。 GMT 是“Greenwich Mean Time”的縮寫,中文叫“格林威治標準時間”,是英國的標準時間,也是世界各地時間的參考標準。中英兩國的標準時差爲8個小時,即英國的當地時間比中國的北京時間晚8小時。
2、UTC(Coordinated Universal Time --協調世界時) 協調世界時是以原子時秒長爲基礎,在時刻上儘量接近於世界時的一種時間計量系統。中國大陸、中國香港、中國澳門、中國臺灣、蒙古國、新加坡、馬來西亞、菲律賓、西澳大利亞州的時間與UTC的時差均爲+8,也就是UTC+8。
3、日曆時間,是用“從一個標準時間點到此時的時間經過的秒數”來表示的時間。日曆時間(Calendar Time)是通過time_t數據類型來表示的,用time_t表示的時間(日曆時間)是從一個時間點(例如:1970年1月1日0時0分0秒)到此時的秒數。time_t實際上是長整型。
4、獲取日曆時間(從1970年1月1日0點到現在所經歷的秒數)。
time_t time(time_t * tloc) typedef long time_t
5、將日曆時間轉化爲格林威治時間,並保存至 tm 結構中 。
struct tm * gmtime(const time_t * timep)
6、將日曆時間轉化爲本地時間,並保存至 tm 結構中 。
struct tm * locatime(const time_t * timep)
7、tm 結構體
struct tm
{
int tm_sec; //秒鐘
int tm_min; //分鐘值
int tm_hour; //小時
int tm_mday; //本月第幾日
int tm_mon; //本月第幾月
int tm_year; // 年
int tm_wday; // 本週第幾日
int tm_yday; // 本年第幾日
int tm_isdst; //
};
#include <time.h>
#include <stdio.h>
int main(void){
struct tm *local;
time_t t;
/* 獲取日曆時間 */
t=time(NULL);
/* 將日曆時間轉化爲本地時間 */
local=localtime(&t);
/*打印當前的小時值*/
printf("Local hour is: %d/n",local->tm_hour);
/* 將日曆時間轉化爲格林威治時間 */
local=gmtime(&t);
printf("UTC hour is: %d/n",local->tm_hour);
return 0;
}
8、將tm 格式的時間轉化爲字符串
char * asctime(const struct tm * tm)
tm 是用 localtime() 轉化來,則輸出本地時間字符串
tm 是用 gmtime() 轉化來,則輸出格林威治時間字符串
9、 將日曆時間轉化爲本地時間的字符串
char * ctime(const time_t * timep)
10、獲取從今日凌晨到現在的時間差 int gettimeofday(struct timeval * tv ,struct timezone * tz )
// 常用於計算事件耗時
//struct timeval { int tv_sec;//秒數 int tv_usec;// 微妙}
#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++;
}
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);
}
//這個程序用於計算function()的運行時間
11、使程序睡眠seconds秒 unsigned int sleep(unsigned int seconds)
12、使程序睡眠usec微秒 unsigned int sleep(unsigned long usec)