I'm trying to print an ISO timestamp with second accuracy.

#include <chrono>
#include <ctime>    
time_t current_time = current_time_point().sec_since_epoch() * 1000;
char* charTime = std::put_time( std::localtime(&current_time), "%FT%T%z" );

I've tried several combinations. I keep getting complaints about the namespace:

error: no member named 'localtime' in namespace 'std'

What is the correct way of doing this using cdt 1.6.1?


eosio.cdt doesn't support time library in its libc, so you cannot use time related functions. eoscc, a customized version of eosio.cdt, supports it. (I'm a writer of that)

You can check it from here.

  • Use std::strftime instead of std::put_time. put_time needs stream.
  • std::time_t accepts seconds, so do not multiply time_point::sec_since_epoch() with 1000.
  • Use std::gmtime instead of std::localtime. localtime requires timezone.
  • In contract runtime, timezone info is not available, so you had better use the format "%FT%T" (or "%FT%TZ" for indicating UTC).
char buffer[32];
time_t current_time = current_time_point().sec_since_epoch();
std::strftime(buffer, sizeof(buffer), "%FT%TZ", std::gmtime(&current_time));

Your Answer

