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

Conversion to and from string

A Duration can be converted to a String(3C++) using either of two overloaded member functions named make_string. The parameterless version of make_string() creates a String in a standard format. It should be used for converting arbitrary Durations to Strings. The second version takes an argument that allows clients to control the format. Since it is designed for formatting times-of-day, it should only be applied to nonnegative Durations less than 24h. The argument is similar to a printf(3S) control string:

        Duration d(0,16,45,30);
        cout << d.make_string("%Hh %Mm %Ss") << endl;

This prints:

        16h 45m 30s

If this version of make_string() is used with a Duration that does not represent a time-of-day, then prior to conversion, a time-of-day will be created by discarding whole days from the original Duration and, if necessary to make the value nonnegative, adding 24h. For example:

       Duration d = Duration::hours(-6);
   

cout << d.make_string("%T") << endl;

prints:

       18:00:00

Conversion from strings to Durations can be accomplished using function make_duration(). For example:

       Duration d = make_duration("6PM");
   

cout << d.make_string("%T") << endl;

also prints:

       18:00:00

If the string passed to make_string() cannot be parsed, the Objection Duration::string_objection will be raised. To learn more about Objections, see the manpage Objection(3C++) and the tutorial ``An Objection Class for Rudimentary Error Handling - Objection(3C++)''. What this means to the client is discussed in the section, ``Error handling''.


Next topic: Stream insertion
Previous topic: Approximate arithmetic operators

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