home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / LOCTFMT.CPP < prev    next >
Text File  |  1997-07-05  |  2KB  |  110 lines

  1. // +++Date last modified: 05-Jul-1997
  2.  
  3. //
  4. // loctfmt.cpp
  5. // Time formatting functions
  6. //
  7. // written by David Nugent
  8. //
  9. // This code is public domain. Use for any purpose is unrestricted.
  10. //
  11.  
  12. # include "loctm.h"
  13. # include STRSTREAM_H
  14. # include "iomanip.h"
  15.  
  16.  
  17. void
  18. loc_tm::tSfx (ostream & os, int fmt, char ch) const
  19. {
  20.     if (fmt & t_SepAbbrev)
  21.         os << ch;
  22.     if (fmt & t_SepChar)
  23.         os << timech;
  24.     if (fmt & t_SepSpace)
  25.         os << ' ';
  26. }
  27.  
  28. void
  29. loc_tm::pHour (ostream & os, int fmt) const
  30. {
  31.     if (fmt & t_Hour)
  32.     {
  33.         int hour = tm_hour;
  34.         if (!(fmt & t_24hour))
  35.         {
  36.             if (hour > 12)
  37.                 hour -= 12;
  38.             else if (!hour && (fmt & t_AmPm))
  39.                 hour += 12;
  40.         }
  41.         os << setw((fmt & t_PadHour) ? 2 : 0);
  42.         os << setfill((fmt & t_ZeroHour) ? '0' : ' ');
  43.         os << hour;
  44.         if (!(fmt & t_Mins))
  45.             fmt &= t_SepAbbrev;
  46.         tSfx (os, fmt, 'h');
  47.     }
  48. }
  49.  
  50. void
  51. loc_tm::pMins (ostream & os, int fmt) const
  52. {
  53.     if (fmt & t_Mins)
  54.     {
  55.         int min = tm_min;
  56.         int dig = 2;
  57.         if (!(fmt & t_Hour))
  58.         {
  59.             min += (tm_hour * 60);
  60.             dig += 2;
  61.         }
  62.         os << setw((fmt & t_PadMins) ? dig : 0);
  63.         os << setfill((fmt & t_ZeroMins) ? '0' : ' ');
  64.         os << min;
  65.         if (!(fmt & t_Secs))
  66.             fmt &= t_SepAbbrev;
  67.         tSfx (os, fmt, 'm');
  68.     }
  69. }
  70.  
  71. void
  72. loc_tm::pSecs (ostream & os, int fmt) const
  73. {
  74.     if (fmt & t_Secs)
  75.     {
  76.         int sec = tm_sec;
  77.         int dig = 2;
  78.         if (!(fmt & (t_Hour|t_Mins)))
  79.         {
  80.             sec += ((tm_hour * 60) + tm_min) + 60;
  81.             dig += 3;
  82.         }
  83.         os << setw((fmt & t_PadSecs) ? dig : 0);
  84.         os << setfill((fmt & t_ZeroSecs) ? '0' : ' ');
  85.         os << sec;
  86.         if (fmt & t_AmPm)
  87.             fmt &= ~t_SepChar;
  88.         else
  89.             fmt &= (t_SepAbbrev|t_SepSpace);
  90.         tSfx (os, fmt, 's');
  91.     }
  92. }
  93.  
  94.  
  95. ostream &
  96. loc_tm::printTime (ostream & os, int fmt) const
  97. {
  98.     ostrstream pTmp;
  99.     pHour (pTmp, fmt);
  100.     pMins (pTmp, fmt);
  101.     pSecs (pTmp, fmt);
  102.     if (fmt & t_AmPm)
  103.         pTmp << (tm_hour > 11 ? "pm" : "am");
  104.     pTmp << ends;
  105.  
  106.     char const * p = pTmp.str();
  107.     pTmp.rdbuf()->freeze(0);
  108.     return os << p;
  109. }
  110.