0

I find it hard to believe this isn't on the website, yet I can't seem to find it.

I would like a function that takes in a format and a number, and returns a string of the date, i.e.

std::string date = DateFromSecondsSinceEpoch("yyyy-mm-dd hh:mm:ss", 1000000000); //2001-09-09 01:46:40

//or
std::string date = DateFromSecondsSinceEpoch("yyyy-mm-dd", 1000000000); //2001-09-09

With a function signature such as:

std::string DateFromSecondsSinceEpoch(const char* format, const long long int seconds);
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 2
    Have a look at things like [`std::chrono::system_clock::from_time_t()`](https://en.cppreference.com/w/cpp/chrono/system_clock/from_time_t), [`std::put_time()`](https://en.cppreference.com/w/cpp/io/manip/put_time), [`std::format()`](https://en.cppreference.com/w/cpp/utility/format/format), etc – Remy Lebeau Jun 22 '21 at 06:02
  • duplicate: [Math to convert seconds since 1970 into date and vice versa](https://stackoverflow.com/a/32158604/995714) – phuclv Jun 22 '21 at 06:35
  • For anyone who sees this post in the future, I didn't exactly find what I wanted in those links, but this here works very well, and answers my question. `std::string DateFromSecondsSinceEpoch(const long long int seconds){ struct tm ts; char buf[11]; localtime_s(&ts, &seconds); strftime(buf, sizeof(buf), "%Y-%m-%d", &ts); return buf;} ` – asjhdbashjdbasjhdbhjb Jun 22 '21 at 07:01

0 Answers0