summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/date_time/test/posix_time/testc_local_adjustor.cpp
blob: d9947a6eb01ab290058d5924b2f997eadc048aec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/* Copyright (c) 2002,2003 CrystalClear Software, Inc.
 * Use, modification and distribution is subject to the 
 * Boost Software License, Version 1.0. (See accompanying
 * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
 * Author: Jeff Garland 
 */

#include <stdexcept>
#include "boost/date_time/posix_time/posix_time.hpp"
#include "boost/date_time/c_local_time_adjustor.hpp"
#include "../testfrmwk.hpp"

int
main() 
{
  using namespace boost::posix_time;
  using namespace boost::gregorian;

  //These are a compile check / test.  They have to be hand inspected
  //b/c they depend on the TZ settings of the machine and hence it is
  //unclear what the results will be
  typedef boost::date_time::c_local_adjustor<ptime> local_adj;
  
  bool btd1e = false;
  bool btd2e = false;
  bool btd3e = false;
  time_duration td1;
  time_duration td2;
  time_duration td3;
  
  try
      {
      ptime t1(date(2002,Jan,1), hours(7)+millisec(5)); 
      std::cout << "UTC <--> TZ Setting of Machine -- No DST" << std::endl;
      ptime t2 = local_adj::utc_to_local(t1);
      std::cout << t2 << " LOCAL is " 
                << t1 << " UTC time "
                << std::endl;
      td1 = t2 - t1;
      std::cout << "A difference of: " << td1
                << std::endl;
      }
  catch (std::runtime_error & re)
      {
      btd1e = true;
      check(re.what(), false);
      }
  
  try
      {
      ptime t3(date(2002,May,1), hours(5)+millisec(5)); 
      std::cout << "UTC <--> TZ Setting of Machine -- In DST" << std::endl;
      ptime t4 = local_adj::utc_to_local(t3);
      std::cout << t4 << " LOCAL is " 
                << t3 << " UTC time "
                << std::endl;
      td2 = t4 - t3;
      std::cout << "A difference of: " << td2
                << std::endl;
      }
  catch (std::runtime_error & re)
      {
      btd2e = true;
      check(re.what(), false);
      }

  try
      {
      ptime t5(date(2040,May,1), hours(5)+millisec(5));
      std::cout << "UTC <--> TZ Setting of Machine -- In DST" << std::endl;
      ptime t6 = local_adj::utc_to_local(t5);
      std::cout << t6 << " LOCAL is " 
                << t5 << " UTC time "
                << std::endl;
      td3 = t6 - t5;
      std::cout << "a difference of: " << td3
                << std::endl;
      }
  catch (std::runtime_error & re)
      {
      btd3e = true;
      check(re.what(), false);
      }
  catch (std::bad_cast&)
      {
      btd3e = true;
      check("32-bit time_t overflow detected", sizeof(std::time_t) < 8);
      }

  // The following tests are unaware of the local time zone, but they
  // should help spot some errors. Manual inspection could still be
  // required.

  // Based on http://stackoverflow.com/questions/8131023
  // All time zones are between -12 and +14
  if (!btd1e)
      {
      check("td1 isn't too low", td1 >= hours(-12));
      check("td1 isn't too high", td1 <= hours(14));
      }
  if (!btd2e)
      {
      check("td2 isn't too low", td2 >= hours(-12));
      check("td2 isn't too high", td2 <= hours(14));
      }
  if (!btd3e)
      {
      check("td3 isn't too low", td3 >= hours(-12));
      check("td3 isn't too high", td3 <= hours(14));
      }

  // Assuming that no one uses DST of more than an hour.
  if (!btd1e && !btd2e)
      {
      check("td1 and td2 are close",
              td1 - td2 <= hours(1) && td2 - td1 <= hours(1));
      }
  if (!btd2e && !btd3e)
      {
      check("td2 and td3 are close",
              td2 - td3 <= hours(2) && td3 - td2 <= hours(2));
      }
  if (!btd1e && !btd3e)
      {
      check("td1 and td3 are close",
              td1 - td3 <= hours(1) && td3 - td1 <= hours(1));
      }

  return printTestStats();
}