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.

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