C++ 日付と時刻
C++ 標準ライブラリは、適切な日付型を提供していません。 C++ は、日付と時刻を操作するための構造体と関数を C から継承します。日付と時刻に関連する関数と構造体にアクセスするには、C++ プログラムに
時間に関連する型は 4 つあります:clock_t、time_t、size_t 、および tm .タイプ - clock_t、size_t、および time_t は、システムの時刻と日付をある種の整数として表すことができます。
構造体型 tm 次の要素を持つC構造体の形式で日付と時刻を保持します-
struct tm {
int tm_sec; // seconds of minutes from 0 to 61
int tm_min; // minutes of hour from 0 to 59
int tm_hour; // hours of day from 0 to 24
int tm_mday; // day of month from 1 to 31
int tm_mon; // month of year from 0 to 11
int tm_year; // year since 1900
int tm_wday; // days since sunday
int tm_yday; // days since January 1st
int tm_isdst; // hours of daylight savings time
}
以下は、C または C++ で日付と時刻を操作する際に使用する重要な関数です。これらの関数はすべて標準 C および C++ ライブラリの一部であり、以下に示す C++ 標準ライブラリを参照して詳細を確認できます。
| Sr.No | 機能と目的 |
|---|---|
| 1 | time_t time(time_t *time); これは、1970 年 1 月 1 日から経過した秒数でシステムの現在のカレンダー時間を返します。システムに時間がない場合は、.1 が返されます。 |
| 2 | char *ctime(const time_t *time); これは、日 月 年 時間:分:秒 年\n\0 の形式の文字列へのポインタを返します。 . |
| 3 | struct tm *localtime(const time_t *time); これは tm へのポインタを返します 現地時間を表す構造。 |
| 4 | clock_t clock(void); これは、呼び出し元のプログラムが実行されている時間を概算する値を返します。時間が利用できない場合は、値 .1 が返されます。 |
| 5 | char * asctime ( const struct tm * time ); これは、次の形式に変換された time が指す構造に格納された情報を含む文字列へのポインターを返します:日 月 日 時間:分:秒 年\n\0 |
| 6 | struct tm *gmtime(const time_t *time); これは、時刻へのポインターを tm 構造体の形式で返します。時刻は、基本的にグリニッジ標準時 (GMT) である協定世界時 (UTC) で表されます。 |
| 7 | time_t mktime(struct tm *time); これは、time が指す構造体で見つかった時間に相当する暦時間を返します。 |
| 8 | double difftime ( time_t time2, time_t time1 ); この関数は、time1 と time2 の差を秒単位で計算します。 |
| 9 | size_t strftime(); この関数は、特定の形式で日付と時刻をフォーマットするために使用できます。 |
現在の日付と時刻
現在のシステムの日付と時刻をローカル時刻または協定世界時 (UTC) として取得するとします。以下は、同じことを達成するための例です-
ライブデモ
#include <iostream>
#include <ctime>
using namespace std;
int main() {
// current date/time based on current system
time_t now = time(0);
// convert now to string form
char* dt = ctime(&now);
cout << "The local date and time is: " << dt << endl;
// convert now to tm struct for UTC
tm *gmtm = gmtime(&now);
dt = asctime(gmtm);
cout << "The UTC date and time is:"<< dt << endl;
}
上記のコードをコンパイルして実行すると、次の結果が生成されます −
The local date and time is: Sat Jan 8 20:07:41 2011 The UTC date and time is:Sun Jan 9 03:07:41 2011
struct tm を使用して時刻をフォーマットする
tm C または C++ で日付と時刻を操作する場合、構造は非常に重要です。この構造体は、前述のように C 構造体の形式で日付と時刻を保持します。ほとんどの場合、関連する関数は tm 構造を利用します。以下は、さまざまな日付と時刻に関連する関数と tm 構造を利用する例です-
この章で構造体を使用する際、C 構造体と矢印 -> 演算子を使用して構造体メンバーにアクセスする方法について基本的な知識があることを前提としています。
ライブデモ
#include <iostream>
#include <ctime>
using namespace std;
int main() {
// current date/time based on current system
time_t now = time(0);
cout << "Number of sec since January 1,1970 is:: " << now << endl;
tm *ltm = localtime(&now);
// print various components of tm structure.
cout << "Year:" << 1900 + ltm->tm_year<<endl;
cout << "Month: "<< 1 + ltm->tm_mon<< endl;
cout << "Day: "<< ltm->tm_mday << endl;
cout << "Time: "<< 5+ltm->tm_hour << ":";
cout << 30+ltm->tm_min << ":";
cout << ltm->tm_sec << endl;
}
上記のコードをコンパイルして実行すると、次の結果が生成されます −
Number of sec since January 1,1970 is:: 1588485717 Year:2020 Month: 5 Day: 3 Time: 11:31:57
C言語