Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

862 lines
24 KiB

  1. #include <urlint.h>
  2. #include <map_kv.h>
  3. #include "coll.hxx"
  4. #include "coletime.hxx"
  5. #include <math.h>
  6. /////////////////////////////////////////////////////////////////////////////
  7. // COleDateTime class HELPER definitions
  8. // Verifies will fail if the needed buffer size is too large
  9. #define MAX_TIME_BUFFER_SIZE 128 // matches that in timecore.cpp
  10. #define MIN_DATE (-657434L) // about year 100
  11. #define MAX_DATE 2958465L // about year 9999
  12. // Half a second, expressed in days
  13. #define HALF_SECOND (1.0/172800.0)
  14. // One-based array of days in year at month start
  15. static int rgMonthDays[13] =
  16. {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365};
  17. static BOOL OleDateFromTm(WORD wYear, WORD wMonth, WORD wDay,
  18. WORD wHour, WORD wMinute, WORD wSecond, DATE& dtDest);
  19. static BOOL TmFromOleDate(DATE dtSrc, struct tm& tmDest);
  20. static void TmConvertToStandardFormat(struct tm& tmSrc);
  21. static double DoubleFromDate(DATE dt);
  22. static DATE DateFromDouble(double dbl);
  23. /////////////////////////////////////////////////////////////////////////////
  24. // COleDateTime class
  25. COleDateTime PASCAL COleDateTime::GetCurrentTime()
  26. {
  27. return COleDateTime(::time(NULL));
  28. }
  29. int COleDateTime::GetYear() const
  30. {
  31. struct tm tmTemp;
  32. if (GetStatus() == valid && TmFromOleDate(m_dt, tmTemp))
  33. return tmTemp.tm_year;
  34. else
  35. return AFX_OLE_DATETIME_ERROR;
  36. }
  37. int COleDateTime::GetMonth() const
  38. {
  39. struct tm tmTemp;
  40. if (GetStatus() == valid && TmFromOleDate(m_dt, tmTemp))
  41. return tmTemp.tm_mon;
  42. else
  43. return AFX_OLE_DATETIME_ERROR;
  44. }
  45. int COleDateTime::GetDay() const
  46. {
  47. struct tm tmTemp;
  48. if (GetStatus() == valid && TmFromOleDate(m_dt, tmTemp))
  49. return tmTemp.tm_mday;
  50. else
  51. return AFX_OLE_DATETIME_ERROR;
  52. }
  53. int COleDateTime::GetHour() const
  54. {
  55. struct tm tmTemp;
  56. if (GetStatus() == valid && TmFromOleDate(m_dt, tmTemp))
  57. return tmTemp.tm_hour;
  58. else
  59. return AFX_OLE_DATETIME_ERROR;
  60. }
  61. int COleDateTime::GetMinute() const
  62. {
  63. struct tm tmTemp;
  64. if (GetStatus() == valid && TmFromOleDate(m_dt, tmTemp))
  65. return tmTemp.tm_min;
  66. else
  67. return AFX_OLE_DATETIME_ERROR;
  68. }
  69. int COleDateTime::GetSecond() const
  70. {
  71. struct tm tmTemp;
  72. if (GetStatus() == valid && TmFromOleDate(m_dt, tmTemp))
  73. return tmTemp.tm_sec;
  74. else
  75. return AFX_OLE_DATETIME_ERROR;
  76. }
  77. int COleDateTime::GetDayOfWeek() const
  78. {
  79. struct tm tmTemp;
  80. if (GetStatus() == valid && TmFromOleDate(m_dt, tmTemp))
  81. return tmTemp.tm_wday;
  82. else
  83. return AFX_OLE_DATETIME_ERROR;
  84. }
  85. int COleDateTime::GetDayOfYear() const
  86. {
  87. struct tm tmTemp;
  88. if (GetStatus() == valid && TmFromOleDate(m_dt, tmTemp))
  89. return tmTemp.tm_yday;
  90. else
  91. return AFX_OLE_DATETIME_ERROR;
  92. }
  93. #ifdef _not_this_
  94. const COleDateTime& COleDateTime::operator=(const VARIANT& varSrc)
  95. {
  96. if (varSrc.vt != VT_DATE)
  97. {
  98. TRY
  99. {
  100. COleVariant varTemp(varSrc);
  101. varTemp.ChangeType(VT_DATE);
  102. m_dt = varTemp.date;
  103. SetStatus(valid);
  104. }
  105. // Catch COleException from ChangeType, but not CMemoryException
  106. CATCH(COleException, e)
  107. {
  108. // Not able to convert VARIANT to DATE
  109. DELETE_EXCEPTION(e);
  110. m_dt = 0;
  111. SetStatus(invalid);
  112. }
  113. END_CATCH
  114. }
  115. else
  116. {
  117. m_dt = varSrc.date;
  118. SetStatus(valid);
  119. }
  120. return *this;
  121. }
  122. #endif //_not_this_
  123. const COleDateTime& COleDateTime::operator=(DATE dtSrc)
  124. {
  125. m_dt = dtSrc;
  126. SetStatus(valid);
  127. return *this;
  128. }
  129. const COleDateTime& COleDateTime::operator=(const time_t& timeSrc)
  130. {
  131. // Convert time_t to struct tm
  132. tm *ptm = localtime(&timeSrc);
  133. if (ptm != NULL)
  134. {
  135. m_status = OleDateFromTm((WORD)ptm->tm_year + 1900,
  136. (WORD)(ptm->tm_mon + 1), (WORD)ptm->tm_mday,
  137. (WORD)ptm->tm_hour, (WORD)ptm->tm_min,
  138. (WORD)ptm->tm_sec, m_dt) ? valid : invalid;
  139. }
  140. else
  141. {
  142. // Local time must have failed (timsSrc before 1/1/70 12am)
  143. SetStatus(invalid);
  144. ASSERT(FALSE);
  145. }
  146. return *this;
  147. }
  148. const COleDateTime& COleDateTime::operator=(const SYSTEMTIME& systimeSrc)
  149. {
  150. m_status = OleDateFromTm(systimeSrc.wYear, systimeSrc.wMonth,
  151. systimeSrc.wDay, systimeSrc.wHour, systimeSrc.wMinute,
  152. systimeSrc.wSecond, m_dt) ? valid : invalid;
  153. return *this;
  154. }
  155. const COleDateTime& COleDateTime::operator=(const FILETIME& filetimeSrc)
  156. {
  157. // Assume UTC FILETIME, so convert to LOCALTIME
  158. FILETIME filetimeLocal;
  159. if (!FileTimeToLocalFileTime( &filetimeSrc, &filetimeLocal))
  160. {
  161. #ifdef _DEBUG
  162. DWORD dwError = GetLastError();
  163. TRACE1("\nFileTimeToLocalFileTime failed. Error = %lu.\n\t", dwError);
  164. #endif // _DEBUG
  165. m_status = invalid;
  166. }
  167. else
  168. {
  169. // Take advantage of SYSTEMTIME -> FILETIME conversion
  170. SYSTEMTIME systime;
  171. m_status = FileTimeToSystemTime(&filetimeLocal, &systime) ?
  172. valid : invalid;
  173. // At this point systime should always be valid, but...
  174. if (GetStatus() == valid)
  175. {
  176. m_status = OleDateFromTm(systime.wYear, systime.wMonth,
  177. systime.wDay, systime.wHour, systime.wMinute,
  178. systime.wSecond, m_dt) ? valid : invalid;
  179. }
  180. }
  181. return *this;
  182. }
  183. BOOL COleDateTime::operator<(const COleDateTime& date) const
  184. {
  185. ASSERT(GetStatus() == valid);
  186. ASSERT(date.GetStatus() == valid);
  187. // Handle negative dates
  188. return DoubleFromDate(m_dt) < DoubleFromDate(date.m_dt);
  189. }
  190. BOOL COleDateTime::operator>(const COleDateTime& date) const
  191. { ASSERT(GetStatus() == valid);
  192. ASSERT(date.GetStatus() == valid);
  193. // Handle negative dates
  194. return DoubleFromDate(m_dt) > DoubleFromDate(date.m_dt);
  195. }
  196. BOOL COleDateTime::operator<=(const COleDateTime& date) const
  197. {
  198. ASSERT(GetStatus() == valid);
  199. ASSERT(date.GetStatus() == valid);
  200. // Handle negative dates
  201. return DoubleFromDate(m_dt) <= DoubleFromDate(date.m_dt);
  202. }
  203. BOOL COleDateTime::operator>=(const COleDateTime& date) const
  204. {
  205. ASSERT(GetStatus() == valid);
  206. ASSERT(date.GetStatus() == valid);
  207. // Handle negative dates
  208. return DoubleFromDate(m_dt) >= DoubleFromDate(date.m_dt);
  209. }
  210. COleDateTime COleDateTime::operator+(const COleDateTimeSpan& dateSpan) const
  211. {
  212. COleDateTime dateResult; // Initializes m_status to valid
  213. // If either operand NULL, result NULL
  214. if (GetStatus() == null || dateSpan.GetStatus() == null)
  215. {
  216. dateResult.SetStatus(null);
  217. return dateResult;
  218. }
  219. // If either operand invalid, result invalid
  220. if (GetStatus() == invalid || dateSpan.GetStatus() == invalid)
  221. {
  222. dateResult.SetStatus(invalid);
  223. return dateResult;
  224. }
  225. // Compute the actual date difference by adding underlying dates
  226. dateResult = DateFromDouble(DoubleFromDate(m_dt) + dateSpan.m_span);
  227. // Validate within range
  228. dateResult.CheckRange();
  229. return dateResult;
  230. }
  231. COleDateTime COleDateTime::operator-(const COleDateTimeSpan& dateSpan) const
  232. {
  233. COleDateTime dateResult; // Initializes m_status to valid
  234. // If either operand NULL, result NULL
  235. if (GetStatus() == null || dateSpan.GetStatus() == null)
  236. {
  237. dateResult.SetStatus(null);
  238. return dateResult;
  239. }
  240. // If either operand invalid, result invalid
  241. if (GetStatus() == invalid || dateSpan.GetStatus() == invalid)
  242. {
  243. dateResult.SetStatus(invalid);
  244. return dateResult;
  245. }
  246. // Compute the actual date difference by subtracting underlying dates
  247. dateResult = DateFromDouble(DoubleFromDate(m_dt) - dateSpan.m_span);
  248. // Validate within range
  249. dateResult.CheckRange();
  250. return dateResult;
  251. }
  252. COleDateTimeSpan COleDateTime::operator-(const COleDateTime& date) const
  253. {
  254. COleDateTimeSpan spanResult;
  255. // If either operand NULL, result NULL
  256. if (GetStatus() == null || date.GetStatus() == null)
  257. {
  258. spanResult.SetStatus(COleDateTimeSpan::null);
  259. return spanResult;
  260. }
  261. // If either operand invalid, result invalid
  262. if (GetStatus() == invalid || date.GetStatus() == invalid)
  263. {
  264. spanResult.SetStatus(COleDateTimeSpan::invalid);
  265. return spanResult;
  266. }
  267. // Return result (span can't be invalid, so don't check range)
  268. return DoubleFromDate(m_dt) - DoubleFromDate(date.m_dt);
  269. }
  270. BOOL COleDateTime::SetDateTime(int nYear, int nMonth, int nDay,
  271. int nHour, int nMin, int nSec)
  272. {
  273. return m_status = OleDateFromTm((WORD)nYear, (WORD)nMonth,
  274. (WORD)nDay, (WORD)nHour, (WORD)nMin, (WORD)nSec, m_dt) ?
  275. valid : invalid;
  276. }
  277. #ifdef _not_yet_
  278. BOOL COleDateTime::ParseDateTime(LPCTSTR lpszDate, DWORD dwFlags, LCID lcid)
  279. {
  280. USES_CONVERSION;
  281. CString strDate = lpszDate;
  282. SCODE sc;
  283. if (FAILED(sc = VarDateFromStr((LPOLESTR)T2COLE(strDate), lcid,
  284. dwFlags, &m_dt)))
  285. {
  286. if (sc == DISP_E_TYPEMISMATCH)
  287. {
  288. // Can't convert string to date, set 0 and invalidate
  289. m_dt = 0;
  290. SetStatus(invalid);
  291. return FALSE;
  292. }
  293. else if (sc == DISP_E_OVERFLOW)
  294. {
  295. // Can't convert string to date, set -1 and invalidate
  296. m_dt = -1;
  297. SetStatus(invalid);
  298. return FALSE;
  299. }
  300. else
  301. {
  302. TRACE0("\nCOleDateTime VarDateFromStr call failed.\n\t");
  303. if (sc == E_OUTOFMEMORY)
  304. AfxThrowMemoryException();
  305. else
  306. AfxThrowOleException(sc);
  307. }
  308. }
  309. SetStatus(valid);
  310. return TRUE;
  311. }
  312. CString COleDateTime::Format(DWORD dwFlags, LCID lcid) const
  313. {
  314. USES_CONVERSION;
  315. CString strDate;
  316. // If null, return empty string
  317. if (GetStatus() == null)
  318. return strDate;
  319. // If invalid, return DateTime resource string
  320. if (GetStatus() == invalid)
  321. {
  322. VERIFY(strDate.LoadString(AFX_IDS_INVALID_DATETIME));
  323. return strDate;
  324. }
  325. COleVariant var;
  326. // Don't need to trap error. Should not fail due to type mismatch
  327. CheckError(VarBstrFromDate(m_dt, lcid, dwFlags, &V_BSTR(&var)));
  328. var.vt = VT_BSTR;
  329. return OLE2CT(V_BSTR(&var));
  330. }
  331. #endif //_not_yet_
  332. CString COleDateTime::Format(LPCTSTR pFormat) const
  333. {
  334. CString strDate;
  335. struct tm tmTemp;
  336. // If null, return empty string
  337. if (GetStatus() == null)
  338. return strDate;
  339. // If invalid, return DateTime resource string
  340. if (GetStatus() == invalid || !TmFromOleDate(m_dt, tmTemp))
  341. {
  342. //VERIFY(strDate.LoadString(AFX_IDS_INVALID_DATETIME));
  343. return strDate;
  344. }
  345. // Convert tm from afx internal format to standard format
  346. TmConvertToStandardFormat(tmTemp);
  347. // Fill in the buffer, disregard return value as it's not necessary
  348. LPTSTR lpszTemp = strDate.GetBufferSetLength(MAX_TIME_BUFFER_SIZE);
  349. _tcsftime(lpszTemp, strDate.GetLength(), pFormat, &tmTemp);
  350. strDate.ReleaseBuffer();
  351. return strDate;
  352. }
  353. CString COleDateTime::Format(UINT nFormatID) const
  354. {
  355. CString strFormat;
  356. //VERIFY(strFormat.LoadString(nFormatID) != 0);
  357. return Format(strFormat);
  358. }
  359. void COleDateTime::CheckRange()
  360. {
  361. if (m_dt > MAX_DATE || m_dt < MIN_DATE) // about year 100 to about 9999
  362. SetStatus(invalid);
  363. }
  364. // serialization
  365. #ifdef _DEBUG
  366. CDumpContext& AFXAPI operator<<(CDumpContext& dc, COleDateTime dateSrc)
  367. {
  368. dc << "\nCOleDateTime Object:";
  369. dc << "\n\tm_status = " << (long)dateSrc.m_status;
  370. COleVariant var(dateSrc);
  371. var.ChangeType(VT_BSTR);
  372. return dc << "\n\tdate = " << (LPCTSTR)var.bstrVal;
  373. }
  374. #endif // _DEBUG
  375. #ifdef _not_yet_
  376. CArchive& AFXAPI operator<<(CArchive& ar, COleDateTime dateSrc)
  377. {
  378. ar << (long)dateSrc.m_status;
  379. return ar << dateSrc.m_dt;
  380. }
  381. CArchive& AFXAPI operator>>(CArchive& ar, COleDateTime& dateSrc)
  382. {
  383. ar >> (long&)dateSrc.m_status;
  384. return ar >> dateSrc.m_dt;
  385. }
  386. #endif //_not_yet_
  387. /////////////////////////////////////////////////////////////////////////////
  388. // COleDateTimeSpan class helpers
  389. #define MAX_DAYS_IN_SPAN 3615897L
  390. /////////////////////////////////////////////////////////////////////////////
  391. // COleDateTimeSpan class
  392. long COleDateTimeSpan::GetHours() const
  393. {
  394. ASSERT(GetStatus() == valid);
  395. double dblTemp;
  396. // Truncate days and scale up
  397. dblTemp = modf(m_span, &dblTemp);
  398. return (long)(dblTemp * 24);
  399. }
  400. long COleDateTimeSpan::GetMinutes() const
  401. {
  402. ASSERT(GetStatus() == valid);
  403. double dblTemp;
  404. // Truncate hours and scale up
  405. dblTemp = modf(m_span * 24, &dblTemp);
  406. return (long)(dblTemp * 60);
  407. }
  408. long COleDateTimeSpan::GetSeconds() const
  409. {
  410. ASSERT(GetStatus() == valid);
  411. double dblTemp;
  412. // Truncate minutes and scale up
  413. dblTemp = modf(m_span * 24 * 60, &dblTemp);
  414. return (long)(dblTemp * 60);
  415. }
  416. const COleDateTimeSpan& COleDateTimeSpan::operator=(double dblSpanSrc)
  417. {
  418. m_span = dblSpanSrc;
  419. SetStatus(valid);
  420. return *this;
  421. }
  422. const COleDateTimeSpan& COleDateTimeSpan::operator=(const COleDateTimeSpan& dateSpanSrc)
  423. {
  424. m_span = dateSpanSrc.m_span;
  425. m_status = dateSpanSrc.m_status;
  426. return *this;
  427. }
  428. COleDateTimeSpan COleDateTimeSpan::operator+(const COleDateTimeSpan& dateSpan) const
  429. {
  430. COleDateTimeSpan dateSpanTemp;
  431. // If either operand Null, result Null
  432. if (GetStatus() == null || dateSpan.GetStatus() == null)
  433. {
  434. dateSpanTemp.SetStatus(null);
  435. return dateSpanTemp;
  436. }
  437. // If either operand Invalid, result Invalid
  438. if (GetStatus() == invalid || dateSpan.GetStatus() == invalid)
  439. {
  440. dateSpanTemp.SetStatus(invalid);
  441. return dateSpanTemp;
  442. }
  443. // Add spans and validate within legal range
  444. dateSpanTemp.m_span = m_span + dateSpan.m_span;
  445. dateSpanTemp.CheckRange();
  446. return dateSpanTemp;
  447. }
  448. COleDateTimeSpan COleDateTimeSpan::operator-(const COleDateTimeSpan& dateSpan) const
  449. {
  450. COleDateTimeSpan dateSpanTemp;
  451. // If either operand Null, result Null
  452. if (GetStatus() == null || dateSpan.GetStatus() == null)
  453. {
  454. dateSpanTemp.SetStatus(null);
  455. return dateSpanTemp;
  456. }
  457. // If either operand Invalid, result Invalid
  458. if (GetStatus() == invalid || dateSpan.GetStatus() == invalid)
  459. {
  460. dateSpanTemp.SetStatus(invalid);
  461. return dateSpanTemp;
  462. }
  463. // Subtract spans and validate within legal range
  464. dateSpanTemp.m_span = m_span - dateSpan.m_span;
  465. dateSpanTemp.CheckRange();
  466. return dateSpanTemp;
  467. }
  468. void COleDateTimeSpan::SetDateTimeSpan(
  469. long lDays, int nHours, int nMins, int nSecs)
  470. {
  471. // Set date span by breaking into fractional days (all input ranges valid)
  472. m_span = lDays + ((double)nHours)/24 + ((double)nMins)/(24*60) +
  473. ((double)nSecs)/(24*60*60);
  474. SetStatus(valid);
  475. }
  476. CString COleDateTimeSpan::Format(LPCTSTR pFormat) const
  477. {
  478. CString strSpan;
  479. struct tm tmTemp;
  480. // If null, return empty string
  481. if (GetStatus() == null)
  482. return strSpan;
  483. // If invalid, return DateTimeSpan resource string
  484. if (GetStatus() == invalid || !TmFromOleDate(m_span, tmTemp))
  485. {
  486. //VERIFY(strSpan.LoadString(AFX_IDS_INVALID_DATETIMESPAN));
  487. return strSpan;
  488. }
  489. // Convert tm from afx internal format to standard format
  490. TmConvertToStandardFormat(tmTemp);
  491. // Fill in the buffer, disregard return value as it's not necessary
  492. LPTSTR lpszTemp = strSpan.GetBufferSetLength(MAX_TIME_BUFFER_SIZE);
  493. _tcsftime(lpszTemp, strSpan.GetLength(), pFormat, &tmTemp);
  494. strSpan.ReleaseBuffer();
  495. return strSpan;
  496. }
  497. CString COleDateTimeSpan::Format(UINT nFormatID) const
  498. {
  499. CString strFormat;
  500. //VERIFY(strFormat.LoadString(nFormatID) != 0);
  501. return Format(strFormat);
  502. }
  503. void COleDateTimeSpan::CheckRange()
  504. {
  505. if (m_span < -MAX_DAYS_IN_SPAN || m_span > MAX_DAYS_IN_SPAN)
  506. SetStatus(invalid);
  507. }
  508. // serialization
  509. #ifdef _DEBUG
  510. CDumpContext& AFXAPI operator<<(CDumpContext& dc, COleDateTimeSpan dateSpanSrc)
  511. {
  512. dc << "\nCOleDateTimeSpan Object:";
  513. dc << "\n\tm_status = " << (long)dateSpanSrc.m_status;
  514. COleVariant var(dateSpanSrc.m_span);
  515. var.ChangeType(VT_BSTR);
  516. return dc << "\n\tdateSpan = " << (LPCTSTR)var.bstrVal;
  517. }
  518. #endif // _DEBUG
  519. #ifdef _not_yet_
  520. CArchive& AFXAPI operator<<(CArchive& ar, COleDateTimeSpan dateSpanSrc)
  521. {
  522. ar << (long)dateSpanSrc.m_status;
  523. return ar << dateSpanSrc.m_span;
  524. }
  525. CArchive& AFXAPI operator>>(CArchive& ar, COleDateTimeSpan& dateSpanSrc)
  526. {
  527. ar >> (long&)dateSpanSrc.m_status;
  528. return ar >> dateSpanSrc.m_span;
  529. }
  530. #endif //_not_yet_
  531. /////////////////////////////////////////////////////////////////////////////
  532. // COleDateTime class HELPERS - implementation
  533. BOOL OleDateFromTm(WORD wYear, WORD wMonth, WORD wDay,
  534. WORD wHour, WORD wMinute, WORD wSecond, DATE& dtDest)
  535. {
  536. // Validate year and month (ignore day of week and milliseconds)
  537. if (wYear > 9999 || wMonth < 1 || wMonth > 12)
  538. return FALSE;
  539. // Check for leap year and set the number of days in the month
  540. BOOL bLeapYear = ((wYear & 3) == 0) &&
  541. ((wYear % 100) != 0 || (wYear % 400) == 0);
  542. int nDaysInMonth =
  543. rgMonthDays[wMonth] - rgMonthDays[wMonth-1] +
  544. ((bLeapYear && wDay == 29 && wMonth == 2) ? 1 : 0);
  545. // Finish validating the date
  546. if (wDay < 1 || wDay > nDaysInMonth ||
  547. wHour > 23 || wMinute > 59 ||
  548. wSecond > 59)
  549. {
  550. return FALSE;
  551. }
  552. // Cache the date in days and time in fractional days
  553. long nDate;
  554. double dblTime;
  555. //It is a valid date; make Jan 1, 1AD be 1
  556. nDate = wYear*365L + wYear/4 - wYear/100 + wYear/400 +
  557. rgMonthDays[wMonth-1] + wDay;
  558. // If leap year and it's before March, subtract 1:
  559. if (wMonth <= 2 && bLeapYear)
  560. --nDate;
  561. // Offset so that 12/30/1899 is 0
  562. nDate -= 693959L;
  563. dblTime = (((long)wHour * 3600L) + // hrs in seconds
  564. ((long)wMinute * 60L) + // mins in seconds
  565. ((long)wSecond)) / 86400.;
  566. dtDest = (double) nDate + ((nDate >= 0) ? dblTime : -dblTime);
  567. return TRUE;
  568. }
  569. BOOL TmFromOleDate(DATE dtSrc, struct tm& tmDest)
  570. {
  571. // The legal range does not actually span year 0 to 9999.
  572. if (dtSrc > MAX_DATE || dtSrc < MIN_DATE) // about year 100 to about 9999
  573. return FALSE;
  574. long nDays; // Number of days since Dec. 30, 1899
  575. long nDaysAbsolute; // Number of days since 1/1/0
  576. long nSecsInDay; // Time in seconds since midnight
  577. long nMinutesInDay; // Minutes in day
  578. long n400Years; // Number of 400 year increments since 1/1/0
  579. long n400Century; // Century within 400 year block (0,1,2 or 3)
  580. long n4Years; // Number of 4 year increments since 1/1/0
  581. long n4Day; // Day within 4 year block
  582. // (0 is 1/1/yr1, 1460 is 12/31/yr4)
  583. long n4Yr; // Year within 4 year block (0,1,2 or 3)
  584. BOOL bLeap4 = TRUE; // TRUE if 4 year block includes leap year
  585. double dblDate = dtSrc; // tempory serial date
  586. // If a valid date, then this conversion should not overflow
  587. nDays = (long)dblDate;
  588. // Round to the second
  589. dblDate += ((dtSrc > 0.0) ? HALF_SECOND : -HALF_SECOND);
  590. nDaysAbsolute = (long)dblDate + 693959L; // Add days from 1/1/0 to 12/30/1899
  591. dblDate = fabs(dblDate);
  592. nSecsInDay = (long)((dblDate - floor(dblDate)) * 86400.);
  593. // Calculate the day of week (sun=1, mon=2...)
  594. // -1 because 1/1/0 is Sat. +1 because we want 1-based
  595. tmDest.tm_wday = (int)((nDaysAbsolute - 1) % 7L) + 1;
  596. // Leap years every 4 yrs except centuries not multiples of 400.
  597. n400Years = (long)(nDaysAbsolute / 146097L);
  598. // Set nDaysAbsolute to day within 400-year block
  599. nDaysAbsolute %= 146097L;
  600. // -1 because first century has extra day
  601. n400Century = (long)((nDaysAbsolute - 1) / 36524L);
  602. // Non-leap century
  603. if (n400Century != 0)
  604. {
  605. // Set nDaysAbsolute to day within century
  606. nDaysAbsolute = (nDaysAbsolute - 1) % 36524L;
  607. // +1 because 1st 4 year increment has 1460 days
  608. n4Years = (long)((nDaysAbsolute + 1) / 1461L);
  609. if (n4Years != 0)
  610. n4Day = (long)((nDaysAbsolute + 1) % 1461L);
  611. else
  612. {
  613. bLeap4 = FALSE;
  614. n4Day = (long)nDaysAbsolute;
  615. }
  616. }
  617. else
  618. {
  619. // Leap century - not special case!
  620. n4Years = (long)(nDaysAbsolute / 1461L);
  621. n4Day = (long)(nDaysAbsolute % 1461L);
  622. }
  623. if (bLeap4)
  624. {
  625. // -1 because first year has 366 days
  626. n4Yr = (n4Day - 1) / 365;
  627. if (n4Yr != 0)
  628. n4Day = (n4Day - 1) % 365;
  629. }
  630. else
  631. {
  632. n4Yr = n4Day / 365;
  633. n4Day %= 365;
  634. }
  635. // n4Day is now 0-based day of year. Save 1-based day of year, year number
  636. tmDest.tm_yday = (int)n4Day + 1;
  637. tmDest.tm_year = n400Years * 400 + n400Century * 100 + n4Years * 4 + n4Yr;
  638. // Handle leap year: before, on, and after Feb. 29.
  639. if (n4Yr == 0 && bLeap4)
  640. {
  641. // Leap Year
  642. if (n4Day == 59)
  643. {
  644. /* Feb. 29 */
  645. tmDest.tm_mon = 2;
  646. tmDest.tm_mday = 29;
  647. goto DoTime;
  648. }
  649. // Pretend it's not a leap year for month/day comp.
  650. if (n4Day >= 60)
  651. --n4Day;
  652. }
  653. // Make n4DaY a 1-based day of non-leap year and compute
  654. // month/day for everything but Feb. 29.
  655. ++n4Day;
  656. // Month number always >= n/32, so save some loop time */
  657. for (tmDest.tm_mon = (n4Day >> 5) + 1;
  658. n4Day > rgMonthDays[tmDest.tm_mon]; tmDest.tm_mon++);
  659. tmDest.tm_mday = (int)(n4Day - rgMonthDays[tmDest.tm_mon-1]);
  660. DoTime:
  661. if (nSecsInDay == 0)
  662. tmDest.tm_hour = tmDest.tm_min = tmDest.tm_sec = 0;
  663. else
  664. {
  665. tmDest.tm_sec = (int)nSecsInDay % 60L;
  666. nMinutesInDay = nSecsInDay / 60L;
  667. tmDest.tm_min = (int)nMinutesInDay % 60;
  668. tmDest.tm_hour = (int)nMinutesInDay / 60;
  669. }
  670. return TRUE;
  671. }
  672. void TmConvertToStandardFormat(struct tm& tmSrc)
  673. {
  674. // Convert afx internal tm to format expected by runtimes (_tcsftime, etc)
  675. tmSrc.tm_year -= 1900; // year is based on 1900
  676. tmSrc.tm_mon -= 1; // month of year is 0-based
  677. tmSrc.tm_wday -= 1; // day of week is 0-based
  678. tmSrc.tm_yday -= 1; // day of year is 0-based
  679. }
  680. double DoubleFromDate(DATE dt)
  681. {
  682. // No problem if positive
  683. if (dt >= 0)
  684. return dt;
  685. // If negative, must convert since negative dates not continuous
  686. // (examples: -1.25 to -.75, -1.50 to -.50, -1.75 to -.25)
  687. double temp = ceil(dt);
  688. return temp - (dt - temp);
  689. }
  690. DATE DateFromDouble(double dbl)
  691. {
  692. // No problem if positive
  693. if (dbl >= 0)
  694. return dbl;
  695. // If negative, must convert since negative dates not continuous
  696. // (examples: -.75 to -1.25, -.50 to -1.50, -.25 to -1.75)
  697. double temp = floor(dbl); // dbl is now whole part
  698. return temp + (temp - dbl);
  699. }