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