FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour "Text" Date/Time to "Number" ?
Posts: 1772
Joined: Thu Sep 05, 2019 05:32 AM
"Text" Date/Time to "Number" ?
Posted: Sun Aug 14, 2022 08:30 AM
hi,

have write my own "phpBB Froum Grabber" but i have use HMG Syntax.

it work well with HMG and Xbase++ phpBB Forum but with FiveWin Forum i have Problem with Date/Time Format
&raquo; Thu Oct 06, 2005 6:24 pm </p>
&raquo; Thu Oct 06, 2005 8:28 pm </p>
&raquo; Fri Oct 07, 2005 7:11 pm </p>
&raquo; Sun Feb 26, 2006 9:17 am </p>

how can i convert those Date/Time String into his Format :-)
Code (fw): Select all Collapse
YYYY-MM-DD HH:MM:SS
greeting,

Jimmy
Posts: 6755
Joined: Wed Feb 15, 2012 08:25 PM
Re: &quot;Text&quot; Date/Time to &quot;Number&quot; ?
Posted: Sun Aug 14, 2022 09:59 AM
Cristobal Navarro

Hay dos tipos de personas: las que te hacen perder el tiempo y las que te hacen perder la noción del tiempo

El secreto de la felicidad no está en hacer lo que te gusta, sino en que te guste lo que haces
Posts: 1772
Joined: Thu Sep 05, 2019 05:32 AM
Re: &quot;Text&quot; Date/Time to &quot;Number&quot; ?
Posted: Sun Aug 14, 2022 05:20 PM
hi,
cnavarro wrote:https://github.com/Petewg/harbour-core/wiki/Date-Time

thx for Answer.

which hb_* Function is to use to convert "Oct 06, 2005" into Type "D" :-)
which DATE Format is need :-)
i do have
Code (fw): Select all Collapse
   SET DATE ANSI
   SET CENTURY ON
greeting,

Jimmy
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: &quot;Text&quot; Date/Time to &quot;Number&quot; ?
Posted: Mon Aug 15, 2022 09:00 AM

Do you wan to convert the string "&raquo; Thu Oct 06, 2005 6:24 pm </p>" into string "2005-10-06 18:24:00" ?
You said you could do it with Xbase++.
Can you please share your XBase++ code that does this conversion?

Regards



G. N. Rao.

Hyderabad, India
Posts: 1772
Joined: Thu Sep 05, 2019 05:32 AM
Re: &quot;Text&quot; Date/Time to &quot;Number&quot; ?
Posted: Mon Aug 15, 2022 07:36 PM
hi,
nageswaragunupudi wrote:Do you wan to convert the string "&raquo; Thu Oct 06, 2005 6:24 pm </p>" into string "2005-10-06 18:24:00" ?

Yes

nageswaragunupudi wrote:
You said you could do it with Xbase++.
Can you please share your XBase++ code that does this conversion?

there is not special Xbase++ Function to convert that String into Date/Time Format

i have made a ""Quick & Dirty" Function to convert
Code (fw): Select all Collapse
FUNCTION StringDate( cLine, cTime )
LOCAL ii, nPosi, cMonth, cNext, cYear, nSec, cMore
LOCAL cYYYY, cMM, cDD, cHHMMSS
LOCAL aMonth := { "Jan", ;
                     "Feb", ;
                     "Mar", ;
                     "Apr", ;
                     "May", ;
                     "Jun", ;
                     "Jul", ;
                     "Aug", ;
                     "Sep", ;
                     "Oct", ;
                     "Nov", ;
                     "Dec" }

   cTime := "00:00:00"

   FOR ii := 1 TO LEN( aMonth )
      IF aMonth[ ii ] $ cLine
         cMM := STRZERO( ii, 2 )
         cMonth := aMonth[ ii ]
         EXIT
      ENDIF
   NEXT
   IF EMPTY( cMM )
      RETURN CTOD( " . . " )
   ENDIF

   nPosi := AT( "&raquo;", cLine )
   IF nPosi > 0
      cLine := SUBSTR( cLine, nPosi + 7 )
   ELSE
      RETURN CTOD( " . . " )
   ENDIF

   //  &raquo; Thu Oct 06, 2005 6:24 pm </p>
   nPosi := AT( cMonth, cLine )
   IF nPosi > 0
      cNext := SUBSTR( cLine, nPosi + 3 )

      nPosi := AT( ",", cNext )
      IF nPosi > 0
         cDD := LTRIM( SUBSTR( cNext, 1, nPosi - 1 ) )

         cYear := LTRIM( SUBSTR( cNext, nPosi + 1 ) )
         cYYYY := SUBSTR( cYear, 1, 4 )

         cMore := SUBSTR( cYear, 5 )
         IF "pm" $ cMore
            nPosi := AT( "pm", cMore )
            cHHMMSS := SUBSTR( cMore, 1, nPosi - 1 )
            nSec := HMS2SEC( cHHMMSS )
            nSec += ( 12 * 60 * 60 )
            cHHMMSS := SEC2HMS( nSec )
         ELSE
            nPosi := AT( "am", cMore )
            cHHMMSS := SUBSTR( cMore, 1, nPosi - 1 )
            nSec := HMS2SEC( cHHMMSS )
            cHHMMSS := SEC2HMS( nSec )
         ENDIF

      ELSE
         RETURN CTOD( " . . " )
      ENDIF
   ELSE
      RETURN CTOD( " . . " )
   ENDIF

   cTime := cHHMMSS

