mardi 4 juillet 2017

convert ANSI C's asctime() format using std::get_time

I want to convert string in ANSI C' format to std::tm object.

Example

Sun Nov 6 08:49:37 1994 ; ANSI C's asctime() format

More info here here.


My code:

#include <string>
#include <ctime>
#include <iomanip>
#include <locale>
bool parseAscTimeDate(std::tm& tm, const std::string& str) {
    std::istringstream ss(str);
    //ss.imbue(std::locale("en_US.UTF8")); //try timezone?
    ss >> std::get_time(&tm, "%a %b  %d %H:%M:%S %Y");
    return !ss.fail();
}

So why this fail?

   /* ANSI C's asctime format */
   std::tm tm;
   if (parseAscTimeDate(tm, "Sun Nov  6 08:49:37 1994"))
       std::cout << "Ok!" << std::endl;
   else
       std::cout << "Wrong!" << std::endl;


additional info

  1. I can't use strptime because there is a bug in musl libc.
  2. I have tried to set some timezone but:

It need not set tzname, timezone, and daylight.

From here

  1. std::get_time example that also fails?

Aucun commentaire:

Enregistrer un commentaire