C語言時間計算

2021-10-01 04:33:00 字數 1607 閱讀 2279

1. clock函式

#include

#include

#include

intmain()

printf

("sum is:%d\n"

,s);

t_end =

clock()

;printf

("diff is: %f\n",(

double

)(t_end-t_begin));

printf

("total cost %f s\n",(

double)(

(t_end-t_begin)

/clocks_per_sec));

return0;

}

2. time函式
#include

#include

#include

intmain()

3. gettimeofday函式
#include

#include

#include

intmain()

通過time()函式獲取當前時間;

將time_t值轉換為tm結構,通過tm結構,我們可以很方便的訪問日期和時間的各個組成部分,一般通過以下兩個函式實現

struct tm *

gmtime

(time_t const

* time_vlaue)

;struct tm *

localtime

(time_t const

* time_value)

;

時間轉換為字串形式,通過strftime實現

size_t strftime

(char

* string, size_t maxsize,

char

const

*format,

struct tm const

* tm_ptr)

具體實現

#include

#include

intmain()

;strftime

(pccurrenttime,

(sizeof

(pccurrenttime)-1

),"%y%m%d%h%m%s"

,psttime)

;printf

("current time is:%s\n"

,pccurrenttime)

;return0;

}

效果如下

asctime將引數所表示的時間值轉換為以下面格式表示的字串

char

*asctime

(struct tm const

*tm_ptr)

mktime函式用於將tm結構轉換為乙個time_t值

time_t mktime(struct tm *tm_ptr)

C 語言計算精確時間

c 語言標準庫包含了時間相關的api。但是所有的精確值都是秒為單位的。那如何精確到公釐或是更精準的計算時間差呢。有如下定義 define clocks per sec 1000000 extern clock t clock void clock t 返回的是cpu的時鐘數,而常量clocks pe...

C語言 計算昨天的時間資訊

計算昨天的時間資訊 這個程式又重新認識了指標 指標定義必須初始化,就是必須對指標賦值位址值 include include include includeusing namespace std 結構體的定義部分 typedef struct systemtime,computingtime type...

計算時間差C語言實現

timetest.cpp 定義控制台應用程式的入口點。include stdafx.h include include include include include include math.h 計算給定的時間是本年的第幾秒 int count int year,int month,int day...