C語言時間與日期函數初學

  1. 日期和時間轉換函數 asctime
函數原型:char * asctime(const struct tm *tblock)

#include<stdio.h>
#include<string.h>
#include<time.h>
int main(void)
{
    struct tm t;
    char str[60];
    // 下面設置結構體 t 的 時間成員變量
    t.tm_sec = 1;//設置當前的秒
    t.tm_min = 30;// 設置當前分鐘
    t.tm_hour = 9;// 設置當前 時
    t.tm_mday = 22;// 設置 日
    t.tm_mon = 11; // 月
    t.tm_year = 56; /*年 這兒可以只寫 56 而不必 寫 1956  大部分情況下,系統會默認 但你寫的的時間 大於某一時間時 就會 在前面加 19 ,有時候 比如 寫的 是 03 他就會是 2003*/
    strcpy(str, asctime(&t));// 將asctime 函數轉換過來的 字符串複製到字符數組 str 中
    printf("%s\n",str);

}
  1. 測定運行時間的函數
函數原型: clock_t clock(void);
功能: 確定所用的處理器時間,也就是在運行時獲得當前處理器時間
例:
    #include<stdio.h>
    #include<time.h>
    #include<dos.h>
    int main(void)
    {
        clock_t start, end;
        start = clock(); // 取得 延遲開始時間
        delay(3000);// 使用延遲函數延遲 3000 毫秒 = 3秒
        end = clock();//取的延時完後的時間
        printf("The time was %f \n",(end - start) / CLK_TCK);// CLK_TCK 爲 時間轉化常量 除以其後 可得到以秒爲單位的時間
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章