Writing an ITime Object to an Output Stream

ITime defines an output operator that writes an ITime object to an output stream in the format hh:mm:ss. If you want to write the object out in a different format, you should convert the object to an IString using the asString member function. This member function accepts a char* argument containing a format specifier. The format specifier is the same one as used by the C library function strftime. The following program displays some valid specifiers and the output they produce:

// Examples of ITime output
#include <istring.hpp>
#include <itime.hpp>
#include <iostream.h>
#include <iomanip.h>  // needed for setw(), to set output stream width
void main() {
   char* FormatStrings[]={
      "%H : %M and %S seconds",    // %H, %M, %S - 2 digits for hrs/mins/secs
      "%r",                        // %r - standard 12-hour clock with am/pm 
      "%T",                        // %T - standard 24 hour clock
      "%T %Z",                     // %Z - local time zone code
      "%1M past %1I %p"            // %1... - One digit for hour/minute
      };                           // %p - am/pm
   cout.setf(ios::left,ios::adjustfield);     // Left-justify output 
   cout << setw(30) << "Format String"        // Title text
        << setw(40) << "Formatted ITime object" << endl; 
   for (int i=0;i<5;i++) {                    // Show each time
      IString Formatted=ITime::now().asString(FormatStrings[i]);
      cout << setw(30) << FormatStrings[i]
           << setw(40) << Formatted << endl;
      }
   } 

The program produces output that looks like the following:

Format String                 Formatted ITime object
%H : %M and %S seconds        16 : 13 and 04 seconds
%r                            04:13:04 PM
%T                            16:13:04
%T %Z                         16:13:04 EST
%1M past %1I %p               13 past 4 PM