File: testtz_database.cpp

package info (click to toggle)
boost1.88 1.88.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 576,932 kB
  • sloc: cpp: 4,149,234; xml: 136,789; ansic: 35,092; python: 33,910; asm: 5,698; sh: 4,604; ada: 1,681; makefile: 1,633; pascal: 1,139; perl: 1,124; sql: 640; yacc: 478; ruby: 271; java: 77; lisp: 24; csh: 6
file content (116 lines) | stat: -rw-r--r-- 5,161 bytes parent folder | download | duplicates (12)
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
/* Copyright (c) 2003-2005 CrystalClear Software, Inc.
 * 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, Bart Garst
 * $Date$
 */


#include "../testfrmwk.hpp"
#include "boost/date_time/gregorian/gregorian.hpp"
#include "boost/date_time/posix_time/posix_time.hpp"
#include "boost/date_time/local_time/custom_time_zone.hpp"
#include "boost/date_time/local_time/local_time_types.hpp"
#include "boost/date_time/local_time/tz_database.hpp"
#include "boost/date_time/local_time/posix_time_zone.hpp"
#include <iostream>

bool run_bad_field_count_test(char const* fn);

int main(int /* argc */, char const* argv[]){
  using namespace boost::gregorian;
  using namespace boost::posix_time;
  using namespace boost::local_time;

  /* NOTE: The testlocal_time_facet tests required full names 
   * be added to some of the date_time_zonespec.csv entries. The 
   * tests here also use those full names. Those entries are:
   * Chicago, Denver, Los_Angeles, New_York, and Phoenix
   * have all had full names added */
 
  // run the exception tests first
  try{
    tz_database tz_db;
    tz_db.load_from_file("missing_file.csv"); // file does not exist
  }catch(data_not_accessible&){
    check("Caught Missing data file exception", true);
  }catch(...){
    check("Caught first unexpected exception", false);
  }
  check("Caught Bad field count exception", run_bad_field_count_test(argv[2]));

  tz_database tz_db;
  try {
    tz_db.load_from_file(argv[1]);
  }catch(...) {
    check("Cannot locate data file - aborting.", false);
    return printTestStats();
  }
    
  time_zone_ptr bad_tz = tz_db.time_zone_from_region("Invalid/name");
  check("Expected null pointer return", bad_tz == time_zone_ptr()); 

  time_zone_ptr nyc_test = tz_db.time_zone_from_region("America/New_York");
  check("nyc Valid pointer", nyc_test != time_zone_ptr() ); 
  check("nyc Abbreviations",nyc_test->std_zone_abbrev() == std::string("EST")); 
  check("nyc Full Name", nyc_test->std_zone_name() == std::string("Eastern Standard Time")); 
  check("nyc Abbreviations",nyc_test->dst_zone_abbrev() == std::string("EDT")); 
  //std::cout << nyc_test->std_zone_name() << std::endl;
  check("nyc Full Name", nyc_test->dst_zone_name() == std::string("Eastern Daylight Time")); 
  check("nyc GMT Offset", nyc_test->base_utc_offset() == hours(-5));
  check("nyc DST Offset", nyc_test->dst_offset() == hours(1));
  //std::cout << nyc_test->dst_local_start_time(2004) << std::endl;
  check("nyc dst start",  nyc_test->dst_local_start_time(2007) == ptime(date(2007, Mar, 11), hours(2)));
  check("nyc dst end", nyc_test->dst_local_end_time(2007) == ptime(date(2007, Nov, 4), hours(2)));
  check("nyc has dst", nyc_test->has_dst());
  
  time_zone_ptr phx_test = tz_db.time_zone_from_region("America/Phoenix");
  check("az Valid pointer", phx_test != time_zone_ptr() ); 
  check("az Abbreviations",phx_test->std_zone_abbrev() == std::string("MST"));  
  check("az Full Name", phx_test->std_zone_name() == std::string("Mountain Standard Time"));  
  check("az Abbreviations", phx_test->dst_zone_abbrev() == std::string(""));  
  check("az Full Name", phx_test->dst_zone_name() == std::string(""));  
  check("az GMT Offset", phx_test->base_utc_offset() == hours(-7));
  check("az DST Offset", phx_test->dst_offset() == hours(0));
  //std::cout << phx_test->dst_local_start_time(2004) << std::endl;
  check("az has dst", phx_test->has_dst() == false);

  //Now add and retrieve a Posix tz spec from the database
  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"));
  tz_db.add_record("United States/Eastern", eastern);
  time_zone_ptr eastern_test = tz_db.time_zone_from_region("United States/Eastern");
  check("eastern Valid pointer", eastern_test != time_zone_ptr() );
  check("eastern Abbreviations", 
      eastern_test->std_zone_abbrev() == std::string("EST"));  
  check("eastern Abbreviations", 
      eastern_test->std_zone_name() == std::string("EST"));  
  check("eastern Abbreviations", 
      eastern_test->dst_zone_abbrev() == std::string("EDT"));  
  check("eastern Abbreviations", 
      eastern_test->dst_zone_name() == std::string("EDT"));  
  check("eastern GMT Offset", eastern_test->base_utc_offset() == hours(-5));
  check("eastern dst start",  eastern_test->dst_local_start_time(2004) == ptime(date(2004, Apr, 4), hours(2)));
  check("eastern dst end", eastern_test->dst_local_end_time(2004) == ptime(date(2004, Oct, 31), hours(2)));
  check("eastern  has dst", eastern_test->has_dst() == true);


  return printTestStats();
}

/* This test only checks to make sure the bad_field_count exception
 * is properly thrown. It does not pay any attention to any other 
 * exception, those are tested elsewhere. */
bool run_bad_field_count_test(char const* fn)
{
  using namespace boost::local_time;
  bool caught_bfc = false;
  tz_database other_db;
  try{
    other_db.load_from_file(fn);
  }catch(bad_field_count&){
    caught_bfc = true;
  }catch(...) {
    // do nothing (file not found)
  }
  return caught_bfc;
}