From luse@cod.NOSC.MIL Tue Sep 27 15:10:41 1988
Path: gmu90x!pyrdc!uunet!husc6!mailrus!ames!lll-tis!helios.ee.lbl.gov!nosc!cod!luse
From: luse@cod.NOSC.MIL (Stephen A. Luse)
Newsgroups: comp.sys.handhelds
Subject: time/date routines for HP-28
Keywords: time, date, calendar, julian day number
Message-ID: <1227@cod.NOSC.MIL>
Date: 27 Sep 88 19:10:41 GMT
Organization: Naval Ocean Systems Center, San Diego
Lines: 458



Here are *all* of the date and time routines that have been developed.
The previously posted clock routine has been enhanced to include the
date as well as the time in a running display.  In addition, I have
provided a routine that will scroll a calendar for any specified
month.  These routines depend on modules that provide useful tools
and building-blocks for other routines.  Modularity (leading to
flexibility) was a priority.  

Those of you that have loaded the previous time routines should
carefully check the following since some of the routines and names
have changed.  To aid you, I've included a CHKSUM routine (modified
from the one provided by Bob Peraino of George Mason University) and 
checksum values for each routine.

				Stephen Luse
				luse@nosc.mil


*******************************************

Note:  The SYSEVAL timer address of #11CAh specified in TM is for 
the HP-28S (Version 2BB).  The HP-28C has two rom versions out, 
and each requires its own SYSEVAL address.  Change the hex address 
to #123Eh for version 1BB or #1266h for version 1CC.  (If you are 
unsure of which version you have, enter #Ah SYSEVAL.)

Enter all code in a subdirectory dedicated to time/date routines.
(You may want to place CHKSUM in your HOME or other utility
directory where so it can be utilized anywhere.)  Check all code by
entering each routine name in level one and execute CHKSUM.  Compare
the computed value with that provided below.  If they disagree,
check your code carefully.  (We have made every effort to provide
accurate code and chksum values.  If you find a discrepancy that you
cannot resolve, please contact me.)

Checksum Values:
	CHKSUM   129 	DCAL	6912 	MTHLST	1A92
	NDAYS	F062 	MTHS	4398 	TMSTMP	59DA
	SETTIME	CEB3 	SETDATE	BBA1 	FMTTIME	5ACF
	CLOCK	C2BD 	TIME	A651 	DATE	FD70
	DM	56C8 	FMTDATE	4B66 	DAYS	3ADB
	DOW2	3034 	CJ2	CF65 	JC2	5E06
	DOW	588C 	CJ	B4B4 	JC	EEA3
	TM	5A2F

*************************************************
Usage of the main routines are described below:

SETTIME - Used to generate a correction factor to offset the internal
timer of your calculator to match actual time.  Set your clock by 
entering a 'sync-time', which is a time in the near future (a minute 
or two away).  This time must be in 24 hour HH.MMSS format.  
Watch your wrist-watch while keeping your finger poised over the 
'SETCLK' menu key and when 'sync-time' arrives PRESS THE KEY!  
The time correction factor is determined and stored in CKOR.

SETDATE - Used to generate a correction factor to offset the
internal timer with the actual date.  Set your date by entering
today's date in MM.DDYYYY format, then executing SETDATE.  The
date correction factor is determined and stored in DKOR.
IMPORTANT!:  You must set the time using SETTIME prior to using
SETDATE.

CLOCK - This is the main time/date routine which can be executed
anytime after you have used SETTIME and SETDATE.  When executed, you
will see the current day of week, month, date, year, and time displayed.
The display is dynamic - that is, the program is running in a loop,
constantly monitoring the internal timer and generating a new date
and time continuously.  (NOTE:  The seconds portion of the time will
not be regular.  The program takes a portion of a second to run, and
may not display every possible second.)  To exit, press any key
*except* ON.  The display will be reset to its original state.

DCAL - Display CALendar - diplays a scrolling calendar for a specified
month.  (Be sure to set the current time and date using SETTIME and
SETDATE prior to execution of DCAL).  Specify a month and year in 
MM.YYYY format and execute DCAL.  To exit, press any key *except*
ON.  After a minimum of scrolling, the display will be reset to its
original state.  (This routine could use some improvement.  The loop
that does the scrolling should check for a keypress after every four
lines are displayed instead of having to scroll the full month
before exiting.  Also, if the scroll is too fast or slow for you,
change the WAIT argument in DCAL from 0.5 to some other value.)

