2017-06-12 50 views
0

我有這個問題。將CString日期密鑰轉換爲長密鑰

這是我如何創建一個日期的關鍵:最近

​​

,我創建了一個新的類型的關鍵:

WORD wKey = static_cast<WORD>(CInPlaceDT::GetLongDate(psEvent->datEvent)); 

GetLongDate方法是:

long CInPlaceDT::GetLongDate(COleDateTime timDate) 
{ 
    long lDate; 

    lDate = (timDate.GetYear() * 10000) + 
      (timDate.GetMonth() * 100) + 
      timDate.GetDay(); 

    return lDate; 
} 

上述代碼沒有任何問題。但我現在處於這種情況,我需要採取包含格式化密鑰(日期)的CString並構建相同的日期long。目前我這樣做:

if (mapSSEventLocations.GetSize() > 0 && m_mapWOSpecialEvents.GetSize() > 0) 
{ 
    // The new SRR format does not use the mapSSEventLocations object anymore. 
    // So we must migrate what we can across. 
    POSITION sPos = mapSSEventLocations.GetStartPosition(); 
    while (sPos != nullptr) 
    { 
     CString strDate, strLocation; 
     mapSSEventLocations.GetNextAssoc(sPos, strDate, strLocation); 
     // We must now find the match 
     // The key is like this: psEvent->datEvent.Format(_T("%Y-%m-%d")); 
     POSITION sPos2 = m_mapWOSpecialEvents.GetStartPosition(); 
     while (sPos2 != nullptr) 
     { 
      WORD wDate; 
      CSpecialEvent *pEvent = nullptr; 
      m_mapWOSpecialEvents.GetNextAssoc(sPos2, wDate, (CObject *&)pEvent); 
      if (pEvent != nullptr) 
      { 
       COleDateTime datEvent; 

       CInPlaceDT::GetOleDateTime(wDate, datEvent); 
       CString strThisKey = datEvent.Format(_T("%Y-%m-%d")); 

       if (strThisKey == strDate) 
       { 
        // We got the match 
        pEvent->SetLocation(strLocation); 
        break; 
       } 
      } 
     } 
    } 
} 

它工作正常。但我想採取strDate並將其轉換爲wDate樣式鍵,以便我可以查找該事件。

回答

1

我有一些舊代碼,使用scanf將文本轉換爲日期,並使用regex添加了第二個版本。我似乎記得一個MFC正則表達式類,但無法找到它。

CString FormatDate(COleDateTime const& dateTime) 
{ 
    // YYYY-MM-DD 
    return dateTime.Format(_T("%Y-%m-%d")); 
} 

long ToLongDate(COleDateTime const& dateTime) 
{ 
    return ((dateTime.GetYear() * 10000) + 
     (dateTime.GetMonth() * 100) + 
     dateTime.GetDay()); 
} 

// the scanf way 
long ToLongDate(CString const& dateText) 
{ 
    int year = 0; 
    int month = 0; 
    int day = 0; 

    if (_stscanf_s(dateText, _T("%d-%d-%d"), &year, &month, &day) != 3) 
    { 
     // invalid date - throw something? 
    } 

    COleDateTime dateTime{ year, month, day, 0, 0, 0 }; 
    //if (dateTime.GetStatus() == COleDateTime::DateTimeStatus::invalid) 
    // invalid date - throw something? 
    return ToLongDate(dateTime); 
} 

// The std::regex way - #include <regex> 
long ToLongDate2(CString const& dateText) 
{ 
    int year = 0; 
    int month = 0; 
    int day = 0; 

    try 
    { 
     std::basic_regex<TCHAR> regularExpression(
      _T("^([0-9]{4})-([0-9]{2})-([0-9]{2})$")); 
     std::match_results<LPCTSTR> match; 

     if (std::regex_search(dateText.GetString(), match, 
      regularExpression) && (match.size() == 4)) 
     { 
      // [0] - is the entire string 
      year = stoi(match[1].str()); 
      month = stoi(match[2].str()); 
      day = stoi(match[3].str()); 
     } 
    } 
    catch (std::exception& e) 
    { 
     // Do something with exception 
    } 

    COleDateTime dateTime{ year, month, day, 0, 0, 0 }; 
    //if (dateTime.GetStatus() == COleDateTime::DateTimeStatus::invalid) 
    return ToLongDate(dateTime); 
}