{"id":6183,"date":"2012-11-02T07:00:00","date_gmt":"2012-11-02T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2012\/11\/02\/how-do-i-parse-a-string-into-a-filetime\/"},"modified":"2012-11-02T07:00:00","modified_gmt":"2012-11-02T07:00:00","slug":"how-do-i-parse-a-string-into-a-filetime","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20121102-00\/?p=6183","title":{"rendered":"How do I parse a string into a FILETIME?"},"content":{"rendered":"<p><p>\nPublic Service Announcement:\nDaylight Saving Time ends in most parts of the United States this weekend.\n<a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2010\/11\/05\/10086404.aspx#10086843\">\nOther parts of the world may change on a different day from the\nUnited States<\/a>.\n<\/p>\n<p>\nThe NLS functions in Win32 provide\n<a HREF=\"http:\/\/msdn.microsoft.com\/en-us\/library\/dd319114(v=VS.85).aspx\">\nfunctions to convert a\n<code>SYSTEMTIME<\/code> into a string<\/a>,\nbut it does not provide any functions to perform the reverse\nconversion.\nHere are few things you can try:\n<\/p>\n<p>\nThe OLE automation\n<a HREF=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms221395.aspx\">\n<code>VarDateFromStr<\/code><\/a> conversion function converts a string into\na <code>DATE<\/code>.\nFrom there, you can convert it to some other format.<\/p>\n<p><pre>\nBOOL SystemTimeFromStr(__in LPCWSTR psz, LCID lcid, __out LPSYSTEMTIME pst)\n{\n  DATE date;\n  return SUCCEEDED(VarDateFromStr(psz, lcid, 0, &amp;date)) &amp;&amp;\n         VariantTimeToSystemTime(date, pst);\n}\nBOOL FileTimeFromStr(__in LPCWSTR psz, LCID lcid, __out LPFILETIME pft)\n{\n  SYSTEMTIME st;\n  return SystemTimeFromStr(psz, lcid, &amp;st) &amp;&amp;\n         SystemTimeToFileTime(&amp;st, pft);\n}\n<\/pre>\n<p>\nIf you have something in\nwhich parses CIM\n<a HREF=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa389799(v=VS.85).aspx\">\ndatetime<\/a>\nformat\n(which\n<a HREF=\"http:\/\/technet.microsoft.com\/en-us\/magazine\/2006.07.scriptingguy.aspx\">\nThe Scripting Guys liken to Klingon<\/a>)\nyou can use the\n<a HREF=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa393687(VS.85).aspx\">\n<code>SWbemDateTime<\/code> object<\/a>.\nSince this is a scripting object, using it from C++ is rather\ncumbersome.\n<\/p>\n<pre>\nBOOL FileTimeFromCIMDateTime(__in LPCWSTR psz, __out LPFILETIME pft)\n{\n BOOL fSuccess = FALSE;\n ISWbemDateTime *pDateTime;\n HRESULT hr = CoCreateInstance(__uuidof(SWbemDateTime), 0,\n                 CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&amp;pDateTime));\n if (SUCCEEDED(hr)) {\n  BSTR bstr = SysAllocString(psz);\n  if (bstr) {\n   hr = pDateTime-&gt;put_Value(bstr);\n   if (SUCCEEDED(hr)) {\n    BSTR bstrFT;\n    hr = pDateTime-&gt;GetFileTime(VARIANT_FALSE, &amp;bstrFT);\n    if (SUCCEEDED(hr)) {\n     __int64 i64FT = _wtoi64(bstrFT);\n     pft-&gt;dwLowDateTime = LODWORD(i64FT);\n     pft-&gt;dwHighDateTime = HIDWORD(i64FT);\n     fSuccess = TRUE;\n     SysFreeString(bstrFT);\n    }\n   }\n   SysFreeString(bstr);\n  }\n  pDateTime-&gt;Release();\n }\n return fSuccess;\n}\n<\/pre>\n<p>\nFrom the managed side, you have\n<a HREF=\"http:\/\/blackrabbitcoder.net\/archive\/2012\/01\/05\/c.net-little-wonders-the-datetime-tryparse-and-parseexact-methods.aspx\">\n<code>Date&shy;Time.Try&shy;Parse<\/code>\nand\n<code>Date&shy;Time.Parse&shy;Exact<\/code> methods<\/a>.\n<\/p>\n<p>\nI leave you to investigate the time zone and locale issues associated\nwith these techniques.\n(Because I can&#8217;t be bothered.)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Public Service Announcement: Daylight Saving Time ends in most parts of the United States this weekend. Other parts of the world may change on a different day from the United States. The NLS functions in Win32 provide functions to convert a SYSTEMTIME into a string, but it does not provide any functions to perform the [&hellip;]<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25,108],"class_list":["post-6183","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code","tag-time"],"acf":[],"blog_post_summary":"<p>Public Service Announcement: Daylight Saving Time ends in most parts of the United States this weekend. Other parts of the world may change on a different day from the United States. The NLS functions in Win32 provide functions to convert a SYSTEMTIME into a string, but it does not provide any functions to perform the [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/6183","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=6183"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/6183\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=6183"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=6183"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=6183"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}