DCL 4.1
Loading...
Searching...
No Matches
Date Class Reference

#include <DateTime.h>

Public Member Functions

 Date ()
 Date (const Date &_src)
 Date (int _year, int _month, int _day)
void assign (int _year, int _month, int _day)
 Date (long _days)
void assign (long _days)
long days () const
const Dateoperator++ ()
const Date operator++ (int)
const Dateoperator-- ()
const Date operator-- (int)
const Dateoperator= (const Date &_src)
const Dateoperator+= (long _days)
const Dateoperator-= (long _days)
void decode (int &_year, int &_month, int &_day) const
int year () const
int month () const
int day () const
int dayOfWeek () const
int dayOfYear () const
int daysInMonth () const
int daysInYear () const
String toString () const
String toStringF (const wchar_t *_format=NULL) const

Static Public Member Functions

static bool isValid (int y, int m, int d)
static bool isLeapYear (int _year)

Static Public Attributes

static const wchar_t * FORMAT_STRING = L"%Y-%m-%d"

Static Protected Member Functions

static long convertGregorianToJulian (int _year, int _month, int _day)
static void convertJulianToGregorian (long uJulianDays, int &_year, int &_month, int &_day)

Protected Attributes

long __jday

Detailed Description

Definition at line 24 of file DateTime.h.

Constructor & Destructor Documentation

◆ Date() [1/4]

__DCL_BEGIN_NAMESPACE Date::Date ( )

Definition at line 29 of file DateTime.cpp.

30{
31 __jday = 0;
32}
long __jday
Definition DateTime.h:77

◆ Date() [2/4]

Date::Date ( const Date & _src)

Definition at line 34 of file DateTime.cpp.

35{
36 __jday = _src.__jday;
37}

◆ Date() [3/4]

Date::Date ( int _year,
int _month,
int _day )

Definition at line 39 of file DateTime.cpp.

40{
41 assign(_year, _month, _day);
42}
void assign(int _year, int _month, int _day)
Definition DateTime.cpp:49

◆ Date() [4/4]

Date::Date ( long _days)

Definition at line 44 of file DateTime.cpp.

45{
46 assign(_days);
47}

Member Function Documentation

◆ assign() [1/2]

void Date::assign ( int _year,
int _month,
int _day )

Definition at line 49 of file DateTime.cpp.

50{
51 __DCL_ASSERT(Date::isValid(_year, _month, _day));
52
54 _year,
55 _month,
56 _day
57 );
58}
#define __DCL_ASSERT(expr)
Definition Object.h:371
static long convertGregorianToJulian(int _year, int _month, int _day)
Definition DateTime.cpp:285
static bool isValid(int y, int m, int d)
Definition DateTime.cpp:350

◆ assign() [2/2]

void Date::assign ( long _days)

Definition at line 63 of file DateTime.cpp.

64{
65 if (_days < 0)
66 __jday = _days + BC_END + 1;
67 else if (_days > 0)
68 __jday = _days + AD_START - 1;
69 else
70 // set NULL date
71 __jday = _days;
72}
#define AD_START
Definition DateTime.cpp:60
#define BC_END
Definition DateTime.cpp:61

◆ convertGregorianToJulian()

long Date::convertGregorianToJulian ( int _year,
int _month,
int _day )
staticprotected

Definition at line 285 of file DateTime.cpp.

288{
289 if (_y == 0) {
290 __DCL_ASSERT(_m == 0 && _d == 0);
291 return 0;
292 }
293
294 _y += 600000;
295
296 int c, ya;
297 if (_m > 2)
298 _m -= 3;
299 else {
300 _m += 9;
301 _y--;
302 }
303 c = _y / 100;
304 ya = _y - 100 * c;
305
306 return (146097 * c) / 4
307 + (1461 * ya) / 4
308 + (153 * _m + 2) / 5
309 + _d
310 + 1721119;
311}

◆ convertJulianToGregorian()

void Date::convertJulianToGregorian ( long uJulianDays,
int & _year,
int & _month,
int & _day )
staticprotected

Definition at line 313 of file DateTime.cpp.

