使用c語言標準庫中的時間函數

#include <stdio.h>
#include <time.h>
int main(void){
  time_t start, finish, now;
  struct tm *ptr;
  char *c, buf1[80];
  double duration;
  
  //記錄程序開始的時間
  start = time(0);
  
  //記錄當前時間,以另外一種方式調用time()
  time(&now);
  
  //將time_t值轉換成tm類型的結構
  ptr = localtime(&now);
  
  //建立並顯示一個包含當前時間的格式字符串
  c = asctime(ptr);
  puts(c);
  getc(stdin);
  
  //使用strftime()函數建立多個不一樣的格式化時間版本
  strftime(buf1, 80, "This is week %U of the year %Y", ptr);
  puts(buf1);
  getc(stdin);
  
  strftime(buf1, 80, "Today is %A, %x", ptr);
  puts(buf1);
  getc(stdin);
  
  strftime(buf1, 80, "It is %M minutes past hour %I.", ptr);
  puts(buf1);
  getc(stdin);
  
  //獲取當前時間和計算程序執行時間
  finish = time(0);
  duration = difftime(finish, start);
  printf("\nProgram execution time using time() = %f seconds.", duration);
  
  //使用clock()計算程序執行時間,並打印出來。
  printf("\nProgram execution time using clock() = %ld thousandths of sec.", clock());
  return 0; 
}
相關文章
相關標籤/搜索