local_date_time.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #include "boost/date_time/gregorian/gregorian.hpp"
  2. #include "boost/date_time/posix_time/posix_time.hpp"
  3. #include "boost/date_time/local_time/local_time.hpp"
  4. #include <iostream>
  5. #include <locale>
  6. int main() {
  7. using namespace boost::gregorian;
  8. using namespace boost::posix_time;
  9. using namespace boost::local_time;
  10. tz_database tz_db;
  11. try {
  12. tz_db.load_from_file("../../data/date_time_zonespec.csv");
  13. }catch(data_not_accessible dna) {
  14. std::cerr << "Error with time zone data file: " << dna.what() << std::endl;
  15. exit(EXIT_FAILURE);
  16. }catch(bad_field_count bfc) {
  17. std::cerr << "Error with time zone data file: " << bfc.what() << std::endl;
  18. exit(EXIT_FAILURE);
  19. }
  20. time_zone_ptr nyc = tz_db.time_zone_from_region("America/New_York");
  21. local_date_time ny_time(date(2004, Aug, 30), hours(10), nyc, true);
  22. typedef boost::date_time::time_facet<local_date_time, char> ldt_facet;
  23. ldt_facet* timefacet = new ldt_facet("%Y-%b-%d %H:%M:%S""%F %Z");
  24. std::locale loc(std::locale::classic(), timefacet);
  25. std::cout << ny_time << std::endl;
  26. // 2004-Aug-30 10:00:00 EDT
  27. std::cout.imbue(loc);
  28. std::cout << ny_time << std::endl;
  29. // 2004-Aug-30 10:00:00 Eastern Daylight Time
  30. return 0;
  31. }
  32. /* Copyright 2004-2005: CrystalClear Software, Inc
  33. * http://www.crystalclearsoftware.com
  34. *
  35. * Subject to the Boost Software License, Version 1.0.
  36. * (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
  37. */