Sujet : Re: Parsing timestamps?
De : the.beez.speaks (at) *nospam* gmail.com (Hans Bezemer)
Groupes : comp.lang.forthDate : 09. Jun 2025, 14:21:52
Autres entêtes
Organisation : KPN B.V.
Message-ID : <nnd$16a55d5e$0e5ab22d@1d9c51e25014f149>
References : 1 2 3 4 5 6
User-Agent : Mozilla Thunderbird
On 08-06-2025 04:38, dxf wrote:
On 8/06/2025 3:07 am, LIT wrote:
My solution is rather straightforward:
>
1 VARIABLE C6
1 VARIABLE C1
>
: TIMESTRSCAN ( addr count -- d )
>R >R 0 0 R> R>
OVER + 1-
DO
I C@ DUP 58 =
IF
DROP
C6 @ 60 * C6 !
1 C1 !
ELSE
48 - C1 @ * C6 @ M* D+
10 C1 !
THEN
-1 +LOOP
1 C6 ! 1 C1 !
;
First of all: are you an 8-bit hobbyist? I mean, you can convert 32-bit values close to 600,000 hours - that's a whopping 68 years - before you overflow this thing. Signed, that is.
Second, stamp out the variables! You use two globals for this trivial routine. This does the same thing:
: timescan
over swap chars + >r 0 tuck begin
over r@ <
while
over c@ [char] : =
if rot + 60 * swap 0 else 10 * over c@ [char] 0 - + then
swap char+ swap
repeat r> drop nip +
;
And the beauty of CHAR is - less magic numbers in your code. But even if you are dedicated to using a double word accumulator, you won't need those variables:
: dtimescan
over swap chars + >r >r 0. r> 0 begin
over r@ <
while
over c@ [char] : =
if
swap >r s>d d+ 60 1 m*/ r> 0
else
10 * over c@ [char] 0 - +
then
swap char+ swap
repeat r> drop nip s>d d+
;
Now - did I win anything? Maybe a T-shirt "May the Forth be with you"?
Virtue is its own reward.
Hans Bezemer