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

  1. // +++Date last modified: 05-Jul-1997
  2.  
  3. //
  4. // locdfmt.cpp
  5. // Date 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. void
  17. loc_tm::dSfx (ostream & os, int fmt) const
  18. {
  19.     if (fmt & d_SepChar)
  20.         os << datech;
  21.     if (fmt & d_SepSpace)
  22.         os << ' ';
  23. }
  24.  
  25. void
  26. loc_tm::pYear (ostream & os, int fmt) const
  27. {
  28.     if (fmt & d_Year)
  29.     {
  30.         int year = tm_year;
  31.         int dig;
  32.         if (fmt & d_YearShort)
  33.         {
  34.             dig = 2;
  35.             year %= 100;
  36.         }
  37.         else
  38.         {
  39.             dig = 4;
  40.             if (year < 200)
  41.                 year += 1900;
  42.         }
  43.         os << setw((fmt & d_PadYear) ? dig : 0);
  44.         os << setfill((fmt & d_ZeroYear) ? '0' : ' ');
  45.         os << year;
  46.         if ((fmt & d_YearFirst))
  47.         {
  48.             fmt &= (d_SepChar|d_SepSpace);
  49.             dSfx (os, fmt);
  50.         }
  51.     }
  52. }
  53.  
  54. void
  55. loc_tm::pMonth (ostream & os, int fmt) const
  56. {
  57.  
  58.     static const char * _months[] =
  59.     {
  60.         "January", "February", "March", "April",
  61.         "May", "June", "July", "August", "September",
  62.         "October", "November", "December"
  63.     };
  64.  
  65.     if (fmt & d_Month)
  66.     {
  67.         int mon = (tm_mon % 12);
  68.         if (fmt & d_MonText)
  69.         {
  70.             char const * tmon = _months[mon];
  71.             if (!(fmt & d_PadMon))
  72.                 os << tmon;
  73.             else
  74.                 for (int x = 0; x < 3; ++x)
  75.                     os << tmon[x];
  76.         }
  77.         else
  78.         {
  79.             ++mon;
  80.             os << setw((fmt & d_PadMon) ? 2 : 0);
  81.             os << setfill((fmt & d_ZeroMon) ? '0' : ' ');
  82.             os << mon;
  83.         }
  84.         if (((fmt & d_Year) && !(fmt & d_YearFirst)) ||
  85.             ((fmt & d_Day) && (fmt & d_MonFirst)))
  86.         {
  87.             fmt &= (d_SepChar|d_SepSpace);
  88.             dSfx (os, fmt);
  89.         }
  90.     }
  91. }
  92.  
  93.  
  94. void
  95. loc_tm::pDate (ostream & os, int fmt) const
  96. {
  97.     if (fmt & d_Day)
  98.     {
  99.         int day = tm_mday;
  100.         os << setw((fmt & d_PadDay) ? 2 : 0);
  101.         os << setfill((fmt & d_ZeroDay) ? '0' : ' ');
  102.         os << day;
  103.         if (!(fmt & d_YearFirst) || !(fmt & d_MonFirst))
  104.         {
  105.             fmt &= (d_SepChar|d_SepSpace);
  106.             dSfx (os, fmt);
  107.         }
  108.     }
  109. }
  110.  
  111.  
  112. ostream &
  113. loc_tm::printDate (ostream & os, int fmt) const
  114. {
  115.     ostrstream pTmp;
  116.  
  117.     static const char * _days[] =
  118.     {
  119.         "Sunday", "Monday", "Tuesday", "Wednesday",
  120.         "Thursday", "Friday", "Saturday"
  121.     };
  122.  
  123.     if (fmt & d_DayOfWeek)
  124.     {
  125.         int day = tm_wday % 7;
  126.         char const * p = _days[day];
  127.         if (fmt & d_PadDay)
  128.             for (int x = 0; x < 3; ++x)
  129.                 pTmp << p[x];
  130.         else
  131.         {
  132.             pTmp << p;
  133.             if (fmt & d_DMY)
  134.                 pTmp << ',';
  135.         }
  136.         if ((fmt & d_DMY) && fmt & d_SepSpace)
  137.             pTmp << ' ';
  138.     }
  139.     if (fmt & d_YearFirst)
  140.         pYear (pTmp, fmt);
  141.     if (fmt & d_MonFirst)
  142.         pMonth (pTmp, fmt);
  143.     pDate (pTmp, fmt);
  144.     if (!(fmt & d_MonFirst))
  145.         pMonth (pTmp, fmt);
  146.     if (!(fmt & d_YearFirst))
  147.         pYear (pTmp, fmt);
  148.     pTmp << ends;
  149.  
  150.     char const * p = pTmp.str();
  151.     pTmp.rdbuf()->freeze(0);
  152.     return os << p;
  153. }
  154.