For starter, please bear with me as a newbie in C++
The end goal is to store the date in the format of DDMMYY, e.g. "120319", in a char
array with 6 bytes.
For started, I have got a wstring
that retrieves a Unix timestamp e.g. "155xxxxxxx".
std::wstring businessday = L"155xxxxxxx"
Then, I convert it to wchar_t*
.
const wchar_t* wcs = businessday.c_str();
Afterwards, after declaring a char array of 10 bytes, I convert the wchar_t*
to a multibyte string.
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);
So now the char
array named buffer
contains the Unix timestamp formatted string i.e. "155xxxxxxx".
How can I convert it to a char
array of 6 bytes using a date format like DDMMYY, i.e. "120319"?
I'm using a pre-standard version of c++ (MS VC++ 6)
In response to user4581301's answer:
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);