• Re: Parsing timestamps?

    From Anthony Howe@21:1/5 to dxf on Sun Oct 6 11:35:24 2024
    On 2024-10-06 03:51, dxf wrote:
    Is there an easier way of doing this? End goal is a double number representing centi-secs.

    Isn't there an ISO 8601 parsing package?

    --
    Anthony C Howe
    achowe@snert.com BarricadeMX & Milters http://nanozen.snert.com/ http://software.snert.com/

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From albert@spenarnc.xs4all.nl@21:1/5 to dxforth@gmail.com on Mon Oct 7 13:00:10 2024
    In article <1f433fabcb4d053d16cbc098dedc6c370608ac01@i2pn2.org>,
    dxf <dxforth@gmail.com> wrote:
    Is there an easier way of doing this? End goal is a double number >representing centi-secs.


    empty decimal

    : SPLIT ( a u c -- a2 u2 a3 u3 ) >r 2dup r> scan 2swap 2 pick - ;
    : >INT ( adr len -- u ) 0 0 2swap >number 2drop drop ;

    : /T ( a u -- $hour $min $sec )
    2 0 do [char] : split 2swap dup if 1 /string then loop
    2 0 do dup 0= if 2rot 2rot then loop ;

    : .T 2swap 2rot cr >int . ." hr " >int . ." min " >int . ." sec " ;

    s" 1:2:3" /t .t
    s" 02:03" /t .t
    s" 03" /t .t
    s" 23:59:59" /t .t
    s" 0:00:03" /t .t

    After ca. 50 years I have completed the $@ $! $+! $C+ $/ with
    $\ . Now I can do this

    "12:03:43" &: $\ TYPE &: $\ TYPE &: $\ TYPE
    43 03 12 OK

    "12:03:43" &: $/ TYPE &: $/ TYPE &: $/ TYPE
    12 03 43 OK

    Insert
    "hr" TYPE
    as required.

    I can't believe the long posts this sparks.

    Groetjes Albert
    --
    Temu exploits Christians: (Disclaimer, only 10 apostles)
    Last Supper Acrylic Suncatcher - 15Cm Round Stained Glass- Style Wall
    Art For Home, Office And Garden Decor - Perfect For Windows, Bars,
    And Gifts For Friends Family And Colleagues.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From sjack@21:1/5 to dxf on Mon Oct 7 16:20:29 2024
    dxf <dxforth@gmail.com> wrote:
    The HH:MM:SS format is easy but how to deal with the variants shown above? They occur in the real world.

    Toad code:
    fload job
    : xx. 0 <# bl hold # # #> type ;
    : tab3. tab rot xx. swap xx. xx. ;

    -- &num ( g -- s )
    -- Convert g-string to numeric string address
    : &num drop 1- ;
    -- Note g-string is ANS string ( addr u )

    -- ts_elms ( "[hh:][mm:]ss<bl>" -- 0 0 ss | 0 mm ss | hh mm ss )
    -- Parse timestamp elements: hh=hours mm=minutes ss=seconds
    -- Input hh: element and hh:mm: combination elements may be left out
    -- if zero(s).
    : ts_elms
    bl word here count
    o+s do i c@ asc : = if bl i c! then loop
    0 0 0 here count
    begin
    bl split dup 0> while &num number drop
    5 roll drop -rot
    repeat 4drop
    ;


    ts_elms 25
    i. tab3. --> 00 00 25
    i. ts_elms 25 tab3. --> 00 00 25
    i. ts_elms 10:25 tab3. --> 00 10 25
    i. ts_elms 2:10:25 tab3. --> 02 10 25
    OK

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From albert@spenarnc.xs4all.nl@21:1/5 to dxforth@gmail.com on Tue Oct 8 09:41:07 2024
    In article <1f433fabcb4d053d16cbc098dedc6c370608ac01@i2pn2.org>,
    dxf <dxforth@gmail.com> wrote:
    Is there an easier way of doing this? End goal is a double number >representing centi-secs.


    empty decimal

    : SPLIT ( a u c -- a2 u2 a3 u3 ) >r 2dup r> scan 2swap 2 pick - ;
    : >INT ( adr len -- u ) 0 0 2swap >number 2drop drop ;

    : /T ( a u -- $hour $min $sec )
    2 0 do [char] : split 2swap dup if 1 /string then loop
    2 0 do dup 0= if 2rot 2rot then loop ;

    : .T 2swap 2rot cr >int . ." hr " >int . ." min " >int . ." sec " ;

    s" 1:2:3" /t .t
    s" 02:03" /t .t
    s" 03" /t .t
    s" 23:59:59" /t .t
    s" 0:00:03" /t .t

    This problem is ill posed. You don't specify what happens
    with less that 3 fields, or the meaning of the fields.
    Normally I make the tests to run before attempting to code.

    Groetjes Albert
    --
    Temu exploits Christians: (Disclaimer, only 10 apostles)
    Last Supper Acrylic Suncatcher - 15Cm Round Stained Glass- Style Wall
    Art For Home, Office And Garden Decor - Perfect For Windows, Bars,
    And Gifts For Friends Family And Colleagues.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From sjack@21:1/5 to Ahmed on Tue Oct 8 15:19:25 2024
    Ahmed <melahi_ahmed@yahoo.fr> wrote:
    I know you don't care about this case, but:

    Yes, originally I had syntax checks, left them out to focus more on getting
    the zeros in.

    -- ts_elms ( "[hh:][mm:]ss<bl>" -- 0 0 ss | 0 mm ss | hh mm ss )
    -- Parse timestamp elements: hh=hours mm=minutes ss=seconds
    -- Input hh: element and hh:mm: combination elements may be left out
    -- if zero(s).
    : ts_elms
    bl word here count
    over c@ asc : = >r ( leading char check )
    2dup + 1- c@ asc : = ( lagging char check )
    r> or if ." --Invalid " 2drop rdrop exit then
    o+s do i c@ asc : = if bl i c! then loop
    0 0 0 here count
    begin
    bl split dup 0> while &num number drop
    5 roll drop -rot
    repeat 4drop
    ;

    [s] Invalid syntax
    i. ts_elms 25: tab3. --> --Invalid
    i. ts_elms :25 tab3. --> --Invalid
    i. ts_elms :25: tab3. --> --Invalid

    [s] Valid syntax
    ts_elms 25
    i. tab3. --> 00 00 25
    i. ts_elms 25 tab3. --> 00 00 25
    i. ts_elms 10:25 tab3. --> 00 10 25
    i. ts_elms 2:10:25 tab3. --> 02 10 25

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Gerry Jackson@21:1/5 to dxf on Fri Oct 18 15:46:27 2024
    On 06/10/2024 08:51, dxf wrote:
    Is there an easier way of doing this? End goal is a double number representing centi-secs.


    empty decimal

    : SPLIT ( a u c -- a2 u2 a3 u3 ) >r 2dup r> scan 2swap 2 pick - ;
    : >INT ( adr len -- u ) 0 0 2swap >number 2drop drop ;

    : /T ( a u -- $hour $min $sec )
    2 0 do [char] : split 2swap dup if 1 /string then loop
    2 0 do dup 0= if 2rot 2rot then loop ;

    : .T 2swap 2rot cr >int . ." hr " >int . ." min " >int . ." sec " ;

    s" 1:2:3" /t .t
    s" 02:03" /t .t
    s" 03" /t .t
    s" 23:59:59" /t .t
    s" 0:00:03" /t .t

    Another solution

    : /t ( ca u -- sec min hour )
    3 \ a count, decremented every recurse
    [: -rot dup 0>
    if 0. 2swap >number 1 /string 2swap drop ( -- ct ca' u' n1 )
    >r rot 1-
    recurse r> swap exit
    then 2drop
    ;] execute
    0 ?do 0 loop \ 0 hours and minutes if missing in source string
    ;
    : .t cr . ." hr " . ." min " . ." sec " ;

    cr
    s" 1:2:3" /t .t
    s" 02:03" /t .t
    s" 03" /t .t
    s" 23:59:59" /t .t
    s" 0:00:03" /t .t
    s" " /t .t
    s" :" /t .t
    s" :53" /t .t
    s" 11/12/13" /t .t \ Different separator
    s" 11::13" /t .t
    s" :::" /t .t
    s" 3:" /t .t
    s" 1:2:" /t .t

    \ Results
    1 hr 2 min 3 sec
    0 hr 2 min 3 sec
    0 hr 0 min 3 sec
    23 hr 59 min 59 sec
    0 hr 0 min 3 sec
    0 hr 0 min 0 sec
    0 hr 0 min 0 sec
    0 hr 0 min 53 sec
    11 hr 12 min 13 sec
    11 hr 0 min 13 sec
    0 hr 0 min 0 sec
    0 hr 0 min 3 sec
    0 hr 1 min 2 sec

    The last two could be regarded as wrong but you indicated elsewhere that
    they wouldn't occur.

    Any non-digit is a separator

    --
    Gerry

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)