开发者

converting string to filetime

开发者 https://www.devze.com 2023-02-10 17:54 出处:网络
What is the fastest way to convert a string of the form \"\"1997-01-08 03:04:01:463\" to filetime? Is there a function which does th开发者_开发技巧at?I am guessing that you are talking about a Windows

What is the fastest way to convert a string of the form ""1997-01-08 03:04:01:463" to filetime? Is there a function which does th开发者_开发技巧at?


I am guessing that you are talking about a Windows FILETIME, which contains the number of 100 nanosec ticks since 1/1/1600.

  1. Use sscanf() or a std::istringstream to parse the string into its components. and populate a SYSTEMTIME struct
  2. Use SystemTimeToFileTime() to convert to a FILETIME

e.g.

FILETIME DecodeTime(const std::string &sTime)
{
    std::istringstream istr(sTime);
    SYSTEMTIME st = { 0 };
    FILETIME ft = { 0 };

    istr >> st.wYear;
    istr.ignore(1, '-');
    istr >> st.wMonth;
    istr.ignore(1, '-');
    istr >> st.wDay;
    istr.ignore(1, ' ');
    istr >> st.wHour;
    istr.ignore(1, ':');
    istr >> st.wMinute;
    istr.ignore(1, ':');
    istr >> st.wSecond;
    istr.ignore(1, '.');
    istr >> st.wMilliseconds;

    // Do validation that istr has no errors and all fields 
    // are in sensible ranges
    // ...

    ::SystemTimeToFileTime(&st, &ft);
    return ft;
}

int main(int argc, char* argv[])
{
    FILETIME ft = DecodeTime("1997-01-08 03:04:01.463");
    return 0;
}


Since you mentioned filetimes i supposed you refer to Windows since *nix doesn't differentiate between file times and system times like Windows does(FILETIME vs. SYSTEMTIME). Unfortunately in either cases you are out of luck since there are no shortcuts to converting such a string to either a FILETIME structure in Windows or time_t in *nix using either system or standard C/C++ library calls.

In order to get lucky, you will most likely have to use a wrapper library, for eg. the Boost library provides such functionality.


Assuming you are on windows, the string looks like SYSTEMTIME and there is a routine called SystemTimeToFileTime http://msdn.microsoft.com/en-us/library/ms724948.aspx that converts it to a FILETIME. Of course you still have to handle tokenization and integer parsing yourself.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号