testfiletime_functions.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /* Copyright (c) 2002,2003, 2004 CrystalClear Software, Inc.
  2. * Use, modification and distribution is subject to the
  3. * Boost Software License, Version 1.0. (See accompanying
  4. * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
  5. * Author: Jeff Garland, Bart Garst
  6. */
  7. #include "boost/date_time/posix_time/posix_time.hpp"
  8. #include "../testfrmwk.hpp"
  9. #include "boost/date_time/filetime_functions.hpp"
  10. #include <cmath>
  11. #if defined(BOOST_HAS_FTIME)
  12. #include <windows.h>
  13. #endif
  14. int main()
  15. {
  16. #if defined(BOOST_HAS_FTIME) // skip tests if no FILETIME
  17. using namespace boost::posix_time;
  18. // adjustor is used to truncate ptime's fractional seconds for
  19. // comparison with SYSTEMTIME's milliseconds
  20. const time_duration::tick_type adjustor = time_duration::ticks_per_second() / 1000;
  21. for(int i = 0; i < 5; ++i){
  22. FILETIME ft;
  23. SYSTEMTIME st;
  24. GetSystemTime(&st);
  25. SystemTimeToFileTime(&st,&ft);
  26. ptime pt = from_ftime<ptime>(ft);
  27. check_equal("ptime year matches systemtime year",
  28. st.wYear, pt.date().year());
  29. check_equal("ptime month matches systemtime month",
  30. st.wMonth, pt.date().month());
  31. check_equal("ptime day matches systemtime day",
  32. st.wDay, pt.date().day());
  33. check_equal("ptime hour matches systemtime hour",
  34. st.wHour, pt.time_of_day().hours());
  35. check_equal("ptime minute matches systemtime minute",
  36. st.wMinute, pt.time_of_day().minutes());
  37. check_equal("ptime second matches systemtime second",
  38. st.wSecond, pt.time_of_day().seconds());
  39. check_equal("truncated ptime fractional second matches systemtime millisecond",
  40. st.wMilliseconds, (pt.time_of_day().fractional_seconds() / adjustor));
  41. // burn up a little time
  42. for (int j=0; j<100000; j++)
  43. {
  44. SYSTEMTIME tmp;
  45. GetSystemTime(&tmp);
  46. }
  47. } // for loop
  48. // check that time_from_ftime works for pre-1970-Jan-01 dates, too
  49. // zero FILETIME should represent 1601-Jan-01 00:00:00.000
  50. FILETIME big_bang_by_ms;
  51. big_bang_by_ms.dwLowDateTime = big_bang_by_ms.dwHighDateTime = 0;
  52. ptime pt = from_ftime<ptime>(big_bang_by_ms);
  53. check_equal("big bang ptime year matches 1601",
  54. 1601, pt.date().year());
  55. check_equal("big bang ptime month matches Jan",
  56. 1, pt.date().month());
  57. check_equal("big bang ptime day matches 1",
  58. 1, pt.date().day());
  59. check_equal("big bang ptime hour matches 0",
  60. 0, pt.time_of_day().hours());
  61. check_equal("big bang ptime minute matches 0",
  62. 0, pt.time_of_day().minutes());
  63. check_equal("big bang ptime second matches 0",
  64. 0, pt.time_of_day().seconds());
  65. check_equal("big bang truncated ptime fractional second matches 0",
  66. 0, (pt.time_of_day().fractional_seconds() / adjustor));
  67. #else // BOOST_HAS_FTIME
  68. // we don't want a forced failure here, not a shortcoming
  69. check("FILETIME not available for this compiler/platform", true);
  70. #endif // BOOST_HAS_FTIME
  71. return printTestStats();
  72. }