TMSTMP - TiMe STamP - Prints the date and time on your printer
(great for keeping track of different versions of your code during
development!)
************************************************************

Description of the Modular routines:

***** Time routines ******

TM - recalls the current value of the internal timer to level 1 in
binary form.

TIME - recalls the current value of the internal timer to level 2
and the current time to level 1 in 24 hour HH.MMSS format.

FMTTIME - ForMaT TIME - formats any time in level 1 in 24 hour
HH.MMSS format to "HH:MM:SS am" (or pm).  Can be used in conjunction
with TIME to format the current time into a useful format.  For
example, << TIME SWAP DROP FMTTIME >> will return the current time
to level 1 in the described format.

****** Date routines *******
Note:  Algorithms for CJ, JC and DOW are from the HP-41 PPC Rom manual
and modified for the HP-28 by Ron Farajit.  Julian Day Numbers are 
based on the Gregorian Calendar.

CJ - date to Julian Day conversion - This routine generates the
Julian day number for a specified date.  Expects YYYY in level 3, MM
in level 2, and DD in level 1.  For example, to calculate the Julian
Day Number for September 21, 1988, enter: 1988 ENTER 9 ENTER 21
ENTER CJ.  Value returned is 2447426.

JC - Julian Day to date conversion - This routine is the inverse of
CJ.  Given a Julian Day Number in level 1, YYYY is returned to level
3, MM to level 2, and DD to level 1.  Note:  CJ and JC provide a
means of calculating dates from other dates. For example, what is
the date 90 days from September 21, 1988?  Solution: 1988 ENTER 9
ENTER 21 ENTER CJ 45 + JC.  See 1988 in level 3, 11 in level 2, 5
in level 1, indicating that November 5, 1988 is 45 days from
September 21, 1988.

DOW - Day Of Week - Generates the Day Number and Day of Week for a
specified date.  (The Day Number is a value from 0 to 6 indicating
the day of the week, where 0 is Sunday, 1 is Monday, and so on.)
For example, to calculate the Day Number and Day of Week for
September 21, 1988, enter: 1988 ENTER 9 ENTER 21 ENTER DOW.  Value 3
is returned to level 2 (indicating Wednesday) and "Weds" is returned
to level 1.

CJ2, JC2, DOW2 -  These functions are the same as those listed
above, except that dates are specified in MM.DDYYYY format instead
of using three levels of the stack.


DM - Returns the current timer value (in decimal as opposed to
binary) adjusted to noon of the current day. 

DATE - Returns current date in MM.DDYYYY format to level 1.

FMTDATE - ForMatT DATE - Given a date in MM.DDYYYY format in level 1,
this routine generates a formatted string in "day, month date, year"
format.

MTHLST - MonTH LiST - Given a month and year in MM.YYYY format in
level 1, this routine generates a string whose elements are
character strings that form the calendar used by DCAL.  (It would be
a simple matter to use MTHLST to create the function PCAL - Print 
CALendar - which would print a calendar on the printer;  and from 
PCAL, one could create PACAL - Print Annual CALendar - which would
print a 12 month calendar on the printer.)

********* Begin Routines *************************************

CHKSUM
<< RCLF HEX 16 STWS
   SWAP RCL ->STR DUP
   # 0h 1 ROT SIZE 
   FOR j OVER j j SUB
     NUM R->B XOR RL
   NEXT ->STR 3 OVER
   SIZE 1 - SUB ROT
   STOF SWAP DROP
>>
DCAL
<< LCD-> SWAP MTHLST
DUP SIZE -> l s
  <<
    WHILE KEY NOT
    REPEAT 0 s 1 -
      FOR k 1 4
        FOR d l k d
+
          IF DUP s >
          THEN s -
          END GET d
DISP
        NEXT .5 WAIT
      NEXT
    END DROP ->LCD
CLMF
  >>
>>

MTHLST
<< DUP IP SWAP FP
10000 * -> mm yy
  << yy mm 1 DOW DROP
-> dow
    << "      " -> s
      << { } s MTHS
mm GET + " " + yy
->STR + s + +
"  S  M  T  W  T  F  S"
+ -> ml
        << ""
          IF dow 0 #
          THEN
            IF 1 dow
