TransWikia.com

How does one print an iso timestamp in a smart contract

EOS.IO Asked by chris de jong on August 20, 2021

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" );
print(charTime);

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?

One Answer

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));
print(buffer);

Answered by conr2d on August 20, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP