mktime()是一個內置的C++函數,它將本地日曆時間轉換為自紀元以來的時間,並將該值作為time_t類型的對象返回。
用法:
time_t mktime( struct tm *time_ptr )
參數:該函數接受強製性參數指針time_ptr,該指針指向包含要轉換的日曆時間的tm對象結構。
返回值:該函數返回兩種類型的值,如下所述:
- 如果傳遞的參數成功,則它將自紀元以來的時間作為類型time_t的對象返回。
- 失敗時返回-1。
以下示例程序旨在說明mktime()函數:
// CPP program to demonstrate the
// mktime() function
#include <bits/stdc++.h>
using namespace std;
int main()
{
time_t tim;
tm* time_ptr;
char weekday[7][20] = { "Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday" };
// Date
int year = 2018;
int month = 6;
int day = 18;
time(&tim);
time_ptr = localtime(&tim);
// tm_year is time since 1900
time_ptr->tm_year = year - 1900;
// Months calculated since January
time_ptr->tm_mon = month - 1;
// Day calculated in the month
time_ptr->tm_mday = day;
// time_ptr pointer to be pass
mktime(time_ptr);
cout << "Tha Day on 18th June 2018 was "
<< weekday[time_ptr->tm_wday];
return 0;
}
輸出:
Tha Day on 18th June 2018 was Monday
相關用法
- C++ log()用法及代碼示例
- C++ div()用法及代碼示例
- C++ fma()用法及代碼示例
- C++ real()用法及代碼示例
- C++ imag()用法及代碼示例
- C++ map key_comp()用法及代碼示例
- C++ regex_iterator()用法及代碼示例
- C++ valarray log()用法及代碼示例
- C++ valarray exp()用法及代碼示例
- C++ valarray cos()用法及代碼示例
注:本文由純淨天空篩選整理自jit_t大神的英文原創作品 mktime() function in C++ STL。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。