クラスブースト:: posix_time :: PTIMEは、場所に依存しない時間をdefindes。これは、型ブースト::グレゴリオ::日付を使用するだけでなく、時間を格納します。
1. PTIME
書式#include <ブースト/ DATE_TIME / posix_time / posix_time.hpp> の#include <ブースト/ DATE_TIME /グレゴリオ/ gregorian.hpp> の#include <iostreamの> 使用して名前空間ブースト:: posix_timeを。 使用して名前空間のブーストを::グレゴリオ。INT ()主 { PTIMEのPT(日付(2014、5、12)、TIME_DURATION(12、0、0)) 。 日付d = pt.date()。 std :: coutの << D << はstd ::てendl; TIME_DURATIONのTD = pt.time_of_day()。 std :: coutの << TD << はstd ::てendl;
ptime pt2 = second_clock::universal_time();
std::cout << pt2.date() << std::endl;
std::cout << pt2.time_of_day() << std::endl; pt2 = from_iso_string("20140512T120000"); std::cout << pt2.date() << std::endl; std::cout << pt2.time_of_day() << std::endl;
リターン 0 ; }
型ブースト:: posix_time :: PTIMEのオブジェクトを初期化するために、コンストラクタの第1及び第2のパラメータとして型ブースト日::グレゴリオ::日付およびタイプブーストの期間:: posix_time :: TIME_DURATIONを渡します。ブースト:: posix_time :: TIME_DURATIONのコンストラクタは、時間を決定する3つのパラメータを取ります。日付と時刻を照会するには、メンバ関数日付()とTIME_OF_DAY()を使用します。
クラスブースト:: posix_time ::現在時刻を返しますsecond_clock。メンバ関数universal_time()は、UTC時刻を返します。LOCAL_TIMEは()現地時刻を返します。自立機能は、boost::posix_time::from_iso_string()
タイプのオブジェクトにISO 8601規格を使用してフォーマットされた文字列に格納された時間に変換します。
2. TIME_DURATION
書式#include <ブースト/ DATE_TIME / posix_time / posix_time.hpp> の#include <iostreamの> 使用して名前空間ブースト:: posix_timeを。INT メイン() { TIME_DURATIONのTD(16、30、0); std :: coutの << td.hours()<< はstd ::てendl; std :: coutの << td.minutes()<< はstd ::てendl; std :: coutの << td.seconds()<< はstd ::てendl; std :: coutの << td.total_seconds()<< はstd ::てendl;
ptime pt1{date{2014, 5, 12}, time_duration{12, 0, 0}}; ptime pt2{date{2014, 5, 12}, time_duration{18, 30, 0}}; time_duration td2 = pt2 - pt1; std::cout << td2.hours() << std::endl; std::cout << td2.minutes() << std::endl; std::cout << td2.seconds() << std::endl;
リターン 0 ; }
時間()分()と秒秒の合計数を返す)(例えばtotal_secondsとしてメンバ関数が、時間期間のそれぞれの部分を返します。
型ブースト:: posix_time :: PTIMEの2倍を互いから減算される場合、タイプブースト:: 2つの時間の間の期間を指定posix_time :: TIME_DURATIONの目的で結果。
3. TIME_PERIOD
書式#include <ブースト/ DATE_TIME / posix_time / posix_time.hpp> の#include <iostreamの> 使用して名前空間ブースト:: posix_timeを。 使用して名前空間のブーストを::グレゴリオ。INT メイン() { PTIMEのPT1(日付(2014、5、12)、TIME_DURATION(12、0、0)) 。 PTIMEのPT2(日付(2014、5、12)、TIME_DURATION(18、30、0)) 。 TIME_PERIODのTP {PT1、PT2}。 std :: coutの std :: cout.setf(STD ::イオス:: boolalpha)。 << tp.contains(PT1)<< はstd ::てendl; std :: coutの << tp.contains(PT2)<< はstd ::てendl; リターン 0 ; }
一般的には、boost::posix_time::time_period
同じように動作しますboost::gregorian::date_period
。これは、メンバ関数を提供contains()
返し、true
期間内の時間内のすべての点について。コンストラクタに渡された終了時間、のでboost::posix_time::time_period
、期間の一部ではない、2番目の呼び出しcontains()
戻り上記の例ではfalse
。
4.イテレータ
書式#include <ブースト/ DATE_TIME / LOCAL_TIME / local_time.hpp> の#include <iostreamの> 使用して名前空間ブースト:: posix_timeを。 使用して名前空間のブーストを::グレゴリオ。INT ()主 { PTIMEのPT(日付(2014、5、12)、TIME_DURATION(12、0、0)) 。 time_iteratorそれ(PT、TIME_DURATION(6、30、0)) 。 std :: coutの << * ++それ<< はstd ::てendl; std :: coutの << * ++それ<< はstd ::てendl; リターン0 ; }
上記の例では、それは時間PTから前方に6.5時間をジャンプして、イテレータを使用しています。イテレータは二回increamentedされているので,,出力がある2014-May-12 18:30:00
と2014-May-13 01:00:00
。