c c 時間函式學習筆記

2021-09-10 23:52:23 字數 1638 閱讀 3722

#include #include #include #include #include using namespace std;

int main(int argc, char * ar**)

// for debug

/*for (vector::iterator it = time.begin(); it != time.end(); it++)

*/// 將tm結構時間轉化為time_t時間

cout << time1[0] << endl;

struct tm *time_info2 = null;

time_info2 = (struct tm*)malloc(sizeof(struct tm));

time_info2->tm_year = time1[0] - 1900;

time_info2->tm_mon = time1[1] - 1;

time_info2->tm_mday = time1[2];

time_info2->tm_hour = time1[3];

time_info2->tm_min = time1[4];

time_info2->tm_sec = time1[5];

time_info2->tm_isdst = 0; //夏令時識別符號也需要賦值,否則會出錯

printf( "the time is %4d_%02d_%02d_%02d_%02d_%02d\n", time_info2->tm_year+1900, time_info2->tm_mon+1, \

time_info2->tm_mday, time_info2->tm_hour, time_info2->tm_min, time_info2->tm_sec);

time_t cur_time2;

cur_time2 = mktime(time_info2);

cout << "the current time is " << cur_time2 << endl;

sleep(2);

time_t raw_time3;

raw_time3 = time(nullptr);

cout << raw_time3 << endl;

// difftime 比較兩個時間相差的秒數,前面的秒數比後面大,則返回正數,否則返回負數

double seconds = difftime(raw_time3, cur_time2);

cout << "the seconds is " << seconds << endl;

free(time_info2);

}

夏令時識別符號也需要賦值,否則會出錯

執行結果如下:

the raw_time is 1550243429

the time buffer is 2019_02_15_23_10_29

2019

0215

2310

292019

the time is 2019_02_15_23_10_29

the current time is 1550243429

1550243431

the seconds is 2

C C 學習筆記 函式

形參變數只有在函式被呼叫時才會分配記憶體,呼叫結束後,立刻釋放記憶體,所以形參變數只有在函式內部有效,不能在函式外部使用。實參可以是常量 變數 表示式 函式等,無論實參是何種型別的資料,在進行函式呼叫時,它們都必須有確定的值,以便把這些值傳送給形參,所以應該提前用賦值 輸入等辦法使實參獲得確定值。實...

C C 數學處理函式

二 n次方 三 平方根 四 向上取整 向下取整 四捨五入 函式c庫 c 庫 作用原型 abs x 求整型數的絕對值 求浮點數的絕對值 c語言 include include intmain void c include include using namespace std intmain void...

C C 數學庫函式

所在函式庫為math.h stdlib.h string.h float.h 1.絕對值 int abs int i 返回整型引數i的絕對值 double cabs struct complex znum 返回複數znum的絕對值 double fabs double x 返回雙精度引數x的絕對值 ...