RWDate(3C++) RWDate(3C++)
NameRWDate - Rogue Wave library class
Synopsis
#include <rw/rwdate.h>RWDate a; // Construct today's date
Description
Class RWDate represents a date, stored as a Julian day number. The
member function isValid() can be used to determine whether an RWDate is a
valid date. For example, isValid() would return FALSE for the date 29
February 1991 because 1991 is not a leap year. See "Using Class RWDate"
in the Tools.h++ User's Guide. RWDate's can be converted to and from
RWTime's, and to and from the Standard C library type struct tm defined
in <time.h>. Note that using a 2-digit year specifier in your code may
lead to less-than-perfect behavior at the turn of the century. We urge
you to create programs that are "millenially correct" by using 4-digit
year specifiers. Note that because the default constructor for this
class creates an instance holding the current date, constructing a large
array of RWDate may be slow.
RWDate v[5000]; // Figures out the current date 5000 times
Those with access to the Standard C++ Library-based versions of the
Tools.h++ template collections should consider the following:
// Figures out the current date just once:
RWTValOrderedVector<RWDate> v(5000, RWDate());
Thanks to the smart allocation scheme of the standard collections, the
above declaration will result in only one call to the default constructor
followed by 5000 invocations of the copy constructor. In the case of
RWDate, the copy constructor amounts to an assignment of one long to
another, resulting in faster creation than the simple array.
Persistence
Page 1
RWDate(3C++) RWDate(3C++)
Simple
Example
#include <rw/rwdate.h>
#include <rw/rstream.h>
main(){
// Today's date
RWDate d;
// Last Sunday's date:
RWDate lastSunday = d.previous("Sunday");
cout << d << endl << lastSunday << endl;
}
Program output:
03/22/91
Public Constructors
03/17/91
RWDate();
Default constructor. Constructs an RWDate with the present date.
RWDate(const RWDate&);
Copy constructor.
RWDate(unsigned day, unsigned year);
Constructs an RWDate with a given day of the year and a given year. The
member function isValid() can be used to test whether the results are a
valid date.
RWDate(unsigned day, unsigned month, unsigned year);
Constructs an RWDate with the given day of the month, month of the year,
and year. Days should be 1-31, months should be 1-12, and the year may
be specified as (for example) 1990, or 90. The member function isValid()
can be used to test whether the results are a valid date.
Page 2
RWDate(3C++) RWDate(3C++)
RWDate(unsigned day, const char* mon, unsigned year,
const RWLocale& locale = RWLocale::global());
Constructs an RWDate with the given day of the month, month and year.
The locale argument is used to convert the month name. Days should be
1-31, months may be specified as (for example): January, JAN, or Jan, and
the year may be specified as (for example) 1990, or 90. The member
function isValid() can be used to test whether the results are a valid
date.
RWDate(istream& s,const RWLocale& locale =
RWLocale::global());
A full line is read, and converted to a date by the locale argument. The
member function isValid() must be used to test whether the results are a
valid date. Because RWLocale cannot rigorously check date input, dates
created in this way should also be reconfirmed by the user.
RWDate(const RWCString& str,
const RWLocale& locale = RWLocale::global());
The string str is converted to a date. The member function isValid()
must be used to test whether the results are a valid date. Because
RWLocale cannot rigorously check date input, dates created in this way
should also be reconfirmed by the user.
RWDate(const RWTime& t,
const RWZone& zone = RWZone::local());
Constructs an RWDate from an RWTime. The time zone used defaults to
local. The member function isValid() must be used to test whether the
results are a valid date.
RWDate(const struct tm*);
Constructs an RWDate from the contents of the struct tm argument members
tm_year, tm_mon, and tm_mday. Note that the numbering of months and
years used in struct tm differs from that used for RWDate and RWTime
operations. struct tm is declared in the standard include file <time.h>.
RWDate(unsigned long jd);
Construct a date from the Julian Day number jd. Note that it is possible
to construct a valid RWDate which represents a day previous to the
beginning of the Gregorian calendar for some locality. Rogue Wave
doesn't know the specifics for your locality, so will not enforce an
Page 3
RWDate(3C++) RWDate(3C++)
arbitrary cutoff for "validity."
Public Member Operators
RWDate&
operator=(const RWDate&);
Assignment operator.
RWDateoperator++();
Prefix increment operator. Adds one day to self, then return the result.
RWDateoperator--();
Prefix decrement operator. Subtracts one day from self, then returns the
result.
RWDateoperator++(int);
Postfix increment operator. Adds one day to self, returning the initial
value.
RWDateoperator--(int);
Postfix decrement operator. Subtracts one day from self, returning the
initial value.
RWDate&
operator+=(unsigned long s);
Adds s days to self, returning self.
RWDate&
operator-=(unsigned long s);
Substracts s days from self, returning self.
Public Member Functions
RWCString
asString(char format = 'x',
const RWLocale& = RWLocale::global()) const;
Page 4
RWDate(3C++) RWDate(3C++)
Returns the date as a string, formatted by the RWLocale argument.
Formats are as defined in the standard C library function strftime().
RWCString
asString(const char* format,
const RWLocale& = RWLocale::global()) const;
Returns the date as a string, formatted by the RWLocale argument.
Formats are as defined in the standard C library function strftime().
RWBoolean
between(const RWDate& a, const RWDate& b) const;
Returns TRUE if this RWDate is between a and b, inclusive.
size_t
binaryStoreSize() const;
Returns the number of bytes necessary to store the object using the
global function
RWFile& operator<<(RWFile&, const RWDate&);
int
compareTo(const RWDate* d) const;
Compares self to the RWDate pointed to by d and returns:
0 if self == *d;
1 if self > *d;
-1 if self < *d.
unsigned
day() const;
Returns the day of the year (1-366) for this date.
unsigned
dayOfMonth() const;
Returns the day of the month (1-31) for this date.
void
extract(struct tm*) const;
Page 5
RWDate(3C++) RWDate(3C++)
Returns with the struct tm argument filled out completely, with the time
members set to 0 and tm_isdst set to -1. Note that the encoding for
months and days of the week used in struct tm differs from that used
elsewhere in RWDate. If the date is invalid, all fields are set to -1.
unsigned
firstDayOfMonth() const;
Returns the day of the year (1-366) corresponding to the first day of
this RWDate's month and year.
unsigned
firstDayOfMonth(unsigned month) const;
Returns the day of the year (1-366) corresponding to the first day of the
month month (1-12) in this RWDate's year.
unsigned
hash() const;
Returns a suitable hashing value.
RWBoolean
isValid() const;
Returns TRUE if this is a valid date, FALSE otherwise. The following two
functions are provided as a service to users who need to manipulate the
date representation directly. The julian day number is not the Julian
date!. The julian day number is calculated using Algorithm 199 from
Communications of the ACM, Volume 6, No. 8, (Aug. 1963), p. 444 and is
valid for any valid Gregorian date in the Gregorian calendar. The
Gregorian calendar was first introduced on Sep. 14, 1752, and was adopted
at various times in various places.
unsigned long
julian() const;
Returns the value of the julian day number..
void
julian(unsigned long j);
Changes the value of the julian day number to j.
Page 6
RWDate(3C++) RWDate(3C++)
RWBoolean
leap() const;
Returns TRUE if the year of this RWDate is a leap year.
RWDate
max(const RWDate& t) const;
Returns the later date of self or t.
RWDate
min(const RWDate& t) const;
Returns the earlier date of self or t.
unsigned
month() const;
Returns the month (1-12) for this date.
RWCString
monthName(const RWLocale& = RWLocale::global()) const;
Returns the name of the month for this date, according to the optional
RWLocale argument.
RWDate
next(unsigned dayNum) const;
Returns the date of the next numbered day of the week, where Monday = 1,
..., Sunday = 7. The variable dayNum must be between 1 and 7, inclusive.
RWDate
next(const char* dayName,
const RWLocale& = RWLocale::global()) const;
Returns the date of the next dayName (for example, the date of the
previous Monday) The weekday name is interpreted according to the
RWLocale argument.
RWDate
previous(unsigned dayNum) const;
Returns the date of the previous numbered day of the week, where Monday =
Page 7
RWDate(3C++) RWDate(3C++)
1, ..., Sunday = 7. The variable dayNum must be between 1 and 7,
inclusive.
RWDate
previous(const char* dayName,
const RWLocale& = RWLocale::global()) const;
Returns the date of the previous dayName (for example, the date of the
previous Monday) The weekday name is interpreted according to the
RWLocale argument.
RWCString
weekDayName(const RWLocale& = RWLocale::global()) const;
Returns the name of the day of the week for this date, according to the
optional RWLocale argument.
unsigned
weekDay() const;
Returns the number of the day of the week for this date, where Monday =
1, ..., Sunday = 7.
unsigned
year() const;
Returns the year of this date.
Static Public Member Functions
static unsigned
dayOfWeek(const char* dayName,
const RWLocale& = RWLocale::global());
Returns the number of the day of the week corresponding to the given
dayName. "Monday" = 1, ..., "Sunday" = 7. Names are interpreted by the
RWLocale argument. Returns 0 if no match is found.
static unsigned
daysInMonthYear(unsigned month, unsigned year);
Returns the number of days in a given month and year. Returns 0 if month
is not between 1 and 12 inclusive.
static unsigned
daysInYear(unsigned year);
Page 8
RWDate(3C++) RWDate(3C++)
Returns the number of days in a given year.
static RWBoolean
dayWithinMonth(unsigned monthNum, unsigned dayNum,
unsigned year);
Returns TRUE if a day (1-31) is within a given month in a given year.
static unsigned
hash(const RWDate& d);
Returns the hash value of d as returned by d.hash().
static unsigned
indexOfMonth(const char* monthName,
const RWLocale& = RWLocale::global());
Returns the number of the month (1-12) corresponding to the given
monthName. Returns 0 for no match.
static unsigned long
jday(unsigned mon, unsigned day, unsigned year);
Returns the Julian day corresponding to the given month (1-12), day (1-
31) and year. Returns zero (0) if the date is invalid.
static RWCString
nameOfMonth(unsigned monNum,
const RWLocale& = RWLocale::global());
Returns the name of month monNum (January = 1, ..., December = 12),
formatted for the given locale.
static RWBoolean
leapYear(unsigned year);
Returns TRUE if a given year is a leap year.
static RWDatenow();
Returns today's date.
Page 9
RWDate(3C++) RWDate(3C++)
static RWCString
weekDayName(unsigned dayNum,
const RWLocale& = RWLocale::global());
Returns the name of the day of the week dayNum (Monday = 1, ..., Sunday =
7), formatted for the given locale.
Related Global Operators
RWBoolean
operator<(const RWDate& d1, const RWDate& d2);
Returns TRUE if the date d1 is before d2.
RWBoolean
operator<=(const RWDate& d1, const RWDate& d2);
Returns TRUE if the date d1 is before or the same as d2.
RWBoolean
operator>(const RWDate& d1, const RWDate& d2);
Returns TRUE if the date d1 is after d2.
RWBoolean
operator>=(const RWDate& d1, const RWDate& d2);
Returns TRUE if the date d1 is after or the same as d2.
RWBoolean
operator==(const RWDate& d1, const RWDate& d2);
Returns TRUE if the date d1 is the same as t2.
RWBoolean
operator!=(const RWDate& d1, const RWDate& d2);
Returns TRUE if the date d1 is not the same as d2.
RWDate
operator+(const RWDate& d, unsigned long s);
RWDate
operator+(unsigned long s, const RWDate& d);
Returns the date s days in the future from the date d.
Page 10
RWDate(3C++) RWDate(3C++)
unsigned long
operator-(const RWDate& d1, const RWDate& d2);
If d1>d2, returns the number of days between d1 and d2. Otherwise, the
result is implementation defined.
RWDate
operator-(const RWDate& d, unsigned long s);
Returns the date s days in the past from d.
ostream&
operator<<(ostream& s, const RWDate& d);
Outputs the date d on ostream s, according to the locale imbued in the
stream (see class RWLocale), or by RWLocale::global() if none.
istream&
operator>>(istream& s, RWDate& t);
Reads t from istream s. One full line is read, and the string contained
is converted according to the locale imbued in the stream (see class
RWLocale), or by RWLocale::global() if none. The function
RWDate::isValid() must be used to test whether the results are a valid
date.
RWvostream&
operator<<(RWvostream&, const RWDate& date);
RWFile&
operator<<(RWFile&, const RWDate& date);
Saves the date date to a virtual stream or RWFile, respectively.
RWvistream&
operator>>(RWvistream&, RWDate& date);
RWFile&
operator>>(RWFile&, RWDate& date);
Restores the date into date from a virtual stream or RWFile,
respectively, replacing the previous contents of date.
Page 11