Is there a standard date/time class in C++?

前端 未结 6 1222
悲哀的现实
悲哀的现实 2020-12-01 14:05

Does C++ stl have a standard time class? Or do I have to convert to c-string before writing to a stream. Example, I want to output the current date/time to a string stream:<

6条回答
  •  情话喂你
    2020-12-01 14:42

    OK. Here is closest I have found about directly writing time to a stream:

    time_t t(time(NULL));   // current time
    tm tm(*localtime(&t));  
    
    std::locale loc("");    // current user locale
    ostringstream sout;
    const std::time_put &tput =
        std::use_facet >(loc);
    tput.put(sout.rdbuf(), sout, _T('\0'), &tm, _T('x'));
    sout << ends;
    
    CString sTest(sout.str().c_str());
    

    A very helpful guide is the Apache C++ Standard Library Reference Guide http://stdcxx.apache.org/doc/stdlibref/time-put.html#sec13

提交回复
热议问题