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.

689 lines
16 KiB

  1. /*
  2. * NOTES:
  3. *
  4. * REVISIONS:
  5. * pcy10Mar95: Initial revision
  6. * pav22Sep95: Fixed leap year check
  7. * djs11Sep95: Port to UNIX
  8. * ntf03Jan96: Added operator<< functionality so that datetime.cxx would compile
  9. * jps20Nov96: Added adjustment for >2000 (+= 100) in DateObj constr.
  10. * tjg22Nov97: Added adjustment Year 2000 adjustment for leap year calculation
  11. * tjg27Mar98: Year adjustment must happen before call to GetDaysInMonth
  12. *
  13. * v-stebe 29Jul2000 added check for NULL in constructor (bug #46328)
  14. */
  15. #ifdef APCDEBUG
  16. #include <iostream.h>
  17. #endif
  18. #include "cdefine.h"
  19. #include "apc.h"
  20. extern "C" {
  21. #include <time.h>
  22. #include <stdio.h>
  23. }
  24. #if (C_OS & C_UNIX)
  25. #include "utils.h"
  26. #endif
  27. #if (C_OS & C_SUNOS4)
  28. #include "incfix.h"
  29. #endif
  30. #include "datetime.h"
  31. INT NumberOfDays[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
  32. /*
  33. Name :TimeObj
  34. Synop :This constructor provides a default of the current time
  35. */
  36. TimeObj::TimeObj ()
  37. {
  38. time_t curr = time(0);
  39. struct tm *currTime = localtime(&curr);
  40. theHours = currTime->tm_hour;
  41. theMinutes = currTime->tm_min;
  42. theSeconds = currTime->tm_sec;
  43. }
  44. TimeObj::TimeObj (PCHAR aTime)
  45. {
  46. INT hour;
  47. INT minute;
  48. CHAR am_or_pm[16];
  49. am_or_pm[0] = 0;
  50. if (sscanf(aTime,"%d:%d %s", &hour, &minute, am_or_pm) == 3) {
  51. theHours = hour;
  52. if ((_strcmpi(am_or_pm, "PM") == 0) && (hour < 12)) {
  53. theHours += 12;
  54. }
  55. else if ((_strcmpi(am_or_pm, "AM") == 0) && (hour == 12)) {
  56. theHours = 0;
  57. }
  58. theMinutes = minute;
  59. theSeconds = 0;
  60. }
  61. else {
  62. theHours = 0;
  63. theMinutes = 0;
  64. theSeconds = 0;
  65. }
  66. }
  67. INT TimeObj:: Equal(PTimeObj atime)
  68. {
  69. if ( (theHours == atime->theHours) &&
  70. (theMinutes == atime->theMinutes) &&
  71. (theSeconds == atime->theSeconds) )
  72. return TRUE;
  73. return FALSE;
  74. }
  75. INT TimeObj::operator < (RTimeObj cmp) const
  76. {
  77. INT ret = FALSE;
  78. if (GetHour() < cmp.GetHour()) {
  79. ret = TRUE;
  80. }
  81. else if(GetHour() == cmp.GetHour() &&
  82. GetMinutes() < cmp.GetMinutes()) {
  83. ret = TRUE;
  84. }
  85. else if(GetHour() == cmp.GetHour() &&
  86. GetMinutes() == cmp.GetMinutes() &&
  87. GetSeconds() < cmp.GetSeconds()) {
  88. ret = TRUE;
  89. }
  90. return ret;
  91. }
  92. #if (C_OS & C_WIN311)
  93. TimeObj::TimeObj(INT aHours, INT aMinutes, INT aSeconds)
  94. {
  95. if ( ((aHours < 0) || (aHours > 23)) ||
  96. ((aMinutes < 0) || (aMinutes > 59)) ||
  97. ((aSeconds < 0) || (aSeconds > 59))) {
  98. SetObjectStatus(ErrINVALID_DATE);
  99. }
  100. #else
  101. TimeObj::TimeObj(ULONG aHours, ULONG aMinutes, ULONG aSeconds)
  102. {
  103. if ( (aHours > 23) ||
  104. (aMinutes > 59) ||
  105. (aSeconds > 59)) {
  106. SetObjectStatus(ErrINVALID_DATE);
  107. }
  108. #endif
  109. else {
  110. theHours = aHours;
  111. theMinutes = aMinutes;
  112. theSeconds = aSeconds;
  113. }
  114. };
  115. #if (C_OS & C_WIN311)
  116. INT TimeObj::GetHour() const
  117. #else
  118. ULONG TimeObj::GetHour() const
  119. #endif
  120. {
  121. return theHours;
  122. };
  123. #if (C_OS & C_WIN311)
  124. INT TimeObj::GetMinutes() const
  125. #else
  126. ULONG TimeObj::GetMinutes() const
  127. #endif
  128. {
  129. return theMinutes;
  130. };
  131. #if (C_OS & C_WIN311)
  132. INT TimeObj::GetSeconds() const
  133. #else
  134. ULONG TimeObj::GetSeconds() const
  135. #endif
  136. {
  137. return theSeconds;
  138. };
  139. #if (C_OS & C_WIN311)
  140. VOID TimeObj::SetHour(INT hour)
  141. #else
  142. VOID TimeObj::SetHour(ULONG hour)
  143. #endif
  144. {
  145. theHours = hour;
  146. };
  147. #if (C_OS & C_WIN311)
  148. VOID TimeObj::SetMinutes(INT minutes)
  149. #else
  150. VOID TimeObj::SetMinutes(ULONG minutes)
  151. #endif
  152. {
  153. theMinutes = minutes;
  154. };
  155. #if (C_OS & C_WIN311)
  156. VOID TimeObj::SetSeconds(INT seconds)
  157. #else
  158. VOID TimeObj::SetSeconds(ULONG seconds)
  159. #endif
  160. {
  161. theSeconds = seconds;
  162. };
  163. WeekObj::WeekObj()
  164. {
  165. time_t curr = time(0);
  166. struct tm *currTime = localtime(&curr);
  167. theWeekDay = currTime->tm_wday;
  168. }
  169. WeekObj::WeekObj(PCHAR weekStr)
  170. {
  171. if (_strcmpi(weekStr, "SUNDAY") == 0)
  172. theWeekDay = 0;
  173. else if (_strcmpi(weekStr, "MONDAY") == 0)
  174. theWeekDay = 1;
  175. else if (_strcmpi(weekStr, "TUESDAY") == 0)
  176. theWeekDay = 2;
  177. else if (_strcmpi(weekStr, "WEDNESDAY") == 0)
  178. theWeekDay = 3;
  179. else if (_strcmpi(weekStr, "THURSDAY") == 0)
  180. theWeekDay = 4;
  181. else if (_strcmpi(weekStr, "FRIDAY") == 0)
  182. theWeekDay = 5;
  183. else if (_strcmpi(weekStr, "SATURDAY") == 0)
  184. theWeekDay = 6;
  185. else {
  186. SetObjectStatus(ErrINVALID_DATE);
  187. theWeekDay = 0;
  188. }
  189. }
  190. #if (C_OS & C_WIN311)
  191. WeekObj::WeekObj(INT aDay)
  192. #else
  193. WeekObj::WeekObj(ULONG aDay)
  194. #endif
  195. {
  196. theWeekDay = aDay;
  197. };
  198. #if (C_OS & C_WIN311)
  199. INT WeekObj::GetWeekDay()
  200. #else
  201. ULONG WeekObj::GetWeekDay()
  202. #endif
  203. {
  204. return theWeekDay;
  205. };
  206. #if (C_OS & C_WIN311)
  207. DateObj::DateObj(INT aMonth, INT aDay, INT aYear)
  208. {
  209. // Y2K fix for the year ... 2000 will be represented by 00 and
  210. // must be incremented. NOTE: This must happen before the
  211. // call to GetDaysInMonth!
  212. if (aYear < 70) {
  213. aYear += 100;
  214. }
  215. INT days = GetDaysInMonth(aMonth, aYear);
  216. if (((aMonth < 0) || (aMonth > 12)) ||
  217. ((aDay < 0) || (aDay > days)) ||
  218. (aYear < 0)) {
  219. SetObjectStatus (ErrINVALID_DATE);
  220. }
  221. #else
  222. DateObj::DateObj(ULONG aMonth, ULONG aDay, ULONG aYear)
  223. {
  224. // Y2K fix for the year ... 2000 will be represented by 00 and
  225. // must be incremented. NOTE: This must happen before the
  226. // call to GetDaysInMonth!
  227. if (aYear < 70) {
  228. aYear += 100;
  229. }
  230. ULONG days = GetDaysInMonth(aMonth, aYear);
  231. if ((aMonth > 12) || (aDay > days) ) {
  232. SetObjectStatus (ErrINVALID_DATE);
  233. }
  234. #endif
  235. else {
  236. theMonth = aMonth;
  237. theDay = aDay;
  238. theYear = aYear;
  239. }
  240. };
  241. /*
  242. C+
  243. Name :DateObj
  244. Synop :This constructor provides a default of the current date
  245. */
  246. DateObj::DateObj ()
  247. {
  248. time_t curr = time(0);
  249. struct tm *currTime = localtime(&curr);
  250. theMonth = currTime->tm_mon + 1;
  251. theDay = currTime->tm_mday;
  252. theYear = currTime->tm_year;
  253. }
  254. #if (C_OS & C_WIN311)
  255. INT DateObj::GetDaysInMonth(const INT aMonth, const INT aYear) const
  256. {
  257. INT ret = NumberOfDays[aMonth-1];
  258. #else
  259. ULONG DateObj::GetDaysInMonth(const ULONG aMonth, const ULONG aYear) const
  260. {
  261. ULONG ret = NumberOfDays[aMonth-1];
  262. #endif
  263. // Need temp_year variable to convert the year back to 4-digits.
  264. // If not, the leap year check below will fail on 2000 (2000 == 100
  265. // with our Y2K fixes).
  266. INT temp_year = aYear + 1900;
  267. if ((((temp_year % 4 == 0) && (temp_year % 100 != 0)) || (temp_year % 400 == 0)) &&
  268. (aMonth == 2)) {
  269. ret++;
  270. }
  271. return ret;
  272. }
  273. INT DateObj::operator == (RDateObj cmp) const
  274. {
  275. INT ret = FALSE;
  276. if ((GetYear() == cmp.GetYear()) &&
  277. (GetMonth() == cmp.GetMonth()) &&
  278. (GetDay() == cmp.GetDay())) {
  279. ret = TRUE;
  280. }
  281. return ret;
  282. }
  283. INT DateObj::operator < (RDateObj cmp) const
  284. {
  285. INT ret = FALSE;
  286. if (GetYear() < cmp.GetYear()) {
  287. ret = TRUE;
  288. }
  289. else if (GetYear() == cmp.GetYear()) {
  290. if (GetMonth() < cmp.GetMonth()) {
  291. ret = TRUE;
  292. }
  293. else if (GetMonth() == cmp.GetMonth()) {
  294. if (GetDay() < cmp.GetDay()) {
  295. ret = TRUE;
  296. }
  297. }
  298. }
  299. return ret;
  300. }
  301. // Return days between this and cmp
  302. //
  303. LONG DateObj::operator - (RDateObj cmp) const
  304. {
  305. RDateObj early = cmp, late = (RDateObj)(*this);
  306. LONG ret = 0;
  307. #if (C_OS & C_WIN311)
  308. // in following code typecast the returns from GetMonth()... etc. to LONG for compatibility
  309. // with 32 bit code.
  310. INT year, month;
  311. #else
  312. ULONG year, month;
  313. #endif
  314. if ((RDateObj)(*this) < cmp) {
  315. return ((early - late) * (-1));
  316. }
  317. if (early.GetYear() == late.GetYear()) {
  318. if (early.GetMonth() == late.GetMonth()) {
  319. #if (C_OS & C_WIN311)
  320. ret += (LONG)(late.GetDay() - early.GetDay());
  321. #else
  322. ret += (late.GetDay() - early.GetDay());
  323. #endif
  324. }
  325. else {
  326. #if (C_OS & C_WIN311)
  327. ret += (LONG)(early.GetDaysInMonth() - early.GetDay());
  328. #else
  329. ret += (early.GetDaysInMonth() - early.GetDay());
  330. #endif
  331. for (month=early.GetMonth()+1; month<late.GetMonth(); month++) {
  332. #if (C_OS & C_WIN311)
  333. ret += (LONG)GetDaysInMonth(month, GetYear());
  334. #else
  335. ret += GetDaysInMonth(month, GetYear());
  336. #endif
  337. }
  338. #if (C_OS & C_WIN311)
  339. ret += (LONG)late.GetDay();
  340. #else
  341. ret += late.GetDay();
  342. #endif
  343. }
  344. }
  345. else {
  346. #if (C_OS & C_WIN311)
  347. ret += (LONG)(early.GetDaysInMonth() - early.GetDay());
  348. #else
  349. ret += (early.GetDaysInMonth() - early.GetDay());
  350. #endif
  351. for (month=early.GetMonth()+1; month<=12; month++) {
  352. #if (C_OS & C_WIN311)
  353. ret += (LONG)GetDaysInMonth(month, early.GetYear());
  354. #else
  355. ret += GetDaysInMonth(month, early.GetYear());
  356. #endif
  357. }
  358. for (year=early.GetYear()+1; year<late.GetYear(); year++) {
  359. for (month=1; month<=12; month++) {
  360. #if (C_OS & C_WIN311)
  361. ret += (LONG)GetDaysInMonth(month, year);
  362. #else
  363. ret += GetDaysInMonth(month, year);
  364. #endif
  365. }
  366. }
  367. for (month=1; month < late.GetMonth(); month++) {
  368. #if (C_OS & C_WIN311)
  369. ret += (LONG)GetDaysInMonth(month, late.GetYear());
  370. #else
  371. ret += GetDaysInMonth(month, late.GetYear());
  372. #endif
  373. }
  374. #if (C_OS & C_WIN311)
  375. ret += (LONG)late.GetDay();
  376. #else
  377. ret += late.GetDay();
  378. #endif
  379. }
  380. return ret;
  381. }
  382. #if (C_OS & C_WIN311)
  383. INT DateObj::GetMonth() const
  384. #else
  385. ULONG DateObj::GetMonth() const
  386. #endif
  387. {
  388. return theMonth;
  389. };
  390. #if (C_OS & C_WIN311)
  391. INT DateObj::GetDay() const
  392. #else
  393. ULONG DateObj::GetDay() const
  394. #endif
  395. {
  396. return theDay;
  397. };
  398. #if (C_OS & C_WIN311)
  399. INT DateObj::GetYear() const
  400. #else
  401. ULONG DateObj::GetYear() const
  402. #endif
  403. {
  404. return theYear;
  405. };
  406. #if (C_OS & C_WIN311)
  407. INT DateObj::GetDaysInMonth() const
  408. #else
  409. ULONG DateObj::GetDaysInMonth() const
  410. #endif
  411. {
  412. return GetDaysInMonth(theMonth,theYear);
  413. };
  414. #if (C_OS & C_WIN311)
  415. VOID DateObj::SetMonth(INT aMonth)
  416. #else
  417. VOID DateObj::SetMonth(ULONG aMonth)
  418. #endif
  419. {
  420. theMonth = aMonth;
  421. };
  422. #if (C_OS & C_WIN311)
  423. VOID DateObj::SetDay(INT aDay)
  424. #else
  425. VOID DateObj::SetDay(ULONG aDay)
  426. #endif
  427. {
  428. theDay = aDay;
  429. };
  430. #if (C_OS & C_WIN311)
  431. VOID DateObj::SetYear(INT aYear)
  432. #else
  433. VOID DateObj::SetYear(ULONG aYear)
  434. #endif
  435. {
  436. theYear = aYear;
  437. };
  438. #if (C_OS & C_WIN311)
  439. DateTimeObj::DateTimeObj(INT aMonth, INT aDay, INT aYear, INT anHour,
  440. INT aMinutes, INT aSeconds)
  441. #else
  442. DateTimeObj::DateTimeObj(ULONG aMonth, ULONG aDay, ULONG aYear, ULONG anHour,
  443. ULONG aMinutes, ULONG aSeconds)
  444. #endif
  445. {
  446. theDate = new DateObj(aMonth, aDay, aYear);
  447. theTime = new TimeObj(anHour, aMinutes, aSeconds);
  448. if ((theDate == NULL) || (theTime == NULL) || (theDate->GetObjectStatus() != ErrNO_ERROR) ||
  449. (theTime->GetObjectStatus() != ErrNO_ERROR))
  450. SetObjectStatus(ErrINVALID_DATE);
  451. };
  452. DateTimeObj::DateTimeObj (RDateTimeObj aDate)
  453. {
  454. theDate = new DateObj(aDate.theDate->GetMonth(),
  455. aDate.theDate->GetDay(),
  456. aDate.theDate->GetYear());
  457. theTime = new TimeObj(aDate.theTime->GetHour(),
  458. aDate.theTime->GetMinutes(),
  459. aDate.theTime->GetSeconds());
  460. }
  461. DateTimeObj::~DateTimeObj()
  462. {
  463. if(theDate) {
  464. delete theDate;
  465. theDate = (PDateObj)NULL;
  466. }
  467. if(theTime) {
  468. delete theTime;
  469. theTime = (PTimeObj)NULL;
  470. }
  471. }
  472. PDateObj DateTimeObj::GetDate() const
  473. {
  474. return theDate;
  475. };
  476. PTimeObj DateTimeObj::GetTime() const
  477. {
  478. return theTime;
  479. };
  480. /*
  481. C+
  482. Name :DateTimeObj
  483. Synop :This constructor provides a default of the current date and time
  484. */
  485. DateTimeObj::DateTimeObj ()
  486. {
  487. theDate = new DateObj;
  488. theTime = new TimeObj;
  489. }
  490. INT DateTimeObj::operator < (RDateTimeObj cmp) const
  491. {
  492. INT ret = FALSE;
  493. if (*(GetDate()) < *(cmp.GetDate())) {
  494. ret = TRUE;
  495. }
  496. else if (*(GetDate()) == *(cmp.GetDate()) &&
  497. *(GetTime()) < *(cmp.GetTime())) {
  498. ret = TRUE;
  499. }
  500. return ret;
  501. }
  502. LONG DateTimeObj::GetSeconds(VOID) {
  503. LONG seconds;
  504. struct tm t;
  505. time_t Now,Then;
  506. t.tm_year = (int)theDate->GetYear();
  507. t.tm_mon = (int)theDate->GetMonth()-1;
  508. t.tm_mday = (int)theDate->GetDay();
  509. t.tm_hour = (int)theTime->GetHour();
  510. t.tm_min = (int)theTime->GetMinutes();
  511. t.tm_sec = (int)theTime->GetSeconds();
  512. t.tm_isdst = -1;
  513. Then = mktime(&t);
  514. Now = time(0);
  515. seconds = (LONG) (difftime(Then,Now));
  516. return (seconds);
  517. }
  518. LONG DateTimeObj::GetMilliseconds(VOID)
  519. {
  520. struct tm t;
  521. t.tm_year = theDate->GetYear();
  522. t.tm_mon = theDate->GetMonth()-1;
  523. t.tm_mday = theDate->GetDay();
  524. t.tm_hour = theTime->GetHour();
  525. t.tm_min = theTime->GetMinutes();
  526. t.tm_sec = theTime->GetSeconds();
  527. t.tm_isdst = -1;
  528. time_t Then = mktime(&t);
  529. time_t Now = time(0);
  530. double sec = difftime(Then,Now);
  531. if(sec < 0) {
  532. sec = 0;
  533. }
  534. return ((LONG)(sec*1000));
  535. }
  536. #ifdef APCDEBUG
  537. ostream & operator<< (ostream& os, DateObj & obj) {
  538. return(obj.printMeOut(os));
  539. }
  540. ostream& DateObj::printMeOut(ostream& os)
  541. {
  542. os << theMonth << "/" << theDay << "/" << theYear;
  543. return os;
  544. }
  545. ostream & operator<< (ostream& os, TimeObj & obj) {
  546. return(obj.printMeOut(os));
  547. }
  548. ostream& TimeObj::printMeOut(ostream& os)
  549. {
  550. os << theHours << ":" << theMinutes << ":" << theSeconds;
  551. return os;
  552. }
  553. ostream & operator<< (ostream& os, DateTimeObj & obj) {
  554. return(obj.printMeOut(os));
  555. }
  556. ostream& DateTimeObj::printMeOut(ostream& os)
  557. {
  558. os << *theDate << " at " << *theTime;
  559. return os;
  560. }
  561. #endif