|
| 1 | +\ TIME.FTH time utilities for Camel99 Forth Nov 24 2012 Brian Fox |
| 2 | + |
| 3 | +\ 32 bit integer we can manage up to 2^32 seconds, or 119304 hrs. |
| 4 | + |
| 5 | +\ INCLUDE DSK1.TOOLS \ debugging |
| 6 | + |
| 7 | +DECIMAL |
| 8 | +: HRS>MINS ( n -- d) 3600 UM* ; |
| 9 | +: MINS>SECS ( n -- d) 60 UM* ; |
| 10 | + |
| 11 | +\ stackcrobatics for 3 items (hours,minutes,seconds) |
| 12 | +: >SSMMHH ( h m s -- s m h) SWAP ROT ; |
| 13 | +: >HHMMSS ( s m h -- h m s) -ROT SWAP ; |
| 14 | + |
| 15 | +: TIME>D ( s m h -- d) \ convert time format to DOUBLE (32bit int) |
| 16 | + HRS>MINS 2>R \ push double to rstack |
| 17 | + MINS>SECS SWAP M+ \ add secs (single) to mins (double) with mixed + |
| 18 | + 2R> D+ ; \ add hrs to sub-total |
| 19 | + |
| 20 | +: D>TIME ( d -- s m h ) \ convert DOUBLE to time |
| 21 | + 3600 UM/MOD ( -- rem hrs) >R |
| 22 | + 60 /MOD ( -- secs mins) |
| 23 | + R> ; ( -- secs mins hrs) |
| 24 | + |
| 25 | +\ Concept from Starting Forth, Brodie. Would have never thought of this :) |
| 26 | +: SEXTAL 6 BASE ! ; |
| 27 | +: <:> [CHAR] : HOLD ; |
| 28 | +: <.> [CHAR] . HOLD ; |
| 29 | +: ##: # SEXTAL # DECIMAL <:> ; |
| 30 | + |
| 31 | +: .TIME ( d -- ) \ expects double int as time in seconds on stack |
| 32 | + BASE @ >R |
| 33 | + <# ##: ##: # # #> TYPE |
| 34 | + R> BASE ! ; |
| 35 | + |
| 36 | +: DU< ( d d -- ?) ROT U> IF 2DROP TRUE ELSE U< THEN ; |
| 37 | +: D= ( d d -- ?) ROT = -ROT = AND ; |
| 38 | + |
| 39 | +: REDUCE2 ( s m h s m h -- d1 d2) \ convert 2 times into 2 doubles |
| 40 | + TIME>D 2>R \ convert top time and push |
| 41 | + TIME>D 2R> \ convert and pop |
| 42 | +; |
| 43 | + |
| 44 | +: ISBEFORE ( s m h s m h -- ?) REDUCE2 DU< ; \ is 1st time before 2nd time |
| 45 | +: ISAFTER ( s m h s m h -- ?) REDUCE2 2SWAP DU< ; \ is 1st time after 2nd time |
| 46 | +: SAMETIME ( s m h s m h -- ?) REDUCE2 D= ; \ are both times the same |
0 commit comments