time_string.hpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // ---- time_string: thin wrapper around std::strftime -------- //
  2. //
  3. // Copyright Gennaro Prota 2006
  4. //
  5. // Distributed under the Boost Software License, Version 1.0.
  6. // (See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. #ifndef BOOST_TIME_STRING_HPP_GP_20060731
  9. #define BOOST_TIME_STRING_HPP_GP_20060731
  10. #include <string>
  11. #include <ctime>
  12. namespace boost {
  13. // Many of the boost tools just need a quick way to obtain
  14. // a formatted "run date" string or similar. This is one.
  15. //
  16. // In case of failure false is returned and result is
  17. // unchanged.
  18. //
  19. inline
  20. bool time_string(std::string & result
  21. , const std::string & format = "%X UTC, %A %d %B %Y")
  22. {
  23. // give up qualifying names and using std::size_t,
  24. // to avoid including "config.hpp"
  25. using namespace std;
  26. const int sz = 256;
  27. char buffer [ sz ] = { 0 };
  28. const time_t no_cal_time ( -1 );
  29. time_t tod;
  30. const bool ok =
  31. time ( &tod ) != no_cal_time
  32. && strftime( buffer, sz, format.c_str(), gmtime( &tod ) ) != 0
  33. ;
  34. if (ok)
  35. result = buffer;
  36. return ok;
  37. }
  38. }
  39. #endif // include guard
粤ICP备19079148号