File: ex_simple_time_zone.xml

package info (click to toggle)
boost1.35 1.35.0-5
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 203,856 kB
  • ctags: 337,867
  • sloc: cpp: 938,683; xml: 56,847; ansic: 41,589; python: 18,999; sh: 11,566; makefile: 664; perl: 494; yacc: 456; asm: 353; csh: 6
file content (100 lines) | stat: -rw-r--r-- 3,909 bytes parent folder | download | duplicates (18)
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
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE library PUBLIC "-//Boost//DTD BoostBook XML V1.0//EN" 
"../../../tools/boostbook/dtd/boostbook.dtd">

<!-- Copyright (c) 2001-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)
-->

<section id="date_time.examples.simple_time_zone">
  <title>Simple Time Zones</title>

  <para> 
    Example usage of custom_time_zone as well as posix_time_zone.
  </para>
  <programlisting>
    <![CDATA[
  /* A simple example for using a custom_time_zone and a posix_time_zone.
   */

  #include "boost/date_time/local_time/local_time.hpp"
  #include <iostream>

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

    /***** custom_time_zone *****/
    
    // create the dependent objects for a custom_time_zone
    time_zone_names tzn("Eastern Standard Time", "EST",
                        "Eastern Daylight Time", "EDT");
    time_duration utc_offset(-5,0,0);
    dst_adjustment_offsets adj_offsets(time_duration(1,0,0), 
                                       time_duration(2,0,0), 
                                       time_duration(2,0,0));
    // rules for this zone are:
    // start on first Sunday of April at 2 am
    // end on last Sunday of October at 2 am
    // so we use a first_last_dst_rule
    first_day_of_the_week_in_month start_rule(Sunday, Apr);
    last_day_of_the_week_in_month    end_rule(Sunday, Oct);
    shared_ptr<dst_calc_rule> nyc_rules(new first_last_dst_rule(start_rule, 
                                                                end_rule));
    // create more dependent objects for a non-dst custom_time_zone
    time_zone_names tzn2("Mountain Standard Time", "MST",
                         "", ""); // no dst means empty dst strings
    time_duration utc_offset2(-7,0,0);
    dst_adjustment_offsets adj_offsets2(time_duration(0,0,0), 
                                        time_duration(0,0,0), 
                                        time_duration(0,0,0));
    // no dst means we need a null pointer to the rules
    shared_ptr<dst_calc_rule> phx_rules;

    // create the custom_time_zones
    time_zone_ptr nyc_1(new custom_time_zone(tzn, utc_offset, 
                                             adj_offsets, nyc_rules));
    time_zone_ptr phx_1(new custom_time_zone(tzn2, utc_offset2, 
                                             adj_offsets2, phx_rules));

    /***** posix_time_zone *****/

    // create posix_time_zones that are the duplicates of the 
    // custom_time_zones created above. See posix_time_zone documentation 
    // for details on full zone names.
    std::string nyc_string, phx_string;
    nyc_string = "EST-05:00:00EDT+01:00:00,M4.1.0/02:00:00,M10.5.0/02:00:00";
    // nyc_string = "EST-05EDT,M4.1.0,M10.5.0"; // shorter when defaults used
    phx_string = "MST-07"; // no-dst
    time_zone_ptr nyc_2(new posix_time_zone(nyc_string));
    time_zone_ptr phx_2(new posix_time_zone(phx_string));
   

    /***** show the sets are equal *****/

    std::cout << "The first zone is in daylight savings from:\n " 
      << nyc_1->dst_local_start_time(2004) << " through "
      << nyc_1->dst_local_end_time(2004) << std::endl;

    std::cout << "The second zone is in daylight savings from:\n " 
      << nyc_2->dst_local_start_time(2004) << " through "
      << nyc_2->dst_local_end_time(2004) << std::endl;

    std::cout << "The third zone (no daylight savings):\n " 
      << phx_1->std_zone_abbrev() << " and "
      << phx_1->base_utc_offset() << std::endl;

    std::cout << "The fourth zone (no daylight savings):\n " 
      << phx_2->std_zone_abbrev() << " and "
      << phx_2->base_utc_offset() << std::endl;

    return 0;
  }
    ]]>
  </programlisting>
</section>