<目次>
(1) C++のtime関数とlocaltime関数の違いについて
(1-1) C++のlocaltime関数の概要とサンプル
(1-2) C++のtime関数の概要とサンプル
(1-3) (参考)「struct tm *」⇒「time_t」への逆変換をするmktime関数
(1) C++のtime関数とlocaltime関数の違いについて
(1-1) C++のlocaltime関数の概要とサンプル
| 関数/オブジェクト | 引数 | 戻り値 | 概要 |
| localtime | const time_t * | struct tm * | (引数) ・「time_t」型のオブジェクトへのポインタ (戻り値) (処理概要) (注意) |
#include <time.h>
#include <iostream>
using namespace std;
int main()
{
//### 前準備 ###//
//# time_t型の変数を定義
time_t rtime1;
//# tm型のポインタを定義
struct tm* tmp1;
//### timeの確認 ###//
//# time関数で変数rtimeにUTC時刻を格納
time(&rtime1);
cout << "TIME#1: " << rtime1 << endl;
//### localtimeの確認 ###//
//# localtime関数で変数rtimeをローカル時刻に変換して格納
tmp1 = localtime(&rtime1);
cout << "TIME#2: " << asctime(tmp1) <<endl;
return 0;
}

(1-2) C++のtime関数の概要とサンプル
(1-3) (参考)「struct tm *」⇒「time_t」への逆変換をするmktime関数
| 関数/オブジェクト | 引数 | 戻り値 | 概要 |
| mktime | struct tm * | time_t | (引数) ・「tm」型の構造体へのポインタ (戻り値) (処理概要) |
(サンプル)
#include <time.h>
#include <iostream>
using namespace std;
int main()
{
//### 前準備 ###//
//# time_t型の変数を定義
time_t rtime1;
time_t rtime2;
//# tm型のポインタを定義
struct tm* tmp1;
struct tm* tmp2;
//### timeの確認 ###//
//# time関数で変数rtimeにUTC時刻を格納
time(&rtime1);
cout << "TIME#1: " << rtime1 << endl;
//### localtimeの確認 ###//
//# time関数で変数rtimeにUTC時刻を格納
time(&rtime2);
//# localtime関数で変数rtimeをローカル時刻に変換して格納
tmp2 = localtime(&rtime2);
//# mktimeで再度、time_t型に逆変換 ⇒元と同じになる事の確認
cout << "TIME#2: " << mktime(tmp2) << endl;
return 0;
}