天天看點

Linux的時間函數

 http://blog.163.com/tianle_han/blog/static/6617826200921754959958/

 一、時間相關說明

格林威治時間表示0時區的标準時間。其他時區的時間和此标準時間均有時間差。UTC(Universal Time Coordinated)是世界協調時間,是格林威治時間在網際網路中的表示方法

二、标準C語言時間函數

1、time(取得本地目前的時間秒數)

#include<time.h>

time_t time(time_t *t);

函數說明  此函數會傳回從公元1970年1月1日的UTC時間從0時0分0秒算起到現在所經過的秒數。如果t 并非空指針的話,此函數也會将傳回值存到t指針所指的記憶體。

傳回值  成功則傳回秒數,失敗則傳回((time_t)-1)值,錯誤原因存于errno中。

time_t 定義為long int

範例  #include<time.h>

mian()

{

long int seconds= time((time_t*)NULL);

printf(“%d/n”,seconds);

}

執行  9.73E+08

2、gmtime(根據本地時間取得目前的UTC時間)

#include<time.h>

struct tm*gmtime(const time_t*timep);

函數說明  gmtime()将參數timep 所指的time_t 結構中的資訊轉換成真實世界所使用的時間日期表示方法,然後将結果由結構tm傳回。

結構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; 

};這個就叫Broken-down  time

int tm_sec 代表目前秒數,正常範圍為0-59,但允許至61秒

int tm_min 代表目前分數,範圍0-59

int tm_hour 從午夜算起的時數,範圍為0-23

int tm_mday 目前月份的日數,範圍01-31

int tm_mon 代表目前月份,從一月算起,範圍從0-11

int tm_year 從1900 年算起至今的年數

int tm_wday 一星期的日數,從星期一算起,範圍為0-6

int tm_yday 從今年1月1日算起至今的天數,範圍為0-365

int tm_isdst 日光節約時間的旗标

此函數傳回的時間日期未經時區轉換,而是UTC時間。

傳回值  傳回結構tm代表目前UTC 時間

範例  #include <time.h>

main(){

char *wday[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};

time_t timep;

struct tm *p;

time(&timep);

p=gmtime(&timep);

printf(“%d%d%d”,(1900+p->tm_year), (1+p->tm_mon),p->tm_mday);

printf(“%s%d;%d;%d/n”, wday[p->tm_wday], p->tm_hour, p->tm_min, p->tm_sec);

}

執行  2000/10/28 Sat 8:15:38

3、localtime(取得當地目前UTC時間和日期) 

#include<time.h>

struct tm *localtime(const time_t * timep);

函數說明  localtime()将參數timep所指的time_t結構中的資訊轉換成真實世界所使用的時間日期表示方法,然後将結果由結構tm傳回。結構tm的定義請參考gmtime()。此函數傳回的時間日期已經轉換成當地時區。

傳回值  傳回結構tm代表目前的當地時間。

範例  #include<time.h>

main(){

char *wday[]={“Sun”,”Mon”,”Tue”,”Wed”,”Thu”,”Fri”,”Sat”};

time_t timep;

struct tm *p;

time(&timep);

p=localtime(&timep);

printf (“%d%d%d ”, (1900+p->tm_year),( l+p->tm_mon), p->tm_mday);

printf(“%s%d:%d:%d/n”, wday[p->tm_wday],p->tm_hour, p->tm_min, p->tm_sec);

}

執行  2000/10/28 Sat 11:12:22

4、ctime(将時間和日期以字元串格式表示) 

#include<time.h>

char *ctime(const time_t *timep);

函數說明  ctime()将參數timep所指的time_t結構中的資訊轉換成真實世界所使用的時間日期表示方法,然後将結果以字元串形态傳回。此函數已經由時區 轉換成當地時間,字元串格式為“Wed Jun 30 21 :49 :08 1993/n”。若再調用相關的時間日期函數,此字元串可能會被破壞。

傳回值  傳回一字元串表示目前當地的時間日期。

範例  #include<time.h>

main()

{

time_t timep;

time (&timep);

printf(“%s”,ctime(&timep));

}

執行  Sat Oct 28 10 : 12 : 05 2000

5、asctime(将時間和日期以字元串格式表示) 

#include<time.h>

char * asctime(const struct tm * timeptr);

函數說明  asctime()将參數timeptr所指的tm結構中的資訊轉換成真實世界所使用的時間日期表示方法,然後将結果以字元串形态傳回。此函數已經由時區轉換成當地時間,字元串格式為:“Wed Jun 30 21:49:08 1993/n”

傳回值  若再調用相關的時間日期函數,此字元串可能會被破壞。此函數與ctime不同處在于傳入的參數是不同的結構。

附加說明  傳回一字元串表示目前當地的時間日期。

範例  #include <time.h>

main()

{

time_t timep;

time (&timep);

printf(“%s”,asctime(gmtime(&timep)));

}

執行  Sat Oct 28 02:10:06 2000

6、mktime(将時間結構資料轉換成經過的秒數) 

