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.

406 lines
8.6 KiB

  1. /*++
  2. Copyright (c) 1997 Microsoft Corporation
  3. Module Name :
  4. datetime.hxx
  5. Abstract:
  6. datetime header
  7. Author:
  8. MuraliK
  9. --*/
  10. #ifndef _DATETIME_H_
  11. #define _DATETIME_H_
  12. #include <readmost.hxx>
  13. #define ENABLE_AUX_COUNTERS ( 1)
  14. inline bool
  15. SameDate(
  16. const SYSTEMTIME* pst1,
  17. const SYSTEMTIME* pst2)
  18. {
  19. // Same (wYear, wMonth), (wDayOfWeek, wDay)
  20. // Ignore (wHour, wMinute), (wSecond, wMilliseconds)
  21. return (((*(DWORD*) &pst1->wDayOfWeek) == (*(DWORD*) &pst2->wDayOfWeek))
  22. && ((*(DWORD*) &pst1->wYear) == (*(DWORD*) &pst2->wYear)));
  23. }
  24. inline bool
  25. SameMinute(
  26. const SYSTEMTIME* pst1,
  27. const SYSTEMTIME* pst2)
  28. {
  29. // Same (wYear, wMonth), (wDayOfWeek, wDay), (wHour, wMinute)
  30. // Ignore (wSecond, wMilliseconds)
  31. // return memcmp(pst1, pst2, 6 * sizeof(WORD)) == 0;
  32. return (((*(DWORD*) &pst1->wHour) == (*(DWORD*) &pst2->wHour))
  33. && ((*(DWORD*) &pst1->wDayOfWeek) == (*(DWORD*) &pst2->wDayOfWeek))
  34. && ((*(DWORD*) &pst1->wYear) == (*(DWORD*) &pst2->wYear)));
  35. }
  36. typedef union {
  37. FILETIME ft;
  38. unsigned __int64 u64;
  39. } FILETIME_UINT64;
  40. #define FILETIME_1_SECOND 10000000
  41. #define FILETIME_1_MILLISECOND 10000
  42. //
  43. // Seconds lookup table
  44. //
  45. IRTL_DLLEXP LPCSTR Month3CharNames(DWORD dwMonth);
  46. IRTL_DLLEXP LPCSTR DayOfWeek3CharNames(DWORD dwDayOfWeek);
  47. extern "C"
  48. IRTL_DLLEXP
  49. BOOL
  50. IISGetCurrentTime(
  51. OUT FILETIME* pft,
  52. OUT SYSTEMTIME* pst);
  53. #define IISGetCurrentTimeAsFileTime(pft) IISGetCurrentTime(pft, NULL)
  54. #define IISGetCurrentTimeAsSystemTime(pst) IISGetCurrentTime(NULL, pst)
  55. #define MAX_FORMATTED_DATETIME_LEN (50)
  56. #define GMT_STRING_SIZE (30)
  57. struct IRTL_DLLEXP DATETIME_FORMAT_ENTRY {
  58. SYSTEMTIME m_stDateTime;
  59. INT m_cchOffsetSeconds; // Offset of two-char seconds field
  60. DWORD m_cbDateTime; // Total length
  61. INT m_cchDateLen; // contains length of date portion alone
  62. CHAR m_rgchDateTime[ MAX_FORMATTED_DATETIME_LEN];
  63. VOID CopyFormattedData(IN const SYSTEMTIME * pst,
  64. OUT CHAR * pchDateTime) const;
  65. BOOL IsHit( IN const SYSTEMTIME * pst) const
  66. {
  67. //
  68. // Ignore seconds & milli-seconds during comparison
  69. //
  70. return SameMinute(&m_stDateTime, pst);
  71. }
  72. }; // struct DATETIME_FORMAT_ENTRY
  73. typedef DATETIME_FORMAT_ENTRY * PDFT_ENTRY;
  74. class IRTL_DLLEXP CDFTCache : public CDataCache<DATETIME_FORMAT_ENTRY>
  75. {
  76. public:
  77. CDFTCache()
  78. {
  79. const_cast<PDFT_ENTRY>(&m_tData)->m_stDateTime.wYear = 0;
  80. }
  81. BOOL IsHit( IN const SYSTEMTIME * pst) const
  82. {
  83. // We need a memory barrier before multiword accesses
  84. _ReadSequence();
  85. // The weird const_cast syntax is necessitated by the volatile
  86. // attribute on m_tData.
  87. return const_cast<PDFT_ENTRY>(&m_tData)->IsHit(pst);
  88. }
  89. BOOL CopyFormattedData(IN const SYSTEMTIME * pst,
  90. OUT CHAR * pchDateTime) const;
  91. DWORD DateTimeChars() const
  92. {
  93. return const_cast<PDFT_ENTRY>(&m_tData)->m_cbDateTime;
  94. }
  95. INT OffsetSeconds() const
  96. {
  97. return const_cast<PDFT_ENTRY>(&m_tData)->m_cchOffsetSeconds;
  98. }
  99. LPCSTR FormattedBuffer() const
  100. {
  101. return const_cast<PDFT_ENTRY>(&m_tData)->m_rgchDateTime;
  102. }
  103. WORD Seconds() const
  104. {
  105. return const_cast<PDFT_ENTRY>(&m_tData)->m_stDateTime.wSecond;
  106. }
  107. };
  108. class IRTL_DLLEXP CACHED_DATETIME_FORMATS {
  109. public:
  110. enum {
  111. CACHE_SIZE = (1<<4), // maintain 16-minute history; must be power of 2
  112. CACHE_MASK = CACHE_SIZE-1
  113. };
  114. CACHED_DATETIME_FORMATS( VOID );
  115. virtual ~CACHED_DATETIME_FORMATS(VOID)
  116. {
  117. }
  118. DWORD
  119. GetFormattedDateTime(
  120. IN CONST SYSTEMTIME * pst,
  121. OUT PCHAR pchDateTime
  122. );
  123. DWORD
  124. GetFormattedCurrentDateTime(
  125. OUT PCHAR pchDateTime
  126. );
  127. virtual
  128. VOID GenerateDateTimeString(
  129. IN PDFT_ENTRY pdft,
  130. IN const SYSTEMTIME * pst) = 0;
  131. private:
  132. // Private copy ctor and op= to prevent compiler synthesizing them.
  133. // Must provide (bad) implementation because we export instantiations.
  134. CACHED_DATETIME_FORMATS(const CACHED_DATETIME_FORMATS&);
  135. CACHED_DATETIME_FORMATS& operator=(const CACHED_DATETIME_FORMATS&);
  136. # if ENABLE_AUX_COUNTERS
  137. public:
  138. LONG m_nMisses;
  139. LONG m_nAccesses;
  140. private:
  141. # endif // ENABLE_AUX_COUNTERS
  142. volatile LONG m_idftCurrent;
  143. CDFTCache m_rgDateTimes[CACHE_SIZE];
  144. }; // class CACHED_DATETIME_FORMATS
  145. typedef CACHED_DATETIME_FORMATS *PCACHED_DATETIME_FORMATS;
  146. //
  147. // Derived classes
  148. //
  149. class IRTL_DLLEXP W3_DATETIME_CACHE : public CACHED_DATETIME_FORMATS {
  150. public:
  151. VOID GenerateDateTimeString(
  152. IN PDFT_ENTRY pdft,
  153. IN const SYSTEMTIME * pst);
  154. };
  155. typedef W3_DATETIME_CACHE *PW3_DATETIME_CACHE;
  156. class IRTL_DLLEXP ASCLOG_DATETIME_CACHE : public CACHED_DATETIME_FORMATS {
  157. private:
  158. DWORD m_tickCount;
  159. SYSTEMTIME m_systime;
  160. public:
  161. ASCLOG_DATETIME_CACHE( ) {
  162. m_tickCount = GetTickCount();
  163. GetLocalTime( &m_systime );
  164. }
  165. VOID GenerateDateTimeString(
  166. IN PDFT_ENTRY pdft,
  167. IN const SYSTEMTIME * pst
  168. );
  169. inline
  170. VOID SetLocalTime(
  171. OUT PSYSTEMTIME pst
  172. ) {
  173. DWORD tmpTime = GetTickCount();
  174. if ( tmpTime - m_tickCount >= 1000) {
  175. GetLocalTime( &m_systime );
  176. m_tickCount = tmpTime;
  177. }
  178. *pst = m_systime;
  179. }
  180. };
  181. typedef ASCLOG_DATETIME_CACHE *PASCLOG_DATETIME_CACHE;
  182. class IRTL_DLLEXP EXTLOG_DATETIME_CACHE : public CACHED_DATETIME_FORMATS {
  183. private:
  184. DWORD m_tickCount;
  185. SYSTEMTIME m_systime;
  186. public:
  187. EXTLOG_DATETIME_CACHE( ) {
  188. m_tickCount = GetTickCount();
  189. IISGetCurrentTimeAsSystemTime( &m_systime );
  190. }
  191. VOID GenerateDateTimeString(
  192. IN PDFT_ENTRY pdft,
  193. IN const SYSTEMTIME * pst);
  194. inline
  195. VOID SetSystemTime(
  196. OUT PSYSTEMTIME pst
  197. ) {
  198. DWORD tmpTime = GetTickCount();
  199. if ( tmpTime - m_tickCount >= 1000) {
  200. IISGetCurrentTimeAsSystemTime( &m_systime );
  201. m_tickCount = tmpTime;
  202. }
  203. *pst = m_systime;
  204. }
  205. };
  206. typedef EXTLOG_DATETIME_CACHE *PEXTLOG_DATETIME_CACHE;
  207. //
  208. // Time-Related APIs
  209. //
  210. IRTL_DLLEXP
  211. BOOL
  212. SystemTimeToGMT(
  213. IN const SYSTEMTIME & st,
  214. OUT CHAR * pszBuff,
  215. IN DWORD cbBuff
  216. );
  217. IRTL_DLLEXP
  218. BOOL
  219. SystemTimeToGMTEx(
  220. IN const SYSTEMTIME & st,
  221. OUT CHAR * pszBuff,
  222. IN DWORD cbBuff,
  223. IN DWORD csecOffset = 0
  224. );
  225. IRTL_DLLEXP
  226. BOOL
  227. FileTimeToGMT(
  228. IN const FILETIME & ft,
  229. OUT CHAR * pszBuff,
  230. IN DWORD cbBuff
  231. );
  232. IRTL_DLLEXP
  233. BOOL
  234. FileTimeToGMTEx(
  235. IN const FILETIME & ft,
  236. OUT CHAR * pszBuff,
  237. IN DWORD cbBuff,
  238. IN DWORD csecOffset = 0
  239. );
  240. IRTL_DLLEXP
  241. BOOL
  242. NtLargeIntegerTimeToSystemTime(
  243. IN const LARGE_INTEGER & liTime,
  244. OUT SYSTEMTIME * pst
  245. );
  246. IRTL_DLLEXP
  247. BOOL
  248. NtLargeIntegerTimeToLocalSystemTime(
  249. IN const LARGE_INTEGER * liTime,
  250. OUT SYSTEMTIME * pst
  251. );
  252. IRTL_DLLEXP
  253. BOOL
  254. NtSystemTimeToLargeInteger(
  255. IN const SYSTEMTIME * pst,
  256. OUT LARGE_INTEGER * pli
  257. );
  258. IRTL_DLLEXP
  259. BOOL
  260. StringTimeToFileTime(
  261. IN const CHAR * pszTime,
  262. OUT LARGE_INTEGER * pliTime
  263. );
  264. IRTL_DLLEXP
  265. DWORD
  266. IsLargeIntegerToDecimalChar(
  267. IN const LARGE_INTEGER * pliValue,
  268. OUT LPSTR pchBuffer
  269. );
  270. BOOL
  271. ZapRegistryKey(
  272. HKEY hkey,
  273. LPCSTR pszRegPath
  274. );
  275. HKEY
  276. CreateKey(
  277. IN HKEY RootKey,
  278. IN LPCSTR KeyName,
  279. IN LPCSTR KeyValue
  280. );
  281. BOOL
  282. IsIPAddressLocal(
  283. IN DWORD LocalIP,
  284. IN DWORD RemoteIP
  285. );
  286. BOOL
  287. IISCreateDirectory(
  288. IN LPCSTR DirectoryName,
  289. IN BOOL fAllowNetDrives
  290. );
  291. VOID
  292. InitializeDateTime();
  293. VOID
  294. TerminateDateTime();
  295. #endif // _DATETIME_H_