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.

832 lines
24 KiB

  1. /*++
  2. Copyright (c) Microsoft Corporation
  3. Module Name:
  4. FileDate.c
  5. Abstract:
  6. This file validate whether a date is valid and if valid then
  7. generates date according to context( Context refers to whether
  8. + or - is specifed . )
  9. Author:
  10. V Vijaya Bhaskar
  11. Revision History:
  12. 14-Jun-2001 : Created by V Vijaya Bhaskar ( Wipro Technologies ).
  13. --*/
  14. #include "Global.h"
  15. #include "FileDate.h"
  16. // Days present in tweleve months . In a leap year there are 29 days in FEB .
  17. DWORD DAYS_IN_A_MONTH[] = { 31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 ,
  18. 31 , 30 , 31 } ;
  19. /******************************************************************************
  20. ** Function Prototypes Local To This File **
  21. ******************************************************************************/
  22. BOOL
  23. GetValidDate(
  24. DWORD *dwDateGreater ,
  25. LPWSTR lpszDate ,
  26. PValid_File_Date pvfdValidFileDate
  27. ) ;
  28. BOOL
  29. DayFrom1900(
  30. PValid_File_Date pvfdValidFileDate
  31. ) ;
  32. BOOL
  33. DayOfTheYear(
  34. PValid_File_Date pvfdValidFileDate
  35. ) ;
  36. BOOL
  37. GetDate(
  38. PValid_File_Date pvfdValidFileDate ,
  39. DWORD dwDate
  40. ) ;
  41. BOOL
  42. IsValidDate(
  43. LPWSTR lpszDate ,
  44. PValid_File_Date pvfdValidFileDate
  45. ) ;
  46. /*************************************************************************
  47. /* Function Definition starts from here . **
  48. *************************************************************************/
  49. BOOL
  50. ValidDateForFile(
  51. OUT DWORD *pdwDateGreater ,
  52. OUT PValid_File_Date pvfdValidFileDate ,
  53. IN LPWSTR lpszDate
  54. )
  55. /*++
  56. Routine Description:
  57. Checks whether the specified date is valid or not . If specified date is
  58. valid , then converts it to a proper date ( dd-mm-yyyy ) .
  59. Arguments:
  60. [ OUT ] *dwDateGreater - Contains value which specifies whether to find a file
  61. created on a date less or greater than the current date .
  62. [ OUT ] pvfdValidFileDate - Contains a date .
  63. [ IN ] lpszDate - Date specified at the command prompt .
  64. Return value:
  65. BOOL .
  66. --*/
  67. {
  68. DWORD dwLength = 0 ;
  69. // Check for memory insufficient.
  70. if( ( NULL == pdwDateGreater ) ||
  71. ( NULL == pvfdValidFileDate ) ||
  72. ( NULL == lpszDate ) )
  73. {
  74. SetLastError( ERROR_INVALID_PARAMETER );
  75. SaveLastError() ;
  76. DISPLAY_GET_REASON() ;
  77. return FALSE;
  78. }
  79. /* Valid date must be have less('-') or greater('+')
  80. character . */
  81. switch( *lpszDate )
  82. {
  83. case MINUS : /* Less than the current date . */
  84. *pdwDateGreater = SMALLER_DATE ;
  85. break ;
  86. case PLUS :
  87. *pdwDateGreater = GREATER_DATE ; /* Greater than the current date .*/
  88. break ;
  89. default :
  90. DISPLAY_INVALID_DATE();
  91. return FALSE ;
  92. }
  93. /* The date should be in either "DD" or "MM/DD/YYYY" . */
  94. if( NULL != FindAChar( lpszDate, _T( '/' ) ) )
  95. {
  96. // Date is in "MM/DD/YYYY". Get length of the date string.
  97. dwLength = StringLength( lpszDate, 0 );
  98. // Check
  99. // (1) date string should be morethan 9 and less than 11
  100. // (2) Should not have double slashes.
  101. // (3) Should be a valid date. Check by IsValidDate() function.
  102. if( ( MIN_DATE_LENGTH > dwLength ) ||
  103. ( MAX_DATE_LENGTH < dwLength ) ||
  104. ( NULL != FindSubString( lpszDate, L"//" ) ) ||
  105. ( FALSE == IsValidDate( lpszDate , pvfdValidFileDate ) ) )
  106. {
  107. DISPLAY_INVALID_DATE();
  108. return FALSE ;
  109. }
  110. }
  111. else
  112. {
  113. /* Date is in "DD" format . */
  114. if( FALSE == GetValidDate( pdwDateGreater , lpszDate , pvfdValidFileDate ) )
  115. {
  116. /* Specified date is not a valid date . "DD" specified date must
  117. be in the range of including 0 - 32768 . */
  118. return FALSE ; /* Invalid date specified . */
  119. }
  120. }
  121. /* Date specified is valid .*/
  122. return TRUE ;
  123. }
  124. BOOL
  125. FileDateValid(
  126. IN DWORD dwDateGreater ,
  127. IN Valid_File_Date vfdValidFileDate ,
  128. IN FILETIME ftFileCreation
  129. )
  130. /*++
  131. Routine Description:
  132. Checks whether the specified file is created on a date which is before or after
  133. or equal to the specified date .
  134. Arguments:
  135. [ IN ] dwDateGreater - Contains value which tells whether to find a file
  136. who's creation date is smaller or greater than the
  137. current date .
  138. [ IN ] vfdValidFileDate - Contains a date .
  139. [ IN ] ftFileCreation - Holds obtained file's creation time .
  140. Return value:
  141. BOOL .
  142. --*/
  143. {
  144. /* Local variables */
  145. DWORD dwDay = 0 ; /* Holds file creation date . */
  146. DWORD dwTime = 0 ; /* Holds file creation time . */
  147. FILETIME ftFileTime ;
  148. // Convert file time to local file time.
  149. if( FALSE == FileTimeToLocalFileTime( &ftFileCreation , &ftFileTime ) )
  150. { // Display error.
  151. SaveLastError() ;
  152. DISPLAY_GET_REASON() ;
  153. return FALSE;
  154. }
  155. /* Convert FILETIME format to DOS time format . */
  156. if( FALSE == FileTimeToDosDateTime( &ftFileTime , (LPWORD) &dwDay ,
  157. (LPWORD) &dwTime ) )
  158. { /* Failed to convert the FILETIME to DATETIME */
  159. SaveLastError() ; /* Save last error occured . */
  160. DISPLAY_GET_REASON() ;
  161. return FALSE ;
  162. }
  163. /* Check whether the file was created on a date specified by user */
  164. switch( dwDateGreater )
  165. {
  166. case SMALLER_DATE : /* File must be created before user specified date. */
  167. /* If current year is smaller or equal to specified year . */
  168. if( vfdValidFileDate.m_dwYear >= ( ( ( dwDay & 0xFE00 ) >> 9 ) + 1980 ) )
  169. {
  170. /* If file creation year is equal to specified year , then check
  171. for month is it smaller or equal to specified month . */
  172. if( vfdValidFileDate.m_dwYear == ( ( ( dwDay & 0xFE00 ) >> 9 ) + 1980 ) )
  173. {
  174. if( vfdValidFileDate.m_dwMonth >= ( ( dwDay & 0x01E0 ) >> 5 ) )
  175. {
  176. /* If file creation month is equal to specified month ,then
  177. check for day is it smaller or equal to specified day .*/
  178. if( vfdValidFileDate.m_dwMonth == ( ( dwDay & 0x01E0 ) >> 5 ) )
  179. {
  180. if( vfdValidFileDate.m_dwDay >= ( dwDay & 0x001F ) )
  181. { // If file creation day is smaller than or equal
  182. // to specified day .
  183. return TRUE ;
  184. }
  185. else
  186. { // If file creation day is smaller to specified day .
  187. return FALSE ;
  188. }
  189. }
  190. else
  191. { // Control will come here only when file creation year is smaller or
  192. // equal to specified year .
  193. return TRUE ;
  194. }
  195. }
  196. }
  197. else
  198. { // Control will come here only when file creation year is smaller or equal
  199. // to specified year .
  200. return TRUE ;
  201. }
  202. }
  203. break ;
  204. case GREATER_DATE :
  205. // If file creation year is greater or equal to specified year .
  206. if( vfdValidFileDate.m_dwYear <= ( ( ( dwDay & 0xFE00 ) >> 9 ) + 1980 ) )
  207. {
  208. // If file creation year is equal to specified year ,
  209. // Then check for month is it greater or equal to specified month .
  210. if( vfdValidFileDate.m_dwYear == ( ( ( dwDay & 0xFE00 ) >> 9 ) + 1980 ) )
  211. {
  212. if( vfdValidFileDate.m_dwMonth <= ( ( dwDay & 0x01E0 ) >> 5 ) )
  213. {
  214. // If file creation month is equal to specified month ,
  215. // Then check for day is it greater or equal to specified day .
  216. if( vfdValidFileDate.m_dwMonth == ( ( dwDay & 0x01E0 ) >> 5 ) )
  217. {
  218. if( vfdValidFileDate.m_dwDay <= ( dwDay & 0x001F ) )
  219. {
  220. return TRUE ; // If file creation day is greater than or equal
  221. // to specified day .
  222. }
  223. else
  224. {
  225. return FALSE ; // If file creation day is greater to specified day .
  226. }
  227. }
  228. else
  229. { // Control will come here only when file creation month is greater or
  230. // equal to specified year .
  231. return TRUE ;
  232. }
  233. }
  234. }
  235. else
  236. { // Control will come here only when file creation year is greater or
  237. // equal to specified year .
  238. return TRUE ;
  239. }
  240. }
  241. break ;
  242. default:
  243. // Display error message since by default '+' should be present.
  244. // Control should never come here.
  245. DISPLAY_INVALID_DATE();
  246. return FALSE ;
  247. }
  248. // Control should never come here.
  249. return FALSE ;
  250. }
  251. BOOL
  252. IsValidDate(
  253. IN LPWSTR lpszDate ,
  254. OUT PValid_File_Date pvfdValidFileDate
  255. )
  256. /*++
  257. Routine Description:
  258. Returns a valid date if specified date is in {+|-}ddmmyyyy format .
  259. Arguments:
  260. [ IN ] lpszDate - Contains a date either in "ddmmyyyy" or "dd" format .
  261. [ OUT ] pvfdValidFileDate - Contains a valid date .
  262. Return value:
  263. BOOL .
  264. --*/
  265. {
  266. LPWSTR lpTemp = NULL;
  267. LPWSTR lpTemp1 = NULL;
  268. WCHAR szDate[ 10 ] ;
  269. // Check for NULL and date string should be between 9 and 11 characters.
  270. if( ( ( NULL == lpszDate ) ? TRUE :
  271. ( ( 11 < StringLength( lpszDate, 0 ) ) ||
  272. ( 9 > StringLength( lpszDate, 0 ) ) ) ) ||
  273. ( NULL == pvfdValidFileDate ) )
  274. {
  275. return FALSE;
  276. }
  277. SecureZeroMemory( szDate, 10 * sizeof( WCHAR ) );
  278. // Move pointer to beyond '+' or '-' in "{+|-}MM/dd/yyyy.
  279. lpTemp = lpszDate + 1;
  280. // Fetching month part from specified date.
  281. // Only first 4 characters( MM/ ) are copied.
  282. StringCopy( szDate, lpTemp, 4 );
  283. // If no '/' is found then display an error.
  284. if( NULL != FindAChar( szDate, _T('/') ) )
  285. {
  286. if( _T( '/' ) == *szDate )
  287. {
  288. return FALSE;
  289. }
  290. // Search for '/' since its the seperating character between MM/dd.
  291. // move 'lpTemp' pointer to point after '/'.
  292. if( _T( '/' ) == *( szDate + 1 ) )
  293. {
  294. // If M/dd is specified.
  295. lpTemp += 2;
  296. szDate[ 1 ] = _T( '\0' );
  297. }
  298. else
  299. {
  300. // If MM/dd is specified.
  301. lpTemp += 3;
  302. szDate[ 2 ] = _T( '\0' );
  303. }
  304. }
  305. else
  306. {
  307. return FALSE;
  308. }
  309. // Check whether '//' is not present.
  310. if( NULL != FindAChar( szDate, _T('/') ) )
  311. return FALSE;
  312. // Save month.
  313. lpTemp1 = NULL;
  314. pvfdValidFileDate->m_dwMonth = _tcstoul( szDate, &lpTemp1, 10 ) ;
  315. if( 0 != StringLength( lpTemp1, 0 ) )
  316. {
  317. return FALSE;
  318. }
  319. // Fetching date part from specified date.
  320. // Only 4 characters( DD/ ) are copied.
  321. StringCopy( szDate, lpTemp, 4 );
  322. // If no '/' is found then display an error.
  323. if( NULL != FindAChar( szDate, _T('/') ) )
  324. {
  325. if( _T( '/' ) == *szDate )
  326. {
  327. return FALSE;
  328. }
  329. // Search for '/' since its the seperating character between MM/dd.
  330. // move 'lpTemp' pointer to point after '/'.
  331. if( _T( '/' ) == *( szDate + 1 ) )
  332. {
  333. // If d/yyyy is specified.
  334. lpTemp += 2;
  335. szDate[ 1 ] = _T( '\0' );
  336. }
  337. else
  338. {
  339. // If dd/yyyy is specified.
  340. lpTemp += 3;
  341. szDate[ 2 ] = _T( '\0' );
  342. }
  343. }
  344. else
  345. {
  346. return FALSE;
  347. }
  348. lpTemp1 = NULL;
  349. pvfdValidFileDate->m_dwDay = _tcstoul( szDate, &lpTemp1, 10 ) ;
  350. // if 'lpTemp1' length is not zero then 'szDate' contains some other
  351. // character other than numbers.
  352. if( 0 != StringLength( lpTemp1, 0 ) )
  353. {
  354. return FALSE;
  355. }
  356. // Fetching year part from specified date.
  357. if( 4 != StringLength( lpTemp, 0 ) )
  358. { // Date contains other than 'yyyy'.
  359. return FALSE;
  360. }
  361. // Copy 'yyyy'.
  362. StringCopy( szDate, lpTemp, 5 );
  363. lpTemp1 = NULL;
  364. pvfdValidFileDate->m_dwYear = _tcstoul( szDate, &lpTemp1 , 10 ) ;
  365. // if 'lpTemp1' length is not zero then 'szDate' contains some other
  366. // character other than numbers.
  367. if( 0 != StringLength( lpTemp1, 0 ) )
  368. {
  369. return FALSE;
  370. }
  371. // Check whether the day or month or year is zero .
  372. if( ( pvfdValidFileDate->m_dwDay <= 0 ) ||
  373. ( pvfdValidFileDate->m_dwMonth <= 0 ) ||
  374. ( pvfdValidFileDate->m_dwYear <= 0 ) )
  375. {
  376. // No need to display any error , since control will go to GetValidDate .
  377. // If specified date is wrong then error is displayed in GetValidDate() .
  378. return FALSE ;
  379. }
  380. // Check whether the current year is a leap year , if yes then check whether
  381. // the current month is februrary.
  382. if( ( IS_A_LEAP_YEAR( pvfdValidFileDate->m_dwYear ) ) &&
  383. ( FEB == pvfdValidFileDate->m_dwMonth ) )
  384. {
  385. // For leap year number of days is 29 . Check for same .
  386. if( pvfdValidFileDate->m_dwDay > DAYS_INFEB_LEAP_YEAR )
  387. {
  388. // No need to display any error here , since control will go to GetValidDate .
  389. // If specified date is wrong then error is displayed in GetValidDate() .
  390. return FALSE ; // Specified date is invalid .
  391. }
  392. else
  393. {
  394. return TRUE ; // Specified date is valid .
  395. }
  396. }
  397. // Since all extra validations are over , so check for other months .
  398. switch( pvfdValidFileDate->m_dwMonth )
  399. {
  400. // Months having 31 days .
  401. case JAN :
  402. case MAR :
  403. case MAY :
  404. case JUL :
  405. case AUG :
  406. case OCT :
  407. case DEC :
  408. // Month have only 31 days but specified date is greater than that, display error .
  409. if( pvfdValidFileDate->m_dwDay > THIRTYONE )
  410. {
  411. return FALSE ;
  412. }
  413. return TRUE ;
  414. // Month having only 28 days .
  415. case FEB :
  416. // Month have only 28 days but specified date is greater than that , display error.
  417. if( pvfdValidFileDate->m_dwDay > DAYS_INFEB )
  418. {
  419. return FALSE ;
  420. }
  421. return TRUE ;
  422. // Months having 30 days .
  423. case APR :
  424. case JUN :
  425. case SEP :
  426. case NOV :
  427. // Month have only 30 days but specified date is greater than that, display error .
  428. if( pvfdValidFileDate->m_dwDay > THIRTY )
  429. {
  430. return FALSE ;
  431. }
  432. return TRUE ;
  433. // If not a valid month .
  434. default :
  435. return FALSE ;
  436. }
  437. }
  438. BOOL
  439. GetValidDate(
  440. IN DWORD *pdwDateGreater ,
  441. IN LPWSTR lpszDate ,
  442. OUT PValid_File_Date pvfdValidFileDate
  443. )
  444. /*++
  445. Routine Description:
  446. Returns a valid date if specified date is in {+|-}dd format .
  447. Arguments:
  448. [ IN ] *pdwDateGreater - Contains value which tells whether to find a file
  449. who's creation date is smaller or greater than the
  450. current date .
  451. [ IN ] lpszDate - Contains a date in "dd" format . Must be in range of
  452. 1 - 32768 .
  453. [ OUT ] pvfdValidFileDate - Contains a valid date .
  454. Return value:
  455. BOOL returning TRUE or FALSE .
  456. --*/
  457. {
  458. // Local variables.
  459. DWORD dwDate = 0 ; // Stores the date specified .
  460. SYSTEMTIME stDateAndTime ;
  461. // Check for memory insufficient.
  462. if( ( NULL == pdwDateGreater ) ||
  463. ( NULL == pvfdValidFileDate ) ||
  464. ( NULL == lpszDate ) )
  465. {
  466. SetLastError( ERROR_INVALID_PARAMETER );
  467. SaveLastError() ;
  468. DISPLAY_GET_REASON() ;
  469. return FALSE;
  470. }
  471. // Convert date from string to number .
  472. dwDate =_tcstoul( ( lpszDate + 1 ) , NULL , 10 ) ;
  473. // Is date specified is in limis .
  474. if( dwDate > 32768 )
  475. {
  476. DISPLAY_INVALID_DATE();
  477. return FALSE ;
  478. }
  479. // Fetch current date and time .
  480. GetLocalTime( &stDateAndTime ) ;
  481. pvfdValidFileDate->m_dwDay = stDateAndTime.wDay ;
  482. pvfdValidFileDate->m_dwMonth = stDateAndTime.wMonth ;
  483. pvfdValidFileDate->m_dwYear = stDateAndTime.wYear ;
  484. // If date to compare is zero then return TRUE , as their
  485. // is nothing to calculate .
  486. if( dwDate == 0 )
  487. {
  488. return TRUE ;
  489. }
  490. // Find how many days are over in current year .
  491. if( FALSE == DayOfTheYear( pvfdValidFileDate ) )
  492. {
  493. return FALSE ;
  494. }
  495. // Find how many days are over from year 1900 .
  496. if( FALSE == DayFrom1900( pvfdValidFileDate ) )
  497. {
  498. return FALSE ;
  499. }
  500. if( *pdwDateGreater == SMALLER_DATE )
  501. {// If files created before the current date is needed then
  502. // subtract days to current date .
  503. if( pvfdValidFileDate->m_dwDay < dwDate )
  504. { // Control should not come here.
  505. SetLastError( ERROR_FILE_NOT_FOUND ) ;
  506. SaveLastError() ;
  507. DISPLAY_GET_REASON() ;
  508. return FALSE ;
  509. }
  510. pvfdValidFileDate->m_dwDay -= dwDate ;
  511. }
  512. else
  513. {// If files created after the current date is needed then
  514. // add days to current date .
  515. if( *pdwDateGreater == GREATER_DATE )
  516. {
  517. pvfdValidFileDate->m_dwDay += dwDate ;
  518. }
  519. else
  520. { // Return False .
  521. DISPLAY_INVALID_DATE();
  522. return FALSE ;
  523. }
  524. }
  525. // If everything is fine then get date from which to start searching file ,
  526. // whether to find files below or above created date .
  527. // 'GetDate' function always returns you a number , should check for return value ,
  528. // is it overflowing or not or getting some negative numbers .
  529. if( FALSE == GetDate( pvfdValidFileDate , pvfdValidFileDate->m_dwDay ) )
  530. {
  531. return FALSE ;
  532. }
  533. return TRUE ;
  534. }
  535. BOOL
  536. DayOfTheYear(
  537. OUT PValid_File_Date pvfdValidFileDate
  538. )
  539. /*++
  540. Routine Description:
  541. Returns the current day of the year .
  542. Arguments:
  543. [ OUT ] pvfdValidFileDate - Will contain a valid date .
  544. Return value:
  545. void is return .
  546. --*/
  547. {
  548. // Check for memory insufficient.
  549. if( NULL == pvfdValidFileDate )
  550. {
  551. SetLastError( ERROR_INVALID_PARAMETER );
  552. SaveLastError() ;
  553. DISPLAY_GET_REASON() ;
  554. return FALSE;
  555. }
  556. // Is current year a leap year .
  557. if( IS_A_LEAP_YEAR( pvfdValidFileDate->m_dwYear ) )
  558. { // Add one day , as a Leap year have 366 days instead of 365 days .
  559. if( 2 < pvfdValidFileDate->m_dwMonth )
  560. {
  561. pvfdValidFileDate->m_dwDay += 1 ;
  562. }
  563. }
  564. // Go on adding number of days in a month from current month to JAN .
  565. for( pvfdValidFileDate->m_dwMonth ;
  566. pvfdValidFileDate->m_dwMonth != 1 ;
  567. pvfdValidFileDate->m_dwMonth-- )
  568. {
  569. pvfdValidFileDate->m_dwDay += DAYS_IN_A_MONTH[ pvfdValidFileDate->m_dwMonth - 2 ] ;
  570. }
  571. pvfdValidFileDate->m_dwDay -= 1 ;
  572. return TRUE;
  573. }
  574. BOOL
  575. DayFrom1900(
  576. OUT PValid_File_Date pvfdValidFileDate
  577. )
  578. /*++
  579. Routine Description:
  580. Returns the current day of the year .
  581. Arguments:
  582. [ OUT ] pvfdValidFileDate - Gives days elapsed in current year and
  583. returns days elapsed from 1900 .
  584. Return value:
  585. void is return .
  586. --*/
  587. {
  588. DWORD dwTemp = 0; // Holds the number of days .
  589. // Check for memory insufficient.
  590. if( NULL == pvfdValidFileDate )
  591. {
  592. SetLastError( ERROR_INVALID_PARAMETER );
  593. SaveLastError() ;
  594. DISPLAY_GET_REASON() ;
  595. return FALSE;
  596. }
  597. // ( Current Year - 1900 ) * 365 will be days between 1900 and current
  598. // year , include Leap year date also .
  599. dwTemp = ( pvfdValidFileDate->m_dwYear - YEAR ) * DAY_IN_A_YEAR ;
  600. // Check whether current year is a leap year . If yes don't add one .
  601. // Its Because : I am taking ( current year - 1900 ) , from 01-JAN.
  602. // On this date their won't be any extra day , its added only after FEB 29 .
  603. if( IS_A_LEAP_YEAR( pvfdValidFileDate->m_dwYear ) )
  604. {
  605. dwTemp += (( pvfdValidFileDate->m_dwYear - YEAR ) / LEAP_YEAR ) ;
  606. }
  607. // else add one to it . Since a leap year is gone and 1900 is a leap year .
  608. else
  609. {
  610. dwTemp += (( pvfdValidFileDate->m_dwYear - YEAR ) / LEAP_YEAR ) + 1 ;
  611. }
  612. // Add obtained days to days already over in current year .
  613. pvfdValidFileDate->m_dwDay += dwTemp ;
  614. return TRUE;
  615. }
  616. BOOL
  617. GetDate(
  618. OUT PValid_File_Date pvfdValidFileDate ,
  619. IN DWORD dwDate
  620. )
  621. /*++
  622. Routine Description:
  623. Returns the date which was after or before the current date.
  624. If today 29-Jun-2001 , then day 20 days before was 09-jun-2001 .
  625. Arguments:
  626. [ OUT ] pvfdValidFileDate - Contains date .
  627. Return value:
  628. void is return .
  629. --*/
  630. {
  631. DWORD dwTemp = 0 ;
  632. // Check for memory insufficient.
  633. if( NULL == pvfdValidFileDate )
  634. {
  635. SetLastError( ERROR_INVALID_PARAMETER );
  636. SaveLastError() ;
  637. DISPLAY_GET_REASON() ;
  638. return FALSE;
  639. }
  640. pvfdValidFileDate->m_dwYear = YEAR;
  641. while( 0 != dwDate )
  642. {
  643. if( IS_A_LEAP_YEAR( pvfdValidFileDate->m_dwYear ) )
  644. {
  645. if( dwDate <= 366 )
  646. {
  647. break;
  648. }
  649. else
  650. {
  651. dwDate -= 366;
  652. }
  653. }
  654. else
  655. {
  656. if( dwDate <= 365 )
  657. {
  658. break;
  659. }
  660. else
  661. {
  662. dwDate -= 365;
  663. }
  664. }
  665. pvfdValidFileDate->m_dwYear += 1;
  666. }
  667. // Loop till don't get a valid date .
  668. for( dwTemp = 0 ; dwDate != 0 ; dwTemp++ )
  669. {
  670. // Is a leap year and is it a FEB month . Extra care must
  671. // be taken for Leap years .
  672. if( ( IS_A_LEAP_YEAR( pvfdValidFileDate->m_dwYear ) ) && ( ( dwTemp + 1 ) == FEB ) )
  673. { // Check whether have enough days to spend on LEAP YEAR FEB month .
  674. // Add an extra day for Leap Year .
  675. if( dwDate > ( DAYS_IN_A_MONTH[ dwTemp ] + 1 ) )
  676. { // Increment month . Decrement days that are in that month .
  677. pvfdValidFileDate->m_dwMonth += 1 ;
  678. dwDate -= DAYS_IN_A_MONTH[ dwTemp ] + 1 ;
  679. }
  680. else
  681. { // Found date .
  682. pvfdValidFileDate->m_dwDay = dwDate ;
  683. dwDate = 0 ;
  684. }
  685. }// end if
  686. else
  687. { // Check whether have enough days to spend on a month .
  688. if( dwDate > ( DAYS_IN_A_MONTH[ dwTemp ] ) )
  689. { // Increment month . Decrement days that are in that month .
  690. pvfdValidFileDate->m_dwMonth += 1 ;
  691. dwDate -= DAYS_IN_A_MONTH[ dwTemp ] ;
  692. }
  693. else
  694. { // Found date .
  695. pvfdValidFileDate->m_dwDay = dwDate ;
  696. dwDate = 0 ;
  697. }
  698. }// end else
  699. }// end for
  700. return TRUE;
  701. }