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.

297 lines
6.5 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. BAIL_ON_FAILURE(SdpTime->SetTimes(StartTime, StopTime));
  76. m_ConfBlob = &ConfBlob;
  77. // the init methods returns void
  78. ENUM_ELEMENT<SDP_TIME>::SuccessInit(*SdpTime, TRUE);
  79. return S_OK;
  80. }
  81. inline
  82. DWORD_PTR TimetToNtpTime(IN time_t TimetVal)
  83. {
  84. return TimetVal + NTP_OFFSET;
  85. }
  86. inline
  87. time_t NtpTimeToTimet(IN DWORD_PTR NtpTime)
  88. {
  89. return NtpTime - NTP_OFFSET;
  90. }
  91. inline HRESULT
  92. SystemTimeToNtpTime(
  93. IN SYSTEMTIME &Time,
  94. OUT DWORD_PTR &NtpDword
  95. )
  96. {
  97. _ASSERTE(FIRST_POSSIBLE_YEAR <= Time.wYear);
  98. // fill in a tm struct with the values
  99. tm NtpTmStruct;
  100. NtpTmStruct.tm_isdst = -1; // no info available about daylight savings time
  101. NtpTmStruct.tm_year = (int)Time.wYear - 1900;
  102. NtpTmStruct.tm_mon = (int)Time.wMonth - 1; // months since january
  103. NtpTmStruct.tm_mday = (int)Time.wDay;
  104. NtpTmStruct.tm_wday = (int)Time.wDayOfWeek;
  105. NtpTmStruct.tm_hour = (int)Time.wHour;
  106. NtpTmStruct.tm_min = (int)Time.wMinute;
  107. NtpTmStruct.tm_sec = (int)Time.wSecond;
  108. // try to convert into a time_t value
  109. time_t TimetVal = mktime(&NtpTmStruct);
  110. if ( -1 == TimetVal )
  111. {
  112. return E_INVALIDARG;
  113. }
  114. // convert the time_t value into an NTP value
  115. NtpDword = TimetToNtpTime(TimetVal);
  116. return S_OK;
  117. }
  118. inline
  119. HRESULT
  120. NtpTimeToSystemTime(
  121. IN DWORD dwNtpTime,
  122. OUT SYSTEMTIME &Time
  123. )
  124. {
  125. // if the gen time is WSTR_GEN_TIME_ZERO then,
  126. // all the out parameters should be set to 0
  127. if (dwNtpTime == 0)
  128. {
  129. memset(&Time, 0, sizeof(SYSTEMTIME));
  130. return S_OK;
  131. }
  132. time_t Timet = NtpTimeToTimet(dwNtpTime);
  133. // get the local tm struct for this time value
  134. tm LocalTm = *localtime(&Timet);
  135. //
  136. // win64: added casts below
  137. //
  138. // set the ref parameters to the tm struct values
  139. Time.wYear = (WORD) ( LocalTm.tm_year + 1900 ); // years since 1900
  140. Time.wMonth = (WORD) ( LocalTm.tm_mon + 1 ); // months SINCE january (0,11)
  141. Time.wDay = (WORD) LocalTm.tm_mday;
  142. Time.wDayOfWeek = (WORD) LocalTm.tm_wday;
  143. Time.wHour = (WORD) LocalTm.tm_hour;
  144. Time.wMinute = (WORD) LocalTm.tm_min;
  145. Time.wSecond = (WORD) LocalTm.tm_sec;
  146. Time.wMilliseconds = (WORD) 0;
  147. return S_OK;
  148. }
  149. STDMETHODIMP TIME::get_StartTime(DOUBLE * pVal)
  150. {
  151. BAIL_IF_NULL(pVal, E_INVALIDARG);
  152. CLock Lock(g_DllLock);
  153. ULONG StartTime;
  154. BAIL_ON_FAILURE(GetElement().GetStartTime(StartTime));
  155. SYSTEMTIME Time;
  156. NtpTimeToSystemTime(StartTime, Time);
  157. DOUBLE vtime;
  158. if (SystemTimeToVariantTime(&Time, &vtime) == FALSE)
  159. {
  160. return E_INVALIDARG;
  161. }
  162. *pVal = vtime;
  163. return S_OK;
  164. }
  165. STDMETHODIMP TIME::put_StartTime(DOUBLE newVal)
  166. {
  167. SYSTEMTIME Time;
  168. if (VariantTimeToSystemTime(newVal, &Time) == FALSE)
  169. {
  170. return E_INVALIDARG;
  171. }
  172. DWORD_PTR dwNtpStartTime;
  173. if (newVal == 0)
  174. {
  175. // unbounded start time
  176. dwNtpStartTime = 0;
  177. }
  178. else if ( FIRST_POSSIBLE_YEAR > Time.wYear )
  179. {
  180. // cannot handle years less than FIRST_POSSIBLE_YEAR
  181. return E_INVALIDARG;
  182. }
  183. else
  184. {
  185. BAIL_ON_FAILURE(SystemTimeToNtpTime(Time, dwNtpStartTime));
  186. }
  187. CLock Lock(g_DllLock);
  188. HRESULT HResult = GetElement().SetStartTime((ULONG) dwNtpStartTime);
  189. return HResult;
  190. }
  191. STDMETHODIMP TIME::get_StopTime(DOUBLE * pVal)
  192. {
  193. BAIL_IF_NULL(pVal, E_INVALIDARG);
  194. CLock Lock(g_DllLock);
  195. ULONG StopTime;
  196. BAIL_ON_FAILURE(GetElement().GetStopTime(StopTime));
  197. SYSTEMTIME Time;
  198. NtpTimeToSystemTime(StopTime, Time);
  199. DOUBLE vtime;
  200. if (SystemTimeToVariantTime(&Time, &vtime) == FALSE)
  201. {
  202. return E_INVALIDARG;
  203. }
  204. *pVal = vtime;
  205. return S_OK;
  206. }
  207. STDMETHODIMP TIME::put_StopTime(DOUBLE newVal)
  208. {
  209. SYSTEMTIME Time;
  210. if (VariantTimeToSystemTime(newVal, &Time) == FALSE)
  211. {
  212. return E_INVALIDARG;
  213. }
  214. DWORD_PTR dwNtpStartTime;
  215. if (newVal == 0)
  216. {
  217. // unbounded start time
  218. dwNtpStartTime = 0;
  219. }
  220. else if ( FIRST_POSSIBLE_YEAR > Time.wYear )
  221. {
  222. // cannot handle years less than FIRST_POSSIBLE_YEAR
  223. return E_INVALIDARG;
  224. }
  225. else
  226. {
  227. BAIL_ON_FAILURE(SystemTimeToNtpTime(Time, dwNtpStartTime));
  228. }
  229. CLock Lock(g_DllLock);
  230. GetElement().SetStopTime((ULONG)dwNtpStartTime);
  231. return S_OK;
  232. }