home *** CD-ROM | disk | FTP | other *** search
/ High Voltage Shareware / high1.zip / high1 / DIR36 / KDC46.ZIP / DATECL.H < prev    next >
C/C++ Source or Header  |  1993-08-04  |  7KB  |  186 lines

  1. /*
  2.  *┌──────────────────────────────────────────────────────────────────────
  3.  *│ File.........: DATECL.H
  4.  *│ Date.........: August 4, 1993
  5.  *│ Author.......: Steve Marcus
  6.  *│ Copyright....: None! Use freely.
  7.  *│ Version......: 4.6  Compile w/MSC++ 7.0 or Borland C++ 3.1
  8.  *│ Usage........: General purpose date conversion, arithmetic,
  9.  *│              :    comparison, and formatting class
  10.  *│ Compile line.: cl /AM /W3 /Zp /D_DOS /D_XXX /Osel /Gs /c datecl4.cpp
  11.  *│              : cl /AM /W3 /Zp /D_DOS /D_XXX /Osel /Gs /c datedemo.cpp
  12.  *│              : where _XXX = { _BCC (Borland C++) or _MSC (Microsoft C++) }
  13.  *│ Link line....:
  14.  *│     link /NOD /ONERROR:NOEXE datedemo date, datedemo, NUL, mlibce;
  15.  *│
  16.  *│ Acknowledgements:
  17.  *│
  18.  *│     Originally inspired by Steve Marcus (CIS 72007,1233)    6/16/91
  19.  *│     Enhanced by Eric Simon (CIS 70540,1522)                 6/29/91
  20.  *│     Further Enhanced by Chris Hill (CIS 72030,2606)         7/11/91
  21.  *│     Still Further Enhanced by Hill & Simon  v3.10           8/05/91
  22.  *│
  23.  *│     "It just keeps on a 'git 'n bedder!"
  24.  *│             by Charles D. Price (CIS 70541,3651) v4.0       6/27/92
  25.  *│
  26.  *│     Sealing the memory leaks...
  27.  *│         some variable casts and string output.
  28.  *│             by Kenneth A. Argo (CIS 71241,3635) v4.1        3/10/93
  29.  *│
  30.  *│     "Yet, more improvements..."
  31.  *│             by Ly Minh Trí (CIS 70641,3127) v4.2            3/13/93
  32.  *│             ............................... v4.3            3/24/93
  33.  *│             ............................... v4.4            6/03/93
  34.  *│             ............................... v4.5            6/21/93
  35.  *│             ............................... v4.6            8/04/93
  36.  *│
  37.  *│    And the quest for the perfect date class continues....
  38.  *│
  39.  *└──────────────────────────────────────────────────────────────────────
  40.  */
  41. #ifndef __cplusplus
  42. #error  Requires C++ Compiler
  43. #endif
  44.  
  45. #ifndef DATECLS_H
  46. #define DATECLS_H
  47.  
  48. #if defined(_BCC)
  49. #define DOSDATE_T   dosdate_t
  50. #elif defined(_MSC)
  51. #define DOSDATE_T   _dosdate_t
  52. #else
  53. #error  Must define _BCC (for Borland C++) or _MSC (for Microsoft C++)
  54. #endif
  55.  
  56. #include <stdio.h>
  57. #include <string.h>
  58. #include <stdlib.h>
  59. #include <iostream.h>
  60. #include <dos.h>
  61.  
  62. #define PUBLIC              // just a couple of friendly reminders!
  63. #define MEMBER
  64.  
  65. #define ABBR_LENGTH 3
  66.  
  67. class Date
  68. {
  69.     public:
  70.         //
  71.         // TML - Put into class so we don't proliferate global names...in the
  72.         //       tradition of the 'ios' class!
  73.         //       Make use of the encapsulation feature of C++
  74.         //
  75.         const enum format_type {MDY, DAY, MONTH, FULL, EUROPEAN};
  76.         const enum {OFF, ON};
  77.         const enum {BUF_SIZE=40};
  78.         const enum { NO_CENTURY  = (unsigned char) 0x02,
  79.                      DATE_ABBR   = (unsigned char) 0x04};
  80.  
  81.     protected:
  82.         unsigned long julian;       // see julDate();  days since 1/1/4713 B.C.
  83.         int year;                   // see NYear4()
  84.         unsigned char month;        // see NMonth()
  85.         unsigned char day;          // see Day()
  86.         unsigned char day_of_week;    // see NDOW();    1 = Sunday, ... 7 = Saturday
  87.  
  88.     private:
  89.         static int DisplayFormat;
  90.         static unsigned char DisplayOptions;
  91.         static char cbuf[BUF_SIZE];     // Date::formatDate()'s buffer space
  92.  
  93.         void julian_to_mdy ();          // convert julian day to mdy
  94.         void julian_to_wday ();         // convert julian day to day_of_week
  95.         void mdy_to_julian ();          // convert mdy to julian day
  96.  
  97.     public:
  98.         Date ();
  99.         Date (long j);
  100.         Date (int m, int d, int y);
  101.         Date (const char * dat);
  102.         Date (const DOSDATE_T &ds);
  103.         Date (const Date &dt);
  104.         virtual ~Date() {}              // Do nothing!
  105.  
  106.         operator char *( void );        // Date to character - via type casting
  107.  
  108.         Date  operator + (long i);
  109.         Date  operator + (int i);
  110.  
  111.         Date  operator - (long i);
  112.         Date  operator - (int i);
  113.         long  operator - (const Date &dt);
  114.  
  115.         const Date &operator += (long i);
  116.         const Date &operator -= (long i);
  117.  
  118.         Date  operator ++ ();               // Prefix increment
  119.         Date  operator ++ (int);            // Postfix increment
  120.         Date  operator -- ();               // Prefix decrement
  121.         Date  operator -- (int);            // Postfix decrement
  122.  
  123.         int operator <  (const Date &dt);   // TML - Convert to member
  124.         int operator <= (const Date &dt);   // functions from 'friend'
  125.         int operator >  (const Date &dt);   // functions
  126.         int operator >= (const Date &dt);
  127.         int operator == (const Date &dt);
  128.         int operator != (const Date &dt);
  129.  
  130.         friend ostream &operator << (ostream &os, const Date &dt);
  131.         friend ostream &operator << (ostream &os, const DOSDATE_T &dt);
  132.  
  133.         const  char * formatDate(int type=DisplayFormat) const;
  134.         static void   setFormat (int format);
  135.         static int    setOption (int option, int action=ON);
  136.  
  137.         long  julDate()     const;    // returns julian date
  138.         int   DOY()         const;  // returns relative date since Jan. 1
  139.         int   isLeapYear()    const;    // returns 1 if leap year, 0 if not
  140.  
  141.         // note that the next functions return a date struct as defined in
  142.         // dos.h (distinct from the Date class)
  143.  
  144.         DOSDATE_T  eom()        const;  // returns last day of month in object
  145.         DOSDATE_T  getDate()    const;  // returns a date structure
  146.  
  147.  
  148.         //─────────────────────────────────────────────────
  149.         // Version 4.0 Extension to Public Interface - CDP
  150.         //─────────────────────────────────────────────────
  151.  
  152.         // These 'Set's modify the date object and actually SET it
  153.         // They all return a reference to self (*this)
  154.  
  155.         const Date &Set();                    // Sets to current system date
  156.         const Date &Set(long lJulian);
  157.         const Date &Set(int nMonth, int nDay, int nYear);
  158.  
  159.         const Date &AddWeeks(int nCount = 1);  //
  160.         const Date &AddMonths(int nCount = 1); // May also pass neg# to 
  161.         const Date &AddYears(int nCount = 1);  // decrement
  162.  
  163.         int Day() const;        // Numeric Day of date object
  164.  
  165.         int DaysInMonth();      // Number of days in month (1..31)
  166.  
  167.         int FirstDOM() const;   // First Day Of Month  (1..7)
  168.  
  169.         const char *CDOW();     // Character Day Of Week ('Sunday'..'Saturday')
  170.         int NDOW() const;       // (1..7)
  171.  
  172.         int WOM();              // Numeric Week Of Month  (1..6)
  173.         int WOY();              // Numeric Week Of Year   (1..52)
  174.  
  175.         const char * CMonth();  // Character Month name
  176.         int  NMonth() const;    // Month Number (1..12)
  177.         Date BOM();             // First Date Of Month
  178.         Date EOM();             // Last Date Of Month
  179.  
  180.         int  NYear4() const;    // eg. 1992
  181.         Date BOY();             // First Date Of Year
  182.         Date EOY();             // Last Date Of Year
  183. };
  184.  
  185. #endif
  186.