Copyright 1999 Rogue Wave Software
Copyright 1999 Sun Microsystems, Inc.
クラス RWDate の使用法 |
3 |
![]() |
ユリウス日を Tools.h++ で使用して恩恵を得るために、その日を知る必要はありません。Tools.h++ が通常の暦の日付をユリウス日に変換するために使用するアルゴリズムは、Communications of the ACM, Volume 6, No.8, Aug. 1963 の 「Algorithm 199」の 444 ページに記載されています。
現在ほとんど全世界で使用されているグレゴリオ暦は、1582 年にグレゴリウス 13 世によって制定され、各国でさまざな時代に採用されました。英国では 1752 年 9 月 14 日に採用され、後に米国でも採用されるようになりました。グレゴリオ暦の採用以前の RWDate は、グレゴリオシステムから溯って推定したものであるという意味でしか有効ではないため、この点について説明しておきます。このような RWDate の出力、またはそのメソッドを使用した特定の日または月名の処理を行なうと、結果が予測できない場合があります。
例
重要なのは、RWDate を使用すると、通常使用している暦の日付を迅速かつ容易に操作できるということです。次に、このクラスの利点を示す例を示します。
ENIAC が初めて起動した 1945 年 2 月 14 日という日付を出力してから、大域ロケールを使用して、今日までの日数を計算し出力します。
#include <rw/rwdate.h>
#include <rw/rstream.h>
int main(){
// ENIAC の起動日
RWDate d(14, "February", 1945);
// 今日
RWDate today;
cout << d.asString("%A, %B %d 19%y")
<< " was the day the ENIAC computer was" << endl
<< "first turned on. "
<< today - d << " days have gone by since then. " << endl;
return 0;
}
|
Wednesday February 14, 1945 was the day the ENIAC computer was first turned on. 18636 days have gone by since then. |
コンストラクタ
RWDate を生成する方法はいくつかあります。たとえば、
RWDate d;
RWDate d1(24, 2001); // 2001 年 1 月 24 日
RWDate d2(24, 01); // 1901 年 1 月 24 日 (2 桁!)
RWDate d(10, 3, 90);// 1990 年 3 月 10 日
RWTime t; // 現在の時刻
RWDate d(t);
RWDate d1(10, "June", 90); // 1990 年 6 月 10 日
RWDate d2(10, "JUN", 90); // 1990 年 6 月 10 日
ユーザーのシステムがフランス語ロケールをサポートしており、フランス語形式の月名を使用したい場合は、次のように記述することができます。
dateloc.cpp #include <rw/rwdate.h> #include <rw/rstream.h> #include <rw/locale.h> #include <rw/cstring.h>
main()
{
RWLocaleSnapshot us("C");
RWLocaleSnapshot french("fr"); // 処理系依存 // 1
RWCString americanDate("10 June 2025");
RWCString frenchDate("10 Juin 2025");
RWDate d(frenchDate, french); // OK // 2
cout << frenchDate << ((d.isValid()) ? " IS " : " IS NOT ")
<< "a valid date (french locale)." << endl << endl;
RWDate bad = RWDate(frenchDate); // 3
cout << frenchDate;
cout << ((bad.isValid() && bad == d) ? " IS " : " IS NOT ")
<< "a valid date (default locale)." << endl << endl;
bad = RWDate(americanDate, french); // 4
cout << americanDate;
cout << ((bad.isValid() && bad == d) ? " IS " : " IS NOT ")
<< "a valid date (french locale)." << endl << endl;
cout << d << endl; // 5
cout << d.asString() << endl; // 6
cout << d.asString('x', french) << endl; // 7
return 0;
}
|
以下、このコードを 1 行ずつ説明します。
RWDate(unsigned day, const char* month, unsigned year,
const RWLocale& locale = RWLocale::global());
第 2 引数の "month" は、特定のロケールの文脈の中でのみ意味を持ちます。この場合、行 1 で作成されたロケールを使用しています。結果は 06/10/1990 という (米国式の) 日付になります。
06/10/90
06/10/90
10.06.90