1.1 概述
世界標准時間(
Coordinated Universal Time
,UTC
),也就是大家所熟知的格林威治標准時間(
Greenwich Mean Time
,GMT
)。世界各地時間也世界標准時間為基准划分為不同的時區,例如,中國的北京時間與UTC
的時差為+8
,也就是UTC+8
。美國是UTC-5
。
Calendar Time
:日歷時間,是用“
從一個標准時間點到此時的時間經過的秒數”
來表示的時間。無論哪一個時區,在同一時刻對同一個標准時間點來說,日歷時間都是一樣的。日歷時間返回自1970-1-1:00:00:00
以來所經過的秒數累計值。
跟日期時間相關的shell
命令
$ date
//
顯示當前日期
$ time
//
顯示程序運行的時間
$ hwclock
//
顯示與設定硬件時鍾
$ clock
//
顯示與設定硬件時鍾,是hwclock
的鏈接文件
$ cal
//
顯示日歷
(1
)date
顯示或設置當前日期時間
$ date
顯示當前日期時間 --
中國北京時間
CST China Standard Time UTC+8:00
2008
年 05
月 01
日
星期四 04:28:27 CST
$ date –u
顯示當前日期時間 --
世界標准時間 UTC
2008
年 04
月 30
日
星期三 20:29:23 UTC
以上兩個時間相比有8
個小時的時差
$ date –R
顯示當前日期時間 – RFC
格式
Thu, 01 May 2008 04:30:25 +0800
$ date -s 20080501
設置日期
$ date -s 20:40:30
設置時間
(2
)time
顯示程序運行時消耗的實際時間,用戶CPU
時間和系統CPU
時間。
$ time a.out
可執行程序a.out
real
0m10.081s
程序開始運行到結束的時間
user
0m0.000s
用戶CPU
時間,
sys
0m0.004s
系統CPU
時間
用戶CPU
時間等於times
函數返回的struct tms
中的tms_utime
和 tms_cutime
和。
系統CPU
時間等於times
函數返回的struct tms
中的tms_stime
和 tms_cstime
和。
(3
)hwclock
顯示與設定硬件時鍾
在Linux
中有硬件時鍾與系統時鍾等兩種時鍾。硬件時鍾是指主機板上的時鍾設備,也就是通常可在BIOS
畫面設定的時鍾。系統時鍾則是指 kernel
中的時鍾。當Linux
啟動時系統時鍾會去讀取硬件時鍾的設定,之后系統時鍾即獨立運作。所有Linux
相關指令與函數都是讀取系統時鍾的設定。
# hwclock –show
顯示硬件時鍾的時間與日期
# hwclock –hctosys
將硬件時鍾調整為與目前的系統時鍾一致
# hwclock –systohc
將硬件時鍾調整為與目前的系統時鍾一致
# hwclock --set --date="20080430 21:30:30"
設定硬件時鍾
# hwclock
同hwclock –show
Clock
命名是hwclock
的鏈接文件
$ ls -al /sbin/clock
lrwxrwxrwx 1 root root 7 03-08 23:59 /sbin/clock -> hwclock
(4
)顯示日歷
$ cal
顯示本年本月的日歷
$ cal month year
顯示指定年月的日歷: cal 4 2008
1.2 跟日期時間有關的數據結構
1.2.1 clock_t 結構
程序開始運行到此時所經過的CPU
時鍾計時單元數用clock
數據類型表示。
typedef long clock_t;
#define CLOCKS_PER_SEC ((clock_t)1000) //
每個時鍾單元是1
毫秒
1.2.2 time_t 結構
日歷時間(
Calendar Time
)是通過time_t
數據類型來表示的,用time_t
表示的時間(日歷時間)是從一個時間點(1970
年1
月1
日0
時0
分0
秒
)到此時的秒數。
typedef long time_t; //
時間值
1.2.3 tm結構
通過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
代表一月) -
取值區間為[0,11] */
int tm_year; /*
年份,其值等於實際年份減去1900 */
int tm_wday; /*
星期 –
取值區間為[0,6]
,其中0
代表星期天,1
代表星期一 */
int tm_yday; /*
從每年1
月1
日開始的天數–
取值區間[0,365]
,其中0
代表1
月1
日 */
int tm_isdst; /*
夏令時標識符,夏令時tm_isdst
為正;不實行夏令時tm_isdst
為0
;*/
};
1.2.4 tms結構
保存着一個進程及其子進程使用的CPU
時間
struct tms{
clock_t tms_utime;
clock_t tms_stime;
clock_t tms_cutime;
clock_t tms_cstime;
}
1.2.5 Utimbuf結構
struct utimbuf{
time_t actime; //
存取時間
time_t modtime; //
修改時間
}
文件的時間
st_atime
文件數據的最后存取時間
st_mtime
文件數據的最后修改時間
st_ctime
文件數據的創建時間
1.2.5 timeval結構
{
time_t tv_sec;
susecond_t tv_usec; //當前妙內的微妙數
};
1.2.6 timer_struct結構
struct timer_struct {
unsigned long expires; //定時器被激活的時刻
void (*fn)(void); //定時器激活后的處理函數 }
1.3 跟日期時間相關的函數
1.3.1 clock函數
#include <time.h>
clock_t clock(void);
返回從程序開始運行到程序中調用clock()
函數之間的CPU
時鍾計時單元數
例
1
:clock
函數的例子
$ vi clock.c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
long
loop = 10000000L;
double duration;
clock_t start, end;
printf("Time to do %ld empty loops is ", loop);
start = clock();
while(loop--) ;
end = clock();
duration = (double)(end-start)/CLOCKS_PER_SEC;
printf("%f seconds\n", duration);
return(0);
}
編譯、運行:
$ gcc clock.c -o clock
$ ./clock
Time to do 10000000 empty loops is 0.220000 seconds
1.3.2 time函數
日歷時間
#include <time.h>
time_t time(time_t *calptr)
;
返回自1970-1-1:00:00:00
以來經過的秒數累計值
例
2
:time
函數的例子
#include <stdio.h>
#include <time.h>
int main(void)
{
time_t now;
time(&now);
printf("now time is %d\n", now);
return(0);
}
編譯、運行:
$ gcc time.c -o time
$ ./time
now time is 1193688148
1.3.3 times函數
程序運行的時間
#include <sys/times.h>
clock_t times(struct tms *buf);
返回自系統自舉后經過的時鍾滴答數
例
3
:times
函數的例子
#include <stdio.h>
#include <sys/times.h>
int main(void)
{
int i;
clock_t start, end;
struct tms tms_start, tms_end;
start = times(&tms_start);
end = times(&tms_end);
printf("start clock time : %d\n", start);
printf("end clock time : %d\n", end);
return(0);
}
編譯、運行:
$ gcc times.c -o times
$ ./times
Start clock time : 1720654909
End clock time : 1720654909
1.3.4 localtime函數
將日歷時間變換成本地時間,考慮到本地時區和夏令時標志。
#include <time.h>
struct tm *localtime(const time_t * calptr);
例
4
:localtime
函數的例子
#include <stdio.h>
#include <time.h>
int main(void)
{
time_t now;
struct tm *tm_now;
time(&now);
tm_now = localtime(&now);
printf("now datetime: %d-%d-%d %d:%d:%d\n", tm_now->tm_year, tm_now->tm_mon, tm_now->tm_mday, tm_now->tm_hour, tm_now->tm_min, tm_now->tm_sec);
return(0);
}
編譯、運行:
$ gcc localtime.c -o localtime
$ ./localtime
now datetime: 107-9-30 5:11:43
1.3.5 gmtime函數
將日歷時間變換成國際標准時間的年月日分秒
#include <time.h>
struct tm *gmtime(const time_t *calptr);
例
5
:gmtime
函數的例子
#include <stdio.h>
#include <time.h>
int main(void)
{
time_t now;
struct tm *tm_now;
time(&now);
tm_now = gmtime(&now);
printf("now datetime: %d-%d-%d %d:%d:%d\n", tm_now->tm_year, tm_now->tm_mon, tm_now->tm_mday, tm_now->tm_hour, tm_now->tm_min, tm_now->tm_sec);
return(0);
}
編譯、運行:
$ gcc gmtime.c -o gmtime
$ ./gmtime
now datetime: 107-9-29 21:15:26
1.3.6 mktime函數
以本地時間的年月日為參數,將其變換成time_t
值
#include <time.h>
time_t mktime(struct tm *tmptr);
例
6
:mktime
函數的例子
#include <stdio.h>
#include <time.h>
int main(void)
{
time_t now, new_time;
struct tm *tm_now;
time(&now);
printf("now time is %ld\n", now);
tm_now = localtime(&now);
new_time = mktime(tm_now);
printf("new time is %ld\n", new_time);
return(0);
}
編譯、運行:
$ gcc mktime.c -o mktime
$ ./mktime
now time is 1193692806
new time is 1193692806
1.3.7 asctime函數
產生形式的26
字節字符串,參數指向年月日等字符串的指針。與date
命令輸出形式類似
#include <time.h>
char *asctime(const struct tm *tmptr);
例
7
:astime
函數的例子
#include <stdio.h>
#include <time.h>
int main(void)
{
time_t now;
struct tm *tm_now;
char *datetime;
time(&now);
tm_now = localtime(&now);
datetime = asctime(tm_now);
printf("now datetime: %s\n", datetime);
return(0);
}
編譯、運行:
$ gcc asctime.c -o asctime
$ ./asctime
now datetime: Tue Oct 30 05:22:21 2007
1.3.8 ctime函數
產生形式的26
字節字符串,參數指向日歷時間的指針。
#include <time.h>
char *ctime(const time_t *calptr);
例
8
:ctime
函數的例子
#include <stdio.h>
#include <time.h>
int main(void)
{
time_t now;
char *datetime;
time(&now);
datetime = ctime(&now);
printf("now datetime: %s\n", datetime);
return(0);
}
編譯、運行:
$ gcc ctime.c -o ctime
$ ./ctime
now datetime: Tue Oct 30 05:23:45 2007
1.3.9 strftime函數
格式化時間輸出
#include <time.h>
size_t strftime(char *buf,size_t maxsize,const char *format,const struct tm *tmptr);
%a
星期幾的簡寫
%A
星期幾的全稱
%b
月分的簡寫
%B
月份的全稱
%c
標准的日期的時間串
%C
年份的后兩位數字
%d
十進制表示的每月的第幾天
%D
月/
天/
年
%e
在兩字符域中,十進制表示的每月的第幾天
%F
年-
月-
日
%g
年份的后兩位數字,使用基於周的年
%G
年分,使用基於周的年
%h
簡寫的月份名
%H 24
小時制的小時
%I 12
小時制的小時
%j
十進制表示的每年的第幾天
%m
十進制表示的月份
%M
十時制表示的分鍾數
%n
新行符
%p
本地的AM
或PM
的等價顯示
%r 12
小時的時間
%R
顯示小時和分鍾:hh:mm
%S
十進制的秒數
%t
水平制表符
%T
顯示時分秒:hh:mm:ss
%u
每周的第幾天,星期一為第一天
(值從0
到6
,星期一為0
)
%U
第年的第幾周,把星期日做為第一天(值從0
到53
)
%V
每年的第幾周,使用基於周的年
%w
十進制表示的星期幾(值從0
到6
,星期天為0
)
%x
標准的日期串
%X
標准的時間串
%y
不帶世紀的十進制年份(值從0
到99
)
%Y
帶世紀部分的十進制年份
%z
,%Z
時區名稱,如果不能得到時區名稱則返回空字符。
%%
百分號
例
9
:strftime
函數的例子
#include <stdio.h>
#include <time.h>
int main(void)
{
time_t now;
struct tm *tm_now;
char
datetime[200];
time(&now);
tm_now = localtime(&now);
strftime(datetime, 200, "%x %X %n%Y-%m-%d %H:%M:%S %nzone: %Z\n", tm_now);
printf("now datetime : %s\n", datetime);
return(0);
}
編譯、運行:
$ gcc strftime.c -o strftime
]$ ./strftime
now datetime : 10/30/07 05:41:47
2007-10-30 05:41:47
zone: CST
1.3.10 utime函數
更改文件的存取和修改時間
#include <time.h>
int utime(const char pathname, const struct utimbuf *times)
;
返回值:成功返回0
,失敗返回-1
times
為空指針,存取和修改時間設置為當前時間
例
10
:
utime
函數的例子
#include <stdio.h>
#include <time.h>
int main(int argc, char *argv[])
{
if(argc < 2){
fprintf(stderr, "Error: usging command file_path");
exit(1);
}
utime(argv[1], NULL);
return(0);
}
編譯、運行:
$ touch file1
$ ls -al file1 //
先創建一個文件file1
,查看一下他的創建時間
-rw-r--r-- 1 hongdy hongdy 3431 05-01 05:59 file1
$ gcc utime.c –o utime
$ ./utime file1
$ ls -al file1
-rw-r--r-- 1 hongdy hongdy 3431 05-01 06:00 file1
1.3.11 gettimeofday函數
取得目前的時間
#include <time.h>
int gettimeofday ( struct& nbsptimeval * tv , struct timezone * tz )
;
函數說明 gettimeofday()會把目前的時間有tv所指的結構返回,當地時區的信息則放到tz所指的結構中。
timeval結構定義為:
struct timeval{
long tv_sec; /*秒*/
long tv_usec; /*微秒*/
};
timezone 結構定義為:
struct timezone{
int tz_minuteswest; /*和Greenwich 時間差了多少分鍾*/
int tz_dsttime; /*日光節約時間的狀態*/
};
上述兩個結構都定義在/usr/include/sys/time.h。tz_dsttime 所代表的狀態如下
DST_NONE /*不使用*/
DST_USA /*美國*/
DST_AUST /*澳洲*/
DST_WET /*西歐*/
DST_MET /*中歐*/
DST_EET /*東歐*/
DST_CAN /*加拿大*/
DST_GB /*大不列顛*/
DST_RUM /*羅馬尼亞*/
DST_TUR /*土耳其*/
DST_AUSTALT /*澳洲(1986年以后)*/
返回值 成功則返回0,失敗返回-1,錯誤代碼存於errno。附加說明EFAULT指針tv和tz所指的內存空間超出存取權限。
times
為空指針,存取和修改時間設置為當前時間
例
10
:gettimeofday
函數的例子
#include<unistd.h>
main(){
struct timeval tv;
struct timezone tz;
gettimeofday (&tv , &tz);
printf(“tv_sec; %d\n”, tv,.tv_sec)
printf(“tv_usec; %d\n”,tv.tv_usec);
printf(“tz_minuteswest; %d\n”, tz.tz_minuteswest);
printf(“tz_dsttime, %d\n”,tz.tz_dsttime);
}
編譯、運行:
tv_usec:136996
tz_minuteswest:-540
tz_dsttime:0
tv_sec: 974857339
tz_minuteswest:-540
tz_dsttime:0
1.3.12 settimeofday函數
設置目前時間
#include<unistd.h>
int settimeofday ( const& nbspstruct timeval *tv,const struct timezone *tz);
函數說明 settimeofday()會把目前時間設成由tv所指的結構信息,當地時區信息則設成tz所指的結構。詳細的說明請參考gettimeofday()。注意,只有root權限才能使用此函數修改時間。
返回值 成功則返回0,失敗返回-1,錯誤代碼存於errno。
錯誤代碼 EPERM 並非由root權限調用settimeofday(),權限不夠。
EINVAL 時區或某個數據是不正確的,無法正確設置時間。
times
為空指針,存取和修改時間設置為當前時間
例
12
:settimeofday
函數的例子
/************************************************
設置操作系統時間
參數:*dt數據格式為"2006-4-20 20:30:30"
調用方法:
char *pt="2006-4-20 20:30:30";
SetSystemTime(pt);
**************************************************/
int SetSystemTime(char *dt)
{
struct rtc_time tm;
struct tm _tm;
struct timeval tv;
time_t timep;
sscanf(dt, "%d-%d-%d %d:%d:%d", &tm.tm_year,
&tm.tm_mon, &tm.tm_mday,&tm.tm_hour,
&tm.tm_min, &tm.tm_sec);
_tm.tm_sec = tm.tm_sec;
_tm.tm_min = tm.tm_min;
_tm.tm_hour = tm.tm_hour;
_tm.tm_mday = tm.tm_mday;
_tm.tm_mon = tm.tm_mon - 1;
_tm.tm_year = tm.tm_year - 1900;
timep = mktime(&_tm);
tv.tv_sec = timep;
tv.tv_usec = 0;
if(settimeofday (&tv, (struct timezone *) 0) < 0)
{
printf("Set system datatime error!\n");
return -1;
}
return 0;
}
設置操作系統時間
參數:*dt數據格式為"2006-4-20 20:30:30"
調用方法:
char *pt="2006-4-20 20:30:30";
SetSystemTime(pt);
**************************************************/
int SetSystemTime(char *dt)
{
struct rtc_time tm;
struct tm _tm;
struct timeval tv;
time_t timep;
sscanf(dt, "%d-%d-%d %d:%d:%d", &tm.tm_year,
&tm.tm_mon, &tm.tm_mday,&tm.tm_hour,
&tm.tm_min, &tm.tm_sec);
_tm.tm_sec = tm.tm_sec;
_tm.tm_min = tm.tm_min;
_tm.tm_hour = tm.tm_hour;
_tm.tm_mday = tm.tm_mday;
_tm.tm_mon = tm.tm_mon - 1;
_tm.tm_year = tm.tm_year - 1900;
timep = mktime(&_tm);
tv.tv_sec = timep;
tv.tv_usec = 0;
if(settimeofday (&tv, (struct timezone *) 0) < 0)
{
printf("Set system datatime error!\n");
return -1;
}
return 0;
}