// time_t_wrapper.cpp: implementation of the time_t_wrapper class. // ////////////////////////////////////////////////////////////////////// #include "stdafx.h" #include "time_t_wrapper.h" #include #include #include #include #include // we write it as a word, so that it can be easily ignored void time_t_to_stream( time_t val, std::ostream & out) { const char * months[] = { "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec" }; if ( val >= 0) { tm details = *localtime( &val); out << std::setw(4) << std::setfill('0') << details.tm_year + 1900 << '_' << std::setw(3) << std::setfill('0') << months[ details.tm_mon] << '_' << std::setw(2) << std::setfill('0') << details.tm_mday << "___" << std::setw(2) << std::setfill('0') << details.tm_hour << ':' << std::setw(2) << std::setfill('0') << details.tm_min << ':' << std::setw(2) << std::setfill('0') << details.tm_sec; } else { out << "invalid_value"; } } void read_time_t_from_stream( time_t & val, std::istream &in) { char c = 0; in >> c; if ( in) { if ( c == 'T') { // contains details as well (which we ignore) std::string ignore; in >> val >> ignore; } else { // no details in.putback( c); in >> val; } } } /* TEST #include int main(int argc, char* argv[]) { time_t_wrapper<> t = time(0); time_t_wrapper<>::write_details() = false; std::cout << t << std::endl; time_t_wrapper<>::write_details() = true; std::cout << t << std::endl; time_t_wrapper<> t2; t2 = time(0); { char ch = 0; time_t_wrapper<>::write_details() = true; std::ostringstream out; out << t << " c"; std::istringstream in( out.str()); time_t_wrapper< time_t_release> t3; in >> t3 >> ch; assert( ( t == t3) && ( ch == 'c')); } { char ch = 0; time_t_wrapper<>::write_details() = false; std::ostringstream out; out << t << " c"; std::istringstream in( out.str()); time_t_wrapper< time_t_release> t3; in >> t3 >> ch; assert( ( t == t3) && ( ch == 'c')); } if ( t2 > t) printf("Hello World!\n"); return 0; } */