flight.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #include "boost/date_time/local_time/local_time.hpp"
  2. #include <iostream>
  3. /* This example shows a program that calculates the arrival time of a plane
  4. * that flys from Phoenix to New York. During the flight New York shifts
  5. * into daylight savings time (Phoenix doesn't because Arizona doesn't use
  6. * DST).
  7. *
  8. *
  9. */
  10. int main()
  11. {
  12. using namespace boost::gregorian;
  13. using namespace boost::local_time;
  14. using namespace boost::posix_time;
  15. //setup some timezones for creating and adjusting local times
  16. //This user editable file can be found in libs/date_time/data.
  17. tz_database tz_db;
  18. try {
  19. tz_db.load_from_file("../../data/date_time_zonespec.csv");
  20. }catch(data_not_accessible dna) {
  21. std::cerr << "Error with time zone data file: " << dna.what() << std::endl;
  22. exit(EXIT_FAILURE);
  23. }catch(bad_field_count bfc) {
  24. std::cerr << "Error with time zone data file: " << bfc.what() << std::endl;
  25. exit(EXIT_FAILURE);
  26. }
  27. time_zone_ptr nyc_tz = tz_db.time_zone_from_region("America/New_York");
  28. //Use a newly created time zone rule
  29. time_zone_ptr phx_tz(new posix_time_zone("MST-07:00:00"));
  30. //local departure time in Phoenix is 11 pm on March 13 2010
  31. // (NY changes to DST on March 14 at 2 am)
  32. local_date_time phx_departure(date(2010, Mar, 13), hours(23),
  33. phx_tz,
  34. local_date_time::NOT_DATE_TIME_ON_ERROR);
  35. local_date_time nyc_departure = phx_departure.local_time_in(nyc_tz);
  36. time_duration flight_length = hours(4) + minutes(30);
  37. local_date_time phx_arrival = phx_departure + flight_length;
  38. local_date_time nyc_arrival = phx_arrival.local_time_in(nyc_tz);
  39. std::cout << "departure PHX time: " << phx_departure << std::endl;
  40. std::cout << "departure NYC time: " << nyc_departure << std::endl;
  41. std::cout << "arrival PHX time: " << phx_arrival << std::endl;
  42. std::cout << "arrival NYC time: " << nyc_arrival << std::endl;
  43. }
  44. /* Copyright 2005: CrystalClear Software, Inc
  45. * http://www.crystalclearsoftware.com
  46. *
  47. * Subject to the Boost Software License, Version 1.0.
  48. * (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
  49. */