s
            THEN 1
dow
              START
"   " +
              NEXT
            END
          END 1
NDAYS mm GET
          IF mm 2 ==
yy 4 MOD 0 == AND
          THEN 1 +
          END
          FOR x " "
+
            IF x 10
<
            THEN " "
+
            END x
->STR +
          NEXT DUP
SIZE -> dl s
          << 0 s 21 /
IP
            FOR x ml
dl x 21 * 1 + DUP 20
+ SUB + 'ml' STO
            NEXT ml
" " +
          >>
        >>
      >>
    >>
  >>
>>

NDAYS
{ 31 28 31 30 31 30
31 31 30 31 30 31 }

MTHS
{ "Jan " "Feb "
"Mar " "Apr " "May "
"June" "July" "Aug "
"Sept" "Oct " "Nov "
"Dec " }

TMSTMP
<< DATE FMTDATE PR1
TIME FMTTIME PR1 3
DROPN CR CR
>>

SETTIME
<< 0 'CKOR' STO TIME
SWAP DROP HMS-> SWAP
HMS-> DUP2
  IF >
  THEN 24 +
  END SWAP - 'CKOR'
STO
>>

SETDATE
<< RCWS SWAP 64 STWS
-> d
  << DM d CJ2
707788800 * - 'DKOR'
STO
  >> STWS
>>

FMTTIME
<< RCLF 58 CHR -> t f
c
  << t 10000 * IP
10000 / 't' STO
    IF t 12 r
    THEN t 12 - 't'
STO " pm"
    ELSE " am"
    END
    IF t 1 <
    THEN t 12 + 't'
STO
    END t IP STD
->STR c + t FP 4 FIX
->STR DUP 3 4 SUB c +
SWAP 5 6 SUB + +
SWAP + f STOF
  >>
>>

CLOCK
<< RCWS 64 STWS LCD->
CLLCD
  WHILE KEY NOT
  REPEAT " " DATE
FMTDATE + 2 DISP
TIME SWAP DROP
FMTTIME "      "
SWAP + 4 DISP
  END DROP ->LCD STWS
CLMF
>>

TIME
<< TM DUP B->R
29491199 / 24 MOD
CKOR + 24 MOD ->HMS
>>

DATE
<< DM DKOR -
707788800 / JC2
>>

DM
<< TIME HMS-> 12 SWAP
- 29491200 * DUP
  IF 0 <
  THEN NEG -
  ELSE +
  END B->R
>>

FMTDATE
<< -> d
  << d DOW2 SWAP DROP
", " + MTHS d IP GET
+ " " + d FP 100 *
IP ->STR + ", " + d
100 * FP 10000 *
->STR +
  >>
>>

DAYS
{ "Sun" "Mon" "Tues"
"Weds" "Thur" "Fri"
"Sat" }

DOW2
<< CJ2 1 + 7 MOD DUP
1 + DAYS SWAP GET
>>

CJ2
<< DUP IP SWAP FP 100
* DUP IP SWAP FP
10000 * ROT ROT CJ
>>

JC2
<< JC 100 / + SWAP
1000000 / +
>>

DOW
<< CJ 1 + 7 MOD DUP 1
+ DAYS SWAP GET
>>

CJ
<<
  << 2.85 - 12 / +
  >> -> y m d yp
  << 367 y m yp EVAL
* IP y m yp EVAL IP
- .75 y m yp EVAL IP
* - d + IP .75 y m
yp EVAL 100 / IP * -
IP 1721115 +
  >>
>>

JC
<< 1721119.2 - DUP
DUP 36524.25 / IP
SWAP OVER + SWAP 4 /
IP - DUP ROT DROP
365.25 SWAP OVER /
IP SWAP OVER * SWAP
ROT ROT IP - .3 -
DUP 30.6 SWAP OVER /
IP SWAP OVER * SWAP
-> k
  << SWAP OVER - 1 +
SWAP DROP k
  >> DUP 9
  IF >
  THEN ROT 1 + SWAP
9 -
  ELSE ROT SWAP 3 +
  END ROT IP
>>

TM
<< RCWS 64 STWS
# 11CAh SYSEVAL 
SWAP STWS
>>

****** End Routines ********************
