You can construct an RWDate in several ways. For example:
Construct an RWDate with the current date[4]:
RWDate d;
Construct an RWDate for a given day of the year (1 -- 365) and a given year, e.g., 1989 or 89. Although the class supports 2-digit year specifiers, we urge you to use the 4-digit variety if possible to avoid difficulties at the turn of the century.
RWDate d1(24, 2001); // 1/24/2001
RWDate d2(24, 01); // 1/24/1901 (oops)
Construct an RWDate for a given day of the month (1 -- 31), month number (1 -- 12), and year:
RWDate d(10, 3, 2015); // 3/10/2015
Construct an RWDate from an RWTime:
RWTime t; // Current time. RWDate d(t);
In addition, you can construct a date using locale-specific strings. If you do nothing, a default locale using United States conventions and names is applied:
RWDate d1(10, "June", 2001); // 6/10/2001 RWDate d2(10, "JUN", 2001); // 6/10/2001
But suppose you need to use French month names. Assuming your system supports a French locale, here's how you might do it:
#include <rw/rwdate.h> #include <rw/rstream.h> #include <rw/locale.h> #include <rw/cstring.h> main() { RWLocaleSnapshot us("C"); RWLocaleSnapshot french("fr"); // or vendor specific // 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; }
Here's a line-by-line description of the previous code:
A snapshot is taken of locale fr. This assumes your system supports the locale. Another common name for this locale is fr_FR.
A date is constructed using the constructor:
RWDate(unsigned day,const char* month,unsigned year, const RWLocale& locale = RWLocale::global());
Note that the second argument month is meaningful only within the context of a locale. In this case, we are using the locale constructed at line 1. The result is the date known in English as June 10, 2002.
Here we attempt to construct the same date using the default locale. This locale recognizes C formatting conventions only. Hence, the date 10 Juin 2002 should be meaningless. Just in case, though, compare with a known valid date.
For the same reason, constructing a date using United States names with a French locale should fail. Just in case, though, compare with a known valid date.
The date constructed at line 2 is printed using the default locale, i.e., United States formatting conventions. The results are:
06/10/25
The date is converted to a string, then printed. Again, the default locale is used. The results are the same:
06/10/25
The date is converted to a string, this time using the locale constructed at line 1. The results are now[5]:
10.06.25