simple_time_zone.cpp 3.2 KB

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