Imported Upstream version 1.57.0
[platform/upstream/boost.git] / libs / date_time / test / posix_time / testc_local_adjustor.cpp
1 /* Copyright (c) 2002,2003 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 
6  */
7
8 #include "boost/date_time/posix_time/posix_time.hpp"
9 #include "boost/date_time/c_local_time_adjustor.hpp"
10 #include "../testfrmwk.hpp"
11
12 int
13 main() 
14 {
15   using namespace boost::posix_time;
16   using namespace boost::gregorian;
17
18   //These are a compile check / test.  They have to be hand inspected
19   //b/c they depend on the TZ settings of the machine and hence it is
20   //unclear what the results will be
21   typedef boost::date_time::c_local_adjustor<ptime> local_adj;
22
23   ptime t1(date(2002,Jan,1), hours(7)+millisec(5)); 
24   std::cout << "UTC <--> TZ Setting of Machine -- No DST" << std::endl;
25   ptime t2 = local_adj::utc_to_local(t1);
26   std::cout << to_simple_string(t2) << " LOCAL is " 
27             << to_simple_string(t1) << " UTC time "
28             << std::endl;
29   time_duration td1 = t2 - t1;
30   std::cout << "A difference of: " << to_simple_string(td1)
31             << std::endl;
32
33
34   ptime t3(date(2002,May,1), hours(5)+millisec(5)); 
35   std::cout << "UTC <--> TZ Setting of Machine -- In DST" << std::endl;
36   ptime t4 = local_adj::utc_to_local(t3);
37   std::cout << to_simple_string(t4) << " LOCAL is " 
38             << to_simple_string(t3) << " UTC time "
39             << std::endl;
40   time_duration td2 = t4 - t3;
41   std::cout << "A difference of: " << to_simple_string(td2)
42             << std::endl;
43
44   ptime t5(date(2040,May,1), hours(5)+millisec(5));
45   std::cout << "UTC <--> TZ Setting of Machine -- In DST" << std::endl;
46   ptime t6 = local_adj::utc_to_local(t5);
47   std::cout << to_simple_string(t6) << " LOCAL is " 
48             << to_simple_string(t5) << " UTC time "
49             << std::endl;
50   time_duration td3 = t6 - t5;
51   std::cout << "a difference of: " << to_simple_string(td3)
52             << std::endl;
53
54   // The following tests are unaware of the local time zone, but they
55   // should help spot some errors. Manual inspection could still be
56   // required.
57
58   // Based on http://stackoverflow.com/questions/8131023
59   // All time zones are between -12 and +14
60   check("td1 isn't too low", td1 >= hours(-12));
61   check("td1 isn't too high", td1 <= hours(14));
62   check("td2 isn't too low", td2 >= hours(-12));
63   check("td2 isn't too high", td2 <= hours(14));
64   check("td3 isn't too low", td3 >= hours(-12));
65   check("td3 isn't too high", td3 <= hours(14));
66
67   // Assuming that no one uses DST of more than an hour.
68   check("td1 and td2 are close",
69           td1 - td2 <= hours(1) && td2 - td1 <= hours(1));
70   check("td2 and td3 are close",
71           td2 - td3 <= hours(2) && td3 - td2 <= hours(2));
72   check("td1 and td3 are close",
73           td1 - td3 <= hours(1) && td3 - td1 <= hours(1));
74
75   return printTestStats();
76 }
77