的问题是,stream >> st.wMonth
失败,因为它试图读取该字符串值May
成数字WORD
,这是不支持转换。您没有重置stream
错误状态,因此>>
和ignore()
的后续使用也会失败,因此在wYear
之后SYSTEMTIME
不会填写。
如果你有一个C++编译器11,可以使用std::get_time()
解析timeString
:
#include <sstream>
#include <iomanip>
bool StringToFileTime(LPCWSTR timeString, FILETIME &ft)
{
SYSTEMTIME st = {0};
std::wistringstream stream(timeString);
std::tm tmb;
// std::get_time() does not support reading milliseconds...
if (!(stream >> std::get_time(&tmb, L"%Y-%b-%d %H:%M:%S")))
return false;
st.wYear = 1900 + tmb.tm_year;
st.wMonth = tmb.tm_mon + 1;
st.wDayOfWeek = tmb.tm_wday;
st.wDay = tmb.tm_mday;
st.wHour = tmb.tm_hour;
st.wMinute = tmb.tm_min;
st.wSecond = tmb.tm_sec;
if (!stream.eof())
{
stream.ignore(1, L'.');
// st.wMilliseconds is a 16bit WORD, so it can only go up to 65535.
// There are only 1000 ms in a second, so 132814 is clearly not
// expressed in milliseconds. Is it nanoseconds? 100-nanoseconds?
// Whatever it is, convert it to st.wMilliseconds as needed...
int iValue;
if (!(stream >> iValue))
return false;
st.wMilliseconds = ...;
}
return SystemTimeToFileTime(&st, &ft);
}
如果你没有一个C++编译器11,你仍然可以使用std::wistringstream
,你只要必须手动提取值:
#include <sstream>
bool StringToFileTime(LPCWSTR timeString, FILETIME &ft)
{
SYSTEMTIME st = {0};
std::wistringstream stream(timeString);
std::string sMonth;
if (!(stream >> st.wYear)) return false;
if (!stream.ignore(1, L'-')) return false;
// st.wMonth is a 16bit WORD, but a month name is given instead.
// parse it into st.wMonth as needed...
if (!std::getline(stream, sMonth, L'-')) return false;
st.wMonth = ...;
if (!stream.ignore(1, L'-')) return false;
if (!(stream >> st.wDay)) return false;
if (!stream.ignore(1, L' ')) return false;
if (!(stream >> st.wHour)) return false;
if (!stream.ignore(1, L':')) return false;
if (!(stream >> st.wMinute)) return false;
if (!stream.ignore(1, L':')) return false;
if (!(stream >> st.wSecond)) return false;
if (!stream.eof())
{
stream.ignore(1, L'.');
// convert to st.wMilliseconds as needed...
int iValue;
if (!(stream >> iValue))
return false;
st.wMilliseconds = ...;
}
return SystemTimeToFileTime(&st, &ft);
}
或者,你可以使用swscanf()
代替std::wistringstream
:
bool StringToFileTime(LPCWSTR timeString, FILETIME &ft)
{
SYSTEMTIME st = {0};
WCHAR szMonth[12];
int iValue = 0;
int numRead = swscanf(timeString, L"%hu-%[^-]-%hu &hu:&hu:&hu.%d", &st.wYear, szMonth, &st.wDay, &st.wHour, &st.wMinute, st.wSecond, &iValue);
if (numRead < 6) return false;
// parse szMonth into st.wMonth as needed...
if (numRead == 7)
{
// convert iValue into st.wMilliseconds as needed...
}
return SystemTimeToFileTime(&st, &ft);
}
尝试一些调试。堆栈溢出不存在为个人执行基本的调试任务。 –
它是一个合法的编程问题。我如何x?这是我所做的尝试。如果它那么基本的大卫,你为什么不回答这个问题? –
你为什么不学习如何调试?你不想这样做吗? –