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.

372 lines
9.2 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: winstl/time/conversion_functions.h
  3. *
  4. * Purpose: Comparison functions for Windows time structures.
  5. *
  6. * Created: 21st November 2003
  7. * Updated: 17th March 2012
  8. *
  9. * Home: http://stlsoft.org/
  10. *
  11. * Copyright (c) 2003-2012, Matthew Wilson and Synesis Software
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright notice, this
  18. * list of conditions and the following disclaimer.
  19. * - Redistributions in binary form must reproduce the above copyright notice,
  20. * this list of conditions and the following disclaimer in the documentation
  21. * and/or other materials provided with the distribution.
  22. * - Neither the name(s) of Matthew Wilson and Synesis Software nor the names of
  23. * any contributors may be used to endorse or promote products derived from
  24. * this software without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  27. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  29. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  30. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  31. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  32. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  33. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  34. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  35. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  36. * POSSIBILITY OF SUCH DAMAGE.
  37. *
  38. * ////////////////////////////////////////////////////////////////////// */
  39. /** \file winstl/time/conversion_functions.h
  40. *
  41. * \brief [C, C++] Conversion functions for Windows time types
  42. * (\ref group__library__time "Time" Library).
  43. */
  44. #ifndef WINSTL_INCL_WINSTL_TIME_H_CONVERSION_FUNCTIONS
  45. #define WINSTL_INCL_WINSTL_TIME_H_CONVERSION_FUNCTIONS
  46. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  47. # define WINSTL_VER_WINSTL_TIME_H_CONVERSION_FUNCTIONS_MAJOR 4
  48. # define WINSTL_VER_WINSTL_TIME_H_CONVERSION_FUNCTIONS_MINOR 1
  49. # define WINSTL_VER_WINSTL_TIME_H_CONVERSION_FUNCTIONS_REVISION 1
  50. # define WINSTL_VER_WINSTL_TIME_H_CONVERSION_FUNCTIONS_EDIT 53
  51. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  52. /* /////////////////////////////////////////////////////////////////////////
  53. * Includes
  54. */
  55. #ifndef WINSTL_INCL_WINSTL_H_WINSTL
  56. # include <winstl/winstl.h>
  57. #endif /* !WINSTL_INCL_WINSTL_H_WINSTL */
  58. #ifndef STLSOFT_INCL_STLSOFT_UTIL_H_LIMIT_TRAITS
  59. # include <stlsoft/util/limit_traits.h>
  60. #endif /* !STLSOFT_INCL_STLSOFT_UTIL_H_LIMIT_TRAITS */
  61. #ifndef STLSOFT_INCL_H_TIME
  62. # define STLSOFT_INCL_H_TIME
  63. # include <time.h>
  64. #endif /* !STLSOFT_INCL_H_TIME */
  65. /* /////////////////////////////////////////////////////////////////////////
  66. * Namespace
  67. */
  68. #if !defined(_WINSTL_NO_NAMESPACE) && \
  69. !defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  70. # if defined(_STLSOFT_NO_NAMESPACE)
  71. /* There is no stlsoft namespace, so must define ::winstl */
  72. namespace winstl
  73. {
  74. # else
  75. /* Define stlsoft::winstl_project */
  76. namespace stlsoft
  77. {
  78. namespace winstl_project
  79. {
  80. # endif /* _STLSOFT_NO_NAMESPACE */
  81. #endif /* !_WINSTL_NO_NAMESPACE */
  82. /* /////////////////////////////////////////////////////////////////////////
  83. * C functions
  84. */
  85. /** \brief Converts a time_t into a FILETIME
  86. *
  87. * \ingroup group__library__time
  88. *
  89. * \note This follows the algorithm provided in MSDN Q167296
  90. *
  91. * \pre (NULL != ft)
  92. */
  93. STLSOFT_INLINE
  94. void
  95. winstl_C_UNIXTimeToFILETIME(
  96. time_t t
  97. , FILETIME* ft
  98. )
  99. {
  100. ws_uint64_t const i = UInt32x32To64(t, 10000000) + STLSOFT_GEN_UINT64_SUFFIX(116444736000000000);
  101. WINSTL_ASSERT(NULL != ft);
  102. ft->dwLowDateTime = (DWORD)i;
  103. ft->dwHighDateTime = (DWORD)(i >> 32);
  104. }
  105. /** Converts a time_t and a microseconds value into a FILETIME
  106. *
  107. * \ingroup group__library__time
  108. *
  109. * \note This follows the algorithm provided in MSDN Q167296
  110. *
  111. * \pre (NULL != ft)
  112. */
  113. STLSOFT_INLINE
  114. void
  115. winstl_C_UNIXTimeToFILETIME_us(
  116. time_t t
  117. , ws_sint32_t usec
  118. , FILETIME* ft
  119. )
  120. {
  121. ws_uint64_t const i = UInt32x32To64(t, 10000000) + Int32x32To64(usec, 10) + STLSOFT_GEN_UINT64_SUFFIX(116444736000000000);
  122. WINSTL_ASSERT(NULL != ft);
  123. ft->dwLowDateTime = (DWORD)i;
  124. ft->dwHighDateTime = (DWORD)(i >> 32);
  125. }
  126. /** Converts a FILETIME into a time_t
  127. *
  128. * \ingroup group__library__time
  129. *
  130. * \note This follows the algorithm provided in MSDN Q167296
  131. *
  132. * \pre (NULL != ft)
  133. * \pre (NULL != microseconds)
  134. */
  135. STLSOFT_INLINE
  136. time_t
  137. winstl_C_FILETIMEToUNIXTime(
  138. FILETIME const* ft
  139. , ws_sint32_t* microseconds
  140. )
  141. {
  142. ws_sint64_t i;
  143. i = ft->dwHighDateTime;
  144. i <<= 32;
  145. i |= ft->dwLowDateTime;
  146. i -= STLSOFT_GEN_UINT64_SUFFIX(116444736000000000);
  147. if(NULL != microseconds)
  148. {
  149. *microseconds = stlsoft_static_cast(ws_sint32_t, (i % 10000000) / 10);
  150. WINSTL_ASSERT(*microseconds >= 0 && *microseconds <= 999999);
  151. }
  152. i /= 10000000;
  153. return stlsoft_static_cast(time_t, i);
  154. }
  155. /* /////////////////////////////////////////////////////////////////////////
  156. * Obsolete symbols
  157. *
  158. * NOTE: these are only defined if:
  159. *
  160. * - we're generating documentation, or
  161. * - STLSOFT_OBSOLETE is specified, or
  162. * - it's STLSoft 1.9 (or earlier)
  163. */
  164. #if defined(STLSOFT_DOCUMENTATION_SKIP_SECTION) || \
  165. defined(STLSOFT_OBSOLETE) || \
  166. _STLSOFT_VER < 0x010a0000
  167. /** \def winstl__UNIXTimeToFILETIME
  168. *
  169. * \deprecated Use winstl_C_UNIXTimeToFILETIME
  170. */
  171. # define winstl__UNIXTimeToFILETIME winstl_C_UNIXTimeToFILETIME
  172. /** \def winstl__UNIXTimeToFILETIME_us
  173. *
  174. * \deprecated Use winstl_C_UNIXTimeToFILETIME_us
  175. */
  176. # define winstl__UNIXTimeToFILETIME_us winstl_C_UNIXTimeToFILETIME_us
  177. /** \def winstl__FILETIMEToUNIXTime
  178. *
  179. * \deprecated Use winstl_C_FILETIMEToUNIXTime
  180. */
  181. # define winstl__FILETIMEToUNIXTime winstl_C_FILETIMEToUNIXTime
  182. #endif /* obsolete || 1.9 */
  183. /* /////////////////////////////////////////////////////////////////////////
  184. * Namespace
  185. */
  186. #ifdef STLSOFT_DOCUMENTATION_SKIP_SECTION
  187. namespace winstl
  188. {
  189. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  190. /* /////////////////////////////////////////////////////////////////////////
  191. * C++ functions
  192. */
  193. #ifdef __cplusplus
  194. # ifdef STLSOFT_CF_64BIT_INT_SUPPORT
  195. /** \brief Converts a time_t into a FILETIME
  196. *
  197. * \ingroup group__library__time
  198. *
  199. * \note This follows the algorithm provided in MSDN Q167296
  200. */
  201. inline
  202. void
  203. UNIXTimeToFILETIME(
  204. time_t t
  205. , FILETIME& ft
  206. )
  207. {
  208. winstl_C_UNIXTimeToFILETIME(t, &ft);
  209. }
  210. /** \brief Converts a time_t into a FILETIME
  211. *
  212. * \ingroup group__library__time
  213. *
  214. * \note This follows the algorithm provided in MSDN Q167296
  215. */
  216. inline
  217. FILETIME
  218. UNIXTimeToFILETIME(
  219. time_t t
  220. )
  221. {
  222. FILETIME ft;
  223. winstl_C_UNIXTimeToFILETIME(t, &ft);
  224. return ft;
  225. }
  226. /** \brief Converts a time_t + microseconds into a FILETIME
  227. *
  228. * \ingroup group__library__time
  229. *
  230. * \note This follows the algorithm provided in MSDN Q167296
  231. */
  232. inline
  233. void
  234. UNIXTimeToFILETIME(
  235. time_t t
  236. , ws_sint32_t usec
  237. , FILETIME& ft
  238. )
  239. {
  240. winstl_C_UNIXTimeToFILETIME_us(t, usec, &ft);
  241. }
  242. /** \brief Converts a time_t + microseconds into a FILETIME
  243. *
  244. * \ingroup group__library__time
  245. *
  246. * \note This follows the algorithm provided in MSDN Q167296
  247. */
  248. inline
  249. FILETIME
  250. UNIXTimeToFILETIME(
  251. time_t t
  252. , ws_sint32_t usec
  253. )
  254. {
  255. FILETIME ft;
  256. winstl_C_UNIXTimeToFILETIME_us(t, usec, &ft);
  257. return ft;
  258. }
  259. /** \brief Converts a FILETIME into a time_t
  260. *
  261. * \ingroup group__library__time
  262. */
  263. inline
  264. time_t
  265. FILETIMEToUNIXTime(
  266. FILETIME const& ft
  267. , ws_sint32_t* microseconds = NULL
  268. )
  269. {
  270. return winstl_C_FILETIMEToUNIXTime(&ft, microseconds);
  271. }
  272. /** \brief Converts a FILETIME into a time_t
  273. *
  274. * \ingroup group__library__time
  275. */
  276. inline
  277. void
  278. FILETIMEToUNIXTime(
  279. FILETIME const& ft
  280. , time_t& t
  281. )
  282. {
  283. t = winstl_C_FILETIMEToUNIXTime(&ft, NULL);
  284. }
  285. /** \brief Converts a FILETIME into a time_t
  286. *
  287. * \ingroup group__library__time
  288. */
  289. inline
  290. void
  291. FILETIMEToUNIXTime(
  292. FILETIME const& ft
  293. , time_t& t
  294. , ws_sint32_t& microseconds
  295. )
  296. {
  297. t = winstl_C_FILETIMEToUNIXTime(&ft, &microseconds);
  298. }
  299. # endif /* STLSOFT_CF_64BIT_INT_SUPPORT */
  300. #endif /* __cplusplus */
  301. /* /////////////////////////////////////////////////////////////////////////
  302. * Unit-testing
  303. */
  304. #ifdef STLSOFT_UNITTEST
  305. # include "./unittest/conversion_functions_unittest_.h"
  306. #endif /* STLSOFT_UNITTEST */
  307. /* ////////////////////////////////////////////////////////////////////// */
  308. #ifndef _WINSTL_NO_NAMESPACE
  309. # if defined(_STLSOFT_NO_NAMESPACE) || \
  310. defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
  311. } /* namespace winstl */
  312. # else
  313. } /* namespace winstl_project */
  314. } /* namespace stlsoft */
  315. # endif /* _STLSOFT_NO_NAMESPACE */
  316. #endif /* !_WINSTL_NO_NAMESPACE */
  317. /* ////////////////////////////////////////////////////////////////////// */
  318. #endif /* !WINSTL_INCL_WINSTL_TIME_H_CONVERSION_FUNCTIONS */
  319. /* ///////////////////////////// end of file //////////////////////////// */