c++ - Invalid null pointer error when converting std::chrono::system_clock::time_point::min() to string -
i following example in nicolai m. josuttis' "the c++ standard library (second edition)", page 152-153, details example print epoch, current time, minimum , maximum times of std::chrono::system_clock
introduced in c++11.
i using microsoft visual studio 2012, , assertion triggered in <xstring>
, due invalid null pointer. occurs on line std::string ts = std::ctime( &t )
in code below after setting tp = std::chrono::system_clock::time_point::min();
#include <chrono> #include <ctime> #include <string> #include <iostream> std::string asstring( const std::chrono::system_clock::time_point& tp ) { std::time_t t = std::chrono::system_clock::to_time_t( tp ); std::string ts = std::ctime( &t ); ts.resize( ts.size()-1 ); return ts; } int main() { std::chrono::system_clock::time_point tp; std::cout << "epoch: " << asstring(tp) << std::endl; tp = std::chrono::system_clock::now(); std::cout << "now: " << asstring(tp) << std::endl; tp = std::chrono::system_clock::time_point::min(); std::cout << "min: " << asstring(tp) << std::endl; tp = std::chrono::system_clock::time_point::max(); std::cout << "max: " << asstring(tp) << std::endl; return 0; }
is due implementation error dinkumware in <chrono>
library, or typo/mistake in book? have gone on code given in book again , again see if have copied out incorrectly, not appear case. i'd grateful insights given.
it looks std::ctime
returns null, indicates incorrect t
value. because call asstring
uses value of time_point
cannot represented in time_t
type.
Comments
Post a Comment