317{
318 if (_j == 0) {
319 _y = 0;
320 _m = 0;
321 _d = 0;
322 }
323 else {
324 _j -= 1721119;
325
326 _y = (4 * _j - 1) / 146097;
327 _j = 4 * _j - 1 - 146097 * _y;
328 _d = _j / 4;
329
330 _j = (4 * _d + 3) / 1461;
331 _d = 4 * _d + 3 - 1461 * _j;
332 _d = (_d + 4) / 4;
333
334 _m = (5 * _d - 3) / 153;
335 _d = 5 * _d - 3 - 153 * _m;
336 _d = (_d + 5 ) / 5;
337
338 _y = 100 * _y + _j;
339 if (_m < 10)
340 _m += 3;
341 else {
342 _m -= 9;
343 _y++;
344 }
345
346 _y -= 600000;
347 }
348}

◆ day()

int Date::day ( ) const

Definition at line 204 of file DateTime.cpp.

205{
206 int year, month, day;
208 return day;
209}
int day() const
Definition DateTime.cpp:204
static void convertJulianToGregorian(long uJulianDays, int &_year, int &_month, int &_day)
Definition DateTime.cpp:313
int month() const
Definition DateTime.cpp:197
int year() const
Definition DateTime.cpp:190

◆ dayOfWeek()

int Date::dayOfWeek ( ) const

Definition at line 211 of file DateTime.cpp.

212{
213 return (((__jday + 1) % 7) + 7) % 7 + 1;
214}

◆ dayOfYear()

int Date::dayOfYear ( ) const

Definition at line 216 of file DateTime.cpp.

217{
218 return __jday - convertGregorianToJulian(year(), 1, 1) + 1;
219}

◆ days()

long Date::days ( ) const

Definition at line 74 of file DateTime.cpp.

75{
76 if (__jday <= BC_END)
77 return __jday - BC_END - 1;
78 else if (__jday >= AD_START)
79 return __jday - AD_START + 1;
80 else {
81 __DCL_ASSERT(__jday == 0);
82 return __jday;
83 }
84}

◆ daysInMonth()

int Date::daysInMonth ( ) const

Definition at line 227 of file DateTime.cpp.

228{
229 int year, month, day;
231 if (month == 2 && isLeapYear(year))
232 return 29;
233
234 return __monthDays__[month];
235}
static bool isLeapYear(int _year)
Definition DateTime.cpp:370

◆ daysInYear()

int Date::daysInYear ( ) const

Definition at line 237 of file DateTime.cpp.

238{
239 int year, month, day;
241
242 if (isLeapYear(year))
243 return 366;
244 return 365;
245}

◆ decode()

void Date::decode ( int & _year,
int & _month,
int & _day ) const

Definition at line 185 of file DateTime.cpp.

186{
187 convertJulianToGregorian(__jday, _year, _month, _day);
188}

◆ isLeapYear()

bool Date::isLeapYear ( int _year)
static

Definition at line 370 of file DateTime.cpp.

371{
372 return (_y % 4 == 0 && _y % 100 != 0) || _y % 400 == 0;
373}

◆ isValid()

bool Date::isValid ( int y,
int m,
int d )
static

Definition at line 350 of file DateTime.cpp.

351{
352 if (_y == 0) {
353 // null date
354 if (_m == 0 && _d == 0)
355 return true;
356 }
357 else {
358 if ((-9999 <= _y && _y <= 9999) &&
359 (0 < _m && _m <= 12)
360 && (0 < _d)) {
361 if (_m == 2 && _d == 29)
362 return Date::isLeapYear(_y);
363 else
364 return _d <= __monthDays__[_m];
365 }
366 }
367 return false;
368}

◆ month()

int Date::month ( ) const

Definition at line 197 of file DateTime.cpp.

198{
199 int year, month, day;
201 return month;
202}

◆ operator++() [1/2]

const Date & Date::operator++ ( )

Definition at line 86 of file DateTime.cpp.

87{
88 __DCL_ASSERT(__jday != 0);
89
90 if (__jday == BC_END)
92 else
93 __jday++;
94
95 return *this;
96}

◆ operator++() [2/2]

