ParseExact - parsing a date with a given format in C++ / MFC
-
Hello, I need a function that would do a parse exact on a CString - given a format - and return a date (or an exception!) Eg COleDateTime timParsed; timParsed.ParseExact("2002 10 3","yyyy mm d"); I am aware of ParseDateTime... but I need something where you actually specify the expected format! And I knoe ParseExact is in .net but hey I am stuck in the nineties! Any idea? Thanks! Jerry
-
Hello, I need a function that would do a parse exact on a CString - given a format - and return a date (or an exception!) Eg COleDateTime timParsed; timParsed.ParseExact("2002 10 3","yyyy mm d"); I am aware of ParseDateTime... but I need something where you actually specify the expected format! And I knoe ParseExact is in .net but hey I am stuck in the nineties! Any idea? Thanks! Jerry
You may use
time_from_string()
fromboost::posix_time
, port the OpenSource code of the BSDstrptime()
function, or write a parser yourself (e.g. usingsscanf()
). -
Hello, I need a function that would do a parse exact on a CString - given a format - and return a date (or an exception!) Eg COleDateTime timParsed; timParsed.ParseExact("2002 10 3","yyyy mm d"); I am aware of ParseDateTime... but I need something where you actually specify the expected format! And I knoe ParseExact is in .net but hey I am stuck in the nineties! Any idea? Thanks! Jerry
-
Hello, I need a function that would do a parse exact on a CString - given a format - and return a date (or an exception!) Eg COleDateTime timParsed; timParsed.ParseExact("2002 10 3","yyyy mm d"); I am aware of ParseDateTime... but I need something where you actually specify the expected format! And I knoe ParseExact is in .net but hey I am stuck in the nineties! Any idea? Thanks! Jerry
Thanks Jochen And Richard, Yes I used an OpenSource version of strptime() that I found here: http://plibc.sourceforge.net/doxygen/strptime_8c-source.html[^]- and I have transformef the char(s) into TCHAR, added _T, etc.... and I ave written this:
{
...
CString strFormatCpp = TranslateFormatDate(strFormatVB);tm timeDate; memset(&timeDate,0,sizeof(tm )); TCHAR \* pRes = strptime (strMyDate, strFormatCpp, &timeDate); if ( pRes != NULL ) { COleDateTime oleDate; oleDate.SetDateTime(1900 + timeDate.tm\_year,timeDate.tm\_mon+1,timeDate.tm\_mday,timeDate.tm\_hour,timeDate.tm\_min,timeDate.tm\_sec); }
}
CString TranslateFormatDate(const CString & strFormat)
{
CString strResult;
int nChar = 0;
while ( nChar < strFormat.GetLength() )
{
switch ( strFormat[nChar] )
{
case 'A':
if ( MatchFormatKey(_T("AMPM"),_T("%p"),strFormat, nChar,strResult) )
continue;
break;
case 'a':
if ( MatchFormatKey(_T("ampm"),_T("%p"),strFormat, nChar,strResult) ) // Lower does not exist in C++?
continue;
case 'y':
if ( MatchFormatKey(_T("yyyy"),_T("%Y"),strFormat, nChar,strResult) )
continue;
if ( MatchFormatKey(_T("yy"),_T("%y"),strFormat, nChar,strResult) )
continue;
break;
case 'M':
if ( MatchFormatKey(_T("MMMM"),_T("%B"),strFormat, nChar,strResult) )
continue;
if ( MatchFormatKey(_T("MMM"),_T("%b"),strFormat, nChar,strResult) )
continue;
if ( MatchFormatKey(_T("MM"),_T("%m"),strFormat, nChar,strResult) )
continue;
if ( MatchFormatKey(_T("M"),_T("%#m"),strFormat, nChar,strResult) )
continue;
break;
case 'd':
if ( MatchFormatKey(_T("dddd"),_T("%A"),strFormat, nChar,strResult) )
continue;
if ( MatchFormatKey(_T("ddd"),_T("%a"),strFormat, nChar,strResult) )
continue;
if ( MatchFormatKey(_T("dd"),_T("%d"),strFormat, nChar,strResult) )
continue;
if ( MatchFormatKey(_T("d"),_T("%#d"),strFormat, nChar,strResult) )
continue;
break;
case 'h':
if ( MatchFormatKey(_T("hh"),_T("%I"),strFormat, nChar,strResult) )
continue;
if ( MatchFormatKey(_T("h"),_T("%#I"),strFormat, nChar,strResult) )
continue;
break;
case 'H':
if ( MatchFormatKey(_T("HH"),_T("%H"),strFormat, nChar,strResult) )
continue;
if ( MatchFormatKey(_T("H"),_T("%#H"),strFormat, -
Hello, I need a function that would do a parse exact on a CString - given a format - and return a date (or an exception!) Eg COleDateTime timParsed; timParsed.ParseExact("2002 10 3","yyyy mm d"); I am aware of ParseDateTime... but I need something where you actually specify the expected format! And I knoe ParseExact is in .net but hey I am stuck in the nineties! Any idea? Thanks! Jerry
Richard pointed out using
strftime( )
. it is standard C.#include
#includeint main ()
{
time_t rawtime;
struct tm * timeinfo;
char buffer [80];time ( &rawtime );
timeinfo = localtime ( &rawtime );strftime (buffer,80,"%Y %m %d ",timeinfo);
puts (buffer);
return 0;
} -
Richard pointed out using
strftime( )
. it is standard C.#include
#includeint main ()
{
time_t rawtime;
struct tm * timeinfo;
char buffer [80];time ( &rawtime );
timeinfo = localtime ( &rawtime );strftime (buffer,80,"%Y %m %d ",timeinfo);
puts (buffer);
return 0;
} -
My apologies, I got mixed up, and cannot for the life of me recall the function that does the parsing. [edit] It's
strptime()
which, unfortunately, is not available in Windows, so you will need to get a copy of an open source version from somewhere. [/edit]One of these days I'm going to think of a really clever signature.
-
Hello, I need a function that would do a parse exact on a CString - given a format - and return a date (or an exception!) Eg COleDateTime timParsed; timParsed.ParseExact("2002 10 3","yyyy mm d"); I am aware of ParseDateTime... but I need something where you actually specify the expected format! And I knoe ParseExact is in .net but hey I am stuck in the nineties! Any idea? Thanks! Jerry
You can still use the ParseDateTime, but you need to set the format first. Here's what I've done... First, get the current format using
GetLocaleInfo(m\_LCID, LOCALE\_SSHORTDATE, m\_csOriginalSDateFormat.GetBuffer(MAX\_PATH + 1), MAX\_PATH); m\_csOriginalSDateFormat.ReleaseBuffer();
(m_csOriginalSDateFormat is a CString) Next have your own CString with the desired format:
m_csNewSDateFormat = _T("M/d/yyyy");
Now, set the desired format, parse the date/time, and restore the original format:
SetLocaleInfo(m_LCID, LOCALE_SSHORTDATE, m_csNewSDateFormat);
oleStartTime.ParseDateTime(csDateTime);
SetLocaleInfo(m_LCID, LOCALE_SSHORTDATE, m_csOriginalSDateFormat);Hope this helps.
Karl - WK5M PP-ASEL-IA (N43CS) PGP Key: 0xDB02E193 PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193
-
My mistake. Didn't notice that CStrings only compile in MFC.
char buffer[256];
CString cs = buffer; -
You can still use the ParseDateTime, but you need to set the format first. Here's what I've done... First, get the current format using
GetLocaleInfo(m\_LCID, LOCALE\_SSHORTDATE, m\_csOriginalSDateFormat.GetBuffer(MAX\_PATH + 1), MAX\_PATH); m\_csOriginalSDateFormat.ReleaseBuffer();
(m_csOriginalSDateFormat is a CString) Next have your own CString with the desired format:
m_csNewSDateFormat = _T("M/d/yyyy");
Now, set the desired format, parse the date/time, and restore the original format:
SetLocaleInfo(m_LCID, LOCALE_SSHORTDATE, m_csNewSDateFormat);
oleStartTime.ParseDateTime(csDateTime);
SetLocaleInfo(m_LCID, LOCALE_SSHORTDATE, m_csOriginalSDateFormat);Hope this helps.
Karl - WK5M PP-ASEL-IA (N43CS) PGP Key: 0xDB02E193 PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193
-
Hello, I need a function that would do a parse exact on a CString - given a format - and return a date (or an exception!) Eg COleDateTime timParsed; timParsed.ParseExact("2002 10 3","yyyy mm d"); I am aware of ParseDateTime... but I need something where you actually specify the expected format! And I knoe ParseExact is in .net but hey I am stuck in the nineties! Any idea? Thanks! Jerry
*FACEPALM* COleDateTime::Format MSDN[^]
COleDateTime t(1999, 3, 19, 22, 15, 0);
CString str = t.Format(_T("%A, %B %d, %Y"));
-
*FACEPALM* COleDateTime::Format MSDN[^]
COleDateTime t(1999, 3, 19, 22, 15, 0);
CString str = t.Format(_T("%A, %B %d, %Y"));
Thanks again... but this is to return a string with a format... What I wanted is to parse a string into a date with a format! Something like
COleDatetime t;
t.IsThisStringADateWithThisFormat(_T("10012012"),"ddMMyyyy");But I have managed (see my post above)! Thanks anyway! Jerry
-
My apologies, I got mixed up, and cannot for the life of me recall the function that does the parsing. [edit] It's
strptime()
which, unfortunately, is not available in Windows, so you will need to get a copy of an open source version from somewhere. [/edit]One of these days I'm going to think of a really clever signature.
-
Richard MacCutchan wrote:
It's
strptime()
which, unfortunately, is not available in WindowsVery odd. Far as I can tell strptime is not part of ANSI C. Which makes me wonder why.