C 語言 時間函數 接着來

  1. 計算時間差函數 double difftime(time_t time2,time_t time1);
// 函數 功能: 計算兩個時間的差值,一秒爲單位
#include<stdio.h>
#include<time.h>
#include<dos.h> 
#include<conio.h>
int main()
{
    time_t first, second;
    clrscr();
    first = time(NULL);// 傳遞參數 NULL 獲得 當前系統時間
    delay(3000);// 調用延遲程序 延遲 3秒
    second = time(NULL);// 這兒其實可以通過 clock 獲得當前處理器時間
    printf("The interval is : %f seconds\n",difftime(second,first));
    getch();// 作用是然控制檯停止下來 該函數又在 conio.h 種聲明
    return 0;    
}
  1. gmtime 將日曆轉換成GMT (格林尼治時間)
// 函數原型 struct tm * gmtime(const time_t *timer)
//例子說明:
// 1. 首先使用 time() 函數獲得一個系統時間
// 2. 在使用gmtime() 將該函數轉換成GMT 時間, 該函數會返回一個指向struct tm 分段日期結構類型的指針
//最後應用asctime函數將分段日期轉換成規定格式的字符串表示


#include<stdio.h>
#include<time.h>
#include<stdlib.h>

int main(void)
{
    time_t t;
    struct tm *gmt;
    t = time(NULL);
    gmt = gmtime(&t);
    printf("GMT is %s",asctime(gmt));
    return 0;
}
  1. 與上一個函數這號相反: localtime() 將 日曆時間轉換結構體類型時間
// 函數原型 struct tm *localtime(const tiem_t *timer)
// 1. 利用time() 獲得系統時間,它是一個日曆時間(日曆時間,是用“從一個標準時間點到此時的時間經過的秒數”來表示的時間)
// 2. 利用函數localtime 將獲取的系統時間(日曆時間)轉換爲分段時間tm
// 3. 利用函數asctime 將該分段時間轉換爲規定的字符串格式的,並顯示
// 4. 利用函數ctime直接將日曆時間轉換爲規定的字符串時間,並顯示

#include<stdio.h>
#include<time.h>
#include<dos.h>
int main(void)
{
    time_t timer;
    struct tm *tblock;
    timer = time(NULL);
    tblock = localtime(&timer);// localtime 傳入的參數爲 time_t 指針
    printf("Local time is: %s\n",asctime(tblock));
    printf("Today's date and time:%s\n",ctime(&timer));
    getch();
    return 0;
}

注:

dos.h 請點擊查看

conio.h 包含函數如下:

cgets(char*);;
cputs(constchar*);
cscanf(constchar*,...);
inpw(unsignedshort);
getch(void);
getche(void);
kbhit(void);
outp(unsignedshort,int);
outpw(unsignedshort,unsignedshort);
putch(int);
ungetch(int);

void_Cdeclclreol(void);
void_Cdeclclrscr(void);
void_Cdecldelline(void);
int_Cdeclgettext(intleft,inttop,intright,intbottom,void*destin);
void_Cdeclgettextinfo(structtext_info*r);
void_Cdeclgotoxy(intx,inty);
void_Cdeclhighvideo(void);
void_Cdeclinsline(void);
void_Cdecllowvideo(void);
int_Cdeclmovetext(intleft,inttop,intright,intbottom,intdestleft,intdesttop);
void_Cdeclnormvideo(void);
int_Cdeclputtext(intleft,inttop,intright,intbottom,void*source);
void_Cdecltextattr(intnewattr);
void_Cdecltextbackground(intnewcolor);
void_Cdecltextcolor(intnewcolor);
void_Cdecltextmode(intnewmode);
int_Cdeclwherex(void);
int_Cdeclwherey(void);
void_Cdeclwindow(intleft,inttop,intright,intbottom);
char*_Cdeclcgets(char*str);
int_Cdeclcprintf(constchar*format,...);
int_Cdeclcputs(constchar*str);
int_Cdeclcscanf(constchar*format,...);
int_Cdeclgetch(void);
int_Cdeclgetche(void);
char*_Cdeclgetpass(constchar*prompt);
int_Cdeclkbhit(void);
int_Cdeclputch(intc);
int_Cdeclungetch(intch);
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章