A small skill, the right to pay tribute to Great Boost.
How to convert UNIX style to standard format
Unix style time units will generally be:
YYMMDD
For example, "0308011644".
However, actually the MFC's COLLDATETIME class does not support this format, and the VardateFromStr behind the COLEDATETIME is also not supported.
In fact, VardateFromstr will only support such approximate UNIX formats:
03 08 01 16:44
So we have to convert the Unix style as an approximate UNIX style, that is,:
From "0308011644"
Go to "03 08 01 16:44".
Then, let VardateFromstr & VarbstrFromDate convert it to "2003-8-1 16:44:00".
First, the "0308011644", we can still use Boost: tokenizer, and the appraisal phone number, here you need to split according to the number of numbers, so you want to use:
INT offsets [] = {2, 2, 2};
Boost :: Offset_separator F (Offsets, Offsets 3);
The above separator tells tokenizer, segmented according to 2, 2, 2, we use this line of code to implement it:
Boost :: tokenizer
The code is as follows, using Boost: Tokenizer Technology and VardateFromstr & VarbstrFromDate technology:
// STL & BOOST header file:
#pragma Warning (Disable: 4786)
#include
#include
/ ************************************************** ******
// Function name: ParseunixDateTime
// The class name: CProcessorthRead
// Function function: Analyze the time unit of UNIX style "0308011644"
The result of coming out is "2003-8-1 16:44:00"
// Process:
// 备:
// Author: Zheng Wei
// Time: August 11, 2003
// Return value: _BSTR_T
// Parameter Description: _BSTR_T & BSTRUNIXDATETIME
*********************************************************** ***** /
_BSTR_T CPROCESSORTHREAD :: ParseunixDateTime (_BSTR_T & BSTRUNIXDATETIME)
{
//
// Special means
/ / In order to correct incoming UNIX style time "0308011644"
// Introduced Boost: Tokenizer, eventually converted to "03 08 01 16:44"
_BSTR_T BSTRDITITIME ("");
{
Std :: string S (BStrunixDatetime);
INT offsets [] = {2, 2, 2};
Boost :: Offset_separator F (Offsets, Offsets 3);
Boost :: tokenizer
Boost :: tokenizer
Boost :: tokenizer
Boost :: tokenizer
/ / The time unit is divided into the way we give, here is cycled:
For (beg = tok1.begin (); /
Beg! = ipend; /
Beg, NINDEX)
{
BSTRDITATETIME = _BSTR_T ((* beg) .c_str ());
// In accordance with the order of years, months, days, time, in order, in advance
Switch (Nindex)
{
Case 0: // 03
Case 1: // 08
Case 2: // 01
BSTRDONEDATETIME = _BSTR_T ("");
Break;
Case 3: // 16
BSTRDONEDATETIME = _BSTR_T (":");
Break;
DEFAULT:
Break;
}
}
// Final results, the time is converted from "0308011644" to "03 08 01 16:44"
//
// Since we have been converted to "03 08 01 16:44", we also need further conversion to standard time "2003-8-1 16:44:00"
//
IF (BSTRDONEDATIME.LENGTH ()> 0)
{
Double ddateNew = 0.0;
LCID LCID;
LCID = GetSystemDefaultLcid ();
HRESULT HR = VardateFromstr (BSTRDONEDATETIME,
LCID,
LOCALE_NOUSEROVERRIDE,
& ddateNew);
BSTR BSTROUT = NULL;
VarbstrFromDate (DDateNew,
LCID,
LOCALE_NOUSEROVERRIDE,
& bstrout);
BSTRDONEDATETIME = _BSTR_T (BSTROUT, FALSE);
}
}
//
Return BSTRDITIME;
}