#include<time.h>

time_t mktime(strcut tm * timeptr);

函數說明  mktime()用來将參數timeptr所指的tm結構資料轉換成從公元1970年1月1日0時0分0 秒算起至今的UTC時間所經過的秒數。

傳回值  傳回經過的秒數。

範例 

#include<time.h>

main()

{

time_t timep;

strcut tm *p;

time(&timep);

printf(“time() : %d /n”,timep);

p=localtime(&timep);

timep = mktime(p);

printf(“time()->localtime()->mktime():%d/n”,timep);

}

執行  time():974943297

time()->localtime()->mktime():974943297

三、linux系統時間函數

1、gettimeofday(取得目前的時間) 

#include <sys/time.h>

#include <unistd.h>

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

函數說明  gettimeofday()會把目前的時間有tv所指的結構傳回,當地時區的資訊則放到tz所指的結構中。

timeval結構定義為:

struct timeval{

long tv_sec;

long tv_usec;

};

timezone 結構定義為:

struct timezone{

int tz_minuteswest;

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

傳回值  成功則傳回0,失敗傳回-1,錯誤代碼存于errno。附加說明EFAULT指針tv和tz所指的記憶體空間超出存取權限。

範例  #include<sys/time.h>

#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_sec: 974857339

tv_usec:136996

tz_minuteswest:-540

tz_dsttime:0

2、settimeofday(設定目前時間)

#include<sys/time.h>

#include<unistd.h>

int settimeofday ( const struct timeval *tv,const struct timezone *tz);

函數說明  settimeofday()會把目前時間設成由tv所指的結構資訊,當地時區資訊則設成tz所指的結構。詳細的說明請參考gettimeofday()。注意,隻有root權限才能使用此函數修改時間。

傳回值  成功則傳回0,失敗傳回-1,錯誤代碼存于errno。

錯誤代碼  EPERM 并非由root權限調用settimeofday(),權限不夠。

EINVAL 時區或某個資料是不正确的,無法正确設定時間。

3、clock_gettime(擷取指定時鐘的時間值)

#include <time.h>

int clock_gettime( clockid_t clock_id,struct timespec * tp );

說明:clock_id指定要擷取時間的時鐘,根據Posix的指定可以是以下值:

CLOCK_REALTIME

Systemwide realtime clock.

CLOCK_MONOTONIC

Represents monotonic time. Cannot be set.

CLOCK_PROCESS_CPUTIME_ID

High resolution per-process timer.

CLOCK_THREAD_CPUTIME_ID

Thread-specific timer.

CLOCK_REALTIME_HR

High resolution version of CLOCK_REALTIME.

CLOCK_MONOTONIC_HR

High resolution version of CLOCK_MONOTONIC.

struct timespec {

time_t tv_sec;       

long  tv_nsec;      

};

4、adjtimex(tune kernel clock)

#include <sys/timex.h>

int adjtimex(struct timex *buf);

說明:

Linux  uses  David L. Mills' clock adjustment algorithm (see RFC 1305).The system call adjtimex() reads and optionally sets adjustment parame-ters  for  this  algorithm.   It  takes a pointer to a timex structure,updates kernel parameters from  field  values,  and  returns  the  same structure  with  current  kernel values.  This structure is declared as follows:

struct timex {

int modes;          

long offset;        

long freq;          

long maxerror;      

long esterror;      

int status;         

long constant;      

long precision;     

long tolerance;     

struct timeval time;

long tick;          

};

The modes field determines which parameters, if any, to  set.   It  may contain a bitwise-or combination of zero or more of the following bits:

#define ADJ_OFFSET            0x0001

#define ADJ_FREQUENCY         0x0002

#define ADJ_MAXERROR          0x0004

#define ADJ_ESTERROR          0x0008

#define ADJ_STATUS            0x0010

#define ADJ_TIMECONST         0x0020

#define ADJ_TICK              0x4000

#define ADJ_OFFSET_SINGLESHOT 0x8001  

Ordinary users are restricted to a zero value for mode.  Only the supe-ruser may set any parameters. 

RETURN VALUE

On success, adjtimex() returns the clock state: 

#define TIME_OK   0

#define TIME_INS  1

#define TIME_DEL  2

#define TIME_OOP  3

#define TIME_WAIT 4

#define TIME_BAD  5  

On failure, adjtimex() returns -1 and sets errno. 

ERRORS

EFAULT

buf does not point to writable memory. 

EINVAL

An attempt is made to set buf.offset to a value outside the range -131071 to +131071, or to set buf.status to a value other than those listed above, or to set buf.tick to a value outside the range 900000/HZ to 1100000/HZ, where HZ is the system  timer interrupt frequency. 

EPERM

buf.mode is non-zero and the caller does not have sufficient privilege.Under Linux the CAP_SYS_TIME capability is required.

CONFORMING TO

adjtimex() is Linux specific and should not be used in programs intended to be portable. See adjtime(3) for a more portable, but less flexible, method of adjusting the system clock.

繼續閱讀