[ C++で開発 ] [ Boostを使うメモ ]

日付と時刻

日付(グレゴリ暦)を扱うboost::gregorianと、時刻を扱うboost::posix_timeの簡単なサンプルです。

目次

グレゴリ暦

日付に関する情報を扱い、日付計算を行う機能を提供しているのが、boost::gregorianです。

時間軸上のある日付を表現するdateクラスと、時間軸上でのある日付と日付の間の日数を表すdate_periodクラスがあります。

日付を保持するクラス date

必要なヘッダーファイル
<boost/date_time/gregorian/gregorian.hpp>
定義される名前空間
boost::gregorian
提供されるクラス
date, date_duration, day_clock, date_period, ...
リンクするライブラリ
libboost_date_time (UNIX)

dateインスタンスの作成方法

年月日(バイナリ)を指定して作成する

dateクラスのコンストラクタで生成します。

using boost::gregorian::date;
date theDay1(2008, 11, 25);  // グレゴリ暦2008年11月25日のdateインスタンスを作成 
date theDay2(2008, Nov, 25); // 月や曜日は名前(enum定義)を使用することができる
年月日(文字列)を指定して作成する

gregorianパッケージの関数from_string/from_undelimited_stringで生成します。

using boost::gregorian::date;
date theDay1 = boost::gregorian::from_string("2008/11/25"); // "2008-11-25"でもよい
date theDay2 = boost::gregorian::from_undelimited_string("20081125"); // 2008年11月25日
現在の日付を取得する

day_clockクラスの静的メンバ関数 local_day/universal_dayで生成します。

using boost::gregorian::date;
using boost::gregorian::day_clock;

date today = day_clock::local_day(); // 実行しているロケールの日付

dateインスタンス内容の表示・文字列化

標準出力ストリーム(std::cout)へ出力します。

using boost::gregorian::date;
date today ...
std::cout << "It is " << today << " today." << std::endl;
It is 2008-Dec-21 today.

文字列(std::string)形式に変換します。文字列には、単純文字列(simple_string)、ISO形式文字列、ISO拡張文字列の3種類があります。

using boost::gregorian::date;
date today ...
std::string today_as_simple = boost::gregorian::to_simple_string(today);
std::string today_as_iso = boost::gregorian::to_iso_string(today);
std::string today_as_extend = boost::gregorian::to_iso_extended_string(today);

std::cout << "Simple string      : " << today_as_simple << std::endl;
std::cout << "ISO string         : " << today_as_iso << std::endl;
std::cout << "Extended ISO string: " << today_as_extend << std::endl;
Simple string      : 2008-Dec-21
ISO string         : 20081222 
Extended ISO string: 2008-12-22

ある日付と日付との間の日数(date_duration)

日付の計算ではある日付とある日付の間の日数や、ある日付に日数を足し引きした別な日付を算出することが必要となります。

このとき、日数を表現するクラスがdate_durationです。

日付と日付の間の日数
using boost::gregorian::date;
using boost::gregorian::date_duration;
date theDay1 = ...;
date theDay2 = ...;
date_duration theDateDuration = theDay2 - theDay1;
long daysBetweenTheDay1AndTheDay2 = theDuration.days();
日付に日数を足す
using boost::gregorian::date;
using boost::gregorian::date_duration;
date theDay1 = ...;
date_duration theDateDuration(10);
date theDay2 = theDay1 + theDateDuration; // theDay2は、theDay1の10日後の日付

ある日付からある日付までの期間(date_period)

開始日・終了日で表現される区間を表します。区間と区間の交差判定などの計算ができます。

期間の生成

2つの日付(date)を指定して期間を生成します。

using boost::gregorian::date;
using boost::gregorian::date_period;
date theDay1 = ...;
date theDay2 = ...;
date_period theInterval(theDay1, theDay2); // theDay1からtheDay2までの区間

その他、開始日付と日数(date_duration)を指定するコンストラクタがあります。

2つの期間の交差判定

date_periodクラスの、intersectsメンバ関数で交差有無の判定が可能です。また、

dateクラスを使用する場合のコンパイル

UNIX(Linux)
work$ g++ main.cpp -o simpledate -lboost_date_time
work$ ./simpledate
    :
work$

時刻

時刻に関する情報を扱い、時刻計算を行う機能を提供しているのが、boost::posix_timeです。

時間軸上のある時刻を表現するptimeクラスと、時間軸上でのある時刻と時刻の間の時間を表すtime_durationクラスやtime_periodクラスなどがあります。

時刻を保持するクラス ptime

必要なヘッダーファイル
<boost/date_time/posix_time/posix_time.hpp>
定義される名前空間
boost::posix_time
提供されるクラス
ptime, time_duration, second_clock, time_period, time_iterator...
リンクするライブラリ
libboost_date_time (UNIX)

ptimeインスタンスの作成方法

現在の時刻を取得する

second_clockクラスの静的メンバ関数 local_time/universal_timeで生成します。

using boost::posix_time::ptime;
using boost::posix_time::second_clock;

ptime now = second_clock::local_time(); // 実行しているロケールの現在時刻
時刻を文字列に変換する
std::cout << "to_
ptime now = second_clock::local_time(); // 実行しているロケールの現在時刻

ptimeインスタンス内容の表示・文字列化

標準出力ストリーム(std::cout)へ出力します。

using boost::date_time::ptime;
ptime now = ...
std::cout << "It is " << now << " now." << std::endl;
It is 2008-Dec-22 00:50:24 now.

文字列(std::string)形式に変換します。文字列には、単純文字列(simple_string)、ISO形式文字列、ISO拡張文字列の3種類があります。

using boost::date_time::ptime;
ptime now = ...
std::string now_as_simple = boost::posix_time::to_simple_string(today);
std::string now_as_iso = boost::posix_time::to_iso_string(today);
std::string now_as_iso_extend = boost::posix_time::to_iso_extended_string(today);

std::cout << "Simple string      : " << now_as_simple << std::endl;
std::cout << "ISO string         : " << now_as_iso << std::endl;
std::cout << "Extended ISO string: " << now_as_iso_extend << std::endl;
Simple string      : 2008-Dec-21 12:34:56
ISO string         : 20081221T123456
Extended ISO string: 2008-12-22T12:34:56