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