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 
8 #include "boost/date_time/posix_time/posix_time.hpp"
9 #include "../testfrmwk.hpp"
10 #include "boost/date_time/filetime_functions.hpp"
11 #include <cmath>
12 
13 #if defined(BOOST_HAS_FTIME)
14 #include <windows.h>
15 #endif
16 
main()17 int main()
18 {
19 #if defined(BOOST_HAS_FTIME) // skip tests if no FILETIME
20 
21   using namespace boost::posix_time;
22 
23   // adjustor is used to truncate ptime's fractional seconds for
24   // comparison with SYSTEMTIME's milliseconds
25   const time_duration::tick_type adjustor = time_duration::ticks_per_second() / 1000;
26 
27   for(int i = 0; i < 5; ++i){
28 
29     FILETIME ft;
30     SYSTEMTIME st;
31     GetSystemTime(&st);
32     SystemTimeToFileTime(&st,&ft);
33 
34     ptime pt = from_ftime<ptime>(ft);
35 
36     check_equal("ptime year matches systemtime year",
37         st.wYear, pt.date().year());
38     check_equal("ptime month matches systemtime month",
39         st.wMonth, pt.date().month());
40     check_equal("ptime day matches systemtime day",
41         st.wDay, pt.date().day());
42     check_equal("ptime hour matches systemtime hour",
43         st.wHour, pt.time_of_day().hours());
44     check_equal("ptime minute matches systemtime minute",
45         st.wMinute, pt.time_of_day().minutes());
46     check_equal("ptime second matches systemtime second",
47         st.wSecond, pt.time_of_day().seconds());
48     check_equal("truncated ptime fractional second matches systemtime millisecond",
49         st.wMilliseconds, (pt.time_of_day().fractional_seconds() / adjustor));
50 
51     // burn up a little time
52     for (int j=0; j<100000; j++)
53     {
54       SYSTEMTIME tmp;
55       GetSystemTime(&tmp);
56     }
57 
58   } // for loop
59 
60   // check that time_from_ftime works for pre-1970-Jan-01 dates, too
61   // zero FILETIME should represent 1601-Jan-01 00:00:00.000
62   FILETIME big_bang_by_ms;
63   big_bang_by_ms.dwLowDateTime = big_bang_by_ms.dwHighDateTime = 0;
64   ptime pt = from_ftime<ptime>(big_bang_by_ms);
65 
66   check_equal("big bang ptime year matches 1601",
67       1601, pt.date().year());
68   check_equal("big bang ptime month matches Jan",
69       1, pt.date().month());
70   check_equal("big bang ptime day matches 1",
71       1, pt.date().day());
72   check_equal("big bang ptime hour matches 0",
73       0, pt.time_of_day().hours());
74   check_equal("big bang ptime minute matches 0",
75       0, pt.time_of_day().minutes());
76   check_equal("big bang ptime second matches 0",
77       0, pt.time_of_day().seconds());
78   check_equal("big bang truncated ptime fractional second matches 0",
79       0, (pt.time_of_day().fractional_seconds() / adjustor));
80 
81 
82 #else // BOOST_HAS_FTIME
83   // we don't want a forced failure here, not a shortcoming
84   check("FILETIME not available for this compiler/platform", true);
85 #endif // BOOST_HAS_FTIME
86 
87   return printTestStats();
88 
89 }
90 
91