Rogue Wave banner
Previous fileTop of DocumentContentsIndex pageNext file
DB Interface Module User's Guide
Rogue Wave web site:  Home Page  |  Main Documentation Page

13.3 Data Formatting

Programmers working with SourcePro DB have several classes available for data type normalization. (See Chapter 7, "The Data Model.") These classes provide data structures to hold complex types, yet allow them to be treated like simple types. These classes include:

The data that all these classes hold are locale sensitive; in other words, legal input and output of these types look different depending on the locale in effect. The issue here is input and output of the data from the point of view of the user of an application. SourcePro DB automatically formats the data when sending to and retrieving from a database. The application programmer must be concerned only with the raw form of the data as it comes into the program or goes out to a program user.

These classes all have associated techniques to allow input and output from various locales. The following sections outline the techniques used for RWDateTime, RWDate, RWTime, and RWDecimalPortable. For more information on these classes, see the Essential Tools Module User's Guide and the SourcePro C++ API Reference Guide.

13.3.1 RWDateTime

As mentioned in Section 7.4.2, "RWDateTime," class RWDateTime from the Essential Tools Module is the primary date and time class of SourcePro DB. It is used to hold a date and a time, primarily in association with moving data in and out of DATETIME (or similar) columns in databases.

Compatibility with SourcePro DB is provided to instances that are initialized, either to RWDateTime::null or to a valid datetime. Once an instance of RWDateTime is initialized, it can be used like any other data type. SourcePro DB performs any formatting or binding automatically in a locale neutral manner. The programmer does not have to address localization issues with regard to getting this data type in and out of the database.

However, getting a valid RWDateTime value from users in various locales can be tricky. The next example demonstrates how to take a string representing a localized date time value and convert it into an RWDateTime instance. Following the conversion, the RWDateTime is streamed to standard output to demonstrate output in various locales.

Example 16: Converting a localized date/time value into an RWDateTime instance

The output for this example should look like this:

13.3.2 RWDBDateTime

Class RWDBDateTime used to be the primary class for representing and manipulating dates and time in the DB Interface Module; however, RWDBDateTime has been deprecated. New code should be written using class RWDateTime.

The DB Interface Module does not include an operator<< for ostreams and instances of RWDBDateTime. The following example shows how to make an operator<< that outputs an RWDBDateTime correctly with whatever locale was imbued in the ostream.

13.3.3 RWDate


The class RWDate has been deprecated and may be removed in a future release. We recommend using RWDateTime.

RWDate is a class from the Essential Tools Module that can also be used as a database data type. The DB Interface Module can handle any formatting issues without intervention when getting this type in and out of a database, as it can for RWDateTime. Of course, as in Section 13.3.1, the trick is getting localized information from a user to initialize an instance of RWDate. The Essential Tools Module gives several options.

First, there is a constructor that takes a reference to an istream and a reference to an RWLocale. This allows direct initialization of the RWDate instance from the istream.

A second technique is shown in the example below. A string initialized with a representation of a localized date is created and then converted to an RWDate with the assistance of an RWLocale reference.

The output for this example should be:

13.3.4 RWDecimalPortable

RWDecimalPortable is a class from Essential Tools Module that is used to hold monetary values. For this job, it is more accurate than float or double. The types float and double introduce errors in monetary values because binary is not entirely accurate for fractional base 10 values. RWDecimalPortable uses the less efficient but perfectly accurate ASCII method.

When an RWDecimalPortable instance is given to the DB Interface Module to be inserted into a table, the module handles any formatting required by the server automatically. However, you do need to get localized values into instances of RWDecimalPortable.

Unfortunately, there is no constructor for RWDecimalPortable that takes an RWLocale as a parameter. When an RWDecimalPortable is initialized from a string, it always assumes that the string is formatted in the style required by the current global locale.

The output for this example should be:

By itself, class RWDecimalPortable cannot produce output with embedded thousand separators. When used in conjunction with the Currency Module, it has that capability.

13.3.5 Formatting Raw Numbers

The class RWLocale from the Essential Tools Module provides an easy way to convert between strings that represent numbers into the basic long or double data types for any locale. The number strings can even contain the national equivalents of the thousand and decimal point separators.

Example 17: Formatting raw numbers

The output from this example should be an SQL insert statement like this:



Previous fileTop of DocumentContentsNo linkNext file

Copyright © Rogue Wave Software, Inc. All Rights Reserved.

The Rogue Wave name and logo, and SourcePro, are registered trademarks of Rogue Wave Software. All other trademarks are the property of their respective owners.
Provide feedback to Rogue Wave about its documentation.