如何将wstring中的Unix时间戳转换为char数组中的格式化日期

epi*_*any 5 c++ visual-c++-6

首先,请以C ++的新手身份与我联系

最终目标是将日期以DDMMYY格式(例如“ 120319”)存储在char具有6个字节的数组中。

首先,我有一个wstring检索Unix时间戳,例如“ 155xxxxxxx”。

std::wstring businessday = L"155xxxxxxx"
Run Code Online (Sandbox Code Playgroud)

然后,将其转换为wchar_t*

const wchar_t* wcs = businessday.c_str();
Run Code Online (Sandbox Code Playgroud)

之后,在声明10个字节的char数组之后,将转换wchar_t*为多字节字符串。

          char buffer[10];
          int ret;

          printf ("wchar_t string: %ls \n",wcs);

          ret = wcstombs ( buffer, wcs, sizeof(buffer) );
          if (ret==32) buffer[31]='\0';
          if (ret) printf ("multibyte string: %s \n",buffer);
Run Code Online (Sandbox Code Playgroud)

因此,现在char名为的数组buffer包含Unix时间戳格式的字符串,即“ 155xxxxxxx”。

如何char使用类似DDMMYY的日期格式(即“ 120319”)将其转换为6个字节的数组?

我正在使用标准版本的C ++(MS VC ++ 6)


回应user4581301的回答

long myLong = std::stol( buffer );
time_t timet = (time_t)myLong;

std::string tz = "TZ=Asia/Singapore";
putenv(tz.data());
std::put_time(std::localtime(&timet), "%c %Z") ;


struct tm * timeinfo = &timet;

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

strftime (buffer,80,"%d%m%Y",timeinfo);
Run Code Online (Sandbox Code Playgroud)