DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
No more ctime(3C) errors - Time(3C++)

Conversion to and from strings

Conversion from Time to String

Conversion from Time to String is accomplished by means of several overloaded functions named make_string(). The simplest version of make_string() takes no parameters; it produces the default representation for the host machine location:

       Time t = Time(1987,Time::july,3) +
                Duration::hours(13);
       cout << t.make_string() << endl;

which prints

       07/03/87 13:00:00

There is also a version that takes a Place argument, which is needed when working with timezones other than the host machine location:

       Time t = Time(1987,Time::july,3,Place::mountain()) +
                Duration::hours(13);
       cout << t.make_string(Place::mountain()) << endl;

which also prints

       07/03/87 13:00:00

Two other versions (with and without Place parameters) take an additional argument which is similar to a printf(3S) control string. The extra argument gives the client control over the representation. For example:

       Time t = Time(1987,Time::july,3,Place::mountain()) +
                Duration::hours(13);
   

cout << t.make_string("%A %x at %r %Z", Place::mountain()) << endl;

prints:

       Friday 07/03/87 at 01:00:00 PM MDT

Conversion from string to Time

Conversion from string (either const char* or String(3C++)) to Time is accomplished using two versions of make_time(), one with and one without a Place parameter. These recognize a variety of date and time-of-day formats. For example, make_time("07/03/87 13:00:00",MDT) will return the value of t in the last example. make_string() uses a table of pointers to strings representing month names, weekday names, and so on. This table, which conforms to standard American usage, can be replaced at runtime by an alternate table conforming to local language and preferences. The functions Time::set_table() and Time::get_table() may be used for changing this table. Consult Time(3C++) for a description of the table and how to change it.

If the string does not represent a valid Time, the Objection Time::string_objection is raised. What this means to the client is discussed in ``Error handling''.


Next topic: Stream insertion
Previous topic: Arithmetic operators

© 2004 The SCO Group, Inc. All rights reserved.
UnixWare 7 Release 7.1.4 - 27 April 2004