Rogue Wave Banner

Click on the banner to return to the user guide home page.

©Copyright 1996 Rogue Wave Software

Constructors

You can construct an RWDate in several ways. For example:

  1. Construct an RWDate with the current date[4]:

    RWDate d;

  2. 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)

  3. 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

  4. 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:

  1. A snapshot is taken of locale fr. This assumes your system supports the locale. Another common name for this locale is fr_FR.

  2. A date is constructed using the constructor:

       RWDate(unsigned day,const char* month,unsigned year,
              const RWLocale& locale = RWLocale::global());
  3. 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.

  4. 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.

  5. 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.

  6. The date constructed at line 2 is printed using the default locale, i.e., United States formatting conventions. The results are:

    06/10/25

  7. The date is converted to a string, then printed. Again, the default locale is used. The results are the same:

    06/10/25

  8. The date is converted to a string, this time using the locale constructed at line 1. The results are now[5]:

    10.06.25


Previous file Table of Contents Next file