OWLNext    7.0
Borland's Object Windows Library for the modern age
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
date.h
Go to the documentation of this file.
1//----------------------------------------------------------------------------
2// ObjectWindows
3// Copyright (c) 1993, 1996 by Borland International, All Rights Reserved
4//
5//----------------------------------------------------------------------------
6
7#if !defined(OWL_DATE_H)
8#define OWL_DATE_H
9
10#include <owl/private/defs.h>
11#if defined(BI_HAS_PRAGMA_ONCE)
12# pragma once
13#endif
14
15#include <owl/defs.h>
17
18namespace owl {
19
20class _OWLCLASS TTime;
21class _OWLCLASS ipstream;
22class _OWLCLASS opstream;
23class _OWLCLASS TSystemTime;
24
25/// Day type.
26typedef unsigned DayTy;
27
28/// Month type.
29typedef unsigned MonthTy;
30
31/// Year type.
32typedef unsigned YearTy;
33
34/// Julian calendar type.
35typedef unsigned long JulTy;
36
37static const JulTy jul1901 = 2415386L; ///< Julian day for 1/1/1901
38
39#include <owl/preclass.h>
40
41/// \addtogroup utility
42/// @{
43/// \class TDate
44// ~~~~~ ~~~~~
45/// The TDate class represents a date. It has members that read, write, and store
46/// dates, and that convert dates to Gregorian calendar dates.
47//
49 public:
50
51/// Lists different print formats.
60
61 /// Construct a TDate with the current date
62
63 TDate();
64
65 /// Construct a TDate with a given day of the year and a given year.
66 /// The base date for this computation is Dec. 31 of the previous year.
67 /// If year == 0, Construct a TDate with Jan. 1, 1901 as the "day zero".
68 /// i.e., TDate(-1,0) = Dec. 31, 1900 and TDate(1,0) = Jan. 2, 1901.
69
71
72 /// Construct a TDate for the given day, month, and year.
73 TDate( DayTy, const tstring& month, YearTy );
75 TDate( const tistream& s ); ///< Read date from stream.
76 TDate( const TTime& ); ///< Construct a TDate from a TTime
77 TDate( const TSystemTime& ); ///< Construct a TDate from a TSystemTime
78 TDate(LPCTSTR str, LPCTSTR format = nullptr);
79 TDate(const tstring& s) {ParseFrom(s.c_str(), nullptr);}
80 TDate(const tstring& s, const tstring& format) {ParseFrom(s.c_str(), format.c_str());}
81
82 TSystemTime GetSystemTime() const;
83
84 tstring AsString() const;
85 int Between( const TDate& d1, const TDate& d2 ) const;
86 int CompareTo( const TDate& ) const;
87 DayTy Day() const; // 1-365
88 int DaysInMonth(MonthTy month=0) const; // 28-31
89 DayTy DayOfMonth() const; // 1-31
90 DayTy FirstDayOfMonth() const;
91 DayTy FirstDayOfMonth( MonthTy ) const;
92 unsigned Hash() const;
93 int IsValid() const;
94 int Leap() const; ///< leap year?
95 TDate Max( const TDate& dt ) const;
96 TDate Min( const TDate& dt ) const;
97 MonthTy Month() const;
98 LPCTSTR NameOfDay() const;
99 LPCTSTR NameOfMonth() const;
100 TDate Previous( const tstring& dayName) const; ///< Return date of previous dayName
101 TDate Previous( DayTy ) const; ///< Same as above, but use day of week
102 DayTy WeekDay(DayTy day=0) const;
103 YearTy Year() const;
104
105 // Date comparisons:
106 int operator < ( const TDate& date ) const;
107 int operator <= ( const TDate& date ) const;
108 int operator > ( const TDate& date ) const;
109 int operator >= ( const TDate& date ) const;
110 int operator == ( const TDate& date ) const;
111 int operator != ( const TDate& date ) const;
112
113 // Arithmetic operators:
114 JulTy operator - ( const TDate& dt ) const;
115 friend _OWLFUNC(TDate) operator + ( const TDate& dt, int dd );
116 friend _OWLFUNC(TDate) operator + ( int dd, const TDate& dt );
117 friend _OWLFUNC(TDate) operator - ( const TDate& dt, int dd );
118 void operator ++ ();
119 void operator -- ();
120 void operator += ( int dd );
121 void operator -= ( int dd );
122
123 // Read or write dates:
124 friend _OWLCFUNC( tostream&) operator << ( tostream& s, const TDate& d );
125 friend _OWLCFUNC( tistream&) operator >> ( tistream& s, TDate& d );
126
127 // Read or write dates on persistent streams
128 friend _OWLCFUNC(opstream&) operator << ( opstream& s, const TDate& d );
130
131 // Static member functions:
132 static LPCTSTR DayName( DayTy weekDayNumber );
133 static DayTy DayOfWeek( const tstring& dayName );
134 static int DayWithinMonth( MonthTy, DayTy, YearTy );
135 static DayTy DaysInYear( YearTy );
136 static MonthTy IndexOfMonth( const tstring& monthName );
137 static JulTy Jday( MonthTy, DayTy, YearTy );
138 static int LeapYear( YearTy year );
139 static LPCTSTR MonthName( MonthTy monthNumber );
140 static HowToPrint SetPrintOption( HowToPrint h );
141
142 // A YearTy value in the range [0..MaxShortYear) will have BaseYear
143 // added to it when converting a year into a Julian day number (the
144 // internal representation of TDate). This affects the TDate
145 // constructors and TDate::Jday.
146 //???????????????????????????????????
147 //static YearTy &BaseYear(); // default: 1900
148 //static YearTy &MaxShortYear(); // default: 999
149
150 // set reset month and days names
157 static void SetIntlNames(TDateType type, const tchar** names);
158 static const tchar* GetIntlName(TDateType type, int index);
159
160 protected:
161
162 static int AssertWeekDayNumber( DayTy d );
163 static int AssertIndexOfMonth( MonthTy m );
164
165 private:
166
167 JulTy Julnum; ///< Julian Day Number (Not same as Julian date.)
168 static HowToPrint PrintOption; ///< Printing with different formats
169
170 void ParseFrom(tistream& ); // Reading dates
171 void ParseFrom(LPCTSTR strDate, LPCTSTR format = nullptr);
172
173 _OWLSTATICFUNC(void) Mdy( MonthTy&, DayTy&, YearTy& ) const;
174 TDate( JulTy j );
175
176};
177/// @}
178
179#include <owl/posclass.h>
180
181
182/*------------------------------------------------------------------------*/
183/* */
184/* TDate inline functions */
185/* */
186/*------------------------------------------------------------------------*/
187
188/// Constructs a TDate object, reading the date from input stream is.
189inline TDate::TDate( const tistream& s )
190{
191 ParseFrom(const_cast<tistream&>(s));
192}
193
195{
196 ParseFrom(str, format);
197}
198
199/// Returns 1 if this TDate object is between d1 and d2, inclusive.
200inline int TDate::Between( const TDate& d1, const TDate& d2 ) const
201{
202 return Julnum >= d1.Julnum && Julnum <= d2.Julnum;
203}
204
205/// Returns the number of the first day of the month for this TDate.
207{
208 return FirstDayOfMonth(Month());
209}
210
211/// Returns 1 if this TDate is valid, 0 otherwise.
212inline int TDate::IsValid() const
213{
214 return Julnum>0;
215}
216
217/// Returns 1 if this TDate's year is a leap year, 0 otherwise.
218inline int TDate::Leap() const
219{
220 return LeapYear(Year());
221}
222
223/// Returns this TDate's day string name.
225{
226 return DayName(WeekDay());
227}
228
229/// Returns this TDate's month string name.
231{
232 return MonthName(Month());
233}
234
235/// Returns 1 if this TDate precedes date. Otherwise returns 0.
236inline int TDate::operator < ( const TDate& date ) const
237{
238 return Julnum < date.Julnum;
239}
240
241/// Returns 1 if this TDate is less than or equal to date, otherwise returns 0.
242inline int TDate::operator <= ( const TDate& date ) const
243{
244 return Julnum <= date.Julnum;
245}
246
247/// Returns 1 if this TDate is greater than date. otherwise returns 0.
248inline int TDate::operator > ( const TDate& date ) const
249{
250 return Julnum > date.Julnum;
251}
252
253/// Returns 1 if this TDate is greater than or equal to date, otherwise returns 0.
254inline int TDate::operator >= ( const TDate& date ) const
255{
256 return Julnum >= date.Julnum;
257}
258
259/// Returns 1 if this TDate is equal to date, otherwise returns 0.
260inline int TDate::operator == ( const TDate& date ) const
261{
262 return Julnum == date.Julnum;
263}
264
265/// Returns 1 if this TDate is not equal to date, otherwise returns 0.
266inline int TDate::operator != ( const TDate& date ) const
267{
268 return Julnum != date.Julnum;
269}
270
271/// Subtracts dt from this TDate and returns the difference.
272inline JulTy TDate::operator - ( const TDate& dt ) const
273{
274 return Julnum - dt.Julnum;
275}
276
277/// Increments this TDate by 1.
279{
280 Julnum += 1;
281}
282
283/// Decrements this TDate by 1.
285{
286 Julnum -= 1;
287}
288
289/// Adds dd to this TDate.
290inline void TDate::operator += ( int dd )
291{
292 Julnum += dd;
293}
294
295/// Subtracts dd from this TDate.
296inline void TDate::operator -= ( int dd )
297{
298 Julnum -= dd;
299}
300
301/// Returns 1 if d is between 1 and 7 inclusive, otherwise returns 0.
303{
304 return d>=1 && d<=7;
305}
306
307/// Returns 1 if m is between 1 and 12 inclusive, otherwise returns 0.
309{
310 return m>=1 && m<=12;
311}
312
313inline TDate::TDate( JulTy j )
314{
315 Julnum = j;
316}
317
318inline unsigned HashValue( TDate& d )
319{
320 return d.Hash();
321}
322
323} // OWL namespace
324
325
326#endif // OWL_DATE_H
The TDate class represents a date.
Definition date.h:48
YearTy Year() const
Returns the year of this TDate.
Definition date.cpp:431
TDate(const tstring &s)
Definition date.h:79
static int LeapYear(YearTy year)
Definition date.cpp:240
LPCTSTR NameOfMonth() const
Returns this TDate's month string name.
Definition date.h:230
int Between(const TDate &d1, const TDate &d2) const
Returns 1 if this TDate object is between d1 and d2, inclusive.
Definition date.h:200
void operator--()
Decrements this TDate by 1.
Definition date.h:284
int IsValid() const
Returns 1 if this TDate is valid, 0 otherwise.
Definition date.h:212
TDate()
Construct a TDate with the current date.
Definition date.cpp:68
static LPCTSTR MonthName(MonthTy monthNumber)
Returns the string name for the given monthNumber (1-12).
Definition date.cpp:249
static int AssertIndexOfMonth(MonthTy m)
Returns 1 if m is between 1 and 12 inclusive, otherwise returns 0.
Definition date.h:308
int operator==(const TDate &date) const
Returns 1 if this TDate is equal to date, otherwise returns 0.
Definition date.h:260
int Leap() const
leap year?
Definition date.h:218
void operator-=(int dd)
Subtracts dd from this TDate.
Definition date.h:296
void operator++()
Increments this TDate by 1.
Definition date.h:278
int operator>=(const TDate &date) const
Returns 1 if this TDate is greater than or equal to date, otherwise returns 0.
Definition date.h:254
TDate(const tstring &s, const tstring &format)
Definition date.h:80
static int AssertWeekDayNumber(DayTy d)
Returns 1 if d is between 1 and 7 inclusive, otherwise returns 0.
Definition date.h:302
static LPCTSTR DayName(DayTy weekDayNumber)
Returns a string name for the weekday number.
Definition date.cpp:146
DayTy FirstDayOfMonth() const
Returns the number of the first day of the month for this TDate.
Definition date.h:206
void operator+=(int dd)
Adds dd to this TDate.
Definition date.h:290
int operator<(const TDate &date) const
Returns 1 if this TDate precedes date. Otherwise returns 0.
Definition date.h:236
friend TDate operator-(const TDate &dt, int dd)
Definition date.cpp:557
MonthTy Month() const
Returns the month of this TDate.
Definition date.cpp:379
HowToPrint
Lists different print formats.
Definition date.h:52
@ Terse
Definition date.h:54
@ WinIntSection
Definition date.h:58
@ Numbers
Definition date.h:55
@ Normal
Definition date.h:53
@ EuropeanNumbers
Definition date.h:56
@ European
Definition date.h:57
int operator<=(const TDate &date) const
Returns 1 if this TDate is less than or equal to date, otherwise returns 0.
Definition date.h:242
int operator!=(const TDate &date) const
Returns 1 if this TDate is not equal to date, otherwise returns 0.
Definition date.h:266
DayTy WeekDay(DayTy day=0) const
Returns 1 (Monday) through 7 (Sunday).
Definition date.cpp:422
LPCTSTR NameOfDay() const
Returns this TDate's day string name.
Definition date.h:224
@ dtMonthNames
Definition date.h:152
@ dtWeekDayNames
Definition date.h:154
@ dtUCMonthNames
Definition date.h:153
@ dtUCWeekDayNames
Definition date.h:155
int operator>(const TDate &date) const
Returns 1 if this TDate is greater than date. otherwise returns 0.
Definition date.h:248
TSystemTime is a class derived from the structure SYSTEMTIME.
Definition wsyscls.h:420
The TTime class encapsulates time functions and characteristics.
Definition time.h:38
ipstream, a specialized input stream derivative of pstream, is the base class for reading (extracting...
Definition objstrm.h:391
Base class for writing streamable objects.
Definition objstrm.h:480
Object Windows Library (OWLNext Core)
Definition animctrl.h:22
unsigned YearTy
Year type.
Definition date.h:32
char tchar
Definition defs.h:77
unsigned long JulTy
Julian calendar type.
Definition date.h:35
unsigned HashValue(TDate &d)
Definition date.h:318
std::istream tistream
Definition strmdefs.h:39
std::string tstring
Definition defs.h:79
unsigned DayTy
Day type.
Definition date.h:26
std::ostream tostream
Definition strmdefs.h:40
unsigned MonthTy
Month type.
Definition date.h:29
General definitions used by all ObjectWindows programs.
#define _OWLSTATICFUNC(p)
Definition defs.h:308
#define _OWLFUNC(p)
Definition defs.h:341
#define _OWLCFUNC(p)
Definition defs.h:342
#define _OWLCLASS
Definition defs.h:338