當前位置:首頁 » 編程語言 » c語言時間函數

c語言時間函數

發布時間: 2022-01-14 03:46:37

1. c語言函數的時間日期函數

函數庫為time.h、dos.h
在時間日期函數里,主要用到的結構有以下幾個:
總時間日期貯存結構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-11*/ │
│ int tm_year; /*自1900的年數*/ │
│ int tm_wday; /*自星期日的天數0-6*/ │
│ int tm_yday; /*自1月1日起的天數,0-365*/ │
│ int tm_isdst; /*是否採用夏時制,採用為正數*/│
│} │
└──────────────────────┘
日期貯存結構date
┌───────────────┐
│struct date │
│{ │
│ int da_year; /*自1900的年數*/│
│ char da_day; /*天數*/ │
│ char da_mon; /*月數 1=Jan*/ │
│} │
└───────────────┘
時間貯存結構time
┌────────────────┐
│struct time │
│{ │
│ unsigned char ti_min; /*分鍾*/│
│ unsigned char ti_hour; /*小時*/│
│ unsigned char ti_hund; │
│ unsigned char ti_sec; /*秒*/ │
│ │
└────────────────┘char *ctime(long *clock)
本函數把clock所指的時間(如由函數time返回的時間)轉換成下列格式的
字元串:Mon Nov 21 11:31:54 1983
char *asctime(struct tm *tm)
本函數把指定的tm結構類的時間轉換成下列格式的字元串:
Mon Nov 21 11:31:54 1983
double difftime(time_t time2,time_t time1)
計算結構time2和time1之間的時間差距(以秒為單位)
struct tm *gmtime(long *clock)本函數把clock所指的時間(如由函數time返回的時間)
轉換成格林威治時間,並以tm結構形式返回
struct tm *localtime(long *clock)本函數把clock所指的時間(如函數time返回的時間)
轉換成當地標准時間,並以tm結構形式返回
void tzset()本函數提供了對UNIX操作系統的兼容性
long dostounix(struct date *dateptr,struct time *timeptr)
本函數將dateptr所指的日期,timeptr所指的時間轉換成UNIX格式,並返回
自格林威治時間1970年1月1日凌晨起到現在的秒數
void unixtodos(long utime,struct date *dateptr,struct time *timeptr)
本函數將自格林威治時間1970年1月1日凌晨起到現在的秒數utime轉換成
DOS格式並保存於用戶所指的結構dateptr和timeptr中
void getdate(struct date *dateblk)本函數將計算機內的日期寫入結構dateblk
中以供用戶使用
void setdate(struct date *dateblk)本函數將計算機內的日期改成
由結構dateblk所指定的日期
void gettime(struct time *timep)本函數將計算機內的時間寫入結構timep中,
以供用戶使用
void settime(struct time *timep)本函數將計算機內的時間改為
由結構timep所指的時間
long time(long *tloc)本函數給出自格林威治時間1970年1月1日凌晨至現在所經
過的秒數,並將該值存於tloc所指的單元中.
int stime(long *tp)本函數將tp所指的時間(例如由time所返回的時間)
寫入計算機中.

2. C語言時間函數time_t

1、time_t // 時間類型(time.h 定義)
struct tm { // 時間結構,time.h 定義如下:
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;
}
time ( &rawtime ); // 獲取時間,以秒計,從1970年1月一日起算,存於rawtime
localtime ( &rawtime ); //轉為當地時間,tm 時間結構
asctime() // 轉為標准ASCII時間格式:
//就是直接列印tm,tm_year 從1900年計算,所以要加1900,月tm_mon,從0計算,所以要加1

2、time函數使用示例

#include<stdio.h>
#include<time.h>
intmain()
{
time_trawtime;
structtm*timeinfo;
time(&rawtime);
timeinfo=localtime(&rawtime);
printf("Thecurrentdate/timeis:%s",asctime(timeinfo));

return0;
}

3. C語言計算時間函數

標准庫的time.h里有時間函數

time_t time (time_t *timer)
計算從1970年1月1日到當前系統時間,並把結果返回給timer變數,
函數本身返回的也是這個結果.time_t這個類型其實就是一個int.

另有:
double difftime ( time_t timer2, time_t timer1 )
把返回time2和time1所儲存的時間的差.

4. C語言調用系統時間函數是什麼啊

C++可以,C貌似不行.
C++里邊有一個CTime
的class
自己在msdn里搜索CTime就有了.
或者你CTime
time;
\\回車
然後輸入"time."
VC會給里列出所有的function
不過C
倒是可以include
<time.h>
里邊有一些和時間有關的function,
有沒有調用系統時間的我就忘記了.畢竟不常用.你可以自己查查MSDN,搜索time.h試試看.

5. c語言調用時間函數

time_t t; /*定義一個time_t型(在time.h中有typedef long time_t;語句,由此可知,time_t類型也就是long類型)的變數*/

time(&t); /*將當前的日歷時間(即從1970-1-1到執行此語句時所經歷的秒數)保存到t中*/

printf("%s/n", ctime(&t)); /*ctime(&t)將把t所指向的日歷時間轉換為系統所提供的一個字元串,這個函數將返回這個字元串的基址,然後由printf語句將這個字元串輸出,從而得到現在的時刻*/
來源http://..com/question/32925135.html?si=1

6. (高分)c語言關於時間的函數

time_t sec = time(NULL);
struct tm t = *localtime(&sec);
//第一次
printf("%.4d-%.2d-%.2d %.2d:%.2d:%.2d\n",info[a].year_in,info[a].mon_in,info[a].mday_in,info[a].hour_in,info[a].min_in,info[a].sec_in);
//第二次
sec = time(NULL);
t = *localtime(&sec);
printf("%.4d-%.2d-%.2d %.2d:%.2d:%.2d\n",info[a].year_in,info[a].mon_in,info[a].mday_in,info[a].hour_in,info[a].min_in,info[a].sec_in);

7. C語言計算時間

在C語言中計算時間,可以使用標准庫中的計時函數——clock()。

函數原型:

clock_tclock(void);

其中clock_t是用來保存時間的數據類型,在time.h文件中,可以找到對它的定義:

#ifndef_CLOCK_T_DEFINED
typedeflongclock_t;
#define_CLOCK_T_DEFINED
#endif


很明顯,clock_t是一個長整形數。在time.h文件中,還定義了一個常量CLOCKS_PER_SEC,它用來表示一秒鍾會有多少個時鍾計時單元,其定義如下:

#defineCLOCKS_PER_SEC((clock_t)1000)

可以看到每過千分之一秒(1毫秒),調用clock()函數返回的值就加1。下面舉個例子,可以使用公式clock()/CLOCKS_PER_SEC來計算一個進程自身的運行時間:

voidelapsed_time()
{
printf("Elapsedtime:%usecs. ",clock()/CLOCKS_PER_SEC);
}

當然,也可以用clock函數來計算的機器運行一個循環或者處理其它事件到底花了多少時間:

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
intmain(void)
{
longi=10000000L;
clock_tstart,finish;
doubleration;
printf("Timetodo%ldemptyloopsis",i);
start=clock();
while(i--);
finish=clock();
ration=(double)(finish-start)/CLOCKS_PER_SEC;
printf("%fseconds ",ration);
system("pause");
}

8. c語言中time函數的用法

頭文件time.h

@函數名稱: localtime
函數原型: struct tm *localtime(const time_t *timer)
函數功能: 返回一個以tm結構表達的機器時間信息
函數返回: 以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;
};
參數說明: timer-使用time()函數獲得的機器時間

