4

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);
karol
  • 380
  • 1
  • 5
  • 16
epiphany
  • 756
  • 1
  • 11
  • 29
  • 1
    Why Visual C++ 6.0? That is ancient technology. That is not even C++03. It is _pre-standardisation_. Can you not upgrade to something from this millennium? – Lightness Races in Orbit Mar 18 '19 at 00:04
  • @JaMiT Not even C++03, and the phrase "an older version of C++" carries with it no useful information. – Lightness Races in Orbit Mar 18 '19 at 00:05
  • @LightnessRacesinOrbit I agree. The OP should have been more precise. (You are aware that those are not my words, right? I copied that phrase from one of the OP's comments to user4581301's answer.) More precision would have made it easier to pick an appropriate tag. – JaMiT Mar 18 '19 at 02:44
  • why should it work in VC6? None of the current OSes can even run that ancient IDE. Also note that VC6 will suffer from y38k problem – phuclv Mar 19 '19 at 16:09
  • @LightnessRacesinOrbit since you've made the same false claim twice now, I will call you out on it. The phrase is not useless; it *does* carry useful information. Not much useful information, but some. It carries the information that answers should not require C++17 features, and that is useful. Hence my edit did improve the question. But, as usual, SO does not approve of incremental improvements -- the unwritten rule of don't try to make things better if you might fail to make them perfect. I'll probably get banned at some point for repeated violations. – JaMiT Mar 22 '19 at 03:29
  • @JaMiT Thanks for your kind words. – Lightness Races in Orbit Mar 22 '19 at 11:28

2 Answers2

3

Easiest way I can think of is to

  1. parse the initial std::wstring into an integer of sufficient size with std::stol or std::wcstol
  2. cast the the integer into a time_t
  3. use std::localtime to convert the time_t into a tm structure
  4. and finally use std::strftime to format the tm struct into the DDMMYY string.

This would result in a 7 byte char array because strftime will apply a null terminator. If you really must have a 6 byte array, memcpy the first six characters in the 7 character array into a six character array.

JaMiT
  • 14,422
  • 4
  • 15
  • 31
user4581301
  • 33,082
  • 7
  • 33
  • 54
  • Also, I'm using an older version of c++, so std:stoi isn't supported – epiphany Mar 13 '19 at 03:38
  • is there an other way to change the wsting to integer – epiphany Mar 13 '19 at 06:57
  • @epiphany From [tag:c++]: _"Unless the question explicitly mentions which version of the C++ standard that is used, it is assumed that the current version is used."_ Please update your question and tags to indicate which version of C++ you are using. Otherwise answers will continue to assume C++17. – JaMiT Mar 16 '19 at 13:14
  • 1
    For older versions of c++: [`std::wcstol`](https://en.cppreference.com/w/cpp/string/wide/wcstol) – JaMiT Mar 16 '19 at 15:22
0

Tested in VS 6.0:

char output[7];
std::wstring input = L"1552869062"; // 2019-03-17 20:31:02 MST
time_t tmp_time = wcstol(input.c_str(), NULL, 10); // _wtoi() works too
strftime(output, sizeof(output), "%d%m%y", localtime(&tmp_time));

output will contain: "170319"
Chris Olsen
  • 3,243
  • 1
  • 27
  • 41
  • `_wtoi()`, just like `atoi()`, is broken in design and [shouldn't be used](https://stackoverflow.com/q/17710018/995714) – phuclv Mar 19 '19 at 16:11