1.變數的定義:
struct tm { int tm_sec; /* (0 - 61) */ int tm_min; /* (0 - 59) */ int tm_hour; /* (0 - 23) */ int tm_mday; /* (1 - 31) */ int tm_mon; /* (0 - 11) */ int tm_year; /* past 1900 */ int tm_wday; /* (0 - 6) */ int tm_yday; /* (0 - 365) */ int tm_isdst; /* daylight savings flag */ }; |
2.一個執行個體:讀取系統時間,顯示:
/*
c語言中與系統時間相關的函數的樣本
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <dos.h>
void main( void )
{
/*時間*/
time_t timer ;
/*指向struct tm的指標*/
struct tm *ptrtime;
/*調用time()函數擷取目前時間*/
timer = time( NULL ) ;
/*調用localtime()函數將獲得的系統時間轉化為指向struct tm的指標指向的結構體*/
ptrtime = localtime( &timer ) ;
/*用asctime()將結構體轉化為字串輸出*/
printf("Now is: %s",asctime( ptrtime ) ) ;
getch();
}
3.相關標頭檔
#include<time.h>
4.其他函數
| asctime() |
| 相關函數 |
time,ctime,gmtime,localtime
|
| 表標頭檔 |
#include<time.h>
|
| 定義函數 |
char * asctime(const struct tm * timeptr);
|
| 函數說明 |
asctime()將參數timeptr所指的tm結構中的資訊轉換成真實世界所使用的時間日期表示方法,然後將結果以字串形態返回。此函數已經由時區轉換成當地時間,字串格式為:“Wed Jun 30 21:49:08 1993/n”
|
| 傳回值 |
若再調用相關的時間日期函數,此字串可能會被破壞。此函數與ctime不同處在於傳入的參數是不同的結構。
|
| 附加說明 |
返回一字串表示目前當地的時間日期。
| gettimeofday(取得目前的時間) |
| 相關函數 |
time,ctime,ftime,settimeofday
|
| 表標頭檔 |
#include <sys/time.h> #include <unistd.h>
|
| 定義函數 |
int gettimeofday ( struct timeval * tv , struct timezone * tz )
|
| 函數說明 |
gettimeofday()會把目前的時間有tv所指的結構返回,當地時區的資訊則放到tz所指的結構中。
|
|