I have the below and it gives me the desired output (ie; 1 day 12:05:33) but it looks overly complicated, is there a more straight forward way of doing this with the least amount of string concatenations?     std::string eta_string ( int64_t s ) {     time_t seconds ( s );     tm p;     gmtime_s ( &p , &seconds );     std::string str;     if ( p.tm_yday > 0 ) {      if ( p.tm_yday == 1 ) {        str = std::to_string ( p.tm_yday ) +=  day ;      } else {        str = std::to_string ( p.tm_yday ) +=  days ;      }      if ( p.tm_hour < 10 ) {        str = str.append ( 0 ) += std::to_string ( p.tm_hour ) += :;      } else {        str = str += std::to_string ( p.tm_hour ) += :;      }      if ( p.tm_min < 10 ) {        str = str.append ( 0 ) += std::to_string ( p.tm_min ) += :;      } else {        str = str += std::to_string ( p.tm_min ) += :;      }      if ( p.tm_sec < 10 ) {        str = str.append ( 0 ) += std::to_string ( p.tm_sec );      } else {        str = str += std::to_string ( p.tm_sec );      }     }     else if ( p.tm_hour > 0 ) {      if ( p.tm_hour < 10 ) {        str = str.append ( 0 ) += std::to_string ( p.tm_hour ) += :;       } else {        str = str += std::to_string ( p.tm_hour ) += :;       }       if ( p.tm_min < 10 ) {        str = str.append ( 0 ) += std::to_string ( p.tm_min ) += :;       } else {        str = str += std::to_string ( p.tm_min ) += :;       }       if ( p.tm_sec < 10 ) {        str = str.append ( 0 ) += std::to_string ( p.tm_sec );       } else {        str = str += std::to_string ( p.tm_sec );       }     } else if ( p.tm_min > 0 ) {      str = 00:;      if ( p.tm_min < 10 ) {        str = str.append ( 0 ) += std::to_string ( p.tm_min ) += :;      } else {        str = str += std::to_string ( p.tm_min ) += :;      }      if ( p.tm_sec < 10 ) {        str = str.append ( 0 ) += std::to_string ( p.tm_sec );      } else {        str = str += std::to_string ( p.tm_sec );      }     } else {      str = 00:00:;      if ( p.tm_sec < 10 ) {        str = str.append ( 0 ) += std::to_string ( p.tm_sec );      } else {        str = str += std::to_string ( p.tm_sec );      }    }    return str; }
Formatting seconds as days hours minutes seconds
c++;datetime;formatting
Try this. It uses a string-based stream to do formatted output. Let the stream put in leading zeroes. When you're done with your output, the str() method returns (a copy of) the embedded string.#include <time.h>#include <sstream>#include <iomanip>std::string eta_string ( int64_t s ){    time_t seconds ( s );    tm p;    gmtime_s ( &p, &seconds );    std::ostringstream os;    if ( p.tm_yday > 0 )        os << p.tm_yday << (p.tm_yday == 1 ?  day  :  days );    os << std::setfill('0') << std::setw(2) << p.tm_hour << ':' <<        std::setfill('0') << std::setw(2) << p.tm_min << ':' <<        std::setfill('0') << std::setw(2) << p.tm_sec;    return os.str();}