Advertisement
jpcordovae

timestamp sql

Nov 14th, 2012
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.19 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime>
  3.  
  4. #include <boost/date_time.hpp>
  5.  
  6. std::string timestamp_sql()
  7. {
  8.     // Get current time, as an example
  9.     boost::posix_time::ptime dt = boost::posix_time::microsec_clock::universal_time();
  10.  
  11.     // Create a time_zone_ptr for the desired time zone and use it to create a local_date_time
  12.     boost::local_time::time_zone_ptr zone(new boost::local_time::posix_time_zone("GMT-3"));
  13.     boost::local_time::local_date_time dt_with_zone(dt, zone);
  14.     std::stringstream strm;
  15.  
  16.     // Set the formatting facet on the stringstream and print the local_date_time to it.
  17.     // Ownership of the boost::local_time::local_time_facet object goes to the created std::locale object.
  18.     strm.imbue(std::locale(std::cout.getloc(), new boost::local_time::local_time_facet("%Y-%m-%d %H:%M:%S UTC%Q")));
  19.     strm << dt_with_zone;
  20.  
  21.     // Print the stream's content to the console
  22.     //cout << strm.str() << endl;
  23.     return strm.str();
  24. }
  25.  
  26. int main(int argc, char ** argv) {
  27.     using namespace std;
  28.     std::cout << timestamp_sql() << std::endl;;
  29.     return 0;
  30. }
  31.    
  32. //new boost::local_time::posix_time_zone("EST")
  33.    
  34. //new boost::local_time::posix_time_zone("EST-05:00:00")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement