Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. ParseExact - parsing a date with a given format in C++ / MFC

ParseExact - parsing a date with a given format in C++ / MFC

Scheduled Pinned Locked Moved C / C++ / MFC
c++csharpjsonhelpquestion
14 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B BadJerry

    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

    J Offline
    J Offline
    Jochen Arndt
    wrote on last edited by
    #2

    You may use time_from_string() from boost::posix_time, port the OpenSource code of the BSD strptime() function, or write a parser yourself (e.g. using sscanf()).

    1 Reply Last reply
    0
    • B BadJerry

      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

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #3

      I have had some success in the past using strftime()[^].

      One of these days I'm going to think of a really clever signature.

      1 Reply Last reply
      0
      • B BadJerry

        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

        B Offline
        B Offline
        BadJerry
        wrote on last edited by
        #4

        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,

        1 Reply Last reply
        0
        • B BadJerry

          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

          S Offline
          S Offline
          Software_Developer
          wrote on last edited by
          #5

          Richard pointed out using strftime( ) . it is standard C.

          #include
          #include

          int 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;
          }

          B 1 Reply Last reply
          0
          • S Software_Developer

            Richard pointed out using strftime( ) . it is standard C.

            #include
            #include

            int 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;
            }

            B Offline
            B Offline
            BadJerry
            wrote on last edited by
            #6

            Yes but I want to parse not to format! Thanks! Jerry

            L S 2 Replies Last reply
            0
            • B BadJerry

              Yes but I want to parse not to format! Thanks! Jerry

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #7

              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.

              J 1 Reply Last reply
              0
              • B BadJerry

                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

                K Offline
                K Offline
                krmed
                wrote on last edited by
                #8

                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

                B 1 Reply Last reply
                0
                • B BadJerry

                  Yes but I want to parse not to format! Thanks! Jerry

                  S Offline
                  S Offline
                  Software_Developer
                  wrote on last edited by
                  #9

                  My mistake. Didn't notice that CStrings only compile in MFC.

                  char buffer[256];
                  CString cs = buffer;

                  CString Management[^]

                  1 Reply Last reply
                  0
                  • K krmed

                    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

                    B Offline
                    B Offline
                    BadJerry
                    wrote on last edited by
                    #10

                    That's clever!

                    1 Reply Last reply
                    0
                    • B BadJerry

                      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

                      S Offline
                      S Offline
                      Software_Developer
                      wrote on last edited by
                      #11

                      *FACEPALM* COleDateTime::Format MSDN[^]

                      COleDateTime t(1999, 3, 19, 22, 15, 0);

                      CString str = t.Format(_T("%A, %B %d, %Y"));

                      B 1 Reply Last reply
                      0
                      • S Software_Developer

                        *FACEPALM* COleDateTime::Format MSDN[^]

                        COleDateTime t(1999, 3, 19, 22, 15, 0);

                        CString str = t.Format(_T("%A, %B %d, %Y"));

                        B Offline
                        B Offline
                        BadJerry
                        wrote on last edited by
                        #12

                        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

                        1 Reply Last reply
                        0
                        • L Lost User

                          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.

                          J Offline
                          J Offline
                          jschell
                          wrote on last edited by
                          #13

                          Richard MacCutchan wrote:

                          It's strptime() which, unfortunately, is not available in Windows

                          Very odd. Far as I can tell strptime is not part of ANSI C. Which makes me wonder why.

                          L 1 Reply Last reply
                          0
                          • J jschell

                            Richard MacCutchan wrote:

                            It's strptime() which, unfortunately, is not available in Windows

                            Very odd. Far as I can tell strptime is not part of ANSI C. Which makes me wonder why.

                            L Offline
                            L Offline
                            Lost User
                            wrote on last edited by
                            #14

                            It's POSIX according to the man page[^].

                            One of these days I'm going to think of a really clever signature.

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups