1 /* Copyright (c) 2003-2005 CrystalClear Software, Inc.
2  * Subject to the Boost Software License, Version 1.0.
3  * (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
4  * Author: Jeff Garland, Bart Garst
5  * $Date$
6  */
7 
8 
9 #include "../testfrmwk.hpp"
10 #include "boost/date_time/gregorian/gregorian.hpp"
11 #include "boost/date_time/posix_time/posix_time.hpp"
12 #include "boost/date_time/local_time/custom_time_zone.hpp"
13 #include "boost/date_time/local_time/local_time_types.hpp"
14 #include "boost/date_time/local_time/tz_database.hpp"
15 #include "boost/date_time/local_time/posix_time_zone.hpp"
16 #include <iostream>
17 
18 bool run_bad_field_count_test();
19 
main()20 int main(){
21   using namespace boost::gregorian;
22   using namespace boost::posix_time;
23   using namespace boost::local_time;
24 
25   /* NOTE: The testlocal_time_facet tests required full names
26    * be added to some of the date_time_zonespec.csv entries. The
27    * tests here also use those full names. Those entries are:
28    * Chicago, Denver, Los_Angeles, New_York, and Phoenix
29    * have all had full names added */
30 
31   // run the exception tests first
32   try{
33     tz_database tz_db;
34     tz_db.load_from_file("missing_file.csv"); // file does not exist
35   }catch(data_not_accessible& e){
36     check("Caught Missing data file exception", true);
37   }catch(...){
38     check("Caught first unexpected exception", false);
39   }
40   check("Caught Bad field count exception", run_bad_field_count_test());
41 
42   /* This test-file is usually run from either $BOOST_ROOT/status, or
43    * $BOOST_ROOT/libs/date_time/test. Therefore, the relative path
44    * to the data file this test depends on will be one of two
45    * possible paths.
46    *
47    * If the first attempt at opening the data file fails, an exception
48    * will be thrown. The handling of that exception consists of
49    * attempting to open it again but from a different location. If that
50    * also fails, we abort the test. */
51   tz_database tz_db;
52   try {
53     // first try to find the data file from the test dir
54     tz_db.load_from_file("../data/date_time_zonespec.csv");
55   }catch(data_not_accessible& e) {
56     // couldn't find the data file so assume we are being run from
57     // boost_root/status and try again
58     tz_db.load_from_file("../libs/date_time/data/date_time_zonespec.csv");
59   }catch(...) {
60     check("Cannot locate data file - aborting.", false);
61     return printTestStats();
62   }
63 
64   time_zone_ptr bad_tz = tz_db.time_zone_from_region("Invalid/name");
65   check("Expected null pointer return", bad_tz == time_zone_ptr());
66 
67   time_zone_ptr nyc_test = tz_db.time_zone_from_region("America/New_York");
68   check("nyc Valid pointer", nyc_test != time_zone_ptr() );
69   check("nyc Abbreviations",nyc_test->std_zone_abbrev() == std::string("EST"));
70   check("nyc Full Name", nyc_test->std_zone_name() == std::string("Eastern Standard Time"));
71   check("nyc Abbreviations",nyc_test->dst_zone_abbrev() == std::string("EDT"));
72   //std::cout << nyc_test->std_zone_name() << std::endl;
73   check("nyc Full Name", nyc_test->dst_zone_name() == std::string("Eastern Daylight Time"));
74   check("nyc GMT Offset", nyc_test->base_utc_offset() == hours(-5));
75   check("nyc DST Offset", nyc_test->dst_offset() == hours(1));
76   //std::cout << nyc_test->dst_local_start_time(2004) << std::endl;
77   check("nyc dst start",  nyc_test->dst_local_start_time(2007) == ptime(date(2007, Mar, 11), hours(2)));
78   check("nyc dst end", nyc_test->dst_local_end_time(2007) == ptime(date(2007, Nov, 4), hours(2)));
79   check("nyc has dst", nyc_test->has_dst());
80 
81   time_zone_ptr phx_test = tz_db.time_zone_from_region("America/Phoenix");
82   check("az Valid pointer", phx_test != time_zone_ptr() );
83   check("az Abbreviations",phx_test->std_zone_abbrev() == std::string("MST"));
84   check("az Full Name", phx_test->std_zone_name() == std::string("Mountain Standard Time"));
85   check("az Abbreviations", phx_test->dst_zone_abbrev() == std::string(""));
86   check("az Full Name", phx_test->dst_zone_name() == std::string(""));
87   check("az GMT Offset", phx_test->base_utc_offset() == hours(-7));
88   check("az DST Offset", phx_test->dst_offset() == hours(0));
89   //std::cout << phx_test->dst_local_start_time(2004) << std::endl;
90   check("az has dst", phx_test->has_dst() == false);
91 
92   //Now add and retrieve a Posix tz spec from the database
93   time_zone_ptr eastern(new posix_time_zone("EST-05:00:00EDT+01:00:00,M4.1.0/02:00:00,M10.5.0/02:00:00"));
94   tz_db.add_record("United States/Eastern", eastern);
95   time_zone_ptr eastern_test = tz_db.time_zone_from_region("United States/Eastern");
96   check("eastern Valid pointer", eastern_test != time_zone_ptr() );
97   check("eastern Abbreviations",
98       eastern_test->std_zone_abbrev() == std::string("EST"));
99   check("eastern Abbreviations",
100       eastern_test->std_zone_name() == std::string("EST"));
101   check("eastern Abbreviations",
102       eastern_test->dst_zone_abbrev() == std::string("EDT"));
103   check("eastern Abbreviations",
104       eastern_test->dst_zone_name() == std::string("EDT"));
105   check("eastern GMT Offset", eastern_test->base_utc_offset() == hours(-5));
106   check("eastern dst start",  eastern_test->dst_local_start_time(2004) == ptime(date(2004, Apr, 4), hours(2)));
107   check("eastern dst end", eastern_test->dst_local_end_time(2004) == ptime(date(2004, Oct, 31), hours(2)));
108   check("eastern  has dst", eastern_test->has_dst() == true);
109 
110 
111   return printTestStats();
112 }
113 
114 /* This test only checks to make sure the bad_field_count exception
115  * is properly thrown. It does not pay any attention to any other
116  * exception, those are tested elsewhere. */
run_bad_field_count_test()117 bool run_bad_field_count_test()
118 {
119   using namespace boost::local_time;
120   bool caught_bfc = false;
121   tz_database other_db;
122   try{
123     other_db.load_from_file("local_time/poorly_formed_zonespec.csv");
124   }catch(bad_field_count& be){
125     caught_bfc = true;
126   }catch(...) {
127     // do nothing (file not found)
128   }
129   try{
130     other_db.load_from_file("../libs/date_time/test/local_time/poorly_formed_zonespec.csv");
131   }catch(bad_field_count& be){
132     caught_bfc = true;
133   }catch(...) {
134     // do nothing (file not found)
135   }
136   return caught_bfc;
137 }
138 
139