RETURN STOD( cYYYY + cMM + cDD )

p.s.
HMS2SEC() and SEC2HMS() are to convert HH:MM:SS <-> Seconds
is there a harbour Function :-)
greeting,

Jimmy
Posts: 195
Joined: Sun Jul 22, 2012 07:01 PM
Re: &quot;Text&quot; Date/Time to &quot;Number&quot; ?
Posted: Tue Aug 16, 2022 03:56 PM
Not sure this is useful to anybody to me, but this is how I convert back and forth from harbour datetime to ISO datetime.

Code (fw): Select all Collapse
// -------------------------------------------------------------------------- //

// DateTimeToISO8601( t )
FUNCTION hb_TToI( t )

   hb_Default( @t, DateTime() )
   t := hb_TToS( t )
   RETURN SubStr( t, 01, 04 ) + "-" + ;
          SubStr( t, 05, 02 ) + "-" + ;
          SubStr( t, 07, 02 ) + "T" + ;
          SubStr( t, 09, 02 ) + ":" + ;
          SubStr( t, 11, 02 ) + ":" + ;
          SubStr( t, 13, 02 ) + "." + ;
          SubStr( t, 15, 03 )

// -------------------------------------------------------------------------- //

// IS8601ToDateTime
FUNCTION hb_IToT( cISO )

   IF Empty( cISO )
      RETURN _EMPTY_DATETIME
   ENDIF

   RETURN hb_DateTime( Val( SubStr( cISO, 01, 04 ) ), ;
                       Val( SubStr( cISO, 06, 02 ) ), ;
                       Val( SubStr( cISO, 09, 02 ) ), ;
                       Val( SubStr( cISO, 12, 02 ) ), ;
                       Val( SubStr( cISO, 15, 02 ) ), ;
                       Val( SubStr( cISO, 18, 02 ) ), ;
                       Val( SubStr( cISO, 21, 03 ) ) )
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: &quot;Text&quot; Date/Time to &quot;Number&quot; ?
Posted: Tue Aug 16, 2022 06:25 PM
Code (fw): Select all Collapse
Secs( <cTimeStr> / <tDateTime> ) --> nSecs
TimeToSec( <cTimeStr> ) --> nSecs


TString( nSecs ) --> cTimeStr
SecToTime( [<nSeconds>], [<lHundredth>] ) --> cTime
Regards



G. N. Rao.

Hyderabad, India
Posts: 195
Joined: Sun Jul 22, 2012 07:01 PM
Re: &quot;Text&quot; Date/Time to &quot;Number&quot; ?
Posted: Tue Aug 16, 2022 07:28 PM

Thanks Rao. The secs() function in harbour actually gets mapped to hb_sec() in dateshb.c, which took me a few minutes to find. But I see a bunch of potentially useful functions there now that I'm going to test to see if I can get rid of the code I'm currently using and revert to native harbour.

Robb

Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: &quot;Text&quot; Date/Time to &quot;Number&quot; ?
Posted: Tue Aug 16, 2022 08:09 PM
rhlawek wrote:Not sure this is useful to anybody to me, but this is how I convert back and forth from harbour datetime to ISO datetime.

Code (fw): Select all Collapse
// -------------------------------------------------------------------------- //

// DateTimeToISO8601( t )
FUNCTION hb_TToI( t )

   hb_Default( @t, DateTime() )
   t := hb_TToS( t )
   RETURN SubStr( t, 01, 04 ) + "-" + ;
          SubStr( t, 05, 02 ) + "-" + ;
          SubStr( t, 07, 02 ) + "T" + ;
          SubStr( t, 09, 02 ) + ":" + ;
          SubStr( t, 11, 02 ) + ":" + ;
          SubStr( t, 13, 02 ) + "." + ;
          SubStr( t, 15, 03 )

// -------------------------------------------------------------------------- //

// IS8601ToDateTime
FUNCTION hb_IToT( cISO )

   IF Empty( cISO )
      RETURN _EMPTY_DATETIME
   ENDIF

   RETURN hb_DateTime( Val( SubStr( cISO, 01, 04 ) ), ;
                       Val( SubStr( cISO, 06, 02 ) ), ;
                       Val( SubStr( cISO, 09, 02 ) ), ;
                       Val( SubStr( cISO, 12, 02 ) ), ;
                       Val( SubStr( cISO, 15, 02 ) ), ;
                       Val( SubStr( cISO, 18, 02 ) ), ;
                       Val( SubStr( cISO, 21, 03 ) ) )


I think these functions can be simplified like this:
Code (fw): Select all Collapse
function ISO2DateTime( cDate )

   local df          := Set( _SET_DATEFORMAT, "YYYY-MM-DD" )
   local tf          := Set( _SET_TIMEFORMAT, "HH:MM:SS.fff" )
   local tDateTime   := HB_CTOT( cDate )

   Set( _SET_DATEFORMAT, df )
   Set( _SET_TIMEFORMAT, tf )

return tDateTime

function DateTime2ISO( tDateTime )

   local df          := Set( _SET_DATEFORMAT, "YYYY-MM-DD" )
   local tf          := Set( _SET_TIMEFORMAT, "HH:MM:SS.fff" )
   local cDate       := StrTran( HB_TTOC( tDateTime ), " ", "T" )

   Set( _SET_DATEFORMAT, df )
   Set( _SET_TIMEFORMAT, tf )

return cDate
Regards



G. N. Rao.

Hyderabad, India

Continue the discussion