1 /* A simple example for using a custom_time_zone and a posix_time_zone.
2  */
3 
4 #include "boost/date_time/local_time/local_time.hpp"
5 #include <iostream>
6 
7 int
main()8 main()
9 {
10   using namespace boost;
11   using namespace local_time;
12   using namespace gregorian;
13   using posix_time::time_duration;
14 
15   /***** custom_time_zone *****/
16 
17   // create the dependent objects for a custom_time_zone
18   time_zone_names tzn("Eastern Standard Time", "EST",
19                       "Eastern Daylight Time", "EDT");
20   time_duration utc_offset(-5,0,0);
21   dst_adjustment_offsets adj_offsets(time_duration(1,0,0),
22                                      time_duration(2,0,0),
23                                      time_duration(2,0,0));
24   // rules for this zone are:
25   // start on first Sunday of April at 2 am
26   // end on last Sunday of October at 2 am
27   // so we use a first_last_dst_rule
28   first_day_of_the_week_in_month start_rule(Sunday, Apr);
29   last_day_of_the_week_in_month    end_rule(Sunday, Oct);
30   shared_ptr<dst_calc_rule> nyc_rules(new first_last_dst_rule(start_rule,
31                                                               end_rule));
32   // create more dependent objects for a non-dst custom_time_zone
33   time_zone_names tzn2("Mountain Standard Time", "MST",
34                        "", ""); // no dst means empty dst strings
35   time_duration utc_offset2(-7,0,0);
36   dst_adjustment_offsets adj_offsets2(time_duration(0,0,0),
37                                       time_duration(0,0,0),
38                                       time_duration(0,0,0));
39   // no dst means we need a null pointer to the rules
40   shared_ptr<dst_calc_rule> phx_rules;
41 
42   // create the custom_time_zones
43   time_zone_ptr nyc_1(new custom_time_zone(tzn, utc_offset, adj_offsets, nyc_rules));
44   time_zone_ptr phx_1(new custom_time_zone(tzn2, utc_offset2, adj_offsets2, phx_rules));
45 
46   /***** posix_time_zone *****/
47 
48   // create posix_time_zones that are the duplicates of the
49   // custom_time_zones created above. See posix_time_zone documentation
50   // for details on full zone names.
51   std::string nyc_string, phx_string;
52   nyc_string = "EST-05:00:00EDT+01:00:00,M4.1.0/02:00:00,M10.5.0/02:00:00";
53   // nyc_string = "EST-05EDT,M4.1.0,M10.5.0"; // shorter when defaults used
54   phx_string = "MST-07"; // no-dst
55   time_zone_ptr nyc_2(new posix_time_zone(nyc_string));
56   time_zone_ptr phx_2(new posix_time_zone(phx_string));
57 
58 
59   /***** show the sets are equal *****/
60 
61   std::cout << "The first zone is in daylight savings from:\n "
62     << nyc_1->dst_local_start_time(2004) << " through "
63     << nyc_1->dst_local_end_time(2004) << std::endl;
64 
65   std::cout << "The second zone is in daylight savings from:\n "
66     << nyc_2->dst_local_start_time(2004) << " through "
67     << nyc_2->dst_local_end_time(2004) << std::endl;
68 
69   std::cout << "The third zone (no daylight savings):\n "
70     << phx_1->std_zone_abbrev() << " and "
71     << phx_1->base_utc_offset() << std::endl;
72 
73   std::cout << "The fourth zone (no daylight savings):\n "
74     << phx_2->std_zone_abbrev() << " and "
75     << phx_2->base_utc_offset() << std::endl;
76 
77   return 0;
78 }
79 
80 /*  Copyright 2001-2005: CrystalClear Software, Inc
81  *  http://www.crystalclearsoftware.com
82  *
83  *  Subject to the Boost Software License, Version 1.0.
84  * (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
85  */
86 
87