RWDateTime
Class RWDateTime, also contained in the Essential Tools Module, is the primary class used for dates and times in the DB Interface Module. This class serves as a compact representation for calendar calculations. It shields you from many of the details of programming with time elements, like leap years, and performs conversions to and from conventional calendar formats.
SourcePro DB compatibility is provided to RWDateTime instances that are initialized to either RWDateTime::null or a valid date. For a full discussion of the many features of this class, please see the Essential Tools Module and the SourcePro API Reference.
When RWDateTime is used to either write or extract data to or from a database, by default it uses the local time zone represented by RWZone::local(). Methods on RWDBDatabase and RWDBConnection allow you to specify a different time zone.
const RWZone* RWDBDatabase::timeZone(const RWZone* zone)
const RWZone& RWDBDatabase::timeZone() const
The first method sets the time zone on the RWDBDatabase instance, and all RWDBConnection instances produced by it after the time zone is set. The RWDBDatabase instance retains a reference to the time zone, hence it is the application's responsibility to ensure that the time zone has a lifetime greater than the RWDBDatabase instance or any object produced from it. The second method allows you to determine the current setting for this RWDBDatabase.
const RWZone* RWDBConnection::timeZone(const RWZone* zone)
const RWZone& RWDBConnection::timeZone() const
The first method sets the time zone for the specific RWDBConnection instance. The RWDBConnection instance retains a reference to the time zone, hence it is the application's responsibility to ensure that the time zone has a lifetime greater than the RWDBConnection instance or any object using it. The second method allows you to determine the current setting for this RWDBConnection.
On this page:
Example
The following example shows how RWDateTime performs date calculations. This code prints out the date January 6, 1990, then calculates and prints the date of the previous Sunday, using the global locale:
#include <rw/db/db.h>
#include <rw/rstream.h>
int
main() {
RWDateTime dd(1990U, 1, 6);
cout << dd.asString() << ", a " << dd.weekDayName() << endl;
RWDateTime prev = dd.previous("Sunday");
cout << "The previous Sunday is: " << prev.asString() << endl;
return 0;
}
Program output:
01/06/90 00:00:00.000, a Saturday
The previous Sunday is: 12/31/89 00:00:00.000
Constructors
An RWDateTime may be constructed in many ways. For example, you can:
Construct an RWDateTime with the current date:
RWDateTime dt(RWDateTime::setCurrentTime);
Construct an RWDateTime for a given year, month, and day:
RWDateTime d1(1924U, 1, 24); // Jan.24, 1924
Construct an RWDateTime from an RWDate, supplying hours, minutes, seconds, and milliseconds:
RWDate d(10, 3, 90); // Mar. 10, 1990
RWDateTime dt(d, 1, 30, 30, 100) // Mar. 10, 1990 1:30:30.100
In the first example, we use the enumerated constructor to construct RWDateTime with current value. If you are constructing an array and must fill in today’s date, you must assign the dates explicitly (see the Essential Tools documentation for more details).
There are many other constructors, including those that use RWDate or RWTime in various ways. There are accessors for each component of an RWDateTime (years, months, days, hours, and so on). RWDateTime provides powerful localization features which may be accessed and changed via the RWDateTime interface. Member operators and functions allow a complete range of arithmetic manipulations on date and time values.
Complete information on the capabilities of RWDateTime can be found in the Essential Tools Module and the SourcePro API Reference.