time.c相關

http://blog.csdn.net/emili/article/details/5642639 

原文:http://www.oklinux.cn/html/developer/cc/20070325/11441.html

SYNOPSIS:
char * asctime(const struct tm * timeptr);將參數timeptr所指的tm結構中的信息轉換成真實世界所使用的時間日期表示方法,然後將結果以字符串形態返回。此函數已經由時區轉換成當地時間,字符串格式爲:“Wed Jun 30 21:49:08 1993/n”
char *ctime(const time_t *timep);將參數timep所指的time_t結構中的信息轉換成真實世界所使用的時間日期表示方法,然後將結果以字符串形態返回。此函數已經由時區轉換成當地時間,字符串格式爲“Wed Jun 30 21 :49 :08 1993/n”。若再調用相關的時間日期函數,此字符串可能會被破壞。
int gettimeofday ( struct timeval * tv , struct timezone * tz ) 會把目前的時間有tv所指的結構返回,當地時區的信息則放到tz所指的結構中。
struct tm*gmtime(const time_t*timep);將參數timep 所指的time_t 結構中的信息轉換成真實世界所使用的時間日期表示方法,然後將結果由結構tm返回。
struct tm *localtime(const time_t * timep);localtime() 將參數timep所指的time_t結構中的信息轉換成真實世界所使用的時間日期表示方法,然後將結果由結構tm返回。結構tm的定義請參考 gmtime()。此函數返回的時間日期已經轉換成當地時區。
time_t mktime(strcut tm * timeptr);用來將參數timeptr所指的tm結構數據轉換成從公元1970年1月1日0時0分0 秒算起至今的UTC時間所經過的秒數。
int settimeofday ( const struct timeval *tv,const struct timezone *tz);會把目前時間設成由tv所指的結構信息,當地時區信息則設成tz所指的結構。詳細的說明請參考gettimeofday()。注意,只有root權限才能使用此函數修改時間。
time_t time(time_t *t); 此函數會返回從公元1970年1月1日的UTC時間從0時0分0秒算起到現在所經過的秒數。如果t 並非空指針的話,此函數也會將返回值存到t指針所指的內存。


 

asctime(將時間和日期以字符串格式表示)

原型:http://www.hackchina.com/r/44568/asctime.c__html
相關函數
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不同處在於傳入的參數是不同的結構。
附加說明
返回一字符串表示目前當地的時間日期。
範例
#include <time.h>
main()
{
time_t timep;
time (&timep);
printf(“%s”,asctime(gmtime(&timep)));
}
執行
Sat Oct 28 02:10:06 2000

 
ctime(將時間和日期以字符串格式表示)
原型:http://www.hackchina.com/r/44568/ctime.c__html

相關函數
time,asctime,gmtime,localtime
表頭文件
#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
 

gettimeofday(取得目前的時間)
相關函數
time,ctime,ftime,settimeofday
表頭文件
#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; /*和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所指的內存空間超出存取權限。
範例
#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


gmtime(取得目前時間和日期)
相關函數
time,asctime,ctime,localtime
表頭文件
#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;
};
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

 

localtime(取得當地目前時間和日期)
相關函數
time, asctime, ctime, gmtime
表頭文件
#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

 

mktime(將時間結構數據轉換成經過的秒數)
相關函數
time,asctime,gmtime,localtime
表頭文件
#include<time.h>
定義函數
time_t mktime(strcut tm * timeptr);
函數說明
mktime()用來將參數timeptr所指的tm結構數據轉換成從公元1970年1月1日0時0分0 秒算起至今的UTC時間所經過的秒數。
返回值
返回經過的秒數。
範例
/* 用time()取得時間(秒數),利用localtime()
轉換成struct tm 再利用mktine()將struct tm轉換成原來的秒數*/
#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

 

settimeofday(設置目前時間)
相關函數
time,ctime,ftime,gettimeofday
表頭文件
#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 時區或某個數據是不正確的,無法正確設置時間。

 

time(取得目前的時間)
相關函數
ctime,ftime,gettimeofday
表頭文件
#include<time.h>
定義函數
time_t time(time_t *t);
函數說明
此函數會返回從公元1970年1月1日的UTC時間從0時0分0秒算起到現在所經過的秒數。如果t 並非空指針的話,此函數也會將返回值存到t指針所指的內存。
返回值
成功則返回秒數,失敗則返回((time_t)-1)值,錯誤原因存於 errno中。
範例
#include<time.h>
main()
{
int seconds= time((time_t*)NULL);
printf(“%d/n”,seconds);
}
執行
9.73E+08

我們可以使用strftime()函數將時間格式化爲我們想要的格式。它的原型如下:
size_t strftime(
char *strDest,
size_t maxsize,
const char *format,
const struct tm *timeptr
);
      我們可以根據format指向字符串中格式命令把timeptr中保存的時間信息放在strDest指向的字符串中,最多向strDest中存放maxsize個字符。該函數返回向strDest指向的字符串中放置的字符數。
      函數strftime()的操作有些類似於sprintf():識別以百分號(%)開始的格式命令集合,格式化輸出結果放在一個字符串中。格式化命令說明串 strDest中各種日期和時間信息的確切表示方法。格式串中的其他字符原樣放進串中。格式命令列在下面,它們是區分大小寫的。
%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)
%W 每年的第幾周,把星期一做爲第一天(值從0到53)
%x 標準的日期串
%X 標準的時間串
%y 不帶世紀的十進制年份(值從0到99)
%Y 帶世紀部分的十制年份
%z,%Z 時區名稱,如果不能得到時區名稱則返回空字符。
%% 百分號

如果想顯示現在是幾點了,並以12小時制顯示,就象下面這段程序:
#i nclude “time.h”
#i nclude “stdio.h”
int main(void)
{
struct tm *ptr;
time_t lt;
char str[80];
lt=time(NULL);
ptr=localtime(<);
strftime(str,100,"It is now %I %p",ptr);
printf(str);
return 0;
}

其運行結果爲:
It is now 4PM


而下面的程序則顯示當前的完整日期:
#include
void main( void )
{
struct tm *newtime;
char tmpbuf[128];
time_t lt1;
time( <1 );
newtime=localtime(<1);
strftime( tmpbuf, 128, "Today is %A, day %d of %B in the year %Y./n", newtime);
printf(tmpbuf);
}
運行結果:
Today is Saturday, day 30 of July in the year 2005.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章