const Date Date::operator++ ( int )

Definition at line 98 of file DateTime.cpp.

99{
100 __DCL_ASSERT(__jday != 0);
101
102 Date r = *this;
103
104 if (__jday == BC_END)
106 else
107 __jday++;
108
109 return r;
110}
ByteString r
Date()
Definition DateTime.cpp:29

◆ operator+=()

const Date & Date::operator+= ( long _days)

Definition at line 144 of file DateTime.cpp.

145{
146 __DCL_ASSERT(__jday != 0);
147
148 if (__jday <= BC_END) {
149 __jday += _days - BC_END;
150
151 if (__jday <= 0)
152 __jday += BC_END;
153 else
154 __jday += AD_START - 1;
155 }
156 else {
158
159 __jday += _days - AD_START;
160
161 if (__jday >= 0)
162 __jday += AD_START;
163 else
164 __jday += BC_END + 1;
165 }
166
167
168 return *this;
169}

◆ operator--() [1/2]

const Date & Date::operator-- ( )

Definition at line 112 of file DateTime.cpp.

113{
114 __DCL_ASSERT(__jday != 0);
115
116 if (__jday == AD_START)
117 __jday = BC_END;
118 else
119 __jday--;
120
121 return *this;
122}

◆ operator--() [2/2]

const Date Date::operator-- ( int )

Definition at line 124 of file DateTime.cpp.

125{
126 __DCL_ASSERT(__jday != 0);
127
128 Date r = *this;
129
130 if (__jday == AD_START)
131 __jday = BC_END;
132 else
133 __jday--;
134
135 return r;
136}

◆ operator-=()

const Date & Date::operator-= ( long _days)

Definition at line 171 of file DateTime.cpp.

172{
173 __DCL_ASSERT(__jday != 0);
174
175 return operator += (-_days);
176}
const Date & operator+=(long _days)
Definition DateTime.cpp:144

◆ operator=()

const Date & Date::operator= ( const Date & _src)

Definition at line 138 of file DateTime.cpp.

139{
140 __jday = _src.__jday;
141 return *this;
142}

◆ toString()

String Date::toString ( ) const

Definition at line 247 of file DateTime.cpp.

248{
249 int year, month, day;
250 decode(year, month, day);
251 return String::format(L"%04d-%02d-%02d", year, month, day);
252}
void decode(int &_year, int &_month, int &_day) const
Definition DateTime.cpp:185

◆ toStringF()

String Date::toStringF ( const wchar_t * _format = NULL) const

Definition at line 256 of file DateTime.cpp.

257{
258 if (!_format)
259 _format = FORMAT_STRING;
260
261 __DCL_ASSERT(*_format != L'\0');
262
263 int year, month, day;
264 decode(year, month, day);
265
266 struct tm _time;
267 memset(&_time, 0, sizeof(struct tm));
268 _time.tm_year = year - 1900;
269 _time.tm_mon = month - 1;
270 _time.tm_mday = day;
271 _time.tm_isdst = -1;
272
273 CharBuffer* buf = CharBuffer::create(DATETIME_FORMAT_BUFFER_SIZE);
274 size_t n = wcsftime(buf->data(), DATETIME_FORMAT_BUFFER_SIZE, _format, &_time);
275 __DCL_ASSERT(buf->__allocLength >= n);
276 buf->__dataLength = n;
277
278 String r = buf;
279 buf->release();
280 return r;
281}
#define DATETIME_FORMAT_BUFFER_SIZE
Definition DateTime.h:21
ByteBuffer * buf
void CharsetConvertException *size_t n
Definition SQLField.cpp:254
static const wchar_t * FORMAT_STRING
Definition DateTime.h:66

◆ year()

int Date::year ( ) const

Definition at line 190 of file DateTime.cpp.

191{
192 int year, month, day;
194 return year;
195}

Member Data Documentation

◆ __jday

long Date::__jday
protected

Definition at line 77 of file DateTime.h.

◆ FORMAT_STRING

const wchar_t * Date::FORMAT_STRING = L"%Y-%m-%d"
static

Definition at line 66 of file DateTime.h.


The documentation for this class was generated from the following files: