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.

314 lines
7.1 KiB

  1. /*
  2. Copyright (c) 1998-1999 Microsoft Corporation
  3. Module Name:
  4. blbtime.cpp
  5. Abstract:
  6. Implementation of CSdpblbApp and DLL registration.
  7. Author:
  8. */
  9. #include "stdafx.h"
  10. #include "blbgen.h"
  11. #include "sdpblb.h"
  12. #include "sdpblob.h"
  13. #include "blbtime.h"
  14. #include "blbreg.h"
  15. // static variables
  16. const IID &TIME::ELEM_IF_ID = IID_ITTime;
  17. // uses GetElement() to access the sdp time instance - calls ENUM_ELEMENT::GetElement()
  18. HRESULT
  19. TIME::Init(
  20. IN CSdpConferenceBlob &ConfBlob
  21. )
  22. {
  23. // create a defalt sdp time instance
  24. SDP_TIME *SdpTime;
  25. try
  26. {
  27. SdpTime = new SDP_TIME();
  28. }
  29. catch(...)
  30. {
  31. SdpTime = NULL;
  32. }
  33. BAIL_IF_NULL(SdpTime, E_OUTOFMEMORY);
  34. // allocate memory for the time blob
  35. TCHAR SdpTimeBlob[50];
  36. TCHAR *BlobPtr = SdpTimeBlob;
  37. // use the Time template to create a Time blob
  38. // parse the Time blob to initialize the sdp Time instance
  39. // if not successful, delete the sdp Time instance and return error
  40. if ( (0 == _stprintf(
  41. SdpTimeBlob,
  42. SDP_REG_READER::GetTimeTemplate(),
  43. GetCurrentNtpTime() + SDP_REG_READER::GetStartTimeOffset(), // start time - current time + start offset,
  44. GetCurrentNtpTime() + SDP_REG_READER::GetStopTimeOffset() // stop time - current time + stop offset
  45. ) ) ||
  46. (!SdpTime->ParseLine(BlobPtr)) )
  47. {
  48. delete SdpTime;
  49. return HRESULT_FROM_ERROR_CODE(GetLastError());
  50. }
  51. m_ConfBlob = &ConfBlob;
  52. // the init methods returns void
  53. ENUM_ELEMENT<SDP_TIME>::SuccessInit(*SdpTime, TRUE);
  54. return S_OK;
  55. }
  56. HRESULT
  57. TIME::Init(
  58. IN CSdpConferenceBlob &ConfBlob,
  59. IN DWORD StartTime,
  60. IN DWORD StopTime
  61. )
  62. {
  63. // create a defalt sdp time instance
  64. SDP_TIME *SdpTime;
  65. try
  66. {
  67. SdpTime = new SDP_TIME();
  68. }
  69. catch(...)
  70. {
  71. SdpTime = NULL;
  72. }
  73. BAIL_IF_NULL(SdpTime, E_OUTOFMEMORY);
  74. // set the time instance start/stop time (also make it valid)
  75. HRESULT hr = SdpTime->SetTimes(StartTime, StopTime);
  76. if ( FAILED(hr) || (S_FALSE==hr) )
  77. {
  78. delete SdpTime;
  79. return hr;
  80. }
  81. m_ConfBlob = &ConfBlob;
  82. // the init methods returns void
  83. ENUM_ELEMENT<SDP_TIME>::SuccessInit(*SdpTime, TRUE);
  84. return S_OK;
  85. }
  86. inline
  87. DWORD_PTR TimetToNtpTime(IN time_t TimetVal)
  88. {
  89. return TimetVal + NTP_OFFSET;
  90. }
  91. inline
  92. time_t NtpTimeToTimet(IN DWORD_PTR NtpTime)
  93. {
  94. return NtpTime - NTP_OFFSET;
  95. }
  96. inline HRESULT
  97. SystemTimeToNtpTime(
  98. IN SYSTEMTIME &Time,
  99. OUT DWORD_PTR &NtpDword
  100. )
  101. {
  102. _ASSERTE(FIRST_POSSIBLE_YEAR <= Time.wYear);
  103. // fill in a tm struct with the values
  104. tm NtpTmStruct;
  105. NtpTmStruct.tm_isdst = -1; // no info available about daylight savings time
  106. NtpTmStruct.tm_year = (int)Time.wYear - 1900;
  107. NtpTmStruct.tm_mon = (int)Time.wMonth - 1; // months since january
  108. NtpTmStruct.tm_mday = (int)Time.wDay;
  109. NtpTmStruct.tm_wday = (int)Time.wDayOfWeek;
  110. NtpTmStruct.tm_hour = (int)Time.wHour;
  111. NtpTmStruct.tm_min = (int)Time.wMinute;
  112. NtpTmStruct.tm_sec = (int)Time.wSecond;
  113. // try to convert into a time_t value
  114. time_t TimetVal = mktime(&NtpTmStruct);
  115. if ( -1 == TimetVal )
  116. {
  117. return E_INVALIDARG;
  118. }
  119. // convert the time_t value into an NTP value
  120. NtpDword = TimetToNtpTime(TimetVal);
  121. return S_OK;
  122. }
  123. inline
  124. HRESULT
  125. NtpTimeToSystemTime(
  126. IN DWORD dwNtpTime,
  127. OUT SYSTEMTIME &Time
  128. )
  129. {
  130. // if the gen time is WSTR_GEN_TIME_ZERO then,
  131. // all the out parameters should be set to 0
  132. if (dwNtpTime == 0)
  133. {
  134. memset(&Time, 0, sizeof(SYSTEMTIME));
  135. return S_OK;
  136. }
  137. time_t Timet = NtpTimeToTimet(dwNtpTime);
  138. // get the local tm struct for this time value
  139. tm* pLocalTm = localtime(&Timet);
  140. if( pLocalTm == NULL )
  141. {
  142. return E_FAIL;
  143. }
  144. //
  145. // win64: added casts below
  146. //
  147. // set the ref parameters to the tm struct values
  148. Time.wYear = (WORD) ( pLocalTm->tm_year + 1900 ); // years since 1900
  149. Time.wMonth = (WORD) ( pLocalTm->tm_mon + 1 ); // months SINCE january (0,11)
  150. Time.wDay = (WORD) pLocalTm->tm_mday;
  151. Time.wDayOfWeek = (WORD) pLocalTm->tm_wday;
  152. Time.wHour = (WORD) pLocalTm->tm_hour;
  153. Time.wMinute = (WORD) pLocalTm->tm_min;
  154. Time.wSecond = (WORD) pLocalTm->tm_sec;
  155. Time.wMilliseconds = (WORD) 0;
  156. return S_OK;
  157. }
  158. STDMETHODIMP TIME::get_StartTime(DOUBLE * pVal)
  159. {
  160. BAIL_IF_NULL(pVal, E_INVALIDARG);
  161. CLock Lock(g_DllLock);
  162. ULONG StartTime;
  163. BAIL_ON_FAILURE(GetElement().GetStartTime(StartTime));
  164. SYSTEMTIME Time;
  165. HRESULT hr = NtpTimeToSystemTime(StartTime, Time);
  166. if( FAILED(hr) )
  167. {
  168. return hr;
  169. }
  170. DOUBLE vtime;
  171. if (SystemTimeToVariantTime(&Time, &vtime) == FALSE)
  172. {
  173. return E_INVALIDARG;
  174. }
  175. *pVal = vtime;
  176. return S_OK;
  177. }
  178. STDMETHODIMP TIME::put_StartTime(DOUBLE newVal)
  179. {
  180. SYSTEMTIME Time;
  181. if (VariantTimeToSystemTime(newVal, &Time) == FALSE)
  182. {
  183. return E_INVALIDARG;
  184. }
  185. DWORD_PTR dwNtpStartTime;
  186. if (newVal == 0)
  187. {
  188. // unbounded start time
  189. dwNtpStartTime = 0;
  190. }
  191. else if ( FIRST_POSSIBLE_YEAR > Time.wYear )
  192. {
  193. // cannot handle years less than FIRST_POSSIBLE_YEAR
  194. return E_INVALIDARG;
  195. }
  196. else
  197. {
  198. BAIL_ON_FAILURE(SystemTimeToNtpTime(Time, dwNtpStartTime));
  199. }
  200. CLock Lock(g_DllLock);
  201. HRESULT HResult = GetElement().SetStartTime((ULONG) dwNtpStartTime);
  202. return HResult;
  203. }
  204. STDMETHODIMP TIME::get_StopTime(DOUBLE * pVal)
  205. {
  206. BAIL_IF_NULL(pVal, E_INVALIDARG);
  207. CLock Lock(g_DllLock);
  208. ULONG StopTime;
  209. BAIL_ON_FAILURE(GetElement().GetStopTime(StopTime));
  210. SYSTEMTIME Time;
  211. HRESULT hr = NtpTimeToSystemTime(StopTime, Time);
  212. if( FAILED(hr) )
  213. {
  214. return hr;
  215. }
  216. DOUBLE vtime;
  217. if (SystemTimeToVariantTime(&Time, &vtime) == FALSE)
  218. {
  219. return E_INVALIDARG;
  220. }
  221. *pVal = vtime;
  222. return S_OK;
  223. }
  224. STDMETHODIMP TIME::put_StopTime(DOUBLE newVal)
  225. {
  226. SYSTEMTIME Time;
  227. if (VariantTimeToSystemTime(newVal, &Time) == FALSE)
  228. {
  229. return E_INVALIDARG;
  230. }
  231. DWORD_PTR dwNtpStartTime;
  232. if (newVal == 0)
  233. {
  234. // unbounded start time
  235. dwNtpStartTime = 0;
  236. }
  237. else if ( FIRST_POSSIBLE_YEAR > Time.wYear )
  238. {
  239. // cannot handle years less than FIRST_POSSIBLE_YEAR
  240. return E_INVALIDARG;
  241. }
  242. else
  243. {
  244. BAIL_ON_FAILURE(SystemTimeToNtpTime(Time, dwNtpStartTime));
  245. }
  246. CLock Lock(g_DllLock);
  247. GetElement().SetStopTime((ULONG)dwNtpStartTime);
  248. return S_OK;
  249. }