ex_simple_time_zone.xml 3.8 KB

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