Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. #include <boost/date_time/posix_time/posix_time.hpp>
  2. #include <iostream>
  3.  
  4. int main( int argc, char** argv )
  5. {
  6. auto now = boost::posix_time::second_clock::local_time();
  7.  
  8. std::stringstream outStr;
  9. {
  10. boost::posix_time::time_facet* facet = new boost::posix_time::time_facet();
  11. facet->format("%Y-%m-%dT%H:%M:%S.%f");
  12. outStr.imbue(std::locale(std::locale::classic(), facet));
  13. outStr << now;
  14. }
  15. std::cout << outStr.str() << std::endl;
  16.  
  17. {
  18. static const std::string format = "%Y-%m-%dT%H:%M:%S.%f";
  19. const std::locale loc = std::locale(std::locale::classic(), new boost::posix_time::time_input_facet(format));
  20. std::istringstream is(outStr.str());
  21. is.imbue(loc);
  22. boost::posix_time::ptime converted;
  23. is >> converted;
  24. std::cout << converted << std::endl;
  25. }
  26.  
  27. {
  28. static const std::string format = "%Y-%m-%dT%H:%M:%S%f";
  29. const std::locale loc = std::locale(std::locale::classic(), new boost::posix_time::time_input_facet(format));
  30. std::istringstream is(outStr.str());
  31. is.imbue(loc);
  32. boost::posix_time::ptime converted;
  33. is >> converted;
  34. std::cout << converted << std::endl;
  35. }
  36.  
  37. return 0;
  38. }
  39.  
  40. 2019-04-30T12:23:29.000000
  41. not-a-date-time
  42. 2019-Apr-30 12:23:29
  43.  
  44. 2019-04-30T12:23:29.000000
  45. 2019-Apr-30 12:23:29
  46. not-a-date-time
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement