Source code of Windows XP (NT5)
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.

3546 lines
113 KiB

  1. /*++
  2. Copyright (c) 1991-2000, Microsoft Corporation All rights reserved.
  3. Module Name:
  4. datetime.c
  5. Abstract:
  6. This file contains the API functions that form properly formatted date
  7. and time strings for a given locale.
  8. APIs found in this file:
  9. GetTimeFormatW
  10. GetDateFormatW
  11. Revision History:
  12. 05-31-91 JulieB Created.
  13. --*/
  14. //
  15. // Include Files.
  16. //
  17. #include "nls.h"
  18. #include "nlssafe.h"
  19. //
  20. // Constant Declarations.
  21. //
  22. #define MAX_DATETIME_BUFFER 256 // max size of buffer
  23. #define NLS_CHAR_LTR_MARK L'\x200e' // left to right reading order mark
  24. #define NLS_CHAR_RTL_MARK L'\x200f' // right to left reading order mark
  25. #define NLS_HEBREW_JUNE 6 // month of June (Hebrew lunar)
  26. //
  27. // Forward Declarations.
  28. //
  29. BOOL
  30. IsValidTime(
  31. LPSYSTEMTIME lpTime);
  32. BOOL
  33. IsValidDate(
  34. LPSYSTEMTIME lpDate);
  35. WORD
  36. GetCalendarYear(
  37. LPWORD *ppRange,
  38. CALID CalNum,
  39. PCALENDAR_VAR pCalInfo,
  40. WORD Year,
  41. WORD Month,
  42. WORD Day);
  43. int
  44. ParseTime(
  45. PLOC_HASH pHashN,
  46. LPSYSTEMTIME pLocalTime,
  47. LPWSTR pFormat,
  48. LPWSTR pTimeStr,
  49. DWORD dwFlags);
  50. int
  51. ParseDate(
  52. PLOC_HASH pHashN,
  53. DWORD dwFlags,
  54. LPSYSTEMTIME pLocalDate,
  55. LPWSTR pFormat,
  56. LPWSTR pDateStr,
  57. CALID CalNum,
  58. PCALENDAR_VAR pCalInfo,
  59. BOOL fLunarLeap);
  60. DWORD
  61. GetAbsoluteDate(
  62. WORD Year,
  63. WORD Month,
  64. WORD Day);
  65. void
  66. GetHijriDate(
  67. LPSYSTEMTIME pDate,
  68. DWORD dwFlags);
  69. LONG
  70. GetAdvanceHijriDate(
  71. DWORD dwFlags);
  72. DWORD
  73. DaysUpToHijriYear(
  74. DWORD HijriYear);
  75. BOOL
  76. GetHebrewDate(
  77. LPSYSTEMTIME pDate,
  78. LPBOOL pLunarLeap);
  79. BOOL
  80. IsValidDateForHebrew(
  81. WORD Year,
  82. WORD Month,
  83. WORD Day);
  84. BOOL
  85. NumberToHebrewLetter(
  86. DWORD Number,
  87. LPWSTR szHebrewNum,
  88. int cchSize);
  89. //-------------------------------------------------------------------------//
  90. // INTERNAL MACROS //
  91. //-------------------------------------------------------------------------//
  92. ////////////////////////////////////////////////////////////////////////////
  93. //
  94. // NLS_COPY_UNICODE_STR
  95. //
  96. // Copies a zero terminated string from pSrc to the pDest buffer. The
  97. // pDest pointer is advanced to the end of the string. Also, the cchDest
  98. // member will be updated with the amount remaining
  99. //
  100. // SECURITY: If the copy fails due to exceeding cchDest, then this macro
  101. // will exit the calling function, returning rcFailure.
  102. //
  103. // DEFINED AS A MACRO.
  104. //
  105. // 04-30-93 JulieB Created.
  106. ////////////////////////////////////////////////////////////////////////////
  107. #define NLS_COPY_UNICODE_STR( pDest, \
  108. cchDest, \
  109. pSrc, \
  110. rcFailure) \
  111. { \
  112. /* \
  113. * Copy the string to the result buffer. \
  114. */ \
  115. if(FAILED(StringCchCopyExW(pDest, \
  116. cchDest, \
  117. pSrc, \
  118. &pDest, \
  119. &cchDest, \
  120. 0))) \
  121. { \
  122. return(rcFailure); \
  123. } \
  124. }
  125. ////////////////////////////////////////////////////////////////////////////
  126. //
  127. // NLS_PAD_INT_TO_UNICODE_STR
  128. //
  129. // Converts an integer value to a unicode string and stores it in the
  130. // buffer provided with the appropriate number of leading zeros. The
  131. // pResultBuf pointer is advanced to the end of the string and the
  132. // cchResultBuf parasm is updated to the amount of space left.
  133. //
  134. // SECURITY: Note that if an attempt is made to overrun our static buffer,
  135. // this macro will exit the calling function (returning rcFailure).
  136. //
  137. // DEFINED AS A MACRO.
  138. //
  139. // 04-30-93 JulieB Created.
  140. ////////////////////////////////////////////////////////////////////////////
  141. #define NLS_PAD_INT_TO_UNICODE_STR( Value, \
  142. Base, \
  143. Padding, \
  144. pResultBuf, \
  145. cchResultBuf, \
  146. rcFailure) \
  147. { \
  148. UNICODE_STRING ObString; /* value string */ \
  149. WCHAR pBuffer[MAX_SMALL_BUF_LEN]; /* ptr to buffer */ \
  150. UINT LpCtr; /* loop counter */ \
  151. \
  152. \
  153. /* \
  154. * Set up unicode string structure. \
  155. */ \
  156. ObString.Length = MAX_SMALL_BUF_LEN * sizeof(WCHAR); \
  157. ObString.MaximumLength = MAX_SMALL_BUF_LEN * sizeof(WCHAR); \
  158. ObString.Buffer = pBuffer; \
  159. \
  160. /* \
  161. * Get the value as a string. If there is an error, then do nothing. \
  162. */ \
  163. if (!RtlIntegerToUnicodeString(Value, Base, &ObString)) \
  164. { \
  165. /* \
  166. * Pad the string with the appropriate number of zeros. \
  167. */ \
  168. for (LpCtr = GET_WC_COUNT(ObString.Length); \
  169. LpCtr < Padding; \
  170. LpCtr++, pResultBuf++, cchResultBuf--) \
  171. { \
  172. *pResultBuf = NLS_CHAR_ZERO; \
  173. } \
  174. \
  175. /* \
  176. * Copy the string to the result buffer. \
  177. * The pResultBuf pointer will be advanced in the macro. \
  178. * The cchResultsBuf value will be updated in the macro. \
  179. */ \
  180. NLS_COPY_UNICODE_STR(pResultBuf, \
  181. cchResultBuf, \
  182. ObString.Buffer, rcFailure) \
  183. } \
  184. }
  185. ////////////////////////////////////////////////////////////////////////////
  186. //
  187. // NLS_STRING_TO_INTEGER
  188. //
  189. // Converts a string to an integer value.
  190. //
  191. // DEFINED AS A MACRO.
  192. //
  193. // 10-19-93 JulieB Created.
  194. ////////////////////////////////////////////////////////////////////////////
  195. #define NLS_STRING_TO_INTEGER( CalNum, \
  196. pCalId ) \
  197. { \
  198. UNICODE_STRING ObUnicodeStr; /* value string */ \
  199. \
  200. \
  201. /* \
  202. * No need to check return value since the calendar number \
  203. * will be validated after this anyway. \
  204. */ \
  205. RtlInitUnicodeString(&ObUnicodeStr, pCalId); \
  206. RtlUnicodeStringToInteger(&ObUnicodeStr, 10, &CalNum); \
  207. }
  208. ////////////////////////////////////////////////////////////////////////////
  209. //
  210. // NLS_INSERT_BIDI_MARK
  211. //
  212. // Based on the user's bidi mark preference, it either adds a
  213. // left to right mark or a right to left mark.
  214. // The pDest pointer is advanced to the next position.
  215. // The cchDest value is updated to the amount of space remaining in pDest.
  216. //
  217. // SECURITY: Note that if an attempt is made to overrun our static buffer,
  218. // this macro will exit the calling function (returning rcFailure).
  219. //
  220. // DEFINED AS A MACRO.
  221. //
  222. // 12-03-96 JulieB Created.
  223. ////////////////////////////////////////////////////////////////////////////
  224. #define NLS_INSERT_BIDI_MARK(pDest, dwFlags, cchDest, rcFailure) \
  225. { \
  226. if (dwFlags & (DATE_LTRREADING | DATE_RTLREADING)) \
  227. { \
  228. if(cchDest <= 1) \
  229. { \
  230. return(rcFailure); \
  231. } \
  232. if (dwFlags & DATE_RTLREADING) \
  233. { \
  234. *pDest = NLS_CHAR_RTL_MARK; \
  235. } \
  236. else \
  237. { \
  238. *pDest = NLS_CHAR_LTR_MARK; \
  239. } \
  240. pDest++; \
  241. cchDest--; \
  242. } \
  243. }
  244. ////////////////////////////////////////////////////////////////////////////
  245. //
  246. // NLS_GREGORIAN_LEAP_YEAR
  247. //
  248. // True if the given Gregorian year is a leap year. False otherwise.
  249. //
  250. // A year is a leap year if it is divisible by 4 and is not a century
  251. // year (multiple of 100) or if it is divisible by 400.
  252. //
  253. // DEFINED AS A MACRO.
  254. //
  255. // 12-04-96 JulieB Created.
  256. ////////////////////////////////////////////////////////////////////////////
  257. #define NLS_GREGORIAN_LEAP_YEAR(Year) \
  258. ((Year % 4 == 0) && ((Year % 100 != 0) || (Year % 400 == 0)))
  259. ////////////////////////////////////////////////////////////////////////////
  260. //
  261. // NLS_HIJRI_LEAP_YEAR
  262. //
  263. // True if the given Hijri year is a leap year. False otherwise.
  264. //
  265. // A year is a leap year if it is the 2nd, 5th, 7th, 10th, 13th, 16th,
  266. // 18th, 21st, 24th, 26th, or 29th year of a 30-year cycle.
  267. //
  268. // DEFINED AS A MACRO.
  269. //
  270. // 12-04-96 JulieB Created.
  271. ////////////////////////////////////////////////////////////////////////////
  272. #define NLS_HIJRI_LEAP_YEAR(Year) \
  273. ((((Year * 11) + 14) % 30) < 11)
  274. ////////////////////////////////////////////////////////////////////////////
  275. //
  276. // ARRAYSIZE
  277. //
  278. // Hnady utility macro to get the size of an array (such as an array of
  279. // WCHARs).
  280. ////////////////////////////////////////////////////////////////////////////
  281. #ifndef ARRAYSIZE
  282. #define ARRAYSIZE(x) (sizeof(x)/sizeof((x)[0]))
  283. #endif
  284. //-------------------------------------------------------------------------//
  285. // API ROUTINES //
  286. //-------------------------------------------------------------------------//
  287. ////////////////////////////////////////////////////////////////////////////
  288. //
  289. // GetTimeFormatW
  290. //
  291. // Returns a properly formatted time string for the given locale. It uses
  292. // either the system time or the specified time. This call also indicates
  293. // how much memory is necessary to contain the desired information.
  294. //
  295. // 04-30-93 JulieB Created.
  296. ////////////////////////////////////////////////////////////////////////////
  297. int WINAPI GetTimeFormatW(
  298. LCID Locale,
  299. DWORD dwFlags,
  300. CONST SYSTEMTIME *lpTime,
  301. LPCWSTR lpFormat,
  302. LPWSTR lpTimeStr,
  303. int cchTime)
  304. {
  305. PLOC_HASH pHashN; // ptr to LOC hash node
  306. SYSTEMTIME LocalTime; // local time structure
  307. LPWSTR pFormat; // ptr to time format string
  308. int Length = 0; // number of characters written
  309. WCHAR pString[MAX_DATETIME_BUFFER]; // ptr to temporary buffer
  310. WCHAR pTemp[MAX_REG_VAL_SIZE]; // temp buffer
  311. //
  312. // Invalid Parameter Check:
  313. // - validate LCID
  314. // - count is negative
  315. // - NULL data pointer AND count is not zero
  316. // - lpFormat length > MAX_DATETIME_BUFFER if not null
  317. //
  318. VALIDATE_LOCALE(Locale, pHashN, FALSE);
  319. if ( (pHashN == NULL) ||
  320. (cchTime < 0) ||
  321. ((lpTimeStr == NULL) && (cchTime != 0)) ||
  322. ((lpFormat) && (NlsStrLenW(lpFormat) >= MAX_DATETIME_BUFFER)) )
  323. {
  324. SetLastError(ERROR_INVALID_PARAMETER);
  325. return (0);
  326. }
  327. //
  328. // Invalid Flags Check:
  329. // - flags other than valid ones
  330. // - lpFormat not NULL AND NoUserOverride flag is set
  331. //
  332. if ( (dwFlags & GTF_INVALID_FLAG) ||
  333. ((lpFormat != NULL) && (dwFlags & LOCALE_NOUSEROVERRIDE)) )
  334. {
  335. SetLastError(ERROR_INVALID_FLAGS);
  336. return (0);
  337. }
  338. //
  339. // Set pFormat to point at the proper format string.
  340. //
  341. if (lpFormat == NULL)
  342. {
  343. //
  344. // Get either the user's time format from the registry or
  345. // the default time format from the locale file.
  346. // This string may be a null string.
  347. //
  348. if (!(dwFlags & LOCALE_NOUSEROVERRIDE) &&
  349. GetUserInfo( Locale,
  350. LOCALE_STIMEFORMAT,
  351. FIELD_OFFSET(NLS_USER_INFO, sTimeFormat),
  352. NLS_VALUE_STIMEFORMAT,
  353. pTemp,
  354. ARRAYSIZE(pTemp),
  355. FALSE ))
  356. {
  357. pFormat = pTemp;
  358. }
  359. else
  360. {
  361. pFormat = (LPWORD)(pHashN->pLocaleHdr) +
  362. pHashN->pLocaleHdr->STimeFormat;
  363. }
  364. }
  365. else
  366. {
  367. //
  368. // Use the format string given by the caller.
  369. //
  370. pFormat = (LPWSTR)lpFormat;
  371. }
  372. //
  373. // Get the current local system time if one is not given.
  374. //
  375. if (lpTime != NULL)
  376. {
  377. //
  378. // Time is given by user. Store in local structure and
  379. // validate it.
  380. //
  381. LocalTime.wHour = lpTime->wHour;
  382. LocalTime.wMinute = lpTime->wMinute;
  383. LocalTime.wSecond = lpTime->wSecond;
  384. LocalTime.wMilliseconds = lpTime->wMilliseconds;
  385. if (!IsValidTime(&LocalTime))
  386. {
  387. SetLastError(ERROR_INVALID_PARAMETER);
  388. return (0);
  389. }
  390. }
  391. else
  392. {
  393. GetLocalTime(&LocalTime);
  394. }
  395. //
  396. // Parse the time format string.
  397. //
  398. Length = ParseTime( pHashN,
  399. &LocalTime,
  400. pFormat,
  401. pString,
  402. dwFlags );
  403. //
  404. // Check cchTime for size of given buffer.
  405. //
  406. if (cchTime == 0)
  407. {
  408. //
  409. // If cchTime is 0, then we can't use lpTimeStr. In this
  410. // case, we simply want to return the length (in characters) of
  411. // the string to be copied.
  412. //
  413. return (Length);
  414. }
  415. else if (cchTime < Length)
  416. {
  417. //
  418. // The buffer is too small for the string, so return an error
  419. // and zero bytes written.
  420. //
  421. SetLastError(ERROR_INSUFFICIENT_BUFFER);
  422. return (0);
  423. }
  424. else if (0 == Length)
  425. {
  426. //
  427. // The buffer is too small for the string, so return an error
  428. // and zero bytes written. A good candidate for a return of
  429. // ERROR_STACK_BUFFER_OVERRUN but thats a bit too much information
  430. //
  431. SetLastError(ERROR_INVALID_PARAMETER);
  432. return (0);
  433. }
  434. //
  435. // Copy the time string to lpTimeStr and null terminate it.
  436. // Return the number of characters copied.
  437. //
  438. if(FAILED(StringCchCopyW(lpTimeStr, Length, pString)))
  439. {
  440. //
  441. // Failure should in theory be impossible, but if we ignore the
  442. // return value, PREfast will complain.
  443. //
  444. SetLastError(ERROR_OUTOFMEMORY);
  445. return (0);
  446. }
  447. return (Length);
  448. }
  449. ////////////////////////////////////////////////////////////////////////////
  450. //
  451. // GetDateFormatW
  452. //
  453. // Returns a properly formatted date string for the given locale. It uses
  454. // either the system date or the specified date. The user may specify
  455. // either the short date format or the long date format. This call also
  456. // indicates how much memory is necessary to contain the desired information.
  457. //
  458. // 04-30-93 JulieB Created.
  459. ////////////////////////////////////////////////////////////////////////////
  460. int WINAPI GetDateFormatW(
  461. LCID Locale,
  462. DWORD dwFlags,
  463. CONST SYSTEMTIME *lpDate,
  464. LPCWSTR lpFormat,
  465. LPWSTR lpDateStr,
  466. int cchDate)
  467. {
  468. PLOC_HASH pHashN; // ptr to LOC hash node
  469. LPWSTR pFormat; // ptr to format string
  470. SYSTEMTIME LocalDate; // local date structure
  471. int Length = 0; // number of characters written
  472. WCHAR pString[MAX_DATETIME_BUFFER]; // ptr to temporary buffer
  473. BOOL fAltCalendar; // if alternate cal flag set
  474. LPWSTR pOptCal; // ptr to optional calendar
  475. PCAL_INFO pCalInfo; // ptr to calendar info
  476. CALID CalNum = 0; // calendar number
  477. ULONG CalDateOffset; // offset to calendar data
  478. ULONG LocDateOffset; // offset to locale data
  479. SIZE_T CacheOffset = 0; // Offset to field in the cache.
  480. LPWSTR pValue; // ptr to registry value to get
  481. WCHAR pTemp[MAX_REG_VAL_SIZE]; // temp buffer
  482. BOOL fLunarLeap = FALSE; // if Hebrew Lunar leap year
  483. LCTYPE LCType;
  484. //
  485. // Invalid Parameter Check:
  486. // - validate LCID
  487. // - count is negative
  488. // - NULL data pointer AND count is not zero
  489. // - lpFormat length > MAX_DATETIME_BUFFER if not null
  490. //
  491. VALIDATE_LOCALE(Locale, pHashN, FALSE);
  492. if ( (pHashN == NULL) ||
  493. (cchDate < 0) ||
  494. ((lpDateStr == NULL) && (cchDate != 0)) ||
  495. ((lpFormat) && (NlsStrLenW(lpFormat) >= MAX_DATETIME_BUFFER)) )
  496. {
  497. SetLastError(ERROR_INVALID_PARAMETER);
  498. return (0);
  499. }
  500. //
  501. // Invalid Flags Check:
  502. // - flags other than valid ones
  503. // - more than one of either ltr reading or rtl reading
  504. // - lpFormat not NULL AND flags not zero
  505. //
  506. if ( (dwFlags & GDF_INVALID_FLAG) ||
  507. (MORE_THAN_ONE(dwFlags, GDF_SINGLE_FLAG)) ||
  508. ((lpFormat != NULL) &&
  509. (dwFlags & (DATE_SHORTDATE | DATE_LONGDATE |
  510. DATE_YEARMONTH | LOCALE_NOUSEROVERRIDE))) )
  511. {
  512. SetLastError(ERROR_INVALID_FLAGS);
  513. return (0);
  514. }
  515. //
  516. // See if the alternate calendar should be used.
  517. //
  518. if (fAltCalendar = (dwFlags & DATE_USE_ALT_CALENDAR))
  519. {
  520. //
  521. // Get the default optional calendar.
  522. //
  523. pOptCal = (LPWORD)(pHashN->pLocaleHdr) +
  524. pHashN->pLocaleHdr->IOptionalCal;
  525. //
  526. // If there is an optional calendar, store the calendar id.
  527. //
  528. if (((POPT_CAL)pOptCal)->CalId != CAL_NO_OPTIONAL)
  529. {
  530. CalNum = ((POPT_CAL)pOptCal)->CalId;
  531. }
  532. }
  533. //
  534. // If there was no alternate calendar, then try (in order):
  535. // - the user's calendar type
  536. // - the system default calendar type
  537. //
  538. if (CalNum == 0)
  539. {
  540. //
  541. // Get the user's calendar type.
  542. //
  543. if ( !(dwFlags & LOCALE_NOUSEROVERRIDE) &&
  544. GetUserInfo( Locale,
  545. LOCALE_ICALENDARTYPE,
  546. FIELD_OFFSET(NLS_USER_INFO, iCalType),
  547. NLS_VALUE_ICALENDARTYPE,
  548. pTemp,
  549. ARRAYSIZE(pTemp),
  550. TRUE ) &&
  551. (pOptCal = IsValidCalendarTypeStr( pHashN, pTemp )) )
  552. {
  553. CalNum = ((POPT_CAL)pOptCal)->CalId;
  554. }
  555. else
  556. {
  557. //
  558. // Get the system default calendar type.
  559. //
  560. NLS_STRING_TO_INTEGER( CalNum,
  561. pHashN->pLocaleFixed->szICalendarType );
  562. }
  563. }
  564. //
  565. // Get the pointer to the appropriate calendar information.
  566. //
  567. if (GetCalendar(CalNum, &pCalInfo))
  568. {
  569. SetLastError(ERROR_INVALID_PARAMETER);
  570. return (0);
  571. }
  572. //
  573. // Set pFormat to point at the proper format string.
  574. //
  575. if (lpFormat == NULL)
  576. {
  577. //
  578. // Find out which flag is set and save the appropriate
  579. // information.
  580. //
  581. switch (dwFlags & (DATE_SHORTDATE | DATE_LONGDATE | DATE_YEARMONTH))
  582. {
  583. case ( 0 ) :
  584. case ( DATE_SHORTDATE ) :
  585. {
  586. //
  587. // Get the offset values for the shortdate.
  588. //
  589. CalDateOffset = (ULONG)FIELD_OFFSET(CALENDAR_VAR, SShortDate);
  590. LocDateOffset = (ULONG)FIELD_OFFSET(LOCALE_VAR, SShortDate);
  591. CacheOffset = FIELD_OFFSET(NLS_USER_INFO, sShortDate);
  592. pValue = NLS_VALUE_SSHORTDATE;
  593. LCType = LOCALE_SSHORTDATE;
  594. break;
  595. }
  596. case ( DATE_LONGDATE ) :
  597. {
  598. //
  599. // Get the offset values for the longdate.
  600. //
  601. CalDateOffset = (ULONG)FIELD_OFFSET(CALENDAR_VAR, SLongDate);
  602. LocDateOffset = (ULONG)FIELD_OFFSET(LOCALE_VAR, SLongDate);
  603. CacheOffset = FIELD_OFFSET(NLS_USER_INFO, sLongDate);
  604. pValue = NLS_VALUE_SLONGDATE;
  605. LCType = LOCALE_SLONGDATE;
  606. break;
  607. }
  608. case ( DATE_YEARMONTH ) :
  609. {
  610. //
  611. // Get the offset values for the year/month.
  612. //
  613. CalDateOffset = (ULONG)FIELD_OFFSET(CALENDAR_VAR, SYearMonth);
  614. LocDateOffset = (ULONG)FIELD_OFFSET(LOCALE_VAR, SYearMonth);
  615. CacheOffset = FIELD_OFFSET(NLS_USER_INFO, sYearMonth);
  616. pValue = NLS_VALUE_SYEARMONTH;
  617. LCType = LOCALE_SYEARMONTH;
  618. break;
  619. }
  620. default :
  621. {
  622. SetLastError(ERROR_INVALID_FLAGS);
  623. return (0);
  624. }
  625. }
  626. //
  627. // Get the proper format string for the given locale.
  628. // This string may be a null string.
  629. //
  630. pFormat = NULL;
  631. if (fAltCalendar && (CalNum != CAL_GREGORIAN))
  632. {
  633. pFormat = (LPWORD)pCalInfo +
  634. *((LPWORD)((LPBYTE)(pCalInfo) + CalDateOffset));
  635. if (*pFormat == 0)
  636. {
  637. pFormat = NULL;
  638. }
  639. }
  640. if (pFormat == NULL)
  641. {
  642. if (!(dwFlags & LOCALE_NOUSEROVERRIDE) &&
  643. GetUserInfo(Locale, LCType, CacheOffset, pValue, pTemp, ARRAYSIZE(pTemp), TRUE))
  644. {
  645. pFormat = pTemp;
  646. }
  647. else
  648. {
  649. pFormat = (LPWORD)pCalInfo +
  650. *((LPWORD)((LPBYTE)(pCalInfo) + CalDateOffset));
  651. if (*pFormat == 0)
  652. {
  653. pFormat = (LPWORD)(pHashN->pLocaleHdr) +
  654. *((LPWORD)((LPBYTE)(pHashN->pLocaleHdr) +
  655. LocDateOffset));
  656. }
  657. }
  658. }
  659. }
  660. else
  661. {
  662. //
  663. // Use the format string given by the caller.
  664. //
  665. pFormat = (LPWSTR)lpFormat;
  666. }
  667. //
  668. // Get the current local system date if one is not given.
  669. //
  670. if (lpDate != NULL)
  671. {
  672. //
  673. // Date is given by user. Store in local structure and
  674. // validate it.
  675. //
  676. LocalDate.wYear = lpDate->wYear;
  677. LocalDate.wMonth = lpDate->wMonth;
  678. LocalDate.wDayOfWeek = lpDate->wDayOfWeek;
  679. LocalDate.wDay = lpDate->wDay;
  680. if (!IsValidDate(&LocalDate))
  681. {
  682. SetLastError(ERROR_INVALID_PARAMETER);
  683. return (0);
  684. }
  685. }
  686. else
  687. {
  688. GetLocalTime(&LocalDate);
  689. }
  690. //
  691. // See if we're dealing with the Hijri or the Hebrew calendar.
  692. //
  693. if (CalNum == CAL_HIJRI)
  694. {
  695. GetHijriDate(&LocalDate, dwFlags);
  696. }
  697. else if (CalNum == CAL_HEBREW)
  698. {
  699. if (!GetHebrewDate(&LocalDate, &fLunarLeap))
  700. {
  701. SetLastError(ERROR_INVALID_PARAMETER);
  702. return (0);
  703. }
  704. }
  705. //
  706. // Parse the date format string.
  707. //
  708. Length = ParseDate( pHashN,
  709. dwFlags,
  710. &LocalDate,
  711. pFormat,
  712. pString,
  713. CalNum,
  714. (PCALENDAR_VAR)pCalInfo,
  715. fLunarLeap );
  716. //
  717. // Check cchDate for size of given buffer.
  718. //
  719. if (cchDate == 0)
  720. {
  721. //
  722. // If cchDate is 0, then we can't use lpDateStr. In this
  723. // case, we simply want to return the length (in characters) of
  724. // the string to be copied.
  725. //
  726. return (Length);
  727. }
  728. else if (cchDate < Length)
  729. {
  730. //
  731. // The buffer is too small for the string, so return an error
  732. // and zero bytes written.
  733. //
  734. SetLastError(ERROR_INSUFFICIENT_BUFFER);
  735. return (0);
  736. }
  737. else if (0 == Length)
  738. {
  739. //
  740. // The buffer is too small for the string, so return an error
  741. // and zero bytes written. A good candidate for a return of
  742. // ERROR_STACK_BUFFER_OVERRUN but thats a bit too much information
  743. //
  744. SetLastError(ERROR_INVALID_PARAMETER);
  745. return(0);
  746. }
  747. //
  748. // Copy the date string to lpDateStr and null terminate it.
  749. // Return the number of characters copied.
  750. //
  751. if(FAILED(StringCchCopyW(lpDateStr, Length, pString)))
  752. {
  753. //
  754. // Failure should in theory be impossible, but if we ignore the
  755. // return value, PREfast will complain.
  756. //
  757. SetLastError(ERROR_OUTOFMEMORY);
  758. return (0);
  759. }
  760. return (Length);
  761. }
  762. //-------------------------------------------------------------------------//
  763. // INTERNAL ROUTINES //
  764. //-------------------------------------------------------------------------//
  765. ////////////////////////////////////////////////////////////////////////////
  766. //
  767. // IsValidTime
  768. //
  769. // Returns TRUE if the given time is valid. Otherwise, it returns FALSE.
  770. //
  771. // 04-30-93 JulieB Created.
  772. ////////////////////////////////////////////////////////////////////////////
  773. BOOL IsValidTime(
  774. LPSYSTEMTIME pTime)
  775. {
  776. //
  777. // Check for invalid time values.
  778. //
  779. if ( (pTime->wHour > 23) ||
  780. (pTime->wMinute > 59) ||
  781. (pTime->wSecond > 59) ||
  782. (pTime->wMilliseconds > 999) )
  783. {
  784. return (FALSE);
  785. }
  786. //
  787. // Return success.
  788. //
  789. return (TRUE);
  790. }
  791. ////////////////////////////////////////////////////////////////////////////
  792. //
  793. // IsValidDate
  794. //
  795. // Returns TRUE if the given date is valid. Otherwise, it returns FALSE.
  796. //
  797. // 04-30-93 JulieB Created.
  798. ////////////////////////////////////////////////////////////////////////////
  799. BOOL IsValidDate(
  800. LPSYSTEMTIME pDate)
  801. {
  802. LARGE_INTEGER Time; // time as a large integer
  803. TIME_FIELDS TimeFields; // time fields structure
  804. //
  805. // Set up time fields structure with the given date.
  806. // Only want to check the DATE values, so pass in a valid time.
  807. //
  808. TimeFields.Year = pDate->wYear;
  809. TimeFields.Month = pDate->wMonth;
  810. TimeFields.Day = pDate->wDay;
  811. TimeFields.Hour = 0;
  812. TimeFields.Minute = 0;
  813. TimeFields.Second = 0;
  814. TimeFields.Milliseconds = 0;
  815. //
  816. // Check for invalid date values.
  817. //
  818. // NOTE: This routine ignores the Weekday field.
  819. //
  820. if (!RtlTimeFieldsToTime(&TimeFields, &Time))
  821. {
  822. return (FALSE);
  823. }
  824. //
  825. // Make sure the given day of the week is valid for the given date.
  826. //
  827. RtlTimeToTimeFields(&Time, &TimeFields);
  828. pDate->wDayOfWeek = TimeFields.Weekday;
  829. //
  830. // Return success.
  831. //
  832. return (TRUE);
  833. }
  834. ////////////////////////////////////////////////////////////////////////////
  835. //
  836. // GetCalendarYear
  837. //
  838. // Adjusts the given year to the given calendar's year.
  839. //
  840. // 10-15-93 JulieB Created.
  841. ////////////////////////////////////////////////////////////////////////////
  842. WORD GetCalendarYear(
  843. LPWORD *ppRange,
  844. CALID CalNum,
  845. PCALENDAR_VAR pCalInfo,
  846. WORD Year,
  847. WORD Month,
  848. WORD Day)
  849. {
  850. LPWORD pRange; // ptr to range position
  851. LPWORD pEndRange; // ptr to the end of the range
  852. //
  853. // Initialize range pointer.
  854. //
  855. *ppRange = NULL;
  856. //
  857. // Adjust the year based on the given calendar
  858. //
  859. switch (CalNum)
  860. {
  861. case ( 0 ) :
  862. case ( CAL_GREGORIAN ) :
  863. case ( CAL_GREGORIAN_US ) :
  864. default :
  865. {
  866. //
  867. // Year value is not changed.
  868. //
  869. break;
  870. }
  871. case ( CAL_JAPAN ) :
  872. case ( CAL_TAIWAN ) :
  873. {
  874. //
  875. // Get pointer to ranges.
  876. //
  877. pRange = ((LPWORD)pCalInfo) + pCalInfo->SEraRanges;
  878. pEndRange = ((LPWORD)pCalInfo) + pCalInfo->SShortDate;
  879. //
  880. // Find the appropriate range.
  881. //
  882. while (pRange < pEndRange)
  883. {
  884. if ((Year > ((PERA_RANGE)pRange)->Year) ||
  885. ((Year == ((PERA_RANGE)pRange)->Year) &&
  886. ((Month > ((PERA_RANGE)pRange)->Month) ||
  887. ((Month == ((PERA_RANGE)pRange)->Month) &&
  888. (Day >= ((PERA_RANGE)pRange)->Day)))))
  889. {
  890. break;
  891. }
  892. pRange += ((PERA_RANGE)pRange)->Offset;
  893. }
  894. //
  895. // Make sure the year is within the given ranges. If it
  896. // is not, then leave the year in the Gregorian format.
  897. //
  898. if (pRange < pEndRange)
  899. {
  900. //
  901. // Convert the year to the appropriate Era year.
  902. // Year = Year - EraYear + 1
  903. //
  904. Year = Year - ((PERA_RANGE)pRange)->Year + 1;
  905. //
  906. // Save the pointer to the range.
  907. //
  908. *ppRange = pRange;
  909. }
  910. break;
  911. }
  912. case ( CAL_KOREA ) :
  913. case ( CAL_THAI ) :
  914. {
  915. //
  916. // Get the first range.
  917. //
  918. pRange = ((LPWORD)pCalInfo) + pCalInfo->SEraRanges;
  919. //
  920. // Add the year offset to the given year.
  921. // Year = Year + EraYear
  922. //
  923. Year += ((PERA_RANGE)pRange)->Year;
  924. //
  925. // Save the range.
  926. //
  927. *ppRange = pRange;
  928. break;
  929. }
  930. }
  931. //
  932. // Return the year.
  933. //
  934. return (Year);
  935. }
  936. ////////////////////////////////////////////////////////////////////////////
  937. //
  938. // ParseTime
  939. //
  940. // Parses the time format string and puts the properly formatted
  941. // local time into the given string buffer. It returns the number of
  942. // characters written to the string buffer.
  943. //
  944. // SECURITY: If an attempt is made to overrun our static buffer, return 0
  945. // to trigger failure.
  946. //
  947. // 04-30-93 JulieB Created.
  948. ////////////////////////////////////////////////////////////////////////////
  949. int ParseTime(
  950. PLOC_HASH pHashN,
  951. LPSYSTEMTIME pLocalTime,
  952. LPWSTR pFormat,
  953. LPWSTR pTimeStr,
  954. DWORD dwFlags)
  955. {
  956. LPWSTR pPos; // ptr to pTimeStr current position
  957. LPWSTR pLastPos; // ptr to pTimeStr last valid position
  958. LPWSTR pLastFormatPos; // ptr to pFormat last parsed string
  959. int Repeat; // number of repetitions of same letter
  960. int BufferedSpaces; // buffered spaces to copy to output buffer
  961. WORD wHour; // hour
  962. WCHAR wchar; // character in format string
  963. LPWSTR pAMPM; // ptr to AM/PM designator
  964. WCHAR pTemp[MAX_REG_VAL_SIZE]; // temp buffer
  965. BOOL bInQuote; // are we in a quoted string or not ?
  966. size_t cchRemaining; // Count of how many charactrs are left in pTimeStr
  967. size_t cchLastRemaining; // How many charactrs are left in pTimeStr at last valid pos
  968. //
  969. // Initialize position pointer.
  970. //
  971. pPos = pTimeStr;
  972. pLastPos = pPos;
  973. pLastFormatPos = pFormat;
  974. cchRemaining = MAX_DATETIME_BUFFER;
  975. cchLastRemaining = cchRemaining;
  976. BufferedSpaces = 0L;
  977. //
  978. // Parse through loop and store the appropriate time information
  979. // in the pTimeStr buffer.
  980. //
  981. while (*pFormat)
  982. {
  983. switch (*pFormat)
  984. {
  985. case ( L'h' ) :
  986. {
  987. //
  988. // Check for forced 24 hour time format.
  989. //
  990. wHour = pLocalTime->wHour;
  991. if (!(dwFlags & TIME_FORCE24HOURFORMAT))
  992. {
  993. //
  994. // Use 12 hour format.
  995. //
  996. if (!(wHour %= 12))
  997. {
  998. wHour = 12;
  999. }
  1000. }
  1001. //
  1002. // Get the number of 'h' repetitions in the format string.
  1003. //
  1004. pFormat++;
  1005. for (Repeat = 0; (*pFormat == L'h'); Repeat++, pFormat++)
  1006. ;
  1007. //
  1008. // Put any buffered spaces into the output buffer.
  1009. //
  1010. while (BufferedSpaces > 0)
  1011. {
  1012. if( cchRemaining <= 1 )
  1013. {
  1014. // Our static buffer will be overrun if we continue, so bail
  1015. return(0);
  1016. }
  1017. BufferedSpaces--;
  1018. *pPos++ = L' ';
  1019. cchRemaining--;
  1020. }
  1021. switch (Repeat)
  1022. {
  1023. case ( 0 ) :
  1024. {
  1025. //
  1026. // Use NO leading zero for the hour.
  1027. // The pPos pointer will be advanced in the macro.
  1028. // The cchRemaining value will be updated in the macro.
  1029. //
  1030. NLS_PAD_INT_TO_UNICODE_STR( wHour,
  1031. 10,
  1032. 1,
  1033. pPos,
  1034. cchRemaining,
  1035. 0 );
  1036. break;
  1037. }
  1038. case ( 1 ) :
  1039. default :
  1040. {
  1041. //
  1042. // Use leading zero for the hour.
  1043. // The pPos pointer will be advanced in the macro.
  1044. // The cchRemaining value will be updated in the macro.
  1045. //
  1046. NLS_PAD_INT_TO_UNICODE_STR( wHour,
  1047. 10,
  1048. 2,
  1049. pPos,
  1050. cchRemaining,
  1051. 0 );
  1052. break;
  1053. }
  1054. }
  1055. //
  1056. // Save the last position in case one of the NO_xxx
  1057. // flags is set.
  1058. //
  1059. pLastPos = pPos;
  1060. cchLastRemaining = cchRemaining;
  1061. pLastFormatPos = pFormat;
  1062. break;
  1063. }
  1064. case ( L'H' ) :
  1065. {
  1066. //
  1067. // Get the number of 'H' repetitions in the format string.
  1068. //
  1069. pFormat++;
  1070. for (Repeat = 0; (*pFormat == L'H'); Repeat++, pFormat++)
  1071. ;
  1072. //
  1073. // Put any buffered spaces into the output buffer.
  1074. //
  1075. while (BufferedSpaces > 0)
  1076. {
  1077. if( cchRemaining <= 1 )
  1078. {
  1079. // Our static buffer will be overrun if we continue, so bail
  1080. return(0);
  1081. }
  1082. BufferedSpaces--;
  1083. *pPos++ = L' ';
  1084. cchRemaining--;
  1085. }
  1086. switch (Repeat)
  1087. {
  1088. case ( 0 ) :
  1089. {
  1090. //
  1091. // Use NO leading zero for the hour.
  1092. // The pPos pointer will be advanced in the macro.
  1093. // The cchRemaining value will be updated in the macro.
  1094. //
  1095. NLS_PAD_INT_TO_UNICODE_STR( pLocalTime->wHour,
  1096. 10,
  1097. 1,
  1098. pPos,
  1099. cchRemaining,
  1100. 0 );
  1101. break;
  1102. }
  1103. case ( 1 ) :
  1104. default :
  1105. {
  1106. //
  1107. // Use leading zero for the hour.
  1108. // The pPos pointer will be advanced in the macro.
  1109. // The cchRemaining value will be updated in the macro.
  1110. //
  1111. NLS_PAD_INT_TO_UNICODE_STR( pLocalTime->wHour,
  1112. 10,
  1113. 2,
  1114. pPos,
  1115. cchRemaining,
  1116. 0 );
  1117. break;
  1118. }
  1119. }
  1120. //
  1121. // Save the last position in case one of the NO_xxx
  1122. // flags is set.
  1123. //
  1124. pLastPos = pPos;
  1125. cchLastRemaining = cchRemaining;
  1126. pLastFormatPos = pFormat;
  1127. break;
  1128. }
  1129. case ( L'm' ) :
  1130. {
  1131. //
  1132. // Get the number of 'm' repetitions in the format string.
  1133. //
  1134. pFormat++;
  1135. for (Repeat = 0; (*pFormat == L'm'); Repeat++, pFormat++)
  1136. ;
  1137. //
  1138. // If the flag TIME_NOMINUTESORSECONDS is set, then
  1139. // skip over the minutes.
  1140. //
  1141. if (dwFlags & TIME_NOMINUTESORSECONDS)
  1142. {
  1143. //
  1144. // Reset position pointer to last postion and break
  1145. // out of this case statement.
  1146. //
  1147. // This will remove any separator(s) between the
  1148. // hours and minutes.
  1149. //
  1150. // 1- Go backward and leave only quoted text
  1151. // 2- Go forward and remove everything until hitting {hHt}
  1152. //
  1153. bInQuote = FALSE;
  1154. while (pFormat != pLastFormatPos)
  1155. {
  1156. if (*pLastFormatPos == NLS_CHAR_QUOTE)
  1157. {
  1158. bInQuote = !bInQuote;
  1159. pLastFormatPos++;
  1160. continue;
  1161. }
  1162. if (bInQuote)
  1163. {
  1164. *pLastPos = *pLastFormatPos;
  1165. pLastPos++;
  1166. cchLastRemaining--;
  1167. }
  1168. pLastFormatPos++;
  1169. }
  1170. bInQuote = FALSE;
  1171. BufferedSpaces = 0;
  1172. while (*pFormat)
  1173. {
  1174. if (*pLastFormatPos == NLS_CHAR_QUOTE)
  1175. {
  1176. bInQuote = !bInQuote;
  1177. }
  1178. if (!bInQuote)
  1179. {
  1180. if (*pFormat == L' ')
  1181. {
  1182. BufferedSpaces++;
  1183. }
  1184. else
  1185. {
  1186. if ((*pFormat == L'h') ||
  1187. (*pFormat == L'H') ||
  1188. (*pFormat == L't'))
  1189. {
  1190. break;
  1191. }
  1192. }
  1193. }
  1194. pFormat++;
  1195. }
  1196. pPos = pLastPos;
  1197. cchRemaining = cchLastRemaining;
  1198. break;
  1199. }
  1200. //
  1201. // Put any buffered spaces into the output buffer.
  1202. //
  1203. while (BufferedSpaces > 0)
  1204. {
  1205. if( cchRemaining <= 1 )
  1206. {
  1207. // Our static buffer will be overrun if we continue, so bail
  1208. return(0);
  1209. }
  1210. BufferedSpaces--;
  1211. *pPos++ = L' ';
  1212. cchRemaining--;
  1213. }
  1214. switch (Repeat)
  1215. {
  1216. case ( 0 ) :
  1217. {
  1218. //
  1219. // Use NO leading zero for the minute.
  1220. // The pPos pointer will be advanced in the macro.
  1221. // The cchRemaining value will be updated in the macro.
  1222. //
  1223. NLS_PAD_INT_TO_UNICODE_STR( pLocalTime->wMinute,
  1224. 10,
  1225. 1,
  1226. pPos,
  1227. cchRemaining,
  1228. 0 );
  1229. break;
  1230. }
  1231. case ( 1 ) :
  1232. default :
  1233. {
  1234. //
  1235. // Use leading zero for the minute.
  1236. // The pPos pointer will be advanced in the macro.
  1237. // The cchRemaining value will be updated in the macro.
  1238. //
  1239. NLS_PAD_INT_TO_UNICODE_STR( pLocalTime->wMinute,
  1240. 10,
  1241. 2,
  1242. pPos,
  1243. cchRemaining,
  1244. 0 );
  1245. break;
  1246. }
  1247. }
  1248. //
  1249. // Save the last position in case one of the NO_xxx
  1250. // flags is set.
  1251. //
  1252. pLastPos = pPos;
  1253. cchLastRemaining = cchRemaining;
  1254. pLastFormatPos = pFormat;
  1255. break;
  1256. }
  1257. case ( L's' ) :
  1258. {
  1259. //
  1260. // Get the number of 's' repetitions in the format string.
  1261. //
  1262. pFormat++;
  1263. for (Repeat = 0; (*pFormat == L's'); Repeat++, pFormat++)
  1264. ;
  1265. //
  1266. // If the flag TIME_NOMINUTESORSECONDS and/or TIME_NOSECONDS
  1267. // is set, then skip over the seconds.
  1268. //
  1269. if (dwFlags & (TIME_NOMINUTESORSECONDS | TIME_NOSECONDS))
  1270. {
  1271. //
  1272. // Reset position pointer to last postion and break
  1273. // out of this case statement.
  1274. //
  1275. // This will remove any separator(s) between the
  1276. // minutes and seconds.
  1277. //
  1278. //
  1279. // 1- Go backward and leave only quoted text
  1280. // 2- Go forward and remove everything till hitting {hmHt}
  1281. //
  1282. bInQuote = FALSE;
  1283. while (pFormat != pLastFormatPos)
  1284. {
  1285. if (*pLastFormatPos == NLS_CHAR_QUOTE)
  1286. {
  1287. bInQuote = !bInQuote;
  1288. pLastFormatPos++;
  1289. continue;
  1290. }
  1291. if (bInQuote)
  1292. {
  1293. *pLastPos = *pLastFormatPos;
  1294. pLastPos++;
  1295. cchLastRemaining--;
  1296. }
  1297. pLastFormatPos++;
  1298. }
  1299. bInQuote = FALSE;
  1300. BufferedSpaces = 0;
  1301. while (*pFormat)
  1302. {
  1303. if (*pLastFormatPos == NLS_CHAR_QUOTE)
  1304. {
  1305. bInQuote = !bInQuote;
  1306. }
  1307. if (!bInQuote)
  1308. {
  1309. if (*pFormat == L' ')
  1310. {
  1311. BufferedSpaces++;
  1312. }
  1313. else
  1314. {
  1315. if ((*pFormat == L'h') ||
  1316. (*pFormat == L'H') ||
  1317. (*pFormat == L't') ||
  1318. (*pFormat == L'm'))
  1319. {
  1320. break;
  1321. }
  1322. }
  1323. }
  1324. pFormat++;
  1325. }
  1326. pPos = pLastPos;
  1327. cchRemaining = cchLastRemaining;
  1328. break;
  1329. }
  1330. //
  1331. // Put any buffered spaces into the output buffer.
  1332. //
  1333. while (BufferedSpaces > 0)
  1334. {
  1335. if( cchRemaining <= 1 )
  1336. {
  1337. // Our static buffer will be overrun if we continue, so bail
  1338. return(0);
  1339. }
  1340. BufferedSpaces--;
  1341. *pPos++ = L' ';
  1342. cchRemaining--;
  1343. }
  1344. switch (Repeat)
  1345. {
  1346. case ( 0 ) :
  1347. {
  1348. //
  1349. // Use NO leading zero for the second.
  1350. // The pPos pointer will be advanced in the macro.
  1351. // The cchRemaining value will be updated in the macro.
  1352. //
  1353. NLS_PAD_INT_TO_UNICODE_STR( pLocalTime->wSecond,
  1354. 10,
  1355. 1,
  1356. pPos,
  1357. cchRemaining,
  1358. 0 );
  1359. break;
  1360. }
  1361. case ( 1 ) :
  1362. default :
  1363. {
  1364. //
  1365. // Use leading zero for the second.
  1366. // The pPos pointer will be advanced in the macro.
  1367. // The cchRemaining value will be updated in the macro.
  1368. //
  1369. NLS_PAD_INT_TO_UNICODE_STR( pLocalTime->wSecond,
  1370. 10,
  1371. 2,
  1372. pPos,
  1373. cchRemaining,
  1374. 0 );
  1375. break;
  1376. }
  1377. }
  1378. //
  1379. // Save the last position in case one of the NO_xxx
  1380. // flags is set.
  1381. //
  1382. pLastPos = pPos;
  1383. cchLastRemaining = cchRemaining;
  1384. pLastFormatPos = pFormat;
  1385. break;
  1386. }
  1387. case ( L't' ) :
  1388. {
  1389. //
  1390. // Get the number of 't' repetitions in the format string.
  1391. //
  1392. pFormat++;
  1393. for (Repeat = 0; (*pFormat == L't'); Repeat++, pFormat++)
  1394. ;
  1395. //
  1396. // Put any buffered spaces into the output buffer.
  1397. //
  1398. while (BufferedSpaces > 0)
  1399. {
  1400. if( cchRemaining <= 1 )
  1401. {
  1402. // Our static buffer will be overrun if we continue, so bail
  1403. return(0);
  1404. }
  1405. BufferedSpaces--;
  1406. *pPos++ = L' ';
  1407. cchRemaining--;
  1408. }
  1409. //
  1410. // If the flag TIME_NOTIMEMARKER is set, then skip over
  1411. // the time marker info.
  1412. //
  1413. if (dwFlags & TIME_NOTIMEMARKER)
  1414. {
  1415. //
  1416. // Reset position pointer to last postion.
  1417. //
  1418. // This will remove any separator(s) between the
  1419. // time (hours, minutes, seconds) and the time
  1420. // marker.
  1421. //
  1422. pPos = pLastPos;
  1423. cchRemaining = cchLastRemaining;
  1424. pLastFormatPos = pFormat;
  1425. //
  1426. // Increment the format pointer until it reaches
  1427. // an h, H, m, or s. This will remove any
  1428. // separator(s) following the time marker.
  1429. //
  1430. while ( (wchar = *pFormat) &&
  1431. (wchar != L'h') &&
  1432. (wchar != L'H') &&
  1433. (wchar != L'm') &&
  1434. (wchar != L's') )
  1435. {
  1436. pFormat++;
  1437. }
  1438. //
  1439. // Break out of this case statement.
  1440. //
  1441. break;
  1442. }
  1443. else
  1444. {
  1445. //
  1446. // Get AM/PM designator.
  1447. // This string may be a null string.
  1448. //
  1449. if (pLocalTime->wHour < 12)
  1450. {
  1451. if (!(dwFlags & LOCALE_NOUSEROVERRIDE) &&
  1452. GetUserInfo( pHashN->Locale,
  1453. LOCALE_S1159,
  1454. FIELD_OFFSET(NLS_USER_INFO, s1159),
  1455. NLS_VALUE_S1159,
  1456. pTemp,
  1457. ARRAYSIZE(pTemp),
  1458. FALSE ))
  1459. {
  1460. pAMPM = pTemp;
  1461. }
  1462. else
  1463. {
  1464. pAMPM = (LPWORD)(pHashN->pLocaleHdr) +
  1465. pHashN->pLocaleHdr->S1159;
  1466. }
  1467. }
  1468. else
  1469. {
  1470. if (!(dwFlags & LOCALE_NOUSEROVERRIDE) &&
  1471. GetUserInfo( pHashN->Locale,
  1472. LOCALE_S2359,
  1473. FIELD_OFFSET(NLS_USER_INFO, s2359),
  1474. NLS_VALUE_S2359,
  1475. pTemp,
  1476. ARRAYSIZE(pTemp),
  1477. FALSE ))
  1478. {
  1479. pAMPM = pTemp;
  1480. }
  1481. else
  1482. {
  1483. pAMPM = (LPWORD)(pHashN->pLocaleHdr) +
  1484. pHashN->pLocaleHdr->S2359;
  1485. }
  1486. }
  1487. if (*pAMPM == 0)
  1488. {
  1489. //
  1490. // Reset position pointer to last postion and break
  1491. // out of this case statement.
  1492. //
  1493. // This will remove any separator(s) between the
  1494. // time (hours, minutes, seconds) and the time
  1495. // marker.
  1496. //
  1497. pPos = pLastPos;
  1498. cchRemaining = cchLastRemaining;
  1499. pLastFormatPos = pFormat;
  1500. break;
  1501. }
  1502. }
  1503. switch (Repeat)
  1504. {
  1505. case ( 0 ) :
  1506. {
  1507. if( cchRemaining <= 1 )
  1508. {
  1509. // Our static buffer will be overrun if we continue, so bail
  1510. return(0);
  1511. }
  1512. //
  1513. // One letter of AM/PM designator.
  1514. //
  1515. *pPos = *pAMPM;
  1516. pPos++;
  1517. cchRemaining--;
  1518. break;
  1519. }
  1520. case ( 1 ) :
  1521. default :
  1522. {
  1523. //
  1524. // Use entire AM/PM designator string.
  1525. // The pPos pointer will be advanced in the macro.
  1526. // The cchRemaining value will be updated in the macro.
  1527. //
  1528. NLS_COPY_UNICODE_STR(pPos, cchRemaining, pAMPM, 0);
  1529. break;
  1530. }
  1531. }
  1532. //
  1533. // Save the last position in case one of the NO_xxx
  1534. // flags is set.
  1535. //
  1536. pLastPos = pPos;
  1537. cchLastRemaining = cchRemaining;
  1538. pLastFormatPos = pFormat;
  1539. break;
  1540. }
  1541. case ( NLS_CHAR_QUOTE ) :
  1542. {
  1543. //
  1544. // Any text enclosed within single quotes should be left
  1545. // in the time string in its exact form (without the
  1546. // quotes), unless it is an escaped single quote ('').
  1547. //
  1548. pFormat++;
  1549. while (*pFormat)
  1550. {
  1551. if (*pFormat != NLS_CHAR_QUOTE)
  1552. {
  1553. if( cchRemaining <= 1 )
  1554. {
  1555. // Our static buffer will be overrun if we continue, so bail
  1556. return(0);
  1557. }
  1558. //
  1559. // Still within the single quote, so copy
  1560. // the character to the buffer.
  1561. //
  1562. *pPos = *pFormat;
  1563. pFormat++;
  1564. pPos++;
  1565. cchRemaining--;
  1566. }
  1567. else
  1568. {
  1569. //
  1570. // Found another quote, so skip over it.
  1571. //
  1572. pFormat++;
  1573. //
  1574. // Make sure it's not an escaped single quote.
  1575. //
  1576. if (*pFormat == NLS_CHAR_QUOTE)
  1577. {
  1578. if( cchRemaining <= 1 )
  1579. {
  1580. // Our static buffer will be overrun if we continue, so bail
  1581. return(0);
  1582. }
  1583. //
  1584. // Escaped single quote, so just write the
  1585. // single quote.
  1586. //
  1587. *pPos = *pFormat;
  1588. pFormat++;
  1589. pPos++;
  1590. cchRemaining--;
  1591. }
  1592. else
  1593. {
  1594. //
  1595. // Found the end quote, so break out of loop.
  1596. //
  1597. break;
  1598. }
  1599. }
  1600. }
  1601. break;
  1602. }
  1603. default :
  1604. {
  1605. if( cchRemaining <= 1 )
  1606. {
  1607. // Our static buffer will be overrun if we continue, so bail
  1608. return(0);
  1609. }
  1610. //
  1611. // Store the character in the buffer. Should be the
  1612. // separator, but copy it even if it isn't.
  1613. //
  1614. *pPos = *pFormat;
  1615. pFormat++;
  1616. pPos++;
  1617. cchRemaining--;
  1618. break;
  1619. }
  1620. }
  1621. }
  1622. //
  1623. // Zero terminate the string.
  1624. //
  1625. *pPos = 0;
  1626. //
  1627. // Return the number of characters written to the buffer, including
  1628. // the null terminator.
  1629. //
  1630. return ((int)((pPos - pTimeStr) + 1));
  1631. }
  1632. ////////////////////////////////////////////////////////////////////////////
  1633. //
  1634. // ParseDate
  1635. //
  1636. // Parses the date format string and puts the properly formatted
  1637. // local date into the given string buffer. It returns the number of
  1638. // characters written to the string buffer.
  1639. //
  1640. // SECURITY: If an attempt is made to overrun our static buffer, return 0
  1641. // to trigger failure.
  1642. //
  1643. // 04-30-93 JulieB Created.
  1644. ////////////////////////////////////////////////////////////////////////////
  1645. int ParseDate(
  1646. PLOC_HASH pHashN,
  1647. DWORD dwFlags,
  1648. LPSYSTEMTIME pLocalDate,
  1649. LPWSTR pFormat,
  1650. LPWSTR pDateStr,
  1651. CALID CalNum,
  1652. PCALENDAR_VAR pCalInfo,
  1653. BOOL fLunarLeap)
  1654. {
  1655. LPWSTR pPos; // ptr to pDateStr current position
  1656. LPWSTR pTemp; // ptr to temp position in format string
  1657. int Repeat; // number of repetitions of same letter
  1658. LPWORD pIncr; // ptr to increment amount (day, month)
  1659. WORD Incr; // increment amount
  1660. BOOL fDayExists = FALSE; // numeric day precedes or follows month
  1661. WORD Year; // year value
  1662. LPWORD pRange = NULL; // ptr to era ranges
  1663. LPWORD pInfo; // ptr to locale or calendar info
  1664. LPWORD pInfoC; // ptr to calendar info
  1665. WCHAR szHebrew[10]; // buffer for Hebrew
  1666. size_t cchRemaining; // Count of how many charactrs are left in pDateStr
  1667. //
  1668. // Initialize position pointer.
  1669. //
  1670. pPos = pDateStr;
  1671. cchRemaining = MAX_DATETIME_BUFFER;
  1672. //
  1673. // Parse through loop and store the appropriate date information
  1674. // in the pDateStr buffer.
  1675. //
  1676. while (*pFormat)
  1677. {
  1678. switch (*pFormat)
  1679. {
  1680. case ( L'd' ) :
  1681. {
  1682. //
  1683. // Insert the layout direction flag, if requested.
  1684. //
  1685. NLS_INSERT_BIDI_MARK(pPos, dwFlags, cchRemaining, 0);
  1686. //
  1687. // Get the number of 'd' repetitions in the format string.
  1688. //
  1689. pFormat++;
  1690. for (Repeat = 0; (*pFormat == L'd'); Repeat++, pFormat++)
  1691. ;
  1692. switch (Repeat)
  1693. {
  1694. case ( 0 ) :
  1695. case ( 1 ) :
  1696. {
  1697. //
  1698. // Set flag for day preceding month. The flag
  1699. // will be used when the MMMM case follows the
  1700. // d or dd case.
  1701. //
  1702. fDayExists = TRUE;
  1703. //
  1704. // Special case the Hebrew calendar.
  1705. //
  1706. if (CalNum == CAL_HEBREW)
  1707. {
  1708. //
  1709. // Convert Day number to Hebrew letter and
  1710. // write it to the buffer.
  1711. //
  1712. if( ! (NumberToHebrewLetter( pLocalDate->wDay,
  1713. szHebrew,
  1714. ARRAYSIZE(szHebrew) )))
  1715. {
  1716. //
  1717. // Operation tried to overrun the static buffer on the stack
  1718. //
  1719. return(0);
  1720. }
  1721. NLS_COPY_UNICODE_STR(pPos, cchRemaining, szHebrew, 0);
  1722. break;
  1723. }
  1724. //
  1725. // Repeat Value:
  1726. // 0 : Use NO leading zero for the day of the month
  1727. // 1 : Use leading zero for the day of the month
  1728. // The pPos pointer will be advanced in the macro.
  1729. // The cchRemaining value will be updated in the macro.
  1730. //
  1731. NLS_PAD_INT_TO_UNICODE_STR( pLocalDate->wDay,
  1732. 10,
  1733. (UINT)(Repeat + 1),
  1734. pPos,
  1735. cchRemaining,
  1736. 0 );
  1737. break;
  1738. }
  1739. case ( 2 ) :
  1740. {
  1741. //
  1742. // Set flag for day preceding month to be FALSE.
  1743. //
  1744. fDayExists = FALSE;
  1745. //
  1746. // Get the abbreviated name for the day of the
  1747. // week.
  1748. // The pPos pointer will be advanced in the macro.
  1749. // The cchRemaining value will be updated in the macro.
  1750. //
  1751. // NOTE: LocalTime structure uses:
  1752. // 0 = Sun, 1 = Mon, etc.
  1753. // Locale file uses:
  1754. // SAbbrevDayName1 = Mon, etc.
  1755. //
  1756. if (pCalInfo->IfNames &&
  1757. (pHashN->Locale != MAKELCID(MAKELANGID(LANG_DIVEHI,SUBLANG_DEFAULT),SORT_DEFAULT )))
  1758. {
  1759. pInfo = (LPWORD)pCalInfo;
  1760. pIncr = &(pCalInfo->SAbbrevDayName1);
  1761. }
  1762. else
  1763. {
  1764. pInfo = (LPWORD)(pHashN->pLocaleHdr);
  1765. pIncr = &(pHashN->pLocaleHdr->SAbbrevDayName1);
  1766. }
  1767. pIncr += (((pLocalDate->wDayOfWeek) + 6) % 7);
  1768. //
  1769. // Copy the abbreviated day name.
  1770. //
  1771. NLS_COPY_UNICODE_STR(pPos, cchRemaining, ((LPWORD)(pInfo) + *pIncr), 0);
  1772. break;
  1773. }
  1774. case ( 3 ) :
  1775. default :
  1776. {
  1777. //
  1778. // Set flag for day preceding month to be FALSE.
  1779. //
  1780. fDayExists = FALSE;
  1781. //
  1782. // Get the full name for the day of the week.
  1783. // The pPos pointer will be advanced in the macro.
  1784. // The cchRemaining value will be updated in the macro.
  1785. //
  1786. // NOTE: LocalTime structure uses:
  1787. // 0 = Sunday, 1 = Monday, etc.
  1788. // Locale file uses:
  1789. // SAbbrevDayName1 = Monday, etc.
  1790. //
  1791. if (pCalInfo->IfNames &&
  1792. (pHashN->Locale != MAKELCID(MAKELANGID(LANG_DIVEHI,SUBLANG_DEFAULT),SORT_DEFAULT )))
  1793. {
  1794. pInfo = (LPWORD)pCalInfo;
  1795. pIncr = &(pCalInfo->SDayName1);
  1796. }
  1797. else
  1798. {
  1799. pInfo = (LPWORD)(pHashN->pLocaleHdr);
  1800. pIncr = &(pHashN->pLocaleHdr->SDayName1);
  1801. }
  1802. pIncr += (((pLocalDate->wDayOfWeek) + 6) % 7);
  1803. //
  1804. // Copy the abbreviated day name.
  1805. //
  1806. NLS_COPY_UNICODE_STR(pPos, cchRemaining, ((LPWORD)(pInfo) + *pIncr), 0);
  1807. break;
  1808. }
  1809. }
  1810. break;
  1811. }
  1812. case ( L'M' ) :
  1813. {
  1814. //
  1815. // Insert the layout direction flag, if requested.
  1816. //
  1817. NLS_INSERT_BIDI_MARK(pPos, dwFlags, cchRemaining, 0);
  1818. //
  1819. // Get the number of 'M' repetitions in the format string.
  1820. //
  1821. pFormat++;
  1822. for (Repeat = 0; (*pFormat == L'M'); Repeat++, pFormat++)
  1823. ;
  1824. switch (Repeat)
  1825. {
  1826. case ( 0 ) :
  1827. case ( 1 ) :
  1828. {
  1829. //
  1830. // Special case the Hebrew calendar.
  1831. //
  1832. if (CalNum == CAL_HEBREW)
  1833. {
  1834. //
  1835. // Convert Month number to Hebrew letter and
  1836. // write it to the buffer.
  1837. //
  1838. if( ! (NumberToHebrewLetter( pLocalDate->wMonth,
  1839. szHebrew,
  1840. ARRAYSIZE(szHebrew) )))
  1841. {
  1842. //
  1843. // Operation tried to overrun the static buffer on the stack
  1844. //
  1845. return(0);
  1846. }
  1847. NLS_COPY_UNICODE_STR(pPos, cchRemaining, szHebrew, 0);
  1848. break;
  1849. }
  1850. //
  1851. // Repeat Value:
  1852. // 0 : Use NO leading zero for the month
  1853. // 1 : Use leading zero for the month
  1854. // The pPos pointer will be advanced in the macro.
  1855. // The cchRemaining value will be updated in the macro.
  1856. //
  1857. NLS_PAD_INT_TO_UNICODE_STR( pLocalDate->wMonth,
  1858. 10,
  1859. (UINT)(Repeat + 1),
  1860. pPos,
  1861. cchRemaining,
  1862. 0 );
  1863. break;
  1864. }
  1865. case ( 2 ) :
  1866. case ( 3 ) :
  1867. default :
  1868. {
  1869. //
  1870. // Check for abbreviated or full month name.
  1871. //
  1872. if (Repeat == 2)
  1873. {
  1874. pInfoC = &(pCalInfo->SAbbrevMonthName1);
  1875. pInfo = &(pHashN->pLocaleHdr->SAbbrevMonthName1);
  1876. }
  1877. else
  1878. {
  1879. pInfoC = &(pCalInfo->SMonthName1);
  1880. pInfo = &(pHashN->pLocaleHdr->SMonthName1);
  1881. }
  1882. //
  1883. // Get the abbreviated name of the month.
  1884. // The pPos pointer will be advanced in the macro.
  1885. // The cchRemaining value will be updated in the macro.
  1886. //
  1887. if (pCalInfo->IfNames &&
  1888. (pHashN->Locale != MAKELCID(MAKELANGID(LANG_DIVEHI,SUBLANG_DEFAULT),SORT_DEFAULT )))
  1889. {
  1890. if ((CalNum == CAL_HEBREW) &&
  1891. (!fLunarLeap) &&
  1892. (pLocalDate->wMonth > NLS_HEBREW_JUNE))
  1893. {
  1894. //
  1895. // Go passed Addar_B.
  1896. //
  1897. pIncr = (pInfoC) +
  1898. (pLocalDate->wMonth);
  1899. }
  1900. else
  1901. {
  1902. pIncr = (pInfoC) +
  1903. (pLocalDate->wMonth - 1);
  1904. }
  1905. //
  1906. // Copy the abbreviated month name.
  1907. //
  1908. NLS_COPY_UNICODE_STR(pPos, cchRemaining, ((LPWORD)(pCalInfo) + *pIncr), 0);
  1909. }
  1910. else
  1911. {
  1912. pIncr = (pInfo) +
  1913. (pLocalDate->wMonth - 1);
  1914. //
  1915. // If we don't already have a numeric day
  1916. // preceding the month name, then check for
  1917. // a numeric day following the month name.
  1918. //
  1919. if (!fDayExists)
  1920. {
  1921. pTemp = pFormat;
  1922. while (*pTemp)
  1923. {
  1924. if ((*pTemp == L'g') || (*pTemp == L'y'))
  1925. {
  1926. break;
  1927. }
  1928. if (*pTemp == L'd')
  1929. {
  1930. for (Repeat = 0;
  1931. (*pTemp == L'd');
  1932. Repeat++, pTemp++)
  1933. ;
  1934. if ((Repeat == 1) || (Repeat == 2))
  1935. {
  1936. fDayExists = TRUE;
  1937. }
  1938. break;
  1939. }
  1940. pTemp++;
  1941. }
  1942. }
  1943. //
  1944. // Check for numeric day immediately preceding
  1945. // or following the month name.
  1946. //
  1947. if (fDayExists)
  1948. {
  1949. Incr = *pIncr + 1 +
  1950. NlsStrLenW(((LPWORD)(pHashN->pLocaleHdr) +
  1951. *pIncr));
  1952. if (Incr != *(pIncr + 1))
  1953. {
  1954. //
  1955. // Copy the special month name -
  1956. // 2nd one in list.
  1957. //
  1958. NLS_COPY_UNICODE_STR(pPos, cchRemaining, ((LPWORD)(pHashN->pLocaleHdr) + Incr), 0);
  1959. break;
  1960. }
  1961. }
  1962. //
  1963. // Just copy the month name.
  1964. //
  1965. NLS_COPY_UNICODE_STR(pPos, cchRemaining, ((LPWORD)(pHashN->pLocaleHdr) + *pIncr), 0);
  1966. }
  1967. break;
  1968. }
  1969. }
  1970. //
  1971. // Set flag for day preceding month to be FALSE.
  1972. //
  1973. fDayExists = FALSE;
  1974. break;
  1975. }
  1976. case ( L'y' ) :
  1977. {
  1978. //
  1979. // Insert the layout direction flag, if requested.
  1980. //
  1981. NLS_INSERT_BIDI_MARK(pPos, dwFlags, cchRemaining, 0);
  1982. //
  1983. // Get the number of 'y' repetitions in the format string.
  1984. //
  1985. pFormat++;
  1986. for (Repeat = 0; (*pFormat == L'y'); Repeat++, pFormat++)
  1987. ;
  1988. //
  1989. // Get proper year for calendar.
  1990. //
  1991. if (pCalInfo->NumRanges)
  1992. {
  1993. if (!pRange)
  1994. {
  1995. //
  1996. // Adjust the year for the given calendar.
  1997. //
  1998. Year = GetCalendarYear( &pRange,
  1999. CalNum,
  2000. pCalInfo,
  2001. pLocalDate->wYear,
  2002. pLocalDate->wMonth,
  2003. pLocalDate->wDay );
  2004. }
  2005. }
  2006. else
  2007. {
  2008. Year = pLocalDate->wYear;
  2009. }
  2010. //
  2011. // Special case the Hebrew calendar.
  2012. //
  2013. if (CalNum == CAL_HEBREW)
  2014. {
  2015. //
  2016. // Convert Year number to Hebrew letter and
  2017. // write it to the buffer.
  2018. //
  2019. if( ! (NumberToHebrewLetter(Year, szHebrew, ARRAYSIZE(szHebrew))))
  2020. {
  2021. //
  2022. // Operation tried to overrun the static buffer on the stack
  2023. //
  2024. return(0);
  2025. }
  2026. NLS_COPY_UNICODE_STR(pPos, cchRemaining, szHebrew, 0);
  2027. }
  2028. else
  2029. {
  2030. //
  2031. // Write the year string to the buffer.
  2032. //
  2033. switch (Repeat)
  2034. {
  2035. case ( 0 ) :
  2036. case ( 1 ) :
  2037. {
  2038. //
  2039. // 1-digit century or 2-digit century.
  2040. // The pPos pointer will be advanced in the macro.
  2041. // The cchRemaining value will be updated in the macro.
  2042. //
  2043. NLS_PAD_INT_TO_UNICODE_STR( (Year % 100),
  2044. 10,
  2045. (UINT)(Repeat + 1),
  2046. pPos,
  2047. cchRemaining,
  2048. 0 );
  2049. break;
  2050. }
  2051. case ( 2 ) :
  2052. case ( 3 ) :
  2053. default :
  2054. {
  2055. //
  2056. // Full century.
  2057. // The pPos pointer will be advanced in the macro.
  2058. // The cchRemaining value will be updated in the macro.
  2059. //
  2060. NLS_PAD_INT_TO_UNICODE_STR( Year,
  2061. 10,
  2062. 2,
  2063. pPos,
  2064. cchRemaining,
  2065. 0 );
  2066. break;
  2067. }
  2068. }
  2069. }
  2070. //
  2071. // Set flag for day preceding month to be FALSE.
  2072. //
  2073. fDayExists = FALSE;
  2074. break;
  2075. }
  2076. case ( L'g' ) :
  2077. {
  2078. //
  2079. // Insert the layout direction flag, if requested.
  2080. //
  2081. NLS_INSERT_BIDI_MARK(pPos, dwFlags, cchRemaining, 0);
  2082. //
  2083. // Get the number of 'g' repetitions in the format string.
  2084. //
  2085. // NOTE: It doesn't matter how many g repetitions
  2086. // there are. They all mean 'gg'.
  2087. //
  2088. pFormat++;
  2089. while (*pFormat == L'g')
  2090. {
  2091. pFormat++;
  2092. }
  2093. //
  2094. // Copy the era string for the current calendar.
  2095. //
  2096. if (pCalInfo->NumRanges)
  2097. {
  2098. //
  2099. // Make sure we have the pointer to the
  2100. // appropriate range.
  2101. //
  2102. if (!pRange)
  2103. {
  2104. //
  2105. // Get the pointer to the correct range and
  2106. // adjust the year for the given calendar.
  2107. //
  2108. Year = GetCalendarYear( &pRange,
  2109. CalNum,
  2110. pCalInfo,
  2111. pLocalDate->wYear,
  2112. pLocalDate->wMonth,
  2113. pLocalDate->wDay );
  2114. }
  2115. //
  2116. // Copy the era string to the buffer, if one exists.
  2117. //
  2118. if (pRange)
  2119. {
  2120. NLS_COPY_UNICODE_STR(pPos,
  2121. cchRemaining,
  2122. ((PERA_RANGE)pRange)->pYearStr +
  2123. NlsStrLenW(((PERA_RANGE)pRange)->pYearStr) + 1,
  2124. 0);
  2125. }
  2126. }
  2127. //
  2128. // Set flag for day preceding month to be FALSE.
  2129. //
  2130. fDayExists = FALSE;
  2131. break;
  2132. }
  2133. case ( NLS_CHAR_QUOTE ) :
  2134. {
  2135. //
  2136. // Insert the layout direction flag, if requested.
  2137. //
  2138. NLS_INSERT_BIDI_MARK(pPos, dwFlags, cchRemaining, 0);
  2139. //
  2140. // Any text enclosed within single quotes should be left
  2141. // in the date string in its exact form (without the
  2142. // quotes), unless it is an escaped single quote ('').
  2143. //
  2144. pFormat++;
  2145. while (*pFormat)
  2146. {
  2147. if (*pFormat != NLS_CHAR_QUOTE)
  2148. {
  2149. if( cchRemaining <= 1 )
  2150. {
  2151. // Our static buffer will be overrun if we continue, so bail
  2152. return(0);
  2153. }
  2154. //
  2155. // Still within the single quote, so copy
  2156. // the character to the buffer.
  2157. //
  2158. *pPos = *pFormat;
  2159. pFormat++;
  2160. pPos++;
  2161. cchRemaining--;
  2162. }
  2163. else
  2164. {
  2165. //
  2166. // Found another quote, so skip over it.
  2167. //
  2168. pFormat++;
  2169. //
  2170. // Make sure it's not an escaped single quote.
  2171. //
  2172. if (*pFormat == NLS_CHAR_QUOTE)
  2173. {
  2174. if( cchRemaining <= 1 )
  2175. {
  2176. // Our static buffer will be overrun if we continue, so bail
  2177. return(0);
  2178. }
  2179. //
  2180. // Escaped single quote, so just write the
  2181. // single quote.
  2182. //
  2183. *pPos = *pFormat;
  2184. pFormat++;
  2185. pPos++;
  2186. cchRemaining--;
  2187. }
  2188. else
  2189. {
  2190. //
  2191. // Found the end quote, so break out of loop.
  2192. //
  2193. break;
  2194. }
  2195. }
  2196. }
  2197. break;
  2198. }
  2199. default :
  2200. {
  2201. if( cchRemaining <= 1 )
  2202. {
  2203. // Our static buffer will be overrun if we continue, so bail
  2204. return(0);
  2205. }
  2206. //
  2207. // Store the character in the buffer. Should be the
  2208. // separator, but copy it even if it isn't.
  2209. //
  2210. *pPos = *pFormat;
  2211. pFormat++;
  2212. pPos++;
  2213. cchRemaining--;
  2214. break;
  2215. }
  2216. }
  2217. }
  2218. //
  2219. // Zero terminate the string.
  2220. //
  2221. *pPos = 0;
  2222. //
  2223. // Return the number of characters written to the buffer, including
  2224. // the null terminator.
  2225. //
  2226. return ((int)((pPos - pDateStr) + 1));
  2227. }
  2228. //-------------------------------------------------------------------------//
  2229. // MIDDLE EAST CALENDAR ROUTINES //
  2230. //-------------------------------------------------------------------------//
  2231. ////////////////////////////////////////////////////////////////////////////
  2232. //
  2233. // GetAbsoluteDate
  2234. //
  2235. // Gets the Absolute date for the given Gregorian date.
  2236. //
  2237. // Computes:
  2238. // Number of Days in Prior Years (both common and leap years) +
  2239. // Number of Days in Prior Months of Current Year +
  2240. // Number of Days in Current Month
  2241. //
  2242. // 12-04-96 JulieB Created.
  2243. ////////////////////////////////////////////////////////////////////////////
  2244. DWORD GetAbsoluteDate(
  2245. WORD Year,
  2246. WORD Month,
  2247. WORD Day)
  2248. {
  2249. DWORD AbsoluteDate = 0; // absolute date
  2250. DWORD GregMonthDays[13] = {0,31,59,90,120,151,181,212,243,273,304,334,365};
  2251. //
  2252. // Check to see if the current year is a Gregorian leap year.
  2253. // If so, add a day.
  2254. //
  2255. if (NLS_GREGORIAN_LEAP_YEAR(Year) && (Month > 2))
  2256. {
  2257. AbsoluteDate++;
  2258. }
  2259. //
  2260. // Add the Number of Days in the Prior Years.
  2261. //
  2262. if (Year = Year - 1)
  2263. {
  2264. AbsoluteDate += ((Year * 365L) + (Year / 4L) - (Year / 100L) + (Year / 400L));
  2265. }
  2266. //
  2267. // Add the Number of Days in the Prior Months of the Current Year.
  2268. //
  2269. AbsoluteDate += GregMonthDays[Month - 1];
  2270. //
  2271. // Add the Number of Days in the Current Month.
  2272. //
  2273. AbsoluteDate += (DWORD)Day;
  2274. //
  2275. // Return the absolute date.
  2276. //
  2277. return (AbsoluteDate);
  2278. }
  2279. //-------------------------------------------------------------------------//
  2280. // HIJRI CALENDAR ROUTINES //
  2281. //-------------------------------------------------------------------------//
  2282. ////////////////////////////////////////////////////////////////////////////
  2283. //
  2284. // GetHijriDate
  2285. //
  2286. // Converts the given Gregorian date to its equivalent Hijri (Islamic)
  2287. // date.
  2288. //
  2289. // Rules for the Hijri calendar:
  2290. // - The Hijri calendar is a strictly Lunar calendar.
  2291. // - Days begin at sunset.
  2292. // - Islamic Year 1 (Muharram 1, 1 A.H.) is equivalent to absolute date
  2293. // 227015 (Friday, July 16, 622 C.E. - Julian).
  2294. // - Leap Years occur in the 2, 5, 7, 10, 13, 16, 18, 21, 24, 26, & 29th
  2295. // years of a 30-year cycle. Year = leap iff ((11y+14) mod 30 < 11).
  2296. // - There are 12 months which contain alternately 30 and 29 days.
  2297. // - The 12th month, Dhu al-Hijjah, contains 30 days instead of 29 days
  2298. // in a leap year.
  2299. // - Common years have 354 days. Leap years have 355 days.
  2300. // - There are 10,631 days in a 30-year cycle.
  2301. // - The Islamic months are:
  2302. // 1. Muharram (30 days) 7. Rajab (30 days)
  2303. // 2. Safar (29 days) 8. Sha'ban (29 days)
  2304. // 3. Rabi I (30 days) 9. Ramadan (30 days)
  2305. // 4. Rabi II (29 days) 10. Shawwal (29 days)
  2306. // 5. Jumada I (30 days) 11. Dhu al-Qada (30 days)
  2307. // 6. Jumada II (29 days) 12. Dhu al-Hijjah (29 days) {30}
  2308. //
  2309. // 12-04-96 JulieB Created.
  2310. ////////////////////////////////////////////////////////////////////////////
  2311. void GetHijriDate(
  2312. LPSYSTEMTIME pDate,
  2313. DWORD dwFlags)
  2314. {
  2315. DWORD AbsoluteDate; // absolute date
  2316. DWORD HijriYear; // Hijri year
  2317. DWORD HijriMonth; // Hijri month
  2318. DWORD HijriDay; // Hijri day
  2319. DWORD NumDays; // number of days
  2320. DWORD HijriMonthDays[13] = {0,30,59,89,118,148,177,207,236,266,295,325,355};
  2321. //
  2322. // Get the absolute date.
  2323. //
  2324. AbsoluteDate = GetAbsoluteDate(pDate->wYear, pDate->wMonth, pDate->wDay);
  2325. //
  2326. // See how much we need to backup or advance
  2327. //
  2328. (LONG)AbsoluteDate += GetAdvanceHijriDate(dwFlags);
  2329. //
  2330. // Calculate the Hijri Year.
  2331. //
  2332. HijriYear = ((AbsoluteDate - 227013L) * 30L / 10631L) + 1;
  2333. if (AbsoluteDate <= DaysUpToHijriYear(HijriYear))
  2334. {
  2335. HijriYear--;
  2336. }
  2337. else if (AbsoluteDate > DaysUpToHijriYear(HijriYear + 1))
  2338. {
  2339. HijriYear++;
  2340. }
  2341. //
  2342. // Calculate the Hijri Month.
  2343. //
  2344. HijriMonth = 1;
  2345. NumDays = AbsoluteDate - DaysUpToHijriYear(HijriYear);
  2346. while ((HijriMonth <= 12) && (NumDays > HijriMonthDays[HijriMonth - 1]))
  2347. {
  2348. HijriMonth++;
  2349. }
  2350. HijriMonth--;
  2351. //
  2352. // Calculate the Hijri Day.
  2353. //
  2354. HijriDay = NumDays - HijriMonthDays[HijriMonth - 1];
  2355. //
  2356. // Save the Hijri date and return.
  2357. //
  2358. pDate->wYear = (WORD)HijriYear;
  2359. pDate->wMonth = (WORD)HijriMonth;
  2360. pDate->wDay = (WORD)HijriDay;
  2361. }
  2362. ////////////////////////////////////////////////////////////////////////////
  2363. //
  2364. // GetAdvanceHijriDate
  2365. //
  2366. // Gets the AddHijriDate value from the registry.
  2367. //
  2368. // 12-04-96 JulieB Created.
  2369. // 05-15-99 SamerA Support +/-3 Advance Hijri Date
  2370. ////////////////////////////////////////////////////////////////////////////
  2371. LONG GetAdvanceHijriDate(
  2372. DWORD dwFlags)
  2373. {
  2374. LONG lAdvance = 0L; // advance hijri date
  2375. HANDLE hKey = NULL; // handle to intl key
  2376. PKEY_VALUE_FULL_INFORMATION pKeyValueFull; // ptr to query info
  2377. BYTE pStatic[MAX_KEY_VALUE_FULLINFO]; // ptr to static buffer
  2378. BOOL IfAlloc = FALSE; // if buffer was allocated
  2379. WCHAR wszAddHijriRegValue[] = L"AddHijriDate"; // registry value
  2380. WCHAR wszAddHijriTempValue[] = L"AddHijriDateTemp"; // temp registry to use (intl.cpl use)
  2381. INT AddHijriStringLength;
  2382. PWSTR pwszValue;
  2383. LONG lData;
  2384. UNICODE_STRING ObUnicodeStr;
  2385. ULONG rc = 0L; // result code
  2386. //
  2387. // Open the Control Panel International registry key.
  2388. //
  2389. OPEN_CPANEL_INTL_KEY(hKey, lAdvance, KEY_READ);
  2390. //
  2391. // Query the registry for the AddHijriDate value.
  2392. //
  2393. pKeyValueFull = (PKEY_VALUE_FULL_INFORMATION)pStatic;
  2394. rc = QueryRegValue( hKey,
  2395. (dwFlags & DATE_ADDHIJRIDATETEMP) ?
  2396. wszAddHijriTempValue :
  2397. wszAddHijriRegValue,
  2398. &pKeyValueFull,
  2399. MAX_KEY_VALUE_FULLINFO,
  2400. &IfAlloc );
  2401. //
  2402. // Close the registry key.
  2403. //
  2404. CLOSE_REG_KEY(hKey);
  2405. //
  2406. // Get the base value length without the NULL terminating char.
  2407. //
  2408. AddHijriStringLength = (sizeof(wszAddHijriRegValue) / sizeof(WCHAR)) - 1;
  2409. //
  2410. // See if the AddHijriDate value is present.
  2411. //
  2412. if (rc != NO_ERROR)
  2413. {
  2414. return (lAdvance);
  2415. }
  2416. //
  2417. // See if the AddHijriDate data is present. If it is, parse the
  2418. // Advance Hijri amount.
  2419. //
  2420. pwszValue = GET_VALUE_DATA_PTR(pKeyValueFull);
  2421. if ((pKeyValueFull->DataLength > 2) &&
  2422. (wcsncmp(pwszValue, wszAddHijriRegValue, AddHijriStringLength) == 0))
  2423. {
  2424. RtlInitUnicodeString( &ObUnicodeStr,
  2425. &pwszValue[AddHijriStringLength]);
  2426. if (NT_SUCCESS(RtlUnicodeStringToInteger(&ObUnicodeStr,
  2427. 10,
  2428. &lData)))
  2429. {
  2430. if ((lData > -3L) && (lData < 3L))
  2431. {
  2432. //
  2433. // AddHijriDate and AddHijriDate-1 both mean -1.
  2434. //
  2435. if (lData == 0L)
  2436. {
  2437. lAdvance = -1L;
  2438. }
  2439. else
  2440. {
  2441. lAdvance = lData;
  2442. }
  2443. }
  2444. }
  2445. }
  2446. //
  2447. // Free the buffer used for the query.
  2448. //
  2449. if (IfAlloc)
  2450. {
  2451. NLS_FREE_MEM(pKeyValueFull);
  2452. }
  2453. //
  2454. // Return the result.
  2455. //
  2456. return (lAdvance);
  2457. }
  2458. ////////////////////////////////////////////////////////////////////////////
  2459. //
  2460. // DaysUpToHijriYear
  2461. //
  2462. // Gets the total number of days (absolute date) up to the given Hijri
  2463. // Year.
  2464. //
  2465. // 12-04-96 JulieB Created.
  2466. ////////////////////////////////////////////////////////////////////////////
  2467. DWORD DaysUpToHijriYear(
  2468. DWORD HijriYear)
  2469. {
  2470. DWORD NumDays; // number of absolute days
  2471. DWORD NumYear30; // number of years up to current 30 year cycle
  2472. DWORD NumYearsLeft; // number of years into 30 year cycle
  2473. //
  2474. // Compute the number of years up to the current 30 year cycle.
  2475. //
  2476. NumYear30 = ((HijriYear - 1) / 30) * 30;
  2477. //
  2478. // Compute the number of years left. This is the number of years
  2479. // into the 30 year cycle for the given year.
  2480. //
  2481. NumYearsLeft = HijriYear - NumYear30 - 1;
  2482. //
  2483. // Compute the number of absolute days up to the given year.
  2484. //
  2485. NumDays = ((NumYear30 * 10631L) / 30L) + 227013L;
  2486. while (NumYearsLeft)
  2487. {
  2488. NumDays += 354L + NLS_HIJRI_LEAP_YEAR(NumYearsLeft);
  2489. NumYearsLeft--;
  2490. }
  2491. //
  2492. // Return the number of absolute days.
  2493. //
  2494. return (NumDays);
  2495. }
  2496. //-------------------------------------------------------------------------//
  2497. // HEBREW CALENDAR ROUTINES //
  2498. //-------------------------------------------------------------------------//
  2499. //
  2500. // Jewish Era in use today is dated from the supposed year of the
  2501. // Creation with its beginning in 3761 B.C.
  2502. //
  2503. #define NLS_LUNAR_ERA_DIFF 3760
  2504. //
  2505. // Hebrew Translation Table.
  2506. //
  2507. CONST BYTE HebrewTable[] =
  2508. {
  2509. 99,99,99,99,99,99,99,99,99,99,
  2510. 99,99,99,99,99,99,99,99,99,99,
  2511. 99,99,99,99,99,99,99,99,99,99,
  2512. 99,99,99,99,99,99,99,99,99,99,
  2513. 99,99,99,99,99,99,99,99,99,99,
  2514. 99,99,99,99,99,99,99,99,99,99,
  2515. 99,99,99,99,99,99,99,99,99,99,
  2516. 99,99,99,99,99,99,99,99,99,99,
  2517. 99,99,99,99,99,99,99,99,99,99,
  2518. 99,99,99,99,99,99,99,99,99,99,
  2519. 99,99,99,99,99,99,99,99,99,99,
  2520. 99,99,99,99,99,99,99,99,99,99,
  2521. 99,99,99,99,99,99,99,99,99,99,
  2522. 99,99,99,99,99,99,99,99,99,99,
  2523. 99,99,99,99,99,99,99,99,99,99,
  2524. 99,99,99,99,99,99,99,99,99,99,
  2525. 99,99,99,99,99,99,7,3,17,3,
  2526. 0,4,11,2,21,6,1,3,13,2,
  2527. 25,4,5,3,16,2,27,6,9,1,
  2528. 20,2,0,6,11,3,23,4,4,2,
  2529. 14,3,27,4,8,2,18,3,28,6,
  2530. 11,1,22,5,2,3,12,3,25,4,
  2531. 6,2,16,3,26,6,8,2,20,1,
  2532. 0,6,11,2,24,4,4,3,15,2,
  2533. 25,6,8,1,19,2,29,6,9,3,
  2534. 22,4,3,2,13,3,25,4,6,3,
  2535. 17,2,27,6,7,3,19,2,31,4,
  2536. 11,3,23,4,5,2,15,3,25,6,
  2537. 6,2,19,1,29,6,10,2,22,4,
  2538. 3,3,14,2,24,6,6,1,17,3,
  2539. 28,5,8,3,20,1,32,5,12,3,
  2540. 22,6,4,1,16,2,26,6,6,3,
  2541. 17,2,0,4,10,3,22,4,3,2,
  2542. 14,3,24,6,5,2,17,1,28,6,
  2543. 9,2,19,3,31,4,13,2,23,6,
  2544. 3,3,15,1,27,5,7,3,17,3,
  2545. 29,4,11,2,21,6,3,1,14,2,
  2546. 25,6,5,3,16,2,28,4,9,3,
  2547. 20,2,0,6,12,1,23,6,4,2,
  2548. 14,3,26,4,8,2,18,3,0,4,
  2549. 10,3,21,5,1,3,13,1,24,5,
  2550. 5,3,15,3,27,4,8,2,19,3,
  2551. 29,6,10,2,22,4,3,3,14,2,
  2552. 26,4,6,3,18,2,28,6,10,1,
  2553. 20,6,2,2,12,3,24,4,5,2,
  2554. 16,3,28,4,8,3,19,2,0,6,
  2555. 12,1,23,5,3,3,14,3,26,4,
  2556. 7,2,17,3,28,6,9,2,21,4,
  2557. 1,3,13,2,25,4,5,3,16,2,
  2558. 27,6,9,1,19,3,0,5,11,3,
  2559. 23,4,4,2,14,3,25,6,7,1,
  2560. 18,2,28,6,9,3,21,4,2,2,
  2561. 12,3,25,4,6,2,16,3,26,6,
  2562. 8,2,20,1,0,6,11,2,22,6,
  2563. 4,1,15,2,25,6,6,3,18,1,
  2564. 29,5,9,3,22,4,2,3,13,2,
  2565. 23,6,4,3,15,2,27,4,7,3,
  2566. 19,2,31,4,11,3,21,6,3,2,
  2567. 15,1,25,6,6,2,17,3,29,4,
  2568. 10,2,20,6,3,1,13,3,24,5,
  2569. 4,3,16,1,27,5,7,3,17,3,
  2570. 0,4,11,2,21,6,1,3,13,2,
  2571. 25,4,5,3,16,2,29,4,9,3,
  2572. 19,6,30,2,13,1,23,6,4,2,
  2573. 14,3,27,4,8,2,18,3,0,4,
  2574. 11,3,22,5,2,3,14,1,26,5,
  2575. 6,3,16,3,28,4,10,2,20,6,
  2576. 30,3,11,2,24,4,4,3,15,2,
  2577. 25,6,8,1,19,2,29,6,9,3,
  2578. 22,4,3,2,13,3,25,4,7,2,
  2579. 17,3,27,6,9,1,21,5,1,3,
  2580. 11,3,23,4,5,2,15,3,25,6,
  2581. 6,2,19,1,29,6,10,2,22,4,
  2582. 3,3,14,2,24,6,6,1,18,2,
  2583. 28,6,8,3,20,4,2,2,12,3,
  2584. 24,4,4,3,16,2,26,6,6,3,
  2585. 17,2,0,4,10,3,22,4,3,2,
  2586. 14,3,24,6,5,2,17,1,28,6,
  2587. 9,2,21,4,1,3,13,2,23,6,
  2588. 5,1,15,3,27,5,7,3,19,1,
  2589. 0,5,10,3,22,4,2,3,13,2,
  2590. 24,6,4,3,15,2,27,4,8,3,
  2591. 20,4,1,2,11,3,22,6,3,2,
  2592. 15,1,25,6,7,2,17,3,29,4,
  2593. 10,2,21,6,1,3,13,1,24,5,
  2594. 5,3,15,3,27,4,8,2,19,6,
  2595. 1,1,12,2,22,6,3,3,14,2,
  2596. 26,4,6,3,18,2,28,6,10,1,
  2597. 20,6,2,2,12,3,24,4,5,2,
  2598. 16,3,28,4,9,2,19,6,30,3,
  2599. 12,1,23,5,3,3,14,3,26,4,
  2600. 7,2,17,3,28,6,9,2,21,4,
  2601. 1,3,13,2,25,4,5,3,16,2,
  2602. 27,6,9,1,19,6,30,2,11,3,
  2603. 23,4,4,2,14,3,27,4,7,3,
  2604. 18,2,28,6,11,1,22,5,2,3,
  2605. 12,3,25,4,6,2,16,3,26,6,
  2606. 8,2,20,4,30,3,11,2,24,4,
  2607. 4,3,15,2,25,6,8,1,18,3,
  2608. 29,5,9,3,22,4,3,2,13,3,
  2609. 23,6,6,1,17,2,27,6,7,3,
  2610. 20,4,1,2,11,3,23,4,5,2,
  2611. 15,3,25,6,6,2,19,1,29,6,
  2612. 10,2,20,6,3,1,14,2,24,6,
  2613. 4,3,17,1,28,5,8,3,20,4,
  2614. 1,3,12,2,22,6,2,3,14,2,
  2615. 26,4,6,3,17,2,0,4,10,3,
  2616. 20,6,1,2,14,1,24,6,5,2,
  2617. 15,3,28,4,9,2,19,6,1,1,
  2618. 12,3,23,5,3,3,15,1,27,5,
  2619. 7,3,17,3,29,4,11,2,21,6,
  2620. 1,3,12,2,25,4,5,3,16,2,
  2621. 28,4,9,3,19,6,30,2,12,1,
  2622. 23,6,4,2,14,3,26,4,8,2,
  2623. 18,3,0,4,10,3,22,5,2,3,
  2624. 14,1,25,5,6,3,16,3,28,4,
  2625. 9,2,20,6,30,3,11,2,23,4,
  2626. 4,3,15,2,27,4,7,3,19,2,
  2627. 29,6,11,1,21,6,3,2,13,3,
  2628. 25,4,6,2,17,3,27,6,9,1,
  2629. 20,5,30,3,10,3,22,4,3,2,
  2630. 14,3,24,6,5,2,17,1,28,6,
  2631. 9,2,21,4,1,3,13,2,23,6,
  2632. 5,1,16,2,27,6,7,3,19,4,
  2633. 30,2,11,3,23,4,3,3,14,2,
  2634. 25,6,5,3,16,2,28,4,9,3,
  2635. 21,4,2,2,12,3,23,6,4,2,
  2636. 16,1,26,6,8,2,20,4,30,3,
  2637. 11,2,22,6,4,1,14,3,25,5,
  2638. 6,3,18,1,29,5,9,3,22,4,
  2639. 2,3,13,2,23,6,4,3,15,2,
  2640. 27,4,7,3,20,4,1,2,11,3,
  2641. 21,6,3,2,15,1,25,6,6,2,
  2642. 17,3,29,4,10,2,20,6,3,1,
  2643. 13,3,24,5,4,3,17,1,28,5,
  2644. 8,3,18,6,1,1,12,2,22,6,
  2645. 2,3,14,2,26,4,6,3,17,2,
  2646. 28,6,10,1,20,6,1,2,12,3,
  2647. 24,4,5,2,15,3,28,4,9,2,
  2648. 19,6,33,3,12,1,23,5,3,3,
  2649. 13,3,25,4,6,2,16,3,26,6,
  2650. 8,2,20,4,30,3,11,2,24,4,
  2651. 4,3,15,2,25,6,8,1,18,6,
  2652. 33,2,9,3,22,4,3,2,13,3,
  2653. 25,4,6,3,17,2,27,6,9,1,
  2654. 21,5,1,3,11,3,23,4,5,2,
  2655. 15,3,25,6,6,2,19,4,33,3,
  2656. 10,2,22,4,3,3,14,2,24,6,
  2657. 6,1,99,99,99,99,99,99,99,99,
  2658. 99,99,99,99,99,99,99,99,99,99,
  2659. 99,99
  2660. };
  2661. //
  2662. // The lunar calendar has 6 different variations of month lengths
  2663. // within a year.
  2664. //
  2665. CONST BYTE LunarMonthLen[7][14] =
  2666. {
  2667. 0,00,00,00,00,00,00,00,00,00,00,00,00,0,
  2668. 0,30,29,29,29,30,29,30,29,30,29,30,29,0, // 3 common year variations
  2669. 0,30,29,30,29,30,29,30,29,30,29,30,29,0,
  2670. 0,30,30,30,29,30,29,30,29,30,29,30,29,0,
  2671. 0,30,29,29,29,30,30,29,30,29,30,29,30,29, // 3 leap year variations
  2672. 0,30,29,30,29,30,30,29,30,29,30,29,30,29,
  2673. 0,30,30,30,29,30,30,29,30,29,30,29,30,29
  2674. };
  2675. ////////////////////////////////////////////////////////////////////////////
  2676. //
  2677. // GetHebrewDate
  2678. //
  2679. // Converts the given Gregorian date to its equivalent Hebrew date.
  2680. //
  2681. // Rules for the Hebrew calendar:
  2682. // - The Hebrew calendar is both a Lunar (months) and Solar (years)
  2683. // calendar, but allows for a week of seven days.
  2684. // - Days begin at sunset.
  2685. // - Leap Years occur in the 3, 6, 8, 11, 14, 17, & 19th years of a
  2686. // 19-year cycle. Year = leap iff ((7y+1) mod 19 < 7).
  2687. // - There are 12 months in a common year and 13 months in a leap year.
  2688. // - In a common year, the 12th month, Adar, has 29 days. In a leap
  2689. // year, the 12th month, Adar I, has 30 days and the 13th month,
  2690. // Adar II, has 29 days.
  2691. // - Common years have 353-355 days. Leap years have 383-385 days.
  2692. // - The Hebrew new year (Rosh HaShanah) begins on the 1st of Tishri,
  2693. // the 7th month in the list below.
  2694. // - The new year may not begin on Sunday, Wednesday, or Friday.
  2695. // - If the new year would fall on a Tuesday and the conjunction of
  2696. // the following year were at midday or later, the new year is
  2697. // delayed until Thursday.
  2698. // - If the new year would fall on a Monday after a leap year, the
  2699. // new year is delayed until Tuesday.
  2700. // - The length of the 8th and 9th months vary from year to year,
  2701. // depending on the overall length of the year.
  2702. // - The length of a year is determined by the dates of the new
  2703. // years (Tishri 1) preceding and following the year in question.
  2704. // - The 8th month is long (30 days) if the year has 355 or 385 days.
  2705. // - The 9th month is short (29 days) if the year has 353 or 383 days.
  2706. // - The Hebrew months are:
  2707. // 1. Nisan (30 days) 7. Tishri (30 days)
  2708. // 2. Iyyar (29 days) 8. Heshvan (29 or 30 days)
  2709. // 3. Sivan (30 days) 9. Kislev (29 or 30 days)
  2710. // 4. Tammuz (29 days) 10. Teveth (29 days)
  2711. // 5. Av (30 days) 11. Shevat (30 days)
  2712. // 6. Elul (29 days) {12. Adar I (30 days)}
  2713. // 12. {13.} Adar {II}(29 days)
  2714. //
  2715. // 12-04-96 JulieB Created.
  2716. ////////////////////////////////////////////////////////////////////////////
  2717. BOOL GetHebrewDate(
  2718. LPSYSTEMTIME pDate,
  2719. LPBOOL pLunarLeap)
  2720. {
  2721. WORD Year, Month, Day; // initial year, month, day
  2722. WORD WeekDay; // day of the week
  2723. BYTE LunarYearCode; // lunar year code
  2724. BYTE LunarMonth, LunarDay; // lunar month and day for Jan 1
  2725. DWORD Absolute1600; // absolute date 1/1/1600
  2726. DWORD AbsoluteDate; // absolute date - absolute date 1/1/1600
  2727. LONG NumDays; // number of days since 1/1
  2728. CONST BYTE *pLunarMonthLen; // ptr to lunar month length array
  2729. //
  2730. // Save the Gregorian date values.
  2731. //
  2732. Year = pDate->wYear;
  2733. Month = pDate->wMonth;
  2734. Day = pDate->wDay;
  2735. //
  2736. // Make sure we have a valid Gregorian date that will fit into our
  2737. // Hebrew conversion limits.
  2738. //
  2739. if (!IsValidDateForHebrew(Year, Month, Day))
  2740. {
  2741. return (FALSE);
  2742. }
  2743. //
  2744. // Get the offset into the LunarMonthLen array and the lunar day
  2745. // for January 1st.
  2746. //
  2747. LunarYearCode = HebrewTable[(Year - 1500) * 2 + 1];
  2748. LunarDay = HebrewTable[(Year - 1500) * 2];
  2749. //
  2750. // See if it's a Lunar leap year.
  2751. //
  2752. *pLunarLeap = (LunarYearCode >= 4);
  2753. //
  2754. // Get the Lunar Month.
  2755. //
  2756. switch (LunarDay)
  2757. {
  2758. case ( 0 ) : // 1/1 is on Shvat 1
  2759. {
  2760. LunarMonth = 5;
  2761. LunarDay = 1;
  2762. break;
  2763. }
  2764. case ( 30 ) : // 1/1 is on Kislev 30
  2765. {
  2766. LunarMonth = 3;
  2767. break;
  2768. }
  2769. case ( 31 ) : // 1/1 is on Shvat 2
  2770. {
  2771. LunarMonth = 5;
  2772. LunarDay = 2;
  2773. break;
  2774. }
  2775. case ( 32 ) : // 1/1 is on Shvat 3
  2776. {
  2777. LunarMonth = 5;
  2778. LunarDay = 3;
  2779. break;
  2780. }
  2781. case ( 33 ) : // 1/1 is on Kislev 29
  2782. {
  2783. LunarMonth = 3;
  2784. LunarDay = 29;
  2785. break;
  2786. }
  2787. default : // 1/1 is on Tevet
  2788. {
  2789. LunarMonth = 4;
  2790. break;
  2791. }
  2792. }
  2793. //
  2794. // Store the values for the start of the new year - 1/1.
  2795. //
  2796. pDate->wYear = Year + NLS_LUNAR_ERA_DIFF;
  2797. pDate->wMonth = (WORD)LunarMonth;
  2798. pDate->wDay = (WORD)LunarDay;
  2799. //
  2800. // Get the absolute date from 1/1/1600.
  2801. //
  2802. Absolute1600 = GetAbsoluteDate(1600, 1, 1);
  2803. AbsoluteDate = GetAbsoluteDate(Year, Month, Day) - Absolute1600;
  2804. //
  2805. // Compute and save the day of the week (Sunday = 0).
  2806. //
  2807. WeekDay = (WORD)(AbsoluteDate % 7);
  2808. pDate->wDayOfWeek = (WeekDay) ? (WeekDay - 1) : 6;
  2809. //
  2810. // If the requested date was 1/1, then we're done.
  2811. //
  2812. if ((Month == 1) && (Day == 1))
  2813. {
  2814. return (TRUE);
  2815. }
  2816. //
  2817. // Calculate the number of days between 1/1 and the requested date.
  2818. //
  2819. NumDays = (LONG)(AbsoluteDate - (GetAbsoluteDate(Year, 1, 1) - Absolute1600));
  2820. //
  2821. // If the requested date is within the current lunar month, then
  2822. // we're done.
  2823. //
  2824. pLunarMonthLen = &(LunarMonthLen[LunarYearCode][0]);
  2825. if ((NumDays + (LONG)LunarDay) <= (LONG)(pLunarMonthLen[LunarMonth]))
  2826. {
  2827. pDate->wDay += (WORD)NumDays;
  2828. return (TRUE);
  2829. }
  2830. //
  2831. // Adjust for the current partial month.
  2832. //
  2833. pDate->wMonth++;
  2834. pDate->wDay = 1;
  2835. //
  2836. // Adjust the Lunar Month and Year (if necessary) based on the number
  2837. // of days between 1/1 and the requested date.
  2838. //
  2839. // Assumes Jan 1 can never translate to the last Lunar month, which
  2840. // is true.
  2841. //
  2842. NumDays -= (LONG)(pLunarMonthLen[LunarMonth] - LunarDay);
  2843. if (NumDays == 1)
  2844. {
  2845. return (TRUE);
  2846. }
  2847. //
  2848. // Get the final Hebrew date.
  2849. //
  2850. do
  2851. {
  2852. //
  2853. // See if we're on the correct Lunar month.
  2854. //
  2855. if (NumDays <= (LONG)(pLunarMonthLen[pDate->wMonth]))
  2856. {
  2857. //
  2858. // Found the right Lunar month.
  2859. //
  2860. pDate->wDay += (WORD)(NumDays - 1);
  2861. return (TRUE);
  2862. }
  2863. else
  2864. {
  2865. //
  2866. // Adjust the number of days and move to the next month.
  2867. //
  2868. NumDays -= (LONG)(pLunarMonthLen[pDate->wMonth++]);
  2869. //
  2870. // See if we need to adjust the Year.
  2871. // Must handle both 12 and 13 month years.
  2872. //
  2873. if ((pDate->wMonth > 13) || (pLunarMonthLen[pDate->wMonth] == 0))
  2874. {
  2875. //
  2876. // Adjust the Year.
  2877. //
  2878. pDate->wYear++;
  2879. LunarYearCode = HebrewTable[(Year + 1 - 1500) * 2 + 1];
  2880. pLunarMonthLen = &(LunarMonthLen[LunarYearCode][0]);
  2881. //
  2882. // Adjust the Month.
  2883. //
  2884. pDate->wMonth = 1;
  2885. //
  2886. // See if this new Lunar year is a leap year.
  2887. //
  2888. *pLunarLeap = (LunarYearCode >= 4);
  2889. }
  2890. }
  2891. } while (NumDays > 0);
  2892. //
  2893. // Return success.
  2894. //
  2895. return (TRUE);
  2896. }
  2897. ////////////////////////////////////////////////////////////////////////////
  2898. //
  2899. // IsValidDateForHebrew
  2900. //
  2901. // Checks to be sure the given Gregorian date is valid. This validation
  2902. // requires that the year be between 1600 and 2239. If it is, it
  2903. // returns TRUE. Otherwise, it returns FALSE.
  2904. //
  2905. // 12-04-96 JulieB Created.
  2906. ////////////////////////////////////////////////////////////////////////////
  2907. BOOL IsValidDateForHebrew(
  2908. WORD Year,
  2909. WORD Month,
  2910. WORD Day)
  2911. {
  2912. WORD GregMonthLen[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
  2913. //
  2914. // Make sure the Year is between 1600 and 2239.
  2915. //
  2916. if ((Year < 1600) || (Year > 2239))
  2917. {
  2918. return (FALSE);
  2919. }
  2920. //
  2921. // Make sure the Month is between 1 and 12.
  2922. //
  2923. if ((Month < 1) || (Month > 12))
  2924. {
  2925. return (FALSE);
  2926. }
  2927. //
  2928. // See if it's a Gregorian leap year. If so, make sure February
  2929. // is allowed to have 29 days.
  2930. //
  2931. if (NLS_GREGORIAN_LEAP_YEAR(Year))
  2932. {
  2933. GregMonthLen[2] = 29;
  2934. }
  2935. //
  2936. // Make sure the Day is within the correct range for the given Month.
  2937. //
  2938. if ((Day < 1) || (Day > GregMonthLen[Month]))
  2939. {
  2940. return (FALSE);
  2941. }
  2942. //
  2943. // Return success.
  2944. //
  2945. return (TRUE);
  2946. }
  2947. ////////////////////////////////////////////////////////////////////////////
  2948. //
  2949. // NumberToHebrewLetter
  2950. //
  2951. // Converts the given number to Hebrew letters according to the numeric
  2952. // value of each Hebrew letter. Basically, this converts the lunar year
  2953. // and the lunar month to letters.
  2954. //
  2955. // The character of a year is described by three letters of the Hebrew
  2956. // alphabet, the first and third giving, respectively, the days of the
  2957. // weeks on which the New Year occurs and Passover begins, while the
  2958. // second is the initial of the Hebrew word for defective, normal, or
  2959. // complete.
  2960. //
  2961. // Defective Year : Both Heshvan and Kislev are defective (353 or 383 days)
  2962. // Normal Year : Heshvan is defective, Kislev is full (354 or 384 days)
  2963. // Complete Year : Both Heshvan and Kislev are full (355 or 385 days)
  2964. //
  2965. // 12-04-96 JulieB Created.
  2966. ////////////////////////////////////////////////////////////////////////////
  2967. BOOL NumberToHebrewLetter(
  2968. DWORD Number,
  2969. LPWSTR szHebrew,
  2970. int cchSize)
  2971. {
  2972. WCHAR szHundreds[4]; // temp buffer for hundreds
  2973. WCHAR cTens, cUnits; // tens and units chars
  2974. DWORD Hundreds, Tens; // hundreds and tens values
  2975. WCHAR szTemp[10]; // temp buffer
  2976. LPWSTR pTemp = szTemp; // temp ptr to temp buffer
  2977. int Length, Ctr; // loop counters
  2978. //
  2979. // Sanity check.
  2980. //
  2981. if (cchSize > 10)
  2982. {
  2983. return (FALSE);
  2984. }
  2985. //
  2986. // Adjust the number if greater than 5000.
  2987. //
  2988. if (Number > 5000)
  2989. {
  2990. Number -= 5000;
  2991. }
  2992. //
  2993. // Clear out the temp buffer.
  2994. //
  2995. RtlZeroMemory(szHundreds, sizeof(szHundreds));
  2996. //
  2997. // Get the Hundreds.
  2998. //
  2999. Hundreds = Number / 100;
  3000. if (Hundreds)
  3001. {
  3002. Number -= Hundreds * 100;
  3003. if (Hundreds > 3)
  3004. {
  3005. szHundreds[2] = L'\x05ea'; // Hebrew Letter Tav
  3006. Hundreds -= 4;
  3007. }
  3008. if (Hundreds > 3)
  3009. {
  3010. szHundreds[1] = L'\x05ea'; // Hebrew Letter Tav
  3011. Hundreds -= 4;
  3012. }
  3013. if (Hundreds > 0)
  3014. {
  3015. if (!szHundreds[1])
  3016. {
  3017. szHundreds[1] = (WCHAR)(L'\x05e6' + Hundreds);
  3018. }
  3019. else
  3020. {
  3021. szHundreds[0] = (WCHAR)(L'\x05e6' + Hundreds);
  3022. }
  3023. }
  3024. if (!szHundreds[1])
  3025. {
  3026. szHundreds[0] = szHundreds[2];
  3027. }
  3028. else
  3029. {
  3030. if (!szHundreds[0])
  3031. {
  3032. szHundreds[0] = szHundreds[1];
  3033. szHundreds[1] = szHundreds[2];
  3034. szHundreds[2] = 0;
  3035. }
  3036. }
  3037. }
  3038. //
  3039. // Get the Tens.
  3040. //
  3041. Tens = Number / 10;
  3042. if (Tens)
  3043. {
  3044. Number -= Tens * 10;
  3045. switch (Tens)
  3046. {
  3047. case ( 1 ) :
  3048. {
  3049. cTens = L'\x05d9'; // Hebrew Letter Yod
  3050. break;
  3051. }
  3052. case ( 2 ) :
  3053. {
  3054. cTens = L'\x05db'; // Hebrew Letter Kaf
  3055. break;
  3056. }
  3057. case ( 3 ) :
  3058. {
  3059. cTens = L'\x05dc'; // Hebrew Letter Lamed
  3060. break;
  3061. }
  3062. case ( 4 ) :
  3063. {
  3064. cTens = L'\x05de'; // Hebrew Letter Mem
  3065. break;
  3066. }
  3067. case ( 5 ) :
  3068. {
  3069. cTens = L'\x05e0'; // Hebrew Letter Nun
  3070. break;
  3071. }
  3072. case ( 6 ) :
  3073. {
  3074. cTens = L'\x05e1'; // Hebrew Letter Samekh
  3075. break;
  3076. }
  3077. case ( 7 ) :
  3078. {
  3079. cTens = L'\x05e2'; // Hebrew Letter Ayin
  3080. break;
  3081. }
  3082. case ( 8 ) :
  3083. {
  3084. cTens = L'\x05e4'; // Hebrew Letter Pe
  3085. break;
  3086. }
  3087. case ( 9 ) :
  3088. {
  3089. cTens = L'\x05e6'; // Hebrew Letter Tsadi
  3090. break;
  3091. }
  3092. }
  3093. }
  3094. else
  3095. {
  3096. cTens = 0;
  3097. }
  3098. //
  3099. // Get the Units.
  3100. //
  3101. cUnits = (WCHAR)(Number ? (L'\x05d0' + Number - 1) : 0);
  3102. if ((cUnits == L'\x05d4') && // Hebrew Letter He
  3103. (cTens == L'\x05d9')) // Hebrew Letter Yod
  3104. {
  3105. cUnits = L'\x05d5'; // Hebrew Letter Vav
  3106. cTens = L'\x05d8'; // Hebrew Letter Tet
  3107. }
  3108. if ((cUnits == L'\x05d5') && // Hebrew Letter Vav
  3109. (cTens == L'\x05d9')) // Hebrew Letter Yod
  3110. {
  3111. cUnits = L'\x05d6'; // Hebrew Letter Zayin
  3112. cTens = L'\x05d8'; // Hebrew Letter Tet
  3113. }
  3114. //
  3115. // Clear out the temp buffer.
  3116. //
  3117. RtlZeroMemory(pTemp, sizeof(szTemp));
  3118. //
  3119. // Copy the appropriate info to the given buffer.
  3120. //
  3121. if (cUnits)
  3122. {
  3123. *pTemp++ = cUnits;
  3124. }
  3125. if (cTens)
  3126. {
  3127. *pTemp++ = cTens;
  3128. }
  3129. if(FAILED(StringCchCopyW(pTemp, ARRAYSIZE(szTemp) - (pTemp - szTemp), szHundreds)))
  3130. {
  3131. //
  3132. // Operation tried to overrun the static buffer on the stack
  3133. //
  3134. return(FALSE);
  3135. }
  3136. if(NlsStrLenW(szTemp) > 1)
  3137. {
  3138. RtlMoveMemory(szTemp + 2, szTemp + 1, NlsStrLenW(szTemp + 1) * sizeof(WCHAR));
  3139. szTemp[1] = L'"';
  3140. }
  3141. else
  3142. {
  3143. szTemp[1] = szTemp[0];
  3144. szTemp[0] = L'\'';
  3145. }
  3146. //
  3147. // Reverse the final string and store it in the given buffer.
  3148. //
  3149. Length = NlsStrLenW(szTemp) - 1;
  3150. if( Length > (cchSize - 1) )
  3151. {
  3152. // Make sure that we won�t overrun the szHebrew.
  3153. return (FALSE);
  3154. }
  3155. for (Ctr = 0; Length >= 0; Ctr++)
  3156. {
  3157. szHebrew[Ctr] = szTemp[Length];
  3158. Length--;
  3159. }
  3160. szHebrew[Ctr] = 0;
  3161. //
  3162. // Return success.
  3163. //
  3164. return (TRUE);
  3165. }