strptime() equivalent on Windows?

2019-01-01 01:44发布

问题:

Is there a good equivalent implementation of strptime() available for Windows? Unfortunately, this POSIX function does not appear to be available.

Open Group description of strptime - summary: it converts a text string such as \"MM-DD-YYYY HH:MM:SS\" into a tm struct, the opposite of strftime().

回答1:

An open-source version (BSD license) of strptime() can be found here: http://cvsweb.netbsd.org/bsdweb.cgi/src/lib/libc/time/strptime.c?rev=HEAD

You\'ll need to add the following declaration to use it:

char *strptime(const char * __restrict, const char * __restrict, struct tm * __restrict);


回答2:

If you don\'t want to port any code or condemn your project to boost, you can do this:

  1. parse the date using sscanf
  2. then copy the integers into a struct tm (subtract 1 from month and 1900 from year -- months are 0-11 and years start in 1900)
  3. finally, use mktime to get a UTC epoch integer

Just remember to set the isdst member of the struct tm to -1, or else you\'ll have daylight savings issues.



回答3:

Assuming you are using Visual Studio 2015 or above, you can use this as a drop-in replacement for strptime:

#include <time.h>
#include <iomanip>
#include <sstream>

extern \"C\" char* strptime(const char* s,
                          const char* f,
                          struct tm* tm) {
  // Isn\'t the C++ standard lib nice? std::get_time is defined such that its
  // format parameters are the exact same as strptime. Of course, we have to
  // create a string stream first, and imbue it with the current C locale, and
  // we also have to make sure we return the right things if it fails, or
  // if it succeeds, but this is still far simpler an implementation than any
  // of the versions in any of the C standard libraries.
  std::istringstream input(s);
  input.imbue(std::locale(setlocale(LC_ALL, nullptr)));
  input >> std::get_time(tm, f);
  if (input.fail()) {
    return nullptr;
  }
  return (char*)(s + input.tellg());
}

Just be aware that for cross platform applications, std::get_time wasn\'t implemented until GCC 5.1, so switching to calling std::get_time directly may not be an option.



回答4:

This does the job:

#include \"stdafx.h\"
#include \"boost/date_time/posix_time/posix_time.hpp\"
using namespace boost::posix_time;

int _tmain(int argc, _TCHAR* argv[])
{
    std::string ts(\"2002-01-20 23:59:59.000\");
    ptime t(time_from_string(ts));
    tm pt_tm = to_tm( t );

Notice, however, that the input string is YYYY-MM-DD



回答5:

One alternative is to use GetSystemTime and send the time information to a function that parses it according to your format using vsnprintf_s. In the example below there is one function that creates a time string with milli second precision. It then sends the string to a function that formats it according to the desired format:

#include <string>
#include <cstdio>
#include <cstdarg>
#include <atlstr.h> 

std::string FormatToISO8601 (const std::string FmtS, ...) {
   CStringA BufferString;
   try {
       va_list VaList;
       va_start (VaList, FmtS);
       BufferString.FormatV (FmtS.c_str(), VaList);
   } catch (...) {}
   return std::string (BufferString);
}

void CreateISO8601String () {
   SYSTEMTIME st;
   GetSystemTime(&st);
   std::string MyISO8601String = FormatToISO8601 (\"%4u-%02u-%02uT%02u:%02u:%02u.%03u\", st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds);
}