time_t 时间格式化字符串

#include <iostream>
#include <iomanip>      // std::put_time
#include <sstream>

using namespace std;
using namespace std::chrono;

std::string Timestamp::localtime()
{
    printf("----%s----%d---
", __FUNCTION__, __LINE__);
    std::ostringstream stream;
    auto now = system_clock::now();
    time_t tt = system_clock::to_time_t(now);
	
#if defined(WIN32) || defined(_WIN32)
    struct tm tm;
    localtime_s(&tm, &tt);
    stream << std::put_time(&tm, "%F %T");
#elif  defined(__linux) || defined(__linux__) 
    char buffer[200] = {0};
    std::string timeString;
    std::strftime(buffer, 200, "%F %T", std::localtime(&tt));
    stream << buffer;
#endif	
    return stream.str();
}
/* strftime example */
#include <stdio.h>      /* puts */
#include <time.h>       /* time_t, struct tm, time, localtime, strftime */

int main ()
{
  time_t rawtime;
  struct tm * timeinfo;
  char buffer [80];

  time (&rawtime);
  timeinfo = localtime (&rawtime);

  strftime (buffer,80,"Now it's %I:%M%p.",timeinfo);
  puts (buffer);

  return 0;
}

https://en.cppreference.com/w/cpp/chrono/c/strftime
https://www.cplusplus.com/reference/ctime/strftime/

原文地址:https://www.cnblogs.com/kuikuitage/p/14656120.html