home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / LOCTM.H < prev    next >
C/C++ Source or Header  |  1997-07-05  |  9KB  |  242 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. //
  4. // loctm.h
  5. // Time functions class (a C++ wrapper for ANSI struct tm)
  6. //
  7. // written by David Nugent
  8. //
  9. // This code is public domain. Use for any purpose is unrestricted.
  10. //
  11.  
  12. # if !defined( _loctm_h )
  13. # define _loctm_h 1
  14. # include <time.h>
  15. # if defined( __MSDOS__ ) || defined( MSDOS )
  16. #  define STRSTREAM_H "strstrea.h"  // 8.3 (sigh)
  17. # else
  18. #  define STRSTREAM_H "strstream.h"
  19. # endif
  20.  
  21.     // Forward declare for streams interface
  22.  
  23. class ostream;
  24. class istream;
  25.  
  26.     // class loc_tm
  27.     // C++ class (mainly I/O) wrapper for ANSI-C struct tm
  28.  
  29. class loc_tm : public tm
  30. {
  31.  
  32.   public:
  33.  
  34.     loc_tm (void) {}                  // Do nothing constructor
  35.     loc_tm (time_t t);     // Set to time, 0 is magic for 'now'
  36.     loc_tm (tm const & t); // Copy constructor
  37.  
  38.     loc_tm & operator= (tm const & t);            // Assignment
  39.     operator time_t (void) const;        // Conversion operator
  40.  
  41.     int is_valid (void) const;             // Test for validity
  42.  
  43.     int compare (loc_tm const & t) const;      // Compare times
  44.     int compare (time_t const tt) const;       // Compare times
  45.  
  46.     enum f_date     // Date format flags
  47.     {
  48.         d_Year          = (int)0x0001,            // Print year
  49.         d_YearShort     = (int)0x0002, // Print last two digits
  50.         d_PadYear       = (int)0x0004,    // Pad year to 2 or 4
  51.         d_ZeroYear      = (int)0x0008,        // Zero fill year
  52.         d_Month         = (int)0x0010,           // Print month
  53.         d_MonText       = (int)0x0020,   // Print month in text
  54.         d_PadMon        = (int)0x0040, // Pad to 2 (trunc to 3)
  55.         d_ZeroMon       = (int)0x0080,       // Zero fill month
  56.         d_Day           = (int)0x0100,            // Print date
  57.         d_DayOfWeek     = (int)0x0200,     // Print day of week
  58.         d_PadDay        = (int)0x0400,         // Pad date to 2
  59.         d_ZeroDay       = (int)0x0800,         // Zero fill day
  60.         d_DMY           = (int)0x0111, // Print date, mth, year
  61.         d_PadDMY        = (int)0x0444,         // Pad all three
  62.         d_ZeroDMY       = (int)0x0888,   // Zero fill all three
  63.         d_YearFirst     = (int)0x1000,      // Print year first
  64.         d_MonFirst      = (int)0x2000,     // Print month first
  65.         d_SepChar       = (int)0x4000,// Separate fields datech
  66.         d_SepSpace      = (int)0x8000  // Separate fields space
  67.     };
  68.     enum date_f                         // Composite date flags
  69.     {
  70.         d_International = (int)(d_DMY|d_MonText|d_PadMon|
  71.                                 d_SepSpace),
  72.         d_IntlShort     = (int)(d_DMY|d_MonText|d_PadMon|
  73.                                 d_SepSpace|d_YearShort),
  74.         d_Usa           = (int)(d_DMY|d_MonFirst|d_PadDMY|
  75.                                 d_ZeroDMY|d_YearShort|d_SepChar),
  76.         d_English       = (int)(d_DMY|d_YearShort|d_PadDMY|
  77.                                 d_ZeroDMY|d_SepChar),
  78.         d_Japanese      = (int)(d_DMY|d_YearFirst|d_PadDMY|
  79.                                 d_ZeroDMY|d_YearShort|d_MonFirst|
  80.                                 d_SepChar),
  81.         d_Full          = (int)(d_DMY|d_DayOfWeek|d_MonText|
  82.                                 d_SepSpace)
  83.     };
  84.  
  85.     enum f_time  // Time format (nb: time zones not implemented)
  86.     {
  87.         t_Secs          = (int)0x0001,         // Print seconds
  88.         t_ZeroSecs      = (int)0x0002,     // Zero fill seconds
  89.         t_PadSecs       = (int)0x0004,      // Pad seconds to 2
  90.         t_SecsAll       = (int)0x0007,
  91.         t_TimeZone      = (int)0x0008,        // Print timezone
  92.         t_Mins          = (int)0x0010,         // Print minutes
  93.         t_ZeroMins      = (int)0x0020,     // Zero fill minutes
  94.         t_PadMins       = (int)0x0040,      // Pad minutes to 2
  95.         t_MinsAll       = (int)0x0070,
  96.         t_TZNumeric     = (int)0x0080,      // Print numeric TZ
  97.         t_Hour          = (int)0x0100,            // Print hour
  98.         t_ZeroHour      = (int)0x0200,        // Zero fill hour
  99.         t_PadHour       = (int)0x0400,  // Pad hour to 2 digits
  100.         t_HourAll       = (int)0x0700,
  101.         t_24hour        = (int)0x0800,          // 24hour clock
  102.         t_SepChar       = (int)0x1000, // Separate field timech
  103.         t_SepSpace      = (int)0x2000, // Separate fields space
  104.         t_SepAbbrev     = (int)0x4000,     // Add abbreviations
  105.         t_AmPm          = (int)0x8000       // Add 'am' or 'pm'
  106.     };
  107.     enum time_f  // Composite time flags
  108.     {
  109.         t_International = (int)(t_HourAll|t_MinsAll|t_SecsAll|
  110.                                 t_24hour|t_SepChar),
  111.         t_ShortTime     = (int)(t_HourAll|t_MinsAll|t_24hour|
  112.                                 t_SepChar),
  113.         t_ClockTime     = (int)(t_Hour|t_PadHour|t_MinsAll|
  114.                                 t_AmPm|t_SepChar),
  115.         t_LongTime      = (int)(t_Hour|t_PadHour|t_MinsAll|
  116.                                 t_SecsAll|t_SepAbbrev|t_SepSpace),
  117.         t_Millitary     = (int)(t_HourAll|t_MinsAll|t_24hour)
  118.     };
  119.  
  120.  
  121.     static char timech;    // Character used for time separator
  122.     static char datech;    // Character used for date separator
  123.     static int datefmt;                  // Default date format
  124.     static int timefmt;                  // Default time format
  125.  
  126.                                               // Output methods
  127.     ostream & print (ostream & os, int df =datefmt,
  128.                                    int tf =timefmt) const;
  129.     ostream & printTime (ostream & os, int f =timefmt) const;
  130.     ostream & printDate (ostream & os, int f =datefmt) const;
  131.  
  132.                                        // Input/parsing methods
  133.     istream & parseTime (istream & is);        // Unimplemented
  134.     istream & parseDate (istream & is);        // Unimplemented
  135.     istream & parse (istream & is);            // Unimplemented
  136.  
  137.   private:
  138.                                                  // Time suffix
  139.     void tSfx (ostream & os, int fmt, char ch) const;
  140.                                        // Time field formatters
  141.     void pHour (ostream & os, int fmt) const;
  142.     void pMins (ostream & os, int fmt) const;
  143.     void pSecs (ostream & os, int fmt) const;
  144.                                                  // Date suffix
  145.     void dSfx (ostream & os, int fmt) const;
  146.                                        // Date field formatters
  147.     void pDate (ostream & os, int fmt) const;
  148.     void pMonth (ostream & os, int fmt) const;
  149.     void pYear (ostream & os, int fmt) const;
  150.  
  151. };
  152.  
  153. inline ostream &                        // Stream output method
  154. operator<< (ostream & os, loc_tm const & t)
  155. {   return t.print(os);     }
  156.  
  157. inline int                                          // Equality
  158. operator== (loc_tm const & t1, loc_tm const & t2)
  159. {   return int(t1.compare(t2) == 0);    }
  160.  
  161. inline int                                        // Inequality
  162. operator!= (loc_tm const & t1, loc_tm const & t2)
  163. {   return int(t1.compare(t2) != 0);    }
  164.  
  165. inline int                                         // Less then
  166. operator<  (loc_tm const & t1, loc_tm const & t2)
  167. {   return int(t1.compare(t2) < 0);     }
  168.  
  169. inline int                                   // Less than/equal
  170. operator<= (loc_tm const & t1, loc_tm const & t2)
  171. {   return int(t1.compare(t2) <= 0);    }
  172.  
  173. inline int                                      // Greater than
  174. operator>  (loc_tm const & t1, loc_tm const & t2)
  175. {   return int(t1.compare(t2) > 0);     }
  176.  
  177. inline int                                // Greater than/equal
  178. operator>=  (loc_tm const & t1, loc_tm const & t2)
  179. {   return int(t1.compare(t2) >= 0);    }
  180.  
  181.  
  182.     // class loc_date
  183.     // Date formatter
  184.  
  185. class loc_date : public loc_tm
  186. {
  187.  
  188.   public:
  189.  
  190.     loc_date (loc_tm const & t, int dtfmt =loc_tm::datefmt)
  191.         : loc_tm (t), _fmt(dtfmt)   {}
  192.     loc_date (time_t t, int dtfmt =loc_tm::datefmt)
  193.         : loc_tm (t), _fmt(dtfmt)   {}
  194.     int fmt (int f)                 {   return _fmt = f;    }
  195.     int fmt (void) const            {   return _fmt;        }
  196.  
  197.     ostream & print (ostream & os) const
  198.         {   return printDate (os, _fmt);    }
  199.  
  200.   private:
  201.  
  202.     int _fmt;
  203.  
  204. };
  205.  
  206. inline ostream &                // Stream output method - date
  207. operator<< (ostream & os, loc_date const & d)
  208. {   return d.print(os);     }
  209.  
  210.  
  211.     // class loc_time
  212.     // Time formatter
  213.  
  214. class loc_time : public loc_tm
  215. {
  216.  
  217.   public:
  218.  
  219.     loc_time (loc_tm const & t, int tmfmt =loc_tm::timefmt)
  220.         : loc_tm (t), _fmt(tmfmt)   {}
  221.     loc_time (time_t t, int tmfmt =loc_tm::timefmt)
  222.         : loc_tm (t), _fmt(tmfmt)   {}
  223.     int fmt (int f)                 {   return _fmt = f;    }
  224.     int fmt (void) const            {   return _fmt;        }
  225.  
  226.     ostream & print (ostream & os) const
  227.         {   return printTime (os, _fmt);    }
  228.  
  229.   private:
  230.  
  231.     int _fmt;
  232.  
  233. };
  234.  
  235. inline ostream &                // Stream output method - time
  236. operator<< (ostream & os, loc_time const & t)
  237. {   return t.print(os);     }
  238.  
  239.  
  240.  
  241. # endif     // _loctm_h
  242.