1

So I have a function that I have made to generate timestamp strings in the form:

"YYYY-MM-DD HH:MM::SS.mmm". However it uses parts of the older std::time_t and std::strftime(), std::localtime() etc...

std::localtime gives a warning that it may be unsafe and to use std::localtime_s, however MSVS does not seem to have that function (or does it!?)... but anyway, what I want to do is try to use only the newer chrono libs.

Here is what I have so far:

std::string get_time_stamp()
{
    char buf[50] = {0};

    // Get the current time
    auto now = std::chrono::system_clock::now();
    // Format the date/time
    std::time_t now_tm_t = std::chrono::system_clock::to_time(now);
    std::strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", std::localtime(&now_tm_t);
    // Get the milliseconds
    int millis = std::chrono::time_point_cast<std::chrono::milliseconds>(now).time_since_epoch().count() % 100;
    // Note use snprintf for gcc
    sprintf_s(buf + strlen(buf), sizeof(buf) - strlen(buf), ".%03d", millis);
    return buf;
}

Can I make this cleaner and using only c++11 chrono (and perhaps not a char array) and avoid functions like strftime and localtime etc...? I find that std::chrono is a bit awkward to use - the simple stuff is simple, but it gets a bit mind-boggling after that...

code_fodder
  • 15,263
  • 17
  • 90
  • 167
  • 4
    You could use a `stringstream` and [`put_time`](http://en.cppreference.com/w/cpp/io/manip/put_time) but you'd still have to convert to a `std::tm`. There are no stream operators (or conversion to string operators) for `std::chrono::time_point` unfortunately... You can look at HH date library: https://howardhinnant.github.io/date/date.html which extends upon `std::chrono`. – Holt May 03 '18 at 14:30
  • MSVC 's `CTime `and `CTimeSpan` are user friendly and involves less casting. – seccpur May 03 '18 at 14:33
  • 1
    The missing parts in chrono will be added in C++20. [Extending to Calendars and Time Zones](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0355r7.html) Doesn't help you much now though. :-( – Bo Persson May 03 '18 at 14:47
  • 3
    The C++20 parts can be experimented with today at https://github.com/HowardHinnant/date – Howard Hinnant May 03 '18 at 14:56
  • 2
    Fwiw, this function is a one-liner (except for using declarations) in C++20: `return format("%F %T", zoned_time{current_zone(), floor(system_clock::now())});` This does use one C++17 language feature which has a C++11-compatible workaround in the github lib: one can use `make_zoned` in place of `zoned_time` to work around the lack of template argument deduction for constructors. – Howard Hinnant May 03 '18 at 15:13
  • This answer may be useful: https://stackoverflow.com/questions/38034033/c-localtime-this-function-or-variable-may-be-unsafe/38034148#38034148 – Galik May 03 '18 at 15:16
  • @Holt yeah, that's quite good actually! - I shied away from stringstream because the formatting is horrendous - but that put_time() is a little gem! - so other then c++20, that's probably the answer I would go with as the best improvement - so feel free to add as an answer – code_fodder May 03 '18 at 15:51
  • @BoPersson ... bugger...lol, but good to know its coming : ) – code_fodder May 03 '18 at 15:52
  • @HowardHinnant ... that just makes it worse...lol ; ) – code_fodder May 03 '18 at 15:53
  • @code_fodder Why would this be worse? It's quite explicit on what you're trying to achieve and is probably much robust than the solution combining `strftime`/`put_time` which forces you to manually deals with everything below `seconds`. – Holt May 03 '18 at 16:40
  • @Holt: New API's are hard to learn. And when they break existing norms and patterns, they are doubly hard to learn. That's not to say that new API's shouldn't ever break norms. If they never did we would never advance as an industry. It's just that when one introduces a different way of doing things, push back comes with the territory. E.g. people hate `` until they get used to it, and then they love it. I personally hated `git`, until I got used to it, and now love it. :-) – Howard Hinnant May 03 '18 at 19:58
  • @Holt Haha, sorry, I meant that you showing me how nice and easy it is makes it worse that I don't yet have access to it yet! (my compiler not being c++20 and all) - can't wait for the updates. I like chrono, but as you say some parts of it take getting used to that is why I am asking this question... but it looks like I am a bit early, so I'll use the put_time for now.... – code_fodder May 03 '18 at 20:41

0 Answers0