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

213 lines
8.3 KiB

  1. /*++
  2. Copyright (C) 1996-2001 Microsoft Corporation
  3. Module Name:
  4. DATETIMEPARSER.H
  5. Abstract:
  6. Validates a date/time string and converts into it's component values.
  7. History:
  8. --*/
  9. #ifndef _datetimeparser_
  10. #define _datetimeparser_
  11. #include "corepol.h"
  12. class POLARITY CDateTimeParser
  13. {
  14. public:
  15. enum { ok = 0, failed, nothingLeft };
  16. //nDayFormatPreference values
  17. typedef enum { dmy, dym, mdy, myd, ydm, ymd } DayFormatPreference;
  18. //Constructor. This takes a DateTime string and parses it.
  19. CDateTimeParser(const TCHAR *pszDateTime);
  20. //Destructor. Tidies up after itself.
  21. ~CDateTimeParser();
  22. //Tidies up and parses a new date and time.
  23. BOOL SetDateTime(const TCHAR *pszDateTime);
  24. //Returns the status of the currently parsed date/time.
  25. BOOL IsValidDateTime() { return m_bValidDateTime; }
  26. //Retrieves the bits and pieces we found. Zero means it was not
  27. //found or was zero! This may change!
  28. unsigned char GetDay() { return m_nDay; }
  29. unsigned char GetMonth() { return m_nMonth; }
  30. unsigned int GetYear() { return m_nYear; }
  31. unsigned char GetHours() { return m_nHours; }
  32. unsigned char GetMinutes() { return m_nMinutes; }
  33. unsigned char GetSeconds() { return m_nSeconds; }
  34. unsigned int GetMicroseconds() { return m_nMicroseconds; }
  35. int GetUTC() { return m_nUTC; }
  36. int FillDMTF(WCHAR* pwszBuffer, size_t cchSize);
  37. // Static helper functions to allow other code to perform quick DMTF DateTime validation
  38. // without executing a lot of unnecessary NLS code.
  39. static BOOL CheckDMTFDateTimeFormat(const TCHAR *wszDateTime, BOOL bFailIfRelative = FALSE,
  40. BOOL bFailIfUnzoned = FALSE);
  41. static BOOL CheckDMTFDateTimeInterval(const TCHAR *wszInterval);
  42. protected:
  43. // Protected constructor, to keep outside code from instantiating us in a half-initialized
  44. // state. This constructor is used by a static helper function for validating DMTF formats.
  45. CDateTimeParser(void);
  46. // Prevents others from accessing
  47. //Resets all the date/time values to the default values.
  48. //If bSQL is TRUE it sets to the SQL default. Otherwise
  49. //sets to the DMTF default.
  50. void ResetDateTime(BOOL bSQL);
  51. void ResetDate(BOOL bSQL);
  52. void ResetTime(BOOL bSQL);
  53. //Does a check to make sure the date/time is in the DMTF
  54. //date/time format.
  55. //Fills in the class date/time elements.
  56. BOOL CheckDMTFDateTimeFormatInternal(const TCHAR *pszDateTime);
  57. //Does a check from the start of the string specified. If
  58. //bCheckTimeAfter set a call to CheckTimeFormat is called
  59. //after each successful parse of the date.
  60. //Fills in the class date/time elements.
  61. BOOL CheckDateFormat(const TCHAR *pszDate, BOOL bCheckTimeAfter);
  62. //Does a check from the start of the string specified. If
  63. //bCheckDateAfter set a call to CheckDateFormat is called
  64. //after each successful parse of the time.
  65. //Fills in the class date/time elements.
  66. BOOL CheckTimeFormat(const TCHAR *pszTime, BOOL bCheckDateAfter);
  67. BOOL DateFormat1(const TCHAR *pszDate, BOOL bCheckTimeAfter);
  68. BOOL DateFormat2(const TCHAR *pszDate, BOOL bCheckTimeAfter);
  69. BOOL DateFormat3(const TCHAR *pszDate, BOOL bCheckTimeAfter);
  70. BOOL DateFormat4(const TCHAR *pszDate, BOOL bCheckTimeAfter);
  71. BOOL DateFormat5(const TCHAR *pszDate, BOOL bCheckTimeAfter);
  72. BOOL DateFormat6(const TCHAR *pszDate, BOOL bCheckTimeAfter);
  73. BOOL DateFormat7(const TCHAR *pszDate, BOOL bCheckTimeAfter);
  74. BOOL DateFormat8(const TCHAR *pszDate, BOOL bCheckTimeAfter);
  75. BOOL DateFormat9(const TCHAR *pszDate, const TCHAR *pszDateSeparator, BOOL bCheckTimeAfter);
  76. BOOL DateFormat10(const TCHAR *pszDate, const TCHAR *pszDateSeparator, BOOL bCheckTimeAfter);
  77. BOOL DateFormat11(const TCHAR *pszDate, const TCHAR *pszDateSeparator, BOOL bCheckTimeAfter);
  78. BOOL DateFormat12(const TCHAR *pszDate, const TCHAR *pszDateSeparator, BOOL bCheckTimeAfter);
  79. BOOL DateFormat13(const TCHAR *pszDate, const TCHAR *pszDateSeparator, BOOL bCheckTimeAfter);
  80. BOOL DateFormat14(const TCHAR *pszDate, const TCHAR *pszDateSeparator, BOOL bCheckTimeAfter);
  81. BOOL DateFormat15(const TCHAR *pszDate, BOOL bCheckTimeAfter);
  82. BOOL TimeFormat1(const TCHAR *pszTime, BOOL bCheckDateAfter);
  83. BOOL TimeFormat2(const TCHAR *pszTime, BOOL bCheckDateAfter);
  84. BOOL TimeFormat3(const TCHAR *pszTime, BOOL bCheckDateAfter);
  85. BOOL TimeFormat4(const TCHAR *pszTime, BOOL bCheckDateAfter);
  86. BOOL TimeFormat5(const TCHAR *pszTime, BOOL bCheckDateAfter);
  87. BOOL TimeFormat6(const TCHAR *pszTime, BOOL bCheckDateAfter);
  88. BOOL TimeFormat7(const TCHAR *pszTime, BOOL bCheckDateAfter);
  89. BOOL TimeFormat8(const TCHAR *pszTime, BOOL bCheckDateAfter);
  90. BOOL TimeFormat9(const TCHAR *pszTime, BOOL bCheckDateAfter);
  91. //Checks for long and short month string. Leading spaces allowed.
  92. int IsValidMonthString(TCHAR *pszString,
  93. const TCHAR *pszSeparator,
  94. TCHAR *pszFullMonth[13],
  95. TCHAR *pszShortMonth[13]);
  96. //Checks for a month as a numeric string. Leading spaces allowed.
  97. //Checks to make sure month is in range 1-12
  98. int IsValidMonthNumber(TCHAR *pszString,
  99. const TCHAR *pszSeparator);
  100. //Checks for valid day number. Does no validation of
  101. //number except checking it is in range of 1-31
  102. //Leading spaces allowed.
  103. int IsValidDayNumber(TCHAR *pszString,
  104. const TCHAR *pszSeparator);
  105. //Checks for valid year number. Does conversion for
  106. //2 digit years. Leading spaces allowed.
  107. int IsValidYearNumber(TCHAR *pszString,
  108. const TCHAR *pszSeparator,
  109. BOOL bFourDigitsOnly);
  110. //Checks for valid hours. Validates for range 0-23
  111. //Leading spaces allowed.
  112. int IsValidHourNumber(TCHAR *pszString,
  113. const TCHAR *pszSeparator);
  114. //Checks for valid minutes. Validates for range 0-59.
  115. //No leading spaces allowed.
  116. int IsValidMinuteNumber(TCHAR *pszString,
  117. const TCHAR *pszSeparator);
  118. //Checks for valid minutes. Validates for range 0-59.
  119. //No leading spaces allowed.
  120. int IsValidSecondNumber(TCHAR *pszString,
  121. const TCHAR *pszSeparator);
  122. //treats the number as thousandth of a second.
  123. int IsValidColonMillisecond(TCHAR *pszString,
  124. const TCHAR *pszSeparator);
  125. //converts value to a thousandth of a second based on number
  126. //of digits included.
  127. int IsValidDotMillisecond(TCHAR *pszString,
  128. const TCHAR *pszSeparator);
  129. //Checks for valid AM/PM string. Leading space is allowed. If
  130. //PM, adds 12 onto hours. Validates hours so it is < 23.
  131. BOOL IsValidAmPmString(TCHAR *pszString,
  132. const TCHAR *pszSeparator,
  133. TCHAR *pszAmPm[2]);
  134. //Checks for a valid [yy]yyMMdd or yyyy day. Validates
  135. //month to 1..12 and day to 1..31
  136. BOOL IsValidYearMonthDayNumber(TCHAR *pszString);
  137. void GetLocalInfoAndAlloc(LCTYPE LCType, LPTSTR &lpLCData);
  138. void GetPreferedDateFormat();
  139. TCHAR* AllocAmPm();
  140. private:
  141. //Localised strings retrieved from GetLocalInfo which holds the long month strings
  142. TCHAR * m_pszFullMonth[13];
  143. //Localised strings retrieved from GetLocalInfo which holds the short month strings
  144. TCHAR * m_pszShortMonth[13];
  145. //Localised strings retrieved from GetLocalInfo which holds the short am/pm strings
  146. TCHAR *m_pszAmPm[2];
  147. //Lets us know if the string is valid or not.
  148. BOOL m_bValidDateTime;
  149. //These are the values which get filled in as we find then throughout the
  150. //parsing.
  151. unsigned char m_nDay;
  152. unsigned char m_nMonth;
  153. unsigned int m_nYear;
  154. unsigned char m_nHours;
  155. unsigned char m_nMinutes;
  156. unsigned char m_nSeconds;
  157. unsigned int m_nMicroseconds;
  158. int m_nUTC;
  159. //Preference for date decoding
  160. DayFormatPreference m_nDayFormatPreference;
  161. };
  162. BOOL POLARITY NormalizeCimDateTime(
  163. IN LPCWSTR pszSrc,
  164. OUT BSTR *strAdjusted
  165. );
  166. #endif