『壹』 在c語言中如何使用系統函數得到當前的日期
獲得日期和時間
這里說的日期和時間就是我們平時所說的年、月、日、時、分、秒等信息。從第2節我們已經知道這些信息都保存在一個名為tm的結構體中,那麼如何將一個日歷時間保存為一個tm結構的對象呢?
其中可以使用的函數是gmtime()和localtime(),這兩個函數的原型為:
struct
tm
*
gmtime(const
time_t
*timer);
struct
tm
*
localtime(const
time_t
*
timer);
其中gmtime()函數是將日歷時間轉化為世界標准時間(即格林尼治時間),並返回一個tm結構體來保存這個時間,而localtime()函數
是將日歷時間轉化為本地時間。比如現在用gmtime()函數獲得的世界標准時間是2005年7月30日7點18分20秒,那麼我用
localtime()函數在中國地區獲得的本地時間會比世界標准時間晚8個小時,即2005年7月30日15點18分20秒。下面是個例子:
#include
"time.h"
#include
"stdio.h"
int
main(void)
{
struct
tm
*local;
time_t
t;
t=time(NUL);
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;
}
運行結果是:
Local
hour
is:
15
UTC
hour
is:
7
固定的時間格式
我們可以通過asctime()函數和ctime()函數將時間以固定的格式顯示出來,兩者的返回值都是char*型的字元串。返回的時間格式為:
星期幾
月份
日期
時:分:秒
年\n{post.content}
例如:Wed
Jan
02
02:03:55
1980\n{post.content}
其中\n是一個換行符,{post.content}是一個空字元,表示字元串結束。下面是兩個函數的原型:
Char
*
asctime(const
struct
tm
*
timeptr);
char
*
ctime(const
time_t
*timer);
其中asctime()函數是通過tm結構來生成具有固定格式的保存時間信息的字元串,而ctime()是通過日歷時間來生成時間字元串。這樣的
話,asctime()函數只是把tm結構對象中的各個域填到時間字元串的相應位置就行了,而ctime()函數需要先參照本地的時間設置,把日歷時間轉
化為本地時間,然後再生成格式化後的字元串。在下面,如果t是一個非空的time_t變數的話,那麼:
printf(ctime(&t));
等價於:
struct
tm
*ptr;
ptr=localtime(&t);
printf(asctime(ptr));
那麼,下面這個程序的兩條printf語句輸出的結果就是不同的了(除非你將本地時區設為世界標准時間所在的時區):
#include
"time.h"
#include
"stdio.h"
int
main(void)
{
struct
tm
*ptr;
time_t
lt;
lt
=time(NUL);
ptr=gmtime(<);
printf(asctime(ptr));
printf(ctime(<));
return
0;
}
運行結果:
Sat
Jul
30
08:43:03
2005
Sat
Jul
30
16:43:03
2005
『貳』 C語言功能:獲取當前日期時間
#include
#include
int
main()
{
time_t
timep;
struct
tm
*p;
time(&timep);
p
=
localtime(&timep);
//此函數獲得的tm結構體的時間,是已經進行過時區轉化為本地時間
printf("%d%02d%02d%02d%02d%02d\n",
1900+p->tm_year,
1+p->tm_mon,
p->tm_mday,
p->tm_hour,
p->tm_min,
p->tm_sec);
return
0;
}
『叄』 13.5怎樣在C語言中取得當前日期或時間
C語言gmtime()函數:獲取當前時間和日期
頭文件:
#include<time.h>
定義函數:
structtm*gmtime(consttime_t*timep);
函數說明:gmtime()將參數timep 所指的time_t 結構中的信息轉換成真實世界所使用的時間日期表示方法,然後將結果由結構tm 返回。
結構tm 的定義為
structtm{
inttm_sec;//代表目前秒數,正常范圍為0-59,但允許至61秒
inttm_min;//代表目前分數,范圍0-59
inttm_hour;//從午夜算起的時數,范圍為0-23
inttm_mday;//目前月份的日數,范圍01-31
inttm_mon;//代表目前月份,從一月算起,范圍從0-11
inttm_year;//從1900年算起至今的年數
inttm_wday;//一星期的日數,從星期一算起,范圍為0-6
inttm_yday;//從今年1月1日算起至今的天數,范圍為0-365
inttm_isdst;//日光節約時間的旗標
};
此函數返回的時間日期未經時區轉換,而是UTC 時間。
返回值:返回結構tm 代表目前UTC 時間。
範例
#include<time.h>
main(){
char*wday[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
time_ttimep;
structtm*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 ",wday[p->tm_wday],p->tm_hour,p->tm_min,p->tm_sec);
}
執行結果:
2000/10/28Sat8:15:38
『肆』 在c語言中如何獲取當前日期
#include
#include
void
main(){
struct
tm
*
tmptr;//時間的結構體
time_t
secnow;
time(&secnow);
tmptr
=
localtime(&secnow);
int
hour1,min1;
hour1
=
tmptr->tm_hour;
printf("the
time
is
%02d",hour1);//輸出當前小時-----------這才是人家提問的要的時間的數字!
}
『伍』 C語言中獲取時間的方法是什麼
C語言的標准庫
函數包括一系列日期和時間處理函數,它們都在頭文件中說明。下面列出了這些函數。在頭文件中定義了三種類型:time_t,struct tm和clock_t。
在中說明的C語言時間函數
time_t time(time_t *timer);
double difftime(time_t time1,time_t time2);
struct tm *gmtime(const time_t *timer);
struct tm *localtime(const time_t *timer);
char *asctime(const struct tm *timeptr);
char *ctime(const time_t *timer);
size_t strftime(char *s,size_t maxsize,const char *format,const struct tm *timeptr);
time_t mktime(struct tm *timeptr);
clock_t clock(void);
下面是我從網上收集到的時間函數集
asctime(將時間和日期以字元串格式表示)
相關函數
time,ctime,gmtime,localtime
表頭文件
#i nclude
定義函數
char * asctime(const struct tm * timeptr);
函數說明
asctime()將參數timeptr所指的tm結構中的信息轉換成真實世界所使用的時間日期表示方法,然後將結果以字元串形態返回。此函數已經由時區轉換成當地時間,字元串格式為:"Wed Jun 30 21:49:08 1993\n"
返回值
若再調用相關的時間日期函數,此字元串可能會被破壞。此函數與ctime不同處在於傳入的參數是不同的結構。
附加說明
返回一字元串表示目前當地的時間日期。
範例
#i nclude
main()
{
time_t timep;
time (&timep);
printf("%s",asctime(gmtime(&timep)));
}
執行
Sat Oct 28 02:10:06 2000
ctime(將時間和日期以字元串格式表示)
相關函數
time,asctime,gmtime,localtime
表頭文件
#i nclude
定義函數
char *ctime(const time_t *timep);
函數說明
ctime ()將參數timep所指的time_t結構中的信息轉換成真實世界所使用的時間日期表示方法,然後將結果以字元串形態返回。此函數已經由時區轉換成當地時間,字元串格式為"Wed Jun 30 21 :49 :08 1993\n"。若再調用相關的時間日期函數,此字元串可能會被破壞。
返回值
返回一字元串表示目前當地的時間日期。
範例
#i nclude
main()
{
time_t timep;
time (&timep);
printf("%s",ctime(&timep));
}
執行
Sat Oct 28 10 : 12 : 05 2000
gettimeofday(取得目前的時間)
相關函數
time,ctime,ftime,settimeofday
表頭文件
#i nclude
#i nclude
定義函數
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所指的內存空間超出存取許可權。
範例
#i nclude
#i nclude
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
表頭文件
#i nclude
定義函數
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 時間
範例
#i nclude
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
表頭文件
#i nclude
定義函數
struct tm *localtime(const time_t * timep);
函數說明
localtime()將參數timep所指的time_t結構中的信息轉換成真實世界所使用的時間日期表示方法,然後將結果由結構tm返回。結構tm的定義請參考gmtime()。此函數返回的時間日期已經轉換成當地時區。
返回值
返回結構tm代表目前的當地時間。
範例
#i nclude
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
表頭文件
#i nclude
定義函數
time_t mktime(strcut tm * timeptr);
函數說明
mktime()用來將參數timeptr所指的tm結構數據轉換成從公元1970年1月1日0時0分0 秒算起至今的UTC時間所經過的秒數。
返回值
返回經過的秒數。
範例
/* 用time()取得時間(秒數),利用localtime()
轉換成struct tm 再利用mktine()將struct tm轉換成原來的秒數*/
#i nclude
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
表頭文件
#i nclude
#i nclude
定義函數
int settimeofday ( const struct timeval *tv,const struct timezone *tz);
函數說明
settimeofday()會把目前時間設成由tv所指的結構信息,當地時區信息則設成tz所指的結構。詳細的說明請參考gettimeofday()。注意,只有root許可權才能使用此函數修改時間。
返回值
成功則返回0,失敗返回-1,錯誤代碼存於errno。
錯誤代碼
EPERM 並非由root許可權調用settimeofday(),許可權不夠。
EINVAL 時區或某個數據是不正確的,無法正確設置時間。
『陸』 如何用C語言獲取當前系統時間
需要利用C語言的時間函數time和localtime,具體說明如下:
一、函數介面介紹:
1、time函數。
形式為time_t time (time_t *__timer);
其中time_t為time.h定義的結構體,一般為長整型。
這個函數會獲取當前時間,並返回。 如果參數__timer非空,會存儲相同值到__timer指向的內存中。
time函數返回的為unix時間戳,即從1970年1月1日(UTC/GMT的午夜)開始所經過的秒數,不考慮閏秒。
由於是秒作為單位的,所以這並不是習慣上的時間,要轉為習慣上的年月日時間形式就需要另外一個函數了。
2、localtime函數。
形式為struct tm *localtime (const time_t *__timer);
其中tm為一個結構體,包含了年月日時分秒等信息。
這種結構是適合用來輸出的。
二、參考代碼:
#include<stdio.h>
#include<time.h>
intmain()
{
time_tt;
structtm*lt;
time(&t);//獲取Unix時間戳。
lt=localtime(&t);//轉為時間結構。
printf("%d/%d/%d%d:%d:%d ",lt->tm_year+1900,lt->tm_mon,lt->tm_mday,lt->tm_hour,lt->tm_min,lt->tm_sec);//輸出結果
return0;
}
注意事項:
struct tm中的tm_year 值為實際年減去1900, 所以輸出的時候要是lt->tm_year+1900。
『柒』 看過來,看過來 C語言獲取系統時間的幾種方式
我們在寫C語言程序的時候,有的時候會用到讀取本機的時間和日期,怎麼做呢?其實很簡單的,下面簡單說一下:
C語言中讀取系統時間的函數為time(),其函數原型為:#include <time.h>time_t time( time_t * ) ;
time_t就是long,函數返回從1970年1月1日(MFC是1899年12月31日)0時0分0秒,到現在的的秒數。
可以調用ctime()函數進行時間轉換輸出:char * ctime(const time_t *timer);
將日歷時間轉換成本地時間,按年月日格式,進行輸出,如:Wed Sep 23 08:43:03 2015C語言還提供了將秒數轉換成相應的時間結構的函數:
struct tm * gmtime(const time_t *timer);//將日歷時間轉化為世界標准時間(即格林尼治時間)
struct tm * localtime(const time_t * timer);//將日歷時間轉為本地時間將通過time()函數返回的值,轉成時間結構structtm :
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日,1代表1月2日,以此類推 */
int tm_isdst; /* 夏令時標識符,實行夏令時的時候,tm_isdst為正。不實行夏令時的進候,tm_isdst為0;不了解情況時,tm_isdst()為負。*/};
編程者可以根據程序功能的情況,靈活的進行日期的讀取與輸出了。
下面給出一段簡單的代碼:
#include<time.h>
intmain()
{
time_ttimep;
structtm*p;
time(&timep);
p=gmtime(&timep);
printf("%d ",p->tm_sec);/*獲取當前秒*/
printf("%d ",p->tm_min);/*獲取當前分*/
printf("%d ",8+p->tm_hour);/*獲取當前時,這里獲取西方的時間,剛好相差八個小時*/
printf("%d ",p->tm_mday);/*獲取當前月份日數,范圍是1-31*/
printf("%d ",1+p->tm_mon);/*獲取當前月份,范圍是0-11,所以要加1*/
printf("%d ",1900+p->tm_year);/*獲取當前年份,從1900開始,所以要加1900*/
printf("%d ",p->tm_yday);/*從今年1月1日算起至今的天數,范圍為0-365*/
}
『捌』 問在C語言里怎麼獲取當前時間和日期
#include <time.h> 要添加這個頭文件。
time_t rawtime;
struct tm * target_time;
time ( &rawtime ); //獲取當前時間,存rawtime里
target_time = localtime ( &rawtime ); //獲取當地時間
利用struct tm,你可以按需取出年月日時分秒星期幾等數值。
---------------------
你的問題:
time_t now;
long int dt=3600; // 時間長度,秒數
now = time (NULL); //獲取當前時間
printf("%s ",ctime(&now)); //直接列印時間
now=now+dt;
printf("%s ",ctime(&now)); // 直接列印加dt後的時間
(當然,你也可以用 ctime(&now) 返回的字元串 通過 MFC 的方法顯示)
『玖』 如何讓c語言自動讀取電腦上的日期及時間
C語言中讀取系統時間的函數為time(),其函數原型為:
#include <time.h>
time_t time( time_t * ) ;
time_t就是long,函數返回從1970年1月1日(MFC是1899年12月31日)0時0分0秒,到現在的的秒數。可以調用ctime()函數進行時間轉換輸出:
char * ctime(const time_t *timer);
將日歷時間轉換成本地時間,按年月日格式,進行輸出,如:
Wed Sep 23 08:43:03 2015
C語言還提供了將秒數轉換成相應的時間結構的函數:
struct tm * gmtime(const time_t *timer); //將日歷時間轉化為世界標准時間(即格林尼治時間)
struct tm * localtime(const time_t * timer); //將日歷時間轉化為本地時間
將通過time()函數返回的值,轉換成時間結構struct 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日,1代表1月2日,以此類推 */
int tm_isdst; /* 夏令時標識符,實行夏令時的時候,tm_isdst為正。不實行夏令時的進候,tm_isdst為0;不了解情況時,tm_isdst()為負。*/
};
編程者可以根據程序功能的情況,靈活的進行日期的讀取與輸出了。
例如:
#include<time.h>
main()
{
time_ttimep;
structtm*p;
time(&timep);
p=gmtime(&timep);
printf("%d ",p->tm_sec);/*獲取當前秒*/
printf("%d ",p->tm_min);/*獲取當前分*/
printf("%d ",8+p->tm_hour);/*獲取當前時,這里獲取西方的時間,剛好相差八個小時*/
printf("%d ",p->tm_mday);/*獲取當前月份日數,范圍是1-31*/
printf("%d ",1+p->tm_mon);/*獲取當前月份,范圍是0-11,所以要加1*/
printf("%d ",1900+p->tm_year);/*獲取當前年份,從1900開始,所以要加1900*/
printf("%d ",p->tm_yday);/*從今年1月1日算起至今的天數,范圍為0-365*/
}
『拾』 請問在C語言里怎麼獲取當前時間和日期(精確到毫秒)
你試試下面的程序:
#include
#include
#include
int
main()
{
clock_t
start,
finish;
double
elapsed_time;
start=clock();
//do
sonething
...
finish=clock();
elapsed_time
=
finish-start;
}
我在vc++
6.0下運行,可以得到以毫秒為單位的計時