home *** CD-ROM | disk | FTP | other *** search
/ PC Media 23 / PC MEDIA CD23.iso / share / prog / date50 / datecl.h < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-09  |  12.2 KB  |  326 lines

  1. /*
  2. *+----------------------------------------------------------------------
  3. *| Header.......: DATECL.H
  4. *| Date.........: Sat  12-01-1994
  5. *| Author.......: James M. Curran,  et al
  6. *| Version......: 5.0   Compile w/MSC++ 7.0 or Borland C++ 3.1 (or later versions)
  7. *| Usage........: General purpose date conversion, arithmetic,
  8. *|              :    comparison, and formatting class
  9. *| Compile line.: cl /AM /W3 /Zp /D_DOS /Osel /Gs /c datecl4.cpp
  10. *|              : cl /AM /W3 /Zp /D_DOS /Osel /Gs /c datedemo.cpp
  11. *| Link line....:
  12. *|    link /NOD /ONERROR:NOEXE datedemo date, datedemo, NUL, mafxcr mlibce;
  13. *|
  14. *| Acknowledgements:
  15. *|
  16. *|    Originally inspired by Steve Marcus (CIS 72007,1233)  6/16/91
  17. *|    Enhanced by Eric Simon (CIS 70540,1522)               6/29/91
  18. *|    Further Enhanced by Chris Hill (CIS 72030,2606)       7/11/91
  19. *|    Still Further Enhanced by Hill & Simon  v3.10         8/05/91
  20. *|
  21. *|    "It jist keeps on a 'git 'n bedder!"
  22. *|       by Charles D. Price (CIS 70541,3651) v4.0          6/27/92
  23. *|
  24. *|     Sealing the memory leaks...
  25. *|         some variable casts and string output.
  26. *|             by Kenneth A. Argo (CIS 71241,3635) v4.1        3/10/93
  27. *|
  28. *|     "Yet, more improvements..."
  29. *|             by Ly Minh TrĂ­ (CIS 73062,512)  v4.2            3/13/93
  30. *|             ............................... v4.3            3/24/93
  31. *|             ............................... v4.4            6/03/93
  32. *|             ............................... v4.5            6/21/93
  33. *|             ............................... v4.6            8/04/93
  34. *|             ............................... v4.7            9/20/93
  35. *|             ............................... v4.8           11/18/93
  36. *|             ............................... v4.9            1/26/94
  37. *|
  38. *|      "All kinds of good stuff..."
  39. *|               by James M. Curran (CIS 72261,655)  v5.0       10/30/94
  40. *|
  41. *|
  42. *|    And the quest for the perfect date class continues....
  43. *|
  44. *+----------------------------------------------------------------------
  45. */
  46. #ifndef __cplusplus
  47. #error  Requires C++ Compiler
  48. #endif
  49.  
  50. #ifndef DATECLS_H
  51. #define DATECLS_H
  52.  
  53. //---------------------- Compatibility Section -----------------------------------------
  54. //
  55. // Here we attempt to smooth out all the variations between different compiliers, by 
  56. // #define-ing several symbols to include or remove, or to use a common name.
  57. //
  58. // The #defines used are :
  59. //
  60. // #define MSDOS        // If target system is MS-DOS based.
  61. // #define DOSDATE_T    // name of the dos_date struct, (need only when "MSDOS" is defined)
  62. // #define NOPOSTFIX    // If compiler cannot handle postfix ++.
  63. // #define f_EXISTS // if a boolean type of one form or another already exists.
  64. //
  65.  
  66.  
  67. #if !defined(BOOLEAN_EXISTS)
  68.     // this simulates (poorly) the new "bool" basic type as defined in the
  69.     // ANSI/ISO C++ committee working papers, where "bool", "true" and "false"
  70.     // are new keywords.   When the standard is finalized, this entire #if/#endif 
  71.     // can be deleted.
  72.     typedef enum __booltag {false, true} bool;
  73. #endif
  74.  
  75.  
  76. #if defined(__MSDOS__)    // Borland uses "__MSDOS__" while Microsoft uses
  77.     #define    MSDOS    TRUE    // "MSDOS".  I'm not sure what WATCOM, et al use, 
  78. #endif                        // but I figure this should cover most of 'em.
  79.  
  80.  
  81.  
  82. #if defined (__BORLANDC__)  || defined (__TURBOC__)
  83.         #define    DOSDATE_T    dosdate_t
  84.         #include <iostream.h>
  85. #elif defined(_MSC_VER) 
  86.         #if defined(_WIN32)
  87.         typedef struct _dosdate_t
  88.             {                            // Current date structure
  89.             unsigned char day;            // Day of the month: 1-31
  90.             unsigned char month;        // Month of the year: 1-12
  91.             unsigned int year;            // Year: 0-119 relative to 1980
  92.             unsigned char dayofweek;    // Day of the week: 0-6 (Sunday is 0)
  93.             } _dosdate_t;
  94.         #endif
  95.         #define DOSDATE_T   _dosdate_t
  96.         #include <iostream.h>
  97.  
  98. #elif defined (__ZTC__)  && __ZTC__ < 0x0600
  99.  
  100.         #define    DOSDATE_T    dos_date_t
  101.         #define    MSDOS            TRUE
  102.         #define NOPOSTFIX
  103.         #include <stream.hpp>
  104.  
  105. #elif defined (__ZTC__) || defined(__SC__)
  106.  
  107.         #define    DOSDATE_T    dos_date_t
  108.         #define    MSDOS            TRUE
  109.         #include <iostream.h>
  110.  
  111. #elif    defined (___WATCOMC__)
  112.         #error Add #defines for Watcom C
  113.         #define    NON_MSDOS    TRUE            // Until #defines are added.
  114.  
  115. #else        // Add other compilers here
  116.  
  117.         #define    NON_MSDOS    TRUE
  118.         #include <iostream.h>
  119.  
  120. #endif
  121.  
  122. #include <stdio.h>
  123. #include <string.h>
  124. #include <stdlib.h>
  125. #include <time.h>
  126.  
  127. #if defined(MSDOS)
  128.     #include <dos.h>
  129. #endif
  130. #if defined(_WIN32)
  131.     #include <windows.h>
  132. #endif
  133.  
  134. #define PUBLIC           // just a couple of friendly reminders!
  135. #define MEMBER
  136.  
  137. #define ABBR_LENGTH 3
  138.  
  139.  
  140. class Date
  141. {
  142.     public:
  143.         //
  144.         // TML - Put into class so we don't proliferate global names...in the
  145.         //       tradition of the 'ios' class!
  146.         //       Make use of the encapsulation feature of C++
  147.         //
  148.         const enum format_type {MDY, DAY, MONTH, FULL, EUROPEAN, COLLATE};
  149.         const enum {OFF, ON};
  150.         const enum {BUF_SIZE=40};
  151.         const enum Actions { NO_CENTURY  = 0x02,
  152.                               DATE_ABBR   = 0x04};
  153.         const enum Wday     {NON_DAY=0, SUNDAY=1,MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY,SATURDAY};                     
  154.         const enum Months   {NON_MONTH=0, JANUARY=1, FEBRUARY, MARCH, APRIL, MAY, JUNE, JULY, AUGUST,
  155.                                 SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER};
  156.     protected:
  157.         unsigned long julian;    // see julDate();  days since 1/1/4713 B.C.
  158.         short     year;            // see NYear4()
  159.         short    month;            // see NMonth()
  160.         short     day;            // see Day()
  161.         char     day_of_week;    // see NDOW();    1 = Sunday, ... 7 = Saturday
  162.         char    separator;
  163.  
  164.     private:
  165.         static            int        DisplayFormat;
  166.         static unsigned int        DisplayOptions;
  167.         static unsigned short    DefaultCentury;
  168.         static unsigned int         startDST;
  169.         static unsigned int         startSTD;
  170.  
  171.  
  172.         void julian_to_mdy ();         // convert julian day to mdy
  173.         void julian_to_wday ();        // convert julian day to day_of_week
  174.         void mdy_to_julian ();         // convert mdy to julian day
  175.  
  176.     public:
  177.         Date ();
  178.         Date (short m, short d, short y);
  179.         Date (long     j);
  180.         Date (const char     *dat);
  181.         Date (const Date     &dt);
  182.         Date (const tm         &TM);
  183.         Date (int weeknum, int dow, short m, short y);
  184.  
  185. #if defined (MSDOS) || defined(_WIN32)
  186.         Date (const DOSDATE_T &ds);
  187. #endif
  188.  
  189.         virtual ~Date() {}              // Do nothing!
  190.  
  191.         operator const char *( void ) const;        // Date to character - via type casting
  192.  
  193.         inline Date Date::operator + (long i)    const    {return Date(julian + i);};
  194.         inline Date Date::operator + (int i)    const    {return Date(julian + (long)i);};
  195.         inline Date Date::operator - (long i)    const     {return Date(julian - i);};
  196.         inline Date Date::operator - (int i)    const    {return Date(julian - (long)i);};
  197.         inline long Date::operator - (const Date &dt)    const    {return ( julian - dt.julian );};
  198.  
  199.         Date &operator += (long i);
  200.         Date &operator -= (long i);
  201.  
  202.         Date  operator ++ ();               // Prefix increment
  203.         Date  operator -- ();               // Prefix decrement
  204.  
  205. #if !defined(NOPOSTFIX)
  206.         Date  operator ++ (int);            // Postfix increment
  207.         Date  operator -- (int);            // Postfix decrement
  208. #endif
  209.         inline int operator <  (const Date &dt) const {return(julian <  dt.julian);};
  210.         inline int operator <= (const Date &dt) const {return(julian <= dt.julian);};
  211.         inline int operator >  (const Date &dt) const {return(julian >  dt.julian);};
  212.         inline int operator >= (const Date &dt) const {return(julian >= dt.julian);};
  213.         inline int operator == (const Date &dt) const {return(julian == dt.julian);};
  214.         inline int operator != (const Date &dt) const {return(julian != dt.julian);};
  215.  
  216.         inline     static void      setFormat (enum format_type format)    {DisplayFormat = format;};
  217.                 static bool        setOption (int option, int action=ON);
  218.                 static int       setCentury(short century);
  219.  
  220.         friend ostream &operator << (ostream &os, enum format_type ft) {Date::setFormat(ft); return(os);};
  221.  
  222. #if defined(MSDOS) || defined(_WIN32)
  223.         friend ostream &operator << (ostream &os, const DOSDATE_T &dt);
  224. #endif
  225.  
  226.         char *formatDate(int type=DisplayFormat) const;
  227.  
  228. inline long    julDate()        const    {return(julian);};    // returns julian date
  229.         int    DOY()            const;                        // returns relative date since Jan. 1
  230.  
  231.         int        isLeapYear()    const;        // returns true if leap year, false if not
  232.         bool     isDST()            const;        // returns true if date is within Daylight
  233.                                                // Savings Time (DST), false if not
  234.  
  235.         // Sets the month and day which DST and STD date begins!  This will
  236.         // enable isDST() to return the correct result for regions other than
  237.         // North America.  Returns true if month and day values are valid, false
  238.         // otherwise - TML
  239.  
  240.         static bool    setDST(unsigned nMonth, unsigned nDay);
  241.         static bool    setSTD(unsigned nMonth, unsigned nDay);
  242.  
  243.  
  244. #if defined (MSDOS) || defined(_WIN32)
  245.         // note that the next functions return a date struct as defined in
  246.         // dos.h (distinct from the Date class)
  247.         DOSDATE_T    eom()        const;  // returns last day of month in object
  248.         DOSDATE_T    getDate()    const;  // returns a date structure
  249.  
  250. #endif
  251.         //-------------------------------------------------
  252.         // Version 4.0 Extension to Public Interface - CDP
  253.         //-------------------------------------------------
  254.  
  255.         // These 'Set's modify the date object and actually SET it.
  256.         // They all return a reference to self (*this)
  257.  
  258.             Date &Set(void);       // Sets to current system date
  259.             Date &Set(long lJulian);
  260.             Date &Set(unsigned int nMonth, unsigned int nDay, unsigned int nYear);
  261.             Date &Set(int weeknum, int dow, short m, short y);
  262.             Date &AddMonths(int nCount = 1); // May also pass neg# to decrement
  263.  
  264.             Date &AddWeeks(int nCount = 1);    //
  265.             Date &AddYears(int nCount = 1);    //
  266.  
  267.             unsigned int DaysInMonth(void) const ;    // Number of days in month (1..31)
  268.  
  269.             int    WOM(void)         const;    // Numeric Week Of Month  (1..6)
  270.             int    WOY(void)         const;    // Numeric Week Of Year  (1..52)
  271.  
  272.                                         // First Day Of Month    (1..7)
  273.     inline    int    FirstDOM(void)    const    {return Date(month, 1, year).NDOW();}
  274.  
  275.                                         // Numeric Day of date object
  276.     inline    int Day(void)        const    {return day;}
  277.  
  278.                                         //    Day Of Week
  279.                                         // Character ('Sunday'..'Saturday')
  280.     inline    const char * CDOW(void)    const    {return(formatDate(DAY));}
  281.                                         // (1..7)
  282.     inline    int    NDOW(void)    const    {return day_of_week;}
  283.  
  284.                                     // eg. 1992
  285.     inline    int NYear4()    const    {return year;}
  286.  
  287.                                     // Month Number (1..12)
  288.     inline    int NMonth()    const    {return month;}
  289.  
  290.                                     // First Date Of Month
  291.     inline    Date BOM()        const     {return(Date(month, 1, year));}
  292.  
  293.                                     // Last Date Of Month
  294.     inline    Date EOM()        const     {return((Date(month, 1, year).AddMonths(1))-1);}
  295.  
  296.                                     // First Date Of Year
  297.     inline    Date BOY()        const    {return(Date(1, 1, year));}
  298.  
  299.                                     // Last Date Of Year
  300.     inline    Date EOY()        const    {return(Date(1, 1, year+1)-1);}
  301.  
  302.                                     // Character Month name
  303.     inline    const char * CMonth() const    {return(formatDate(MONTH));}
  304.  
  305. #ifndef NO_HOLIDAYS
  306.     inline  static Date    NewYearsDay(short year)    {return(Date(JANUARY, 1, year));}
  307.     inline  static Date    ValentinesDay(short year)    {return(Date(FEBRUARY, 14, year));}
  308.     inline  static Date    PresidentsDay(short year)    {return(Date(3, MONDAY, FEBRUARY, year));}
  309.     inline  static Date    StPatricksDay(short year)    {return(Date(MARCH, 17, year));}
  310.     inline  static Date    MothersDay(short year)    {return(Date(2, SUNDAY, MAY, year));}
  311.     inline  static Date    MemorialDay(short year)    {return(Date(0, MONDAY, MAY, year));}
  312.     inline  static Date    FlagDay(short year)        {return(Date(JUNE, 14, year));}
  313.     inline  static Date    FathersDay(short year)    {return(Date(3, SUNDAY, JUNE, year));}
  314.     inline  static Date    CanadaDay(short year)        {return(Date(JULY, 1, year));}
  315.     inline  static Date    IndependenceDay(short year)    {return(Date(JULY, 4, year));}
  316.     inline  static Date    BastilleDay(short year)    {return(Date(JULY, 14, year));}
  317.     inline  static Date    LaborDay(short year)        {return(Date(1, MONDAY, SEPTEMBER, year));}
  318.     inline  static Date    VeteransDay(short year)    {return(Date(NOVEMBER, 11, year));}
  319.     inline  static Date    ThanksgivingDay(short year)    {return(Date(4, THURSDAY, NOVEMBER, year));}
  320.     inline  static Date    ChristmasDay(short year)    {return(Date(DECEMBER, 25, year));}
  321. #endif    // NO_HOLIDAYS
  322.  
  323. };
  324.  
  325. #endif
  326.