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.

619 lines
14 KiB

  1. //***************************************************************************
  2. //
  3. // Copyright (c) 2000 Microsoft Corporation
  4. //
  5. // datetime.h
  6. //
  7. // alanbos 20-Jan-00 Created.
  8. //
  9. // Datetime helper implementation.
  10. //
  11. //***************************************************************************
  12. #ifndef _DATETIME_H_
  13. #define _DATETIME_H_
  14. #define WBEMDT_DMTF_LEN 25
  15. #define WBEMDT_DMTF_SPOS 14
  16. #define WBEMDT_DMTF_UPOS 21
  17. #define WBEMDT_MIN_YEAR 0
  18. #define WBEMDT_MAX_YEAR 9999
  19. #define WBEMDT_MIN_MONTH 1
  20. #define WBEMDT_MAX_MONTH 12
  21. #define WBEMDT_MIN_DAY 1
  22. #define WBEMDT_MAX_DAY 31
  23. #define WBEMDT_MIN_DAYINT 0
  24. #define WBEMDT_MAX_DAYINT 99999999
  25. #define WBEMDT_MIN_HOURS 0
  26. #define WBEMDT_MAX_HOURS 23
  27. #define WBEMDT_MIN_MINUTES 0
  28. #define WBEMDT_MAX_MINUTES 59
  29. #define WBEMDT_MIN_SECONDS 0
  30. #define WBEMDT_MAX_SECONDS 59
  31. #define WBEMDT_MIN_MICROSEC 0
  32. #define WBEMDT_MAX_MICROSEC 999999
  33. #define WBEMDT_MIN_UTC -720
  34. #define WBEMDT_MAX_UTC 720
  35. #define INVALID_TIME 0xffffffffffffffff
  36. //***************************************************************************
  37. //
  38. // CLASS NAME:
  39. //
  40. // CSWbemDateTime
  41. //
  42. // DESCRIPTION:
  43. //
  44. // Implements the ISWbemDateTime interface.
  45. //
  46. //***************************************************************************
  47. class CSWbemDateTime : public ISWbemDateTime,
  48. public IObjectSafety,
  49. public ISupportErrorInfo,
  50. public IProvideClassInfo
  51. {
  52. private:
  53. // Private helper class for all the messy business
  54. class WBEMTime
  55. {
  56. private:
  57. class WBEMTimeSpan
  58. {
  59. private:
  60. ULONGLONG m_Time;
  61. friend class WBEMTime;
  62. public:
  63. WBEMTimeSpan (
  64. int iMinutes
  65. )
  66. {
  67. m_Time = iMinutes * 60;
  68. m_Time *= 10000000;
  69. }
  70. };
  71. public:
  72. WBEMTime () { m_uTime = INVALID_TIME ; }
  73. WBEMTime ( const SYSTEMTIME &st ) { *this = st ; }
  74. const WBEMTime &operator= ( const SYSTEMTIME &st ) ;
  75. const WBEMTime &operator= ( const FILETIME &ft ) ;
  76. WBEMTime operator+ ( const WBEMTimeSpan &ts ) const;
  77. WBEMTime operator- ( const WBEMTimeSpan &sub ) const;
  78. BOOL GetSYSTEMTIME ( SYSTEMTIME *pst ) const;
  79. BOOL GetFILETIME ( FILETIME *pst ) const;
  80. BOOL GetDMTF ( SYSTEMTIME &st, long &offset ) const;
  81. BOOL GetDMTF ( SYSTEMTIME &st ) const;
  82. void Clear ( void ) { m_uTime = INVALID_TIME ; }
  83. bool IsOk () const { return m_uTime != INVALID_TIME ? true : false; }
  84. static LONG WINAPI GetLocalOffsetForDate(const SYSTEMTIME *pst);
  85. private:
  86. ULONGLONG m_uTime;
  87. };
  88. CDispatchHelp m_Dispatch;
  89. VARIANT_BOOL m_bYearSpecified;
  90. VARIANT_BOOL m_bMonthSpecified;
  91. VARIANT_BOOL m_bDaySpecified;
  92. VARIANT_BOOL m_bHoursSpecified;
  93. VARIANT_BOOL m_bMinutesSpecified;
  94. VARIANT_BOOL m_bSecondsSpecified;
  95. VARIANT_BOOL m_bMicrosecondsSpecified;
  96. VARIANT_BOOL m_bUTCSpecified;
  97. VARIANT_BOOL m_bIsInterval;
  98. long m_iYear;
  99. long m_iMonth;
  100. long m_iDay;
  101. long m_iHours;
  102. long m_iMinutes;
  103. long m_iSeconds;
  104. long m_iMicroseconds;
  105. long m_iUTC;
  106. bool CheckField (
  107. LPWSTR pValue,
  108. ULONG len,
  109. VARIANT_BOOL &bIsSpecified,
  110. long &iValue,
  111. long maxValue,
  112. long minValue
  113. );
  114. bool CheckUTC (
  115. LPWSTR pValue,
  116. VARIANT_BOOL &bIsSpecified,
  117. long &iValue,
  118. bool bParseSign = true
  119. );
  120. DWORD m_dwSafetyOptions;
  121. // Records the least significant bit of a filetime
  122. DWORD m_dw100nsOverflow;
  123. protected:
  124. long m_cRef; //Object reference count
  125. public:
  126. CSWbemDateTime(void);
  127. virtual ~CSWbemDateTime(void);
  128. //Non-delegating object IUnknown
  129. STDMETHODIMP QueryInterface(REFIID, LPVOID*);
  130. STDMETHODIMP_(ULONG) AddRef(void);
  131. STDMETHODIMP_(ULONG) Release(void);
  132. // IDispatch
  133. STDMETHODIMP GetTypeInfoCount(UINT* pctinfo)
  134. {return m_Dispatch.GetTypeInfoCount(pctinfo);}
  135. STDMETHODIMP GetTypeInfo(UINT itinfo, LCID lcid, ITypeInfo** pptinfo)
  136. {return m_Dispatch.GetTypeInfo(itinfo, lcid, pptinfo);}
  137. STDMETHODIMP GetIDsOfNames(REFIID riid, OLECHAR** rgszNames,
  138. UINT cNames, LCID lcid, DISPID* rgdispid)
  139. {return m_Dispatch.GetIDsOfNames(riid, rgszNames, cNames,
  140. lcid,
  141. rgdispid);}
  142. STDMETHODIMP Invoke(DISPID dispidMember, REFIID riid, LCID lcid,
  143. WORD wFlags, DISPPARAMS* pdispparams, VARIANT* pvarResult,
  144. EXCEPINFO* pexcepinfo, UINT* puArgErr)
  145. {return m_Dispatch.Invoke(dispidMember, riid, lcid, wFlags,
  146. pdispparams, pvarResult, pexcepinfo, puArgErr);}
  147. // ISWbemDateTime methods
  148. HRESULT STDMETHODCALLTYPE get_Value(
  149. /* [retval][out] */ BSTR __RPC_FAR *value) ;
  150. HRESULT STDMETHODCALLTYPE put_Value(
  151. /* [in] */ BSTR __RPC_FAR value) ;
  152. HRESULT STDMETHODCALLTYPE get_Year(
  153. /* [retval][out] */ long __RPC_FAR *value)
  154. {
  155. ResetLastErrors ();
  156. *value = m_iYear;
  157. return S_OK;
  158. }
  159. HRESULT STDMETHODCALLTYPE put_Year(
  160. /* [in] */ long __RPC_FAR value)
  161. {
  162. HRESULT hr = WBEM_S_NO_ERROR;
  163. ResetLastErrors ();
  164. if ((value > WBEMDT_MAX_YEAR) || (value < WBEMDT_MIN_YEAR))
  165. hr = wbemErrValueOutOfRange;
  166. else
  167. m_iYear = value;
  168. if (FAILED(hr))
  169. m_Dispatch.RaiseException (hr);
  170. return hr;
  171. }
  172. HRESULT STDMETHODCALLTYPE get_Month(
  173. /* [retval][out] */ long __RPC_FAR *value)
  174. {
  175. ResetLastErrors ();
  176. *value = m_iMonth;
  177. return S_OK;
  178. }
  179. HRESULT STDMETHODCALLTYPE put_Month(
  180. /* [in] */ long __RPC_FAR value)
  181. {
  182. HRESULT hr = WBEM_S_NO_ERROR;
  183. ResetLastErrors ();
  184. if ((value > WBEMDT_MAX_MONTH) || (value < WBEMDT_MIN_MONTH))
  185. hr = wbemErrValueOutOfRange;
  186. else
  187. m_iMonth = value;
  188. if (FAILED(hr))
  189. m_Dispatch.RaiseException (hr);
  190. return hr;
  191. }
  192. HRESULT STDMETHODCALLTYPE get_Day(
  193. /* [retval][out] */ long __RPC_FAR *value)
  194. {
  195. ResetLastErrors ();
  196. *value = m_iDay;
  197. return S_OK;
  198. }
  199. HRESULT STDMETHODCALLTYPE put_Day(
  200. /* [in] */ long __RPC_FAR value)
  201. {
  202. HRESULT hr = S_OK;
  203. ResetLastErrors ();
  204. if (((VARIANT_TRUE == m_bIsInterval) && ((value > WBEMDT_MAX_DAYINT) || (value < WBEMDT_MIN_DAYINT))) ||
  205. ((VARIANT_FALSE == m_bIsInterval) &&((value > WBEMDT_MAX_DAY) || (value < WBEMDT_MIN_DAY))))
  206. hr = wbemErrValueOutOfRange;
  207. else
  208. m_iDay = value;
  209. if (FAILED(hr))
  210. m_Dispatch.RaiseException (hr);
  211. return hr;
  212. }
  213. HRESULT STDMETHODCALLTYPE get_Hours(
  214. /* [retval][out] */ long __RPC_FAR *value)
  215. {
  216. ResetLastErrors ();
  217. *value = m_iHours;
  218. return S_OK;
  219. }
  220. HRESULT STDMETHODCALLTYPE put_Hours(
  221. /* [in] */ long __RPC_FAR value)
  222. {
  223. HRESULT hr = S_OK;
  224. ResetLastErrors ();
  225. if ((value > WBEMDT_MAX_HOURS) || (value < WBEMDT_MIN_HOURS))
  226. hr = wbemErrValueOutOfRange;
  227. else
  228. m_iHours = value;
  229. if (FAILED(hr))
  230. m_Dispatch.RaiseException (hr);
  231. return hr;
  232. }
  233. HRESULT STDMETHODCALLTYPE get_Minutes(
  234. /* [retval][out] */ long __RPC_FAR *value)
  235. {
  236. ResetLastErrors ();
  237. *value = m_iMinutes;
  238. return S_OK;
  239. }
  240. HRESULT STDMETHODCALLTYPE put_Minutes(
  241. /* [in] */ long __RPC_FAR value)
  242. {
  243. HRESULT hr = S_OK;
  244. ResetLastErrors ();
  245. if ((value > WBEMDT_MAX_MINUTES) || (value < WBEMDT_MIN_MINUTES))
  246. hr = wbemErrValueOutOfRange;
  247. else
  248. m_iMinutes = value;
  249. if (FAILED(hr))
  250. m_Dispatch.RaiseException (hr);
  251. return hr;
  252. }
  253. HRESULT STDMETHODCALLTYPE get_Seconds(
  254. /* [retval][out] */ long __RPC_FAR *value)
  255. {
  256. ResetLastErrors ();
  257. *value = m_iSeconds;
  258. return S_OK;
  259. }
  260. HRESULT STDMETHODCALLTYPE put_Seconds(
  261. /* [in] */ long __RPC_FAR value)
  262. {
  263. HRESULT hr = S_OK;
  264. ResetLastErrors ();
  265. if ((value > WBEMDT_MAX_SECONDS) || (value < WBEMDT_MIN_SECONDS))
  266. hr = wbemErrValueOutOfRange;
  267. else
  268. m_iSeconds = value;
  269. if (FAILED(hr))
  270. m_Dispatch.RaiseException (hr);
  271. return hr;
  272. }
  273. HRESULT STDMETHODCALLTYPE get_Microseconds(
  274. /* [retval][out] */ long __RPC_FAR *value)
  275. {
  276. ResetLastErrors ();
  277. *value = m_iMicroseconds;
  278. return S_OK;
  279. }
  280. HRESULT STDMETHODCALLTYPE put_Microseconds(
  281. /* [in] */ long __RPC_FAR value)
  282. {
  283. HRESULT hr = S_OK;
  284. ResetLastErrors ();
  285. if ((value > WBEMDT_MAX_MICROSEC) || (value < WBEMDT_MIN_MICROSEC))
  286. hr = wbemErrValueOutOfRange;
  287. else
  288. m_iMicroseconds = value;
  289. if (FAILED(hr))
  290. m_Dispatch.RaiseException (hr);
  291. return hr;
  292. }
  293. HRESULT STDMETHODCALLTYPE get_UTC(
  294. /* [retval][out] */ long __RPC_FAR *value)
  295. {
  296. ResetLastErrors ();
  297. *value = m_iUTC;
  298. return S_OK;
  299. }
  300. HRESULT STDMETHODCALLTYPE put_UTC(
  301. /* [in] */ long __RPC_FAR value)
  302. {
  303. HRESULT hr = S_OK;
  304. ResetLastErrors ();
  305. if ((value > WBEMDT_MAX_UTC) || (value < WBEMDT_MIN_UTC))
  306. hr = wbemErrValueOutOfRange;
  307. else
  308. m_iUTC = value;
  309. if (FAILED(hr))
  310. m_Dispatch.RaiseException (hr);
  311. return hr;
  312. }
  313. HRESULT STDMETHODCALLTYPE get_YearSpecified(
  314. /* [retval][out] */ VARIANT_BOOL __RPC_FAR *value)
  315. {
  316. ResetLastErrors ();
  317. *value = m_bYearSpecified;
  318. return S_OK;
  319. }
  320. HRESULT STDMETHODCALLTYPE put_YearSpecified(
  321. /* [in] */ VARIANT_BOOL __RPC_FAR value)
  322. {
  323. ResetLastErrors ();
  324. m_bYearSpecified = value;
  325. return S_OK;
  326. }
  327. HRESULT STDMETHODCALLTYPE get_MonthSpecified(
  328. /* [retval][out] */ VARIANT_BOOL __RPC_FAR *value)
  329. {
  330. ResetLastErrors ();
  331. *value = m_bMonthSpecified;
  332. return S_OK;
  333. }
  334. HRESULT STDMETHODCALLTYPE put_MonthSpecified(
  335. /* [in] */ VARIANT_BOOL __RPC_FAR value)
  336. {
  337. ResetLastErrors ();
  338. m_bMonthSpecified = value;
  339. return S_OK;
  340. }
  341. HRESULT STDMETHODCALLTYPE get_DaySpecified(
  342. /* [retval][out] */ VARIANT_BOOL __RPC_FAR *value)
  343. {
  344. ResetLastErrors ();
  345. *value = m_bDaySpecified;
  346. return S_OK;
  347. }
  348. HRESULT STDMETHODCALLTYPE put_DaySpecified(
  349. /* [in] */ VARIANT_BOOL __RPC_FAR value)
  350. {
  351. ResetLastErrors ();
  352. m_bDaySpecified = value;
  353. return S_OK;
  354. }
  355. HRESULT STDMETHODCALLTYPE get_HoursSpecified(
  356. /* [retval][out] */ VARIANT_BOOL __RPC_FAR *value)
  357. {
  358. ResetLastErrors ();
  359. *value = m_bHoursSpecified;
  360. return S_OK;
  361. }
  362. HRESULT STDMETHODCALLTYPE put_HoursSpecified(
  363. /* [in] */ VARIANT_BOOL __RPC_FAR value)
  364. {
  365. ResetLastErrors ();
  366. m_bHoursSpecified = value;
  367. return S_OK;
  368. }
  369. HRESULT STDMETHODCALLTYPE get_MinutesSpecified(
  370. /* [retval][out] */ VARIANT_BOOL __RPC_FAR *value)
  371. {
  372. ResetLastErrors ();
  373. *value = m_bMinutesSpecified;
  374. return S_OK;
  375. }
  376. HRESULT STDMETHODCALLTYPE put_MinutesSpecified(
  377. /* [in] */ VARIANT_BOOL __RPC_FAR value)
  378. {
  379. ResetLastErrors ();
  380. m_bMinutesSpecified = value;
  381. return S_OK;
  382. }
  383. HRESULT STDMETHODCALLTYPE get_SecondsSpecified(
  384. /* [retval][out] */ VARIANT_BOOL __RPC_FAR *value)
  385. {
  386. ResetLastErrors ();
  387. *value = m_bSecondsSpecified;
  388. return S_OK;
  389. }
  390. HRESULT STDMETHODCALLTYPE put_SecondsSpecified(
  391. /* [in] */ VARIANT_BOOL __RPC_FAR value)
  392. {
  393. ResetLastErrors ();
  394. m_bSecondsSpecified = value;
  395. return S_OK;
  396. }
  397. HRESULT STDMETHODCALLTYPE get_MicrosecondsSpecified(
  398. /* [retval][out] */ VARIANT_BOOL __RPC_FAR *value)
  399. {
  400. ResetLastErrors ();
  401. *value = m_bMicrosecondsSpecified;
  402. return S_OK;
  403. }
  404. HRESULT STDMETHODCALLTYPE put_MicrosecondsSpecified(
  405. /* [in] */ VARIANT_BOOL __RPC_FAR value)
  406. {
  407. ResetLastErrors ();
  408. m_bMicrosecondsSpecified = value;
  409. return S_OK;
  410. }
  411. HRESULT STDMETHODCALLTYPE get_UTCSpecified(
  412. /* [retval][out] */ VARIANT_BOOL __RPC_FAR *value)
  413. {
  414. ResetLastErrors ();
  415. *value = m_bUTCSpecified;
  416. return S_OK;
  417. }
  418. HRESULT STDMETHODCALLTYPE put_UTCSpecified(
  419. /* [in] */ VARIANT_BOOL __RPC_FAR value)
  420. {
  421. ResetLastErrors ();
  422. m_bUTCSpecified = value;
  423. return S_OK;
  424. }
  425. HRESULT STDMETHODCALLTYPE get_IsInterval(
  426. /* [retval][out] */ VARIANT_BOOL __RPC_FAR *value)
  427. {
  428. ResetLastErrors ();
  429. *value = m_bIsInterval;
  430. return S_OK;
  431. }
  432. HRESULT STDMETHODCALLTYPE put_IsInterval(
  433. /* [in] */ VARIANT_BOOL __RPC_FAR value)
  434. {
  435. ResetLastErrors ();
  436. if ((VARIANT_TRUE == m_bIsInterval) && (VARIANT_FALSE == value))
  437. {
  438. if (0 == m_iDay)
  439. m_iDay = 1;
  440. else if (WBEMDT_MAX_DAY < m_iDay)
  441. m_iDay = WBEMDT_MAX_DAY;
  442. }
  443. m_bIsInterval = value;
  444. return S_OK;
  445. }
  446. HRESULT STDMETHODCALLTYPE GetVarDate(
  447. /*[in]*/ VARIANT_BOOL bIsLocal,
  448. /*[out, retval]*/ DATE *dVarDate) ;
  449. HRESULT STDMETHODCALLTYPE SetVarDate(
  450. /*[in]*/ DATE dVarDate,
  451. /*[in, optional]*/ VARIANT_BOOL bIsLocal) ;
  452. HRESULT STDMETHODCALLTYPE GetFileTime (
  453. /*[in, optional]*/ VARIANT_BOOL bIsLocal,
  454. /*[out, retval]*/ BSTR *strFileTime
  455. );
  456. HRESULT STDMETHODCALLTYPE SetFileTime (
  457. /*[in]*/ BSTR strFileTime,
  458. /*[in, optional]*/ VARIANT_BOOL bIsLocal
  459. );
  460. // IObjectSafety methods
  461. HRESULT STDMETHODCALLTYPE SetInterfaceSafetyOptions
  462. (
  463. /* [in] */ REFIID riid,
  464. /* [in] */ DWORD dwOptionSetMask,
  465. /* [in] */ DWORD dwEnabledOptions
  466. )
  467. {
  468. if ((IID_IDispatch != riid) && (IID_ISWbemDateTime != riid))
  469. return E_NOINTERFACE;
  470. if (dwOptionSetMask &
  471. ~(INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACESAFE_FOR_UNTRUSTED_CALLER))
  472. return E_FAIL;
  473. m_dwSafetyOptions = (dwEnabledOptions & dwOptionSetMask);
  474. return S_OK;
  475. }
  476. HRESULT STDMETHODCALLTYPE GetInterfaceSafetyOptions(
  477. /* [in] */ REFIID riid,
  478. /* [out] */ DWORD __RPC_FAR *pdwSupportedOptions,
  479. /* [out] */ DWORD __RPC_FAR *pdwEnabledOptions
  480. )
  481. {
  482. if ((IID_IDispatch != riid) && (IID_ISWbemDateTime != riid))
  483. return E_NOINTERFACE;
  484. if (pdwSupportedOptions) *pdwSupportedOptions =
  485. (INTERFACESAFE_FOR_UNTRUSTED_DATA|INTERFACESAFE_FOR_UNTRUSTED_CALLER);
  486. if (pdwEnabledOptions) *pdwEnabledOptions = m_dwSafetyOptions;
  487. return S_OK;
  488. }
  489. // ISupportErrorInfo methods
  490. HRESULT STDMETHODCALLTYPE InterfaceSupportsErrorInfo
  491. (
  492. /* [in] */ REFIID riid
  493. )
  494. {
  495. return (IID_ISWbemDateTime == riid) ? S_OK : S_FALSE;
  496. }
  497. // IProvideClassInfo methods
  498. HRESULT STDMETHODCALLTYPE GetClassInfo
  499. (
  500. /* [in,out] */ ITypeInfo **ppTI
  501. )
  502. {
  503. return m_Dispatch.GetClassInfo (ppTI);
  504. };
  505. };
  506. #endif // _DATETIME_H