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)
9 #include "boost/date_time/posix_time/posix_time.hpp"
10 #include "boost/date_time/c_local_time_adjustor.hpp"
11 #include "../testfrmwk.hpp"
16 using namespace boost::posix_time;
17 using namespace boost::gregorian;
19 //These are a compile check / test. They have to be hand inspected
20 //b/c they depend on the TZ settings of the machine and hence it is
21 //unclear what the results will be
22 typedef boost::date_time::c_local_adjustor<ptime> local_adj;
33 ptime t1(date(2002,Jan,1), hours(7)+millisec(5));
34 std::cout << "UTC <--> TZ Setting of Machine -- No DST" << std::endl;
35 ptime t2 = local_adj::utc_to_local(t1);
36 std::cout << to_simple_string(t2) << " LOCAL is "
37 << to_simple_string(t1) << " UTC time "
40 std::cout << "A difference of: " << to_simple_string(td1)
43 catch (std::runtime_error & re)
46 check(re.what(), false);
51 ptime t3(date(2002,May,1), hours(5)+millisec(5));
52 std::cout << "UTC <--> TZ Setting of Machine -- In DST" << std::endl;
53 ptime t4 = local_adj::utc_to_local(t3);
54 std::cout << to_simple_string(t4) << " LOCAL is "
55 << to_simple_string(t3) << " UTC time "
58 std::cout << "A difference of: " << to_simple_string(td2)
61 catch (std::runtime_error & re)
64 check(re.what(), false);
69 ptime t5(date(2040,May,1), hours(5)+millisec(5));
70 std::cout << "UTC <--> TZ Setting of Machine -- In DST" << std::endl;
71 ptime t6 = local_adj::utc_to_local(t5);
72 std::cout << to_simple_string(t6) << " LOCAL is "
73 << to_simple_string(t5) << " UTC time "
76 std::cout << "a difference of: " << to_simple_string(td3)
79 catch (std::runtime_error & re)
82 check(re.what(), false);
84 catch (std::bad_cast&)
87 check("32-bit time_t overflow detected", sizeof(std::time_t) < 8);
90 // The following tests are unaware of the local time zone, but they
91 // should help spot some errors. Manual inspection could still be
94 // Based on http://stackoverflow.com/questions/8131023
95 // All time zones are between -12 and +14
98 check("td1 isn't too low", td1 >= hours(-12));
99 check("td1 isn't too high", td1 <= hours(14));
103 check("td2 isn't too low", td2 >= hours(-12));
104 check("td2 isn't too high", td2 <= hours(14));
108 check("td3 isn't too low", td3 >= hours(-12));
109 check("td3 isn't too high", td3 <= hours(14));
112 // Assuming that no one uses DST of more than an hour.
113 if (!btd1e && !btd2e)
115 check("td1 and td2 are close",
116 td1 - td2 <= hours(1) && td2 - td1 <= hours(1));
118 if (!btd2e && !btd3e)
120 check("td2 and td3 are close",
121 td2 - td3 <= hours(2) && td3 - td2 <= hours(2));
123 if (!btd1e && !btd3e)
125 check("td1 and td3 are close",
126 td1 - td3 <= hours(1) && td3 - td1 <= hours(1));
129 return printTestStats();