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

#include <DateTime.h>

Public Member Functions

 Date ()
 Date (const Date &src)
 Date (int nYear, int nMonth, int nDay)
void assign (int nYear, int nMonth, int nDay)
 Date (long nDays)
void assign (long nDays)
long days () const
const Dateoperator++ ()
const Date operator++ (int)
const Dateoperator-- ()
const Date operator-- (int)
const Dateoperator= (const Date &src)
const Dateoperator+= (long nDays)
const Dateoperator-= (long nDays)
void decode (int &nYear, int &nMonth, int &nDay) 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 *pszFormat=NULL) const

Static Public Member Functions

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

Static Public Attributes

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

Static Protected Member Functions

static long convertGregorianToJulian (int nYear, int nMonth, int nDay)
static void convertJulianToGregorian (long uJulianDays, int &nYear, int &nMonth, int &nDay)

Protected Attributes

long m_nJDay

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 m_nJDay = 0;
32}
long m_nJDay
Definition DateTime.h:77

◆ Date() [2/4]

Date::Date ( const Date & src)

Definition at line 34 of file DateTime.cpp.

35{
36 m_nJDay = src.m_nJDay;
37}

◆ Date() [3/4]

Date::Date ( int nYear,
int nMonth,
int nDay )

Definition at line 39 of file DateTime.cpp.

40{
41 assign(nYear, nMonth, nDay);
42}
void assign(int nYear, int nMonth, int nDay)
Definition DateTime.cpp:49

◆ Date() [4/4]

Date::Date ( long nDays)

Definition at line 44 of file DateTime.cpp.

45{
46 assign(nDays);
47}

Member Function Documentation

◆ assign() [1/2]

void Date::assign ( int nYear,
int nMonth,
int nDay )

Definition at line 49 of file DateTime.cpp.

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

◆ assign() [2/2]

void Date::assign ( long nDays)

Definition at line 63 of file DateTime.cpp.

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

◆ convertGregorianToJulian()

long Date::convertGregorianToJulian ( int nYear,
int nMonth,
int nDay )
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 & nYear,
int & nMonth,
int & nDay )
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 nYear, nMonth, nDay;
207 convertJulianToGregorian(m_nJDay, nYear, nMonth, nDay);
208 return nDay;
209}
static void convertJulianToGregorian(long uJulianDays, int &nYear, int &nMonth, int &nDay)
Definition DateTime.cpp:313

◆ dayOfWeek()

int Date::dayOfWeek ( ) const

Definition at line 211 of file DateTime.cpp.

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

◆ dayOfYear()

int Date::dayOfYear ( ) const

Definition at line 216 of file DateTime.cpp.

217{
218 return m_nJDay - convertGregorianToJulian(year(), 1, 1) + 1;
219}
int year() const
Definition DateTime.cpp:190

◆ days()

long Date::days ( ) const

Definition at line 74 of file DateTime.cpp.

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

◆ daysInMonth()

int Date::daysInMonth ( ) const

Definition at line 227 of file DateTime.cpp.

228{
229 int nYear, nMonth, nDay;
230 convertJulianToGregorian(m_nJDay, nYear, nMonth, nDay);
231 if (nMonth == 2 && isLeapYear(nYear))
232 return 29;
233
234 return monthDays[nMonth];
235}
static bool isLeapYear(int nYear)
Definition DateTime.cpp:370

◆ daysInYear()

int Date::daysInYear ( ) const

Definition at line 237 of file DateTime.cpp.

238{
239 int nYear, nMonth, nDay;
240 convertJulianToGregorian(m_nJDay, nYear, nMonth, nDay);
241
242 if (isLeapYear(nYear))
243 return 366;
244 return 365;
245}

◆ decode()

void Date::decode ( int & nYear,
int & nMonth,
int & nDay ) const

Definition at line 185 of file DateTime.cpp.

186{
187 convertJulianToGregorian(m_nJDay, nYear, nMonth, nDay);
188}

◆ isLeapYear()

bool Date::isLeapYear ( int nYear)
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 nYear, nMonth, nDay;
200 convertJulianToGregorian(m_nJDay, nYear, nMonth, nDay);
201 return nMonth;
202}

◆ operator++() [1/2]

const Date & Date::operator++ ( )

Definition at line 86 of file DateTime.cpp.

87{
89
90 if (m_nJDay == BC_END)
92 else
93 m_nJDay++;
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(m_nJDay != 0);
101
102 Date dSave = m_nJDay;
103
104 if (m_nJDay == BC_END)
106 else
107 m_nJDay++;
108
109 return dSave;
110}
Date()
Definition DateTime.cpp:29

◆ operator+=()

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

Definition at line 144 of file DateTime.cpp.

145{
146 __DCL_ASSERT(m_nJDay != 0);
147
148 if (m_nJDay <= BC_END) {
149 m_nJDay += nDays - BC_END;
150
151 if (m_nJDay <= 0)
152 m_nJDay += BC_END;
153 else
154 m_nJDay += AD_START - 1;
155 }
156 else {
158
159 m_nJDay += nDays - AD_START;
160
161 if (m_nJDay >= 0)
162 m_nJDay += AD_START;
163 else
164 m_nJDay += 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(m_nJDay != 0);
115
116 if (m_nJDay == AD_START)
117 m_nJDay = BC_END;
118 else
119 m_nJDay--;
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(m_nJDay != 0);
127
128 Date dSave = m_nJDay;
129
130 if (m_nJDay == AD_START)
131 m_nJDay = BC_END;
132 else
133 m_nJDay--;
134
135 return dSave;
136}

◆ operator-=()

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

Definition at line 171 of file DateTime.cpp.

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

◆ operator=()

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

Definition at line 138 of file DateTime.cpp.

139{
140 m_nJDay = src.m_nJDay;
141 return *this;
142}

◆ toString()

String Date::toString ( ) const

Definition at line 247 of file DateTime.cpp.

248{
249 int nYear, nMonth, nDay;
250 decode(nYear, nMonth, nDay);
251 return String::format(L"%04d-%02d-%02d", nYear, nMonth, nDay);
252}
void decode(int &nYear, int &nMonth, int &nDay) const
Definition DateTime.cpp:185

◆ toStringF()

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

Definition at line 256 of file DateTime.cpp.

257{
258 if (!pszFormat)
259 pszFormat = FORMAT_STRING;
260
261 __DCL_ASSERT(*pszFormat != L'\0');
262
263 int nYear, nMonth, nDay;
264 decode(nYear, nMonth, nDay);
265
266 struct tm t;
267 memset(&t, 0, sizeof(struct tm));
268 t.tm_year = nYear - 1900;
269 t.tm_mon = nMonth - 1;
270 t.tm_mday = nDay;
271 t.tm_isdst = -1;
272
273 CharBuffer* buf = CharBuffer::create(DATETIME_FORMAT_BUFFER_SIZE);
274 size_t n = wcsftime(buf->data(), DATETIME_FORMAT_BUFFER_SIZE, pszFormat, &t);
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
ByteString r
ByteBuffer * buf
void CharsetConvertException *size_t n
Definition SQLField.cpp:253
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 nYear, nMonth, nDay;
193 convertJulianToGregorian(m_nJDay, nYear, nMonth, nDay);
194 return nYear;
195}

Member Data Documentation

◆ FORMAT_STRING

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

Definition at line 66 of file DateTime.h.

◆ m_nJDay

long Date::m_nJDay
protected

Definition at line 77 of file DateTime.h.


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