#include <time.h>
#include <stdio.h>
#include <dos.h>
int main()
{
time_t timer;
struct tm *tblock;
timer=time(NULL);
tblock=localtime(&timer);
printf("Local time is: %s",asctime(tblock));
return 0;
}

@函數名稱: asctime
函數原型: char* asctime(struct tm * ptr)
函數功能: 得到機器時間(日期時間轉換為ASCII碼)
函數返回: 返回的時間字元串格式為:星期,月,日,小時:分:秒,年
參數說明: 結構指針ptr應通過函數localtime()和gmtime()得到
所屬文件: <time.h>

#include <stdio.h>
#include <string.h>
#include <time.h>
int main()
{
struct tm t;
char str[80];
t.tm_sec=1;
t.tm_min=3;
t.tm_hour=7;
t.tm_mday=22;
t.tm_mon=11;
t.tm_year=56;
t.tm_wday=4;
t.tm_yday=0;
t.tm_isdst=0;
strcpy(str,asctime(&t));
printf("%s",str);
return 0;
}

@函數名稱: ctime
函數原型: char *ctime(long time)
函數功能: 得到日歷時間
函數返回: 返回字元串格式:星期,月,日,小時:分:秒,年
參數說明: time-該參數應由函數time獲得
所屬文件: <time.h>

#include <stdio.h>
#include <time.h>
int main()
{
time_t t;
time(&t);
printf("Today's date and time: %s",ctime(&t));
return 0;
}

@函數名稱: difftime
函數原型: double difftime(time_t time2, time_t time1)
函數功能: 得到兩次機器時間差,單位為秒
函數返回: 時間差,單位為秒
參數說明: time1-機器時間一,time2-機器時間二.該參數應使用time函數獲得
所屬文件: <time.h>

#include <time.h>
#include <stdio.h>
#include <dos.h>
#include <conio.h>
int main()
{
time_t first, second;
clrscr();
first=time(NULL);
delay(2000);
second=time(NULL);
printf("The difference is: %f seconds",difftime(second,first));
getch();
return 0;
}

@函數名稱: gmtime
函數原型: struct tm *gmtime(time_t *time)
函數功能: 得到以結構tm表示的時間信息
函數返回: 以結構tm表示的時間信息指針
參數說明: time-用函數time()得到的時間信息
所屬文件: <time.h>

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <dos.h>
char *tzstr="TZ=PST8PDT";
int main()
{
time_t t;
struct tm *gmt, *area;
putenv(tzstr);
tzset();
t=time(NULL);
area=localtime(&t);
printf("Local time is:%s", asctime(area));
gmt=gmtime(&t);
printf("GMT is:%s", asctime(gmt));
return 0;
}

@函數名稱: time
函數原型: time_t time(time_t *timer)
函數功能: 得到機器的日歷時間或者設置日歷時間
函數返回: 機器日歷時間
參數說明: timer=NULL時得到機器日歷時間,timer=時間數值時,用於設置日歷時間,time_t是一個long類型
所屬文件: <time.h>

#include <time.h>
#include <stdio.h>
#include <dos.h>
int main()
{
time_t t;
t=time();
printf("The number of seconds since January 1,1970 is %ld",t);
return 0;
}

@函數名稱: tzset
函數原型: void tzset(void)
函數功能: UNIX兼容函數,用於得到時區,在DOS環境下無用途
函數返回:
參數說明:
所屬文件: <time.h>

#include <time.h>
#include <stdlib.h>
#include <stdio.h>
int main()
{
time_t td;
putenv("TZ=PST8PDT");
tzset();
time(&td);
printf("Current time=%s",asctime(localtime(&td)));
return 0;
}

9. C語言裡面的時間函數怎麼用

函數名: difftime
功 能: 計算兩個時刻之間的時間差
用 法: double difftime(time_t time2, time_t time1);
程序例:

include <time.h>

10. C語言的時間函數

程序似乎有問題,無效數據 輸入什麼都是無效的

熱點內容
玩qq三國要什麼樣的電腦配置 發布:2024-09-20 00:50:57 瀏覽:445
樹莓派zero編譯驅動 發布:2024-09-20 00:50:56 瀏覽:483
上傳文件文件夾找不到 發布:2024-09-20 00:26:32 瀏覽:915
承台箍筋加密區 發布:2024-09-20 00:26:31 瀏覽:228
筆記本什麼配置能流暢運行cf 發布:2024-09-20 00:14:19 瀏覽:952
實測華為編譯器 發布:2024-09-19 23:50:52 瀏覽:822
linux匯總 發布:2024-09-19 23:46:39 瀏覽:453
阿里雲伺服器環境搭建教程 發布:2024-09-19 23:21:58 瀏覽:837
黃色文件夾圖標 發布:2024-09-19 23:19:22 瀏覽:684
mysql資料庫導出導入 發布:2024-09-19 23:00:47 瀏覽:183