time_string.hpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. //
  9. // ------------------------------------------------------------------
  10. //
  11. // $Id$
  12. #ifndef BOOST_TIME_STRING_HPP_GP_20060731
  13. #define BOOST_TIME_STRING_HPP_GP_20060731
  14. #include <string>
  15. #include <ctime>
  16. namespace boost {
  17. // Many of the boost tools just need a quick way to obtain
  18. // a formatted "run date" string or similar. This is one.
  19. //
  20. // In case of failure false is returned and result is
  21. // unchanged.
  22. //
  23. inline
  24. bool time_string(std::string & result
  25. , const std::string & format = "%X UTC, %A %d %B %Y")
  26. {
  27. // give up qualifying names and using std::size_t,
  28. // to avoid including "config.hpp"
  29. using namespace std;
  30. const int sz = 256;
  31. char buffer [ sz ] = { 0 };
  32. const time_t no_cal_time ( -1 );
  33. time_t tod;
  34. const bool ok =
  35. time ( &tod ) != no_cal_time
  36. && strftime( buffer, sz, format.c_str(), gmtime( &tod ) ) != 0
  37. ;
  38. if (ok)
  39. result = buffer;
  40. return ok;
  41. }
  42. }
  43. #endif // include guard
粤ICP备19079148号