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.

289 lines
6.5 KiB

  1. //---------------------------------------------------------------------------
  2. // TimeConv.cpp : Date time conversion routines
  3. //
  4. // Copyright (c) 1996 Microsoft Corporation, All Rights Reserved
  5. // Developed by Sheridan Software Systems, Inc.
  6. //---------------------------------------------------------------------------
  7. #include "stdafx.h"
  8. #include "timeconv.h"
  9. #include <math.h>
  10. #include <limits.h>
  11. // needed for ASSERTs and FAIL
  12. //
  13. SZTHISFILE
  14. //-----------------------------------------------------------------------
  15. // The following routines convert between differnt data/time formats
  16. // they return TRUE if successful otherwise they return FALSE
  17. //-----------------------------------------------------------------------
  18. BOOL VDConvertToFileTime(DBTIMESTAMP * pDBTimeStamp, FILETIME * pFileTime)
  19. {
  20. ASSERT_POINTER(pFileTime, FILETIME);
  21. ASSERT_POINTER(pDBTimeStamp, DBTIMESTAMP);
  22. if (pFileTime &&
  23. pDBTimeStamp)
  24. {
  25. SYSTEMTIME st;
  26. st.wYear = pDBTimeStamp->year;
  27. st.wMonth = pDBTimeStamp->month;
  28. st.wDay = pDBTimeStamp->day;
  29. st.wDayOfWeek = 0; // ?
  30. st.wHour = pDBTimeStamp->hour;
  31. st.wMinute = pDBTimeStamp->minute;
  32. st.wSecond = pDBTimeStamp->second;
  33. st.wMilliseconds = (USHORT)(pDBTimeStamp->fraction / 1000000);
  34. return SystemTimeToFileTime(&st, pFileTime);
  35. }
  36. return FALSE;
  37. }
  38. BOOL VDConvertToFileTime(DBDATE * pDBDate, FILETIME * pFileTime)
  39. {
  40. ASSERT_POINTER(pFileTime, FILETIME);
  41. ASSERT_POINTER(pDBDate, DBDATE);
  42. if (pFileTime &&
  43. pDBDate)
  44. {
  45. SYSTEMTIME st;
  46. st.wYear = pDBDate->year;
  47. st.wMonth = pDBDate->month;
  48. st.wDay = pDBDate->day;
  49. st.wDayOfWeek = 0;
  50. st.wHour = 0;
  51. st.wMinute = 0;
  52. st.wSecond = 0;
  53. st.wMilliseconds = 0;
  54. return SystemTimeToFileTime(&st, pFileTime);
  55. }
  56. return FALSE;
  57. }
  58. BOOL VDConvertToFileTime(DBTIME * pDBTime, FILETIME * pFileTime)
  59. {
  60. ASSERT_POINTER(pFileTime, FILETIME);
  61. ASSERT_POINTER(pDBTime, DBTIME);
  62. if (pFileTime &&
  63. pDBTime)
  64. {
  65. SYSTEMTIME st;
  66. st.wYear = 1996; // any date will do since we
  67. st.wMonth = 07; // are only interested in the time
  68. st.wDay = 30;
  69. st.wDayOfWeek = 2;
  70. st.wHour = pDBTime->hour;
  71. st.wMinute = pDBTime->minute;
  72. st.wSecond = pDBTime->second;
  73. st.wMilliseconds = 0;
  74. return SystemTimeToFileTime(&st, pFileTime);
  75. }
  76. return FALSE;
  77. }
  78. BOOL VDConvertToFileTime(DATE * pDate, FILETIME * pFileTime)
  79. {
  80. ASSERT_POINTER(pDate, DATE);
  81. ASSERT_POINTER(pFileTime, FILETIME);
  82. if (pDate &&
  83. pFileTime)
  84. {
  85. WORD wFatDate;
  86. WORD wFatTime;
  87. if (VariantTimeToDosDateTime(*pDate, &wFatDate, &wFatTime))
  88. return DosDateTimeToFileTime(wFatDate, wFatTime, pFileTime);
  89. }
  90. return FALSE;
  91. }
  92. BOOL VDConvertToDBTimeStamp(FILETIME * pFileTime, DBTIMESTAMP * pDBTimeStamp)
  93. {
  94. SYSTEMTIME st;
  95. ASSERT_POINTER(pFileTime, FILETIME);
  96. ASSERT_POINTER(pDBTimeStamp, DBTIMESTAMP);
  97. if (pFileTime &&
  98. pDBTimeStamp &&
  99. FileTimeToSystemTime(pFileTime, &st))
  100. {
  101. pDBTimeStamp->year = st.wYear;
  102. pDBTimeStamp->month = st.wMonth;
  103. pDBTimeStamp->day = st.wDay;
  104. pDBTimeStamp->hour = st.wHour;
  105. pDBTimeStamp->minute = st.wMinute;
  106. pDBTimeStamp->second = st.wSecond;
  107. pDBTimeStamp->fraction = (ULONG)st.wMilliseconds * 1000000;
  108. return TRUE;
  109. }
  110. return FALSE;
  111. }
  112. BOOL VDConvertToDBTimeStamp(DATE * pDate, DBTIMESTAMP * pDBTimeStamp)
  113. {
  114. ASSERT_POINTER(pDate, DATE);
  115. ASSERT_POINTER(pDBTimeStamp, DBTIMESTAMP);
  116. if (pDate &&
  117. pDBTimeStamp)
  118. {
  119. FILETIME filetime;
  120. if (VDConvertToFileTime(pDate, &filetime))
  121. return VDConvertToDBTimeStamp(&filetime, pDBTimeStamp);
  122. }
  123. return FALSE;
  124. }
  125. BOOL VDConvertToDBDate(FILETIME * pFileTime, DBDATE * pDBDate)
  126. {
  127. SYSTEMTIME st;
  128. ASSERT_POINTER(pFileTime, FILETIME);
  129. ASSERT_POINTER(pDBDate, DBDATE);
  130. if (pFileTime &&
  131. pDBDate &&
  132. FileTimeToSystemTime(pFileTime, &st))
  133. {
  134. pDBDate->year = st.wYear;
  135. pDBDate->month = st.wMonth;
  136. pDBDate->day = st.wDay;
  137. return TRUE;
  138. }
  139. return FALSE;
  140. }
  141. BOOL VDConvertToDBDate(DATE * pDate, DBDATE * pDBDate)
  142. {
  143. ASSERT_POINTER(pDate, DATE);
  144. ASSERT_POINTER(pDBDate, DBDATE);
  145. if (pDate &&
  146. pDBDate)
  147. {
  148. FILETIME filetime;
  149. if (VDConvertToFileTime(pDate, &filetime))
  150. return VDConvertToDBDate(&filetime, pDBDate);
  151. }
  152. return FALSE;
  153. }
  154. BOOL VDConvertToDBTime(FILETIME * pFileTime, DBTIME * pDBTime)
  155. {
  156. SYSTEMTIME st;
  157. ASSERT_POINTER(pFileTime, FILETIME);
  158. ASSERT_POINTER(pDBTime, DBTIME);
  159. if (pFileTime &&
  160. pDBTime &&
  161. FileTimeToSystemTime(pFileTime, &st))
  162. {
  163. pDBTime->hour = st.wHour;
  164. pDBTime->minute = st.wMinute;
  165. pDBTime->second = st.wSecond;
  166. return TRUE;
  167. }
  168. return FALSE;
  169. }
  170. BOOL VDConvertToDBTime(DATE * pDate, DBTIME * pDBTime)
  171. {
  172. ASSERT_POINTER(pDate, DATE);
  173. ASSERT_POINTER(pDBTime, DBTIME);
  174. if (pDate &&
  175. pDBTime)
  176. {
  177. FILETIME filetime;
  178. if (VDConvertToFileTime(pDate, &filetime))
  179. return VDConvertToDBTime(&filetime, pDBTime);
  180. }
  181. return FALSE;
  182. }
  183. BOOL VDConvertToDate(FILETIME * pFileTime, DATE * pDate)
  184. {
  185. ASSERT_POINTER(pDate, DATE);
  186. ASSERT_POINTER(pFileTime, FILETIME);
  187. if (pDate &&
  188. pFileTime)
  189. {
  190. WORD wFatDate;
  191. WORD wFatTime;
  192. if (FileTimeToDosDateTime(pFileTime, &wFatDate, &wFatTime))
  193. return DosDateTimeToVariantTime(wFatDate, wFatTime, pDate);
  194. }
  195. return FALSE;
  196. }
  197. BOOL VDConvertToDate(DBTIMESTAMP * pDBTimeStamp, DATE * pDate)
  198. {
  199. ASSERT_POINTER(pDate, DATE);
  200. ASSERT_POINTER(pDBTimeStamp, DBTIMESTAMP);
  201. if (pDate &&
  202. pDBTimeStamp)
  203. {
  204. FILETIME fileTime;
  205. if (VDConvertToFileTime(pDBTimeStamp, &fileTime))
  206. return VDConvertToDate(&fileTime, pDate);
  207. }
  208. return FALSE;
  209. }
  210. BOOL VDConvertToDate(DBTIME * pDBTime, DATE * pDate)
  211. {
  212. ASSERT_POINTER(pDate, DATE);
  213. ASSERT_POINTER(pDBTime, DBTIME);
  214. if (pDate &&
  215. pDBTime)
  216. {
  217. FILETIME fileTime;
  218. if (VDConvertToFileTime(pDBTime, &fileTime))
  219. {
  220. DATE date;
  221. if (VDConvertToDate(&fileTime, &date))
  222. {
  223. DATE dateIntegerPart;
  224. // return only the time (fractional part of double)
  225. *pDate = modf(date, &dateIntegerPart);
  226. }
  227. }
  228. }
  229. return FALSE;
  230. }
  231. BOOL VDConvertToDate(DBDATE * pDBDate, DATE * pDate)
  232. {
  233. ASSERT_POINTER(pDate, DATE);
  234. ASSERT_POINTER(pDBDate, DBDATE);
  235. if (pDate &&
  236. pDBDate)
  237. {
  238. FILETIME fileTime;
  239. if (VDConvertToFileTime(pDBDate, &fileTime))
  240. return VDConvertToDate(&fileTime, pDate);
  241. }
  242. return FALSE;
  243. }