[comp.sys.handhelds] HP48SX Calendar Utilities

ebergman@isis.cs.du.edu (Eric Bergman-Terrell) (11/18/90)

                             HP 48SX Calendar Utilities
























                       Copyright (C) 1990 Eric Bergman-Terrell

                                 All Rights Reserved



           This article cannot be published without written permission of
                                     the author.






          1  Introduction


          Hewlett  Packard's  HP  48SX  programmable  calculator  has  many
          powerful commands for performing date calculations.  This article
          presents several programs that  use  the  HP 48SX's date commands
          to:

              o  Perform date / Julian day number conversions

              o  Calculate the day of the week for a given date

              o  Calculate the date of Easter

              o  Generate calendars

              o  Automatically  adjust  the  HP  48SX  clock  for  daylight
                 savings time

          The  programs  can  be  used  on  a  stand-alone  basis or can be
          incorporated into your own programs.

          To  type in and use the programs in this article, you must have a
          basic familiarity  with  the  use  of  the  HP 48SX.  For further
          information on using the HP 48SX, refer to the  HP  48SX  Owner's
          Manual.

          The programs in this article are public domain, and can be freely
          distributed.  This article, however, is copyrighted and cannot be
          published without written permission of the author.

          Please send comments,  corrections,  etc. to the address given in
          section 14 on page 17.


          2  Conventions


          Whenever possible,  the conventions of HP 48SX Owner's Manual are
          used.  Throughout this article "ORANGE" means the left-shift key,
          and "BLUE" means the right-shift key.


          3  Preparations


          Before you type in  the  programs in this article, you should set
          the calculator's date format  and  create  a subdirectory for the
          programs, as described below.








                                        - 2 -






          3.1  Date Format

          The HP 48SX supports two  date  formats,  MM/DD/YY  and DD.MM.YY.
          While the programs in  this  article  will  work with either date
          format, the examples all assume that MM/DD/YY format is in use.

          To  make  sure  that your HP 48SX is in MM/DD/YY format, type the
          following:

          42 +/- ENTER CF

          For more information about date  formats, see the HP 48SX Owner's
          Manual, Volume I pp. 441, 442.

          3.2  A Subdirectory for the Programs

          It is  a  good  idea  to save all programs in this article in one
          subdirectory.  Create  a  subdirectory named 'CAL' to contain the
          programs that you will type in:

          BLUE HOME ' CAL ORANGE MEM CRDIR

          For  more  information  about  subdirectories,  see  the  HP 48SX
          Owner's Manual, Volume I pp. 118 - 124.


          4  Converting Dates to Date Numbers


          The  HP  48SX  expresses  dates  as  real  numbers  in one of two
          formats: MM/DD/YY or DD.MM.YY (see HP 48SX Owner's Manual, Volume
          I p. 441).  Program PKD  converts  a  month, day, and year into a
          date number using the current date format.

          PKD (Convert Month, Day, and Year to Date Number)

                 +-----------------------+------------------------+
                 |       Arguments       |        Results         |
                 |-----------------------+------------------------|
                 | 3:  day               | 3:                     |
                 |-----------------------+------------------------|
                 | 2:  month             | 2:                     |
                 |-----------------------+------------------------|
                 | 1:  year              | 1:  date number        |
                 +-----------------------+------------------------+












                                        - 3 -






          Program:                    Comments:

          \<< \-> M D Y               If system flag -42 is
            \<< Y 1000000 /           clear, date format is
              IF -42 FC?              MM/DD/YY.  Otherwise
              THEN D 100 / M          date format is
              ELSE M 100 / D          DD.MM.YY
              END + +
            \>>
          \>>

          ENTER ' PKD STO (Store the program in the 'CAL' subdirectory).

          Checksum:      # 40794d
          Bytes:         138

          Example:  Convert the date 3/2/1963 to a date number.

          3 ENTER 2 ENTER 1963 ENTER PKD

          +----------------------+
          |4:                    |
          |3:                    |
          |2:                    |
          |1:            3.021963|
          +----------------------+


          5  Converting Date Numbers to Dates


          Program UPKD converts  a  date number to the corresponding month,
          day, and year.

          UPKD (Convert Date Number to Month, Day, and Year)

                 +-----------------------+------------------------+
                 |       Arguments       |        Results         |
                 |-----------------------+------------------------|
                 | 3:                    | 3:  month              |
                 |-----------------------+------------------------|
                 | 2:                    | 2:  day                |
                 |-----------------------+------------------------|
                 | 1:  date number       | 1:  year               |
                 +-----------------------+------------------------+












                                        - 4 -






          Program:                    Comments:

          \<< \-> D                   Convert date number to
            \<< D                     day, month, and year.
              IF -42 FC?
              THEN IP D FP
          100 *
              ELSE FP 100 *
          IP D
              END IP D 100 *
          FP 10000 *
            \>>
          \>>

          ENTER ' UPKD STO (Store the program in the 'CAL' subdirectory).

          Checksum:      # 31313d
          Bytes:         148.5

          Example:  Convert  the  date number 3.021963 to the corresponding
          month, day, and year.

          3.021963 ENTER UPKD

          +----------------------+
          |4:                    |
          |3:                   3|
          |2:                   2|
          |1:                1963|
          +----------------------+


          6  Converting Date Numbers to Julian Day Numbers


          The Julian day  number  for  a  date  is the number of days since
          Greenwich mean noon  January  1,  4713  B.C. and that date.  When
          dates are expressed  as  Julian day numbers date calculations are
          often much simpler.  Consequently many programs that perform date
          calculations express dates as Julian day numbers.

          The following program converts a date number to the corresponding
          Julian day number.  It is used in several of the programs in this
          article.

          JD (Date to Julian Day Number Conversion)

                 +-----------------------+------------------------+
                 |       Arguments       |        Results         |
                 |-----------------------+------------------------|
                 | 1:  date number       | 1:  Julian day number  |
                 +-----------------------+------------------------+





                                        - 5 -






          Program:                    Comments:

          \<< 1 1 1583 PKD SWAP
          DDAYS 2299238.5 +
          \>>

          ENTER ' JD STO (Store the program in the 'CAL' subdirectory).

          Checksum:      # 37291d
          Bytes:         56.5

          Example:  Calculate the Julian day number for 3/2/1963.

          3 ENTER 2 ENTER 1963 ENTER PKD JD

          +----------------------+
          |4:                    |
          |3:                    |
          |2:                    |
          |1:           2438090.5|
          +----------------------+


          7  Converting Julian Day Numbers to Date Numbers


          The  following program is the inverse of program  JD  -  given  a
          Julian day number it calculates the corresponding date number.

          CD (Julian Day Number to Calendar Date Conversion)

                 +-----------------------+------------------------+
                 |       Arguments       |        Results         |
                 |-----------------------+------------------------|
                 | 1:  Julian day number | 1:  date number        |
                 +-----------------------+------------------------+

          Program:                    Comments:

          \<< 2299238.5 - 1 1
          1583 PKD SWAP DATE+
          \>>

          ENTER ' CD STO (Store the program in the 'CAL' subdirectory).

          Checksum:      # 54424d
          Bytes:         56.5

          Example:    Calculate  the  date  number  for  Julian day  number
          2438090.5.







                                        - 6 -






          2438090.5 ENTER CD

          +----------------------+
          |4:                    |
          |3:                    |
          |2:                    |
          |1:            3.021963|
          +----------------------+


          8  Limitations of Date Calculations


          The programs in this  article  are only valid for 1583 and later.
          This limitation exists because the Gregorian calendar  system was
          adopted in 1582.


          9  Calculating the Day of the Week


          The following program calculates the day of the week for  a given
          date.

          DOW (Day of Week Calculation)

                 +-----------------------+------------------------+
                 |       Arguments       |        Results         |
                 |-----------------------+------------------------|
                 | 1:  date number       | 1:  day of week        |
                 +-----------------------+------------------------+

          Program:                    Comments:

          \<< JD 1.5 + 7 MOD 1        1 = Sunday,
          +                           2 = Monday, ...,
          \>>                         7 = Saturday

          ENTER ' DOW STO (Store the program in the 'CAL' subdirectory).

          Checksum:      # 45064d
          Bytes:         46















                                        - 7 -






          Example:  Calculate the day of the week of 3/2/1963.

          3 ENTER 2 ENTER 1963 ENTER PKD DOW

          +----------------------+
          |4:                    |
          |3:                    |
          |2:                    |
          |1:                   7|
          +----------------------+

          3/2/1963 was a Saturday.


          10  Calculating the Date of Easter


          Easter  is  defined  as the Sunday  after  the  first  full  moon
          occurring immediately after the Spring  equinox.    The following
          program computes the date of Easter for a given year.

          EASTER (Calculate the Date of Easter)

                 +-----------------------+------------------------+
                 |       Arguments       |        Results         |
                 |-----------------------+------------------------|
                 | 1:  year              | 1:  date of Easter     |
                 +-----------------------+------------------------+





























                                        - 8 -






          Program:                    Comments:

          \<< 0 0 0 0 0 0 0 0 0       There is a great deal
          0 0 0 0 \-> Y A B C D       of astronomy hidden
          E F G H I K L M N           in these simple
            \<< Y 19 MOD 'A'          calculations!
          STO Y 100 / IP 'B'
          STO Y 100 MOD 'C'           This program is based
          STO B 4 / 'D' STO B         on a method given in
          4 MOD 'E' STO B 8 +         "Astronomical Formulae
          25 / IP 'F' STO B F         for Calculators" (see
          - 1 + 3 / IP 'G'            section 13 on page
          STO 19 A * B + D -          16).
          G - 15 + 30 MOD 'H'
          STO C 4 / IP 'I'
          STO C 4 MOD 'K' STO
          32 2 E * + 2 I * +
          H - K - 7 MOD 'L'
          STO A 11 H * + 22 L
          * + 451 / IP 'M'
          STO H L + 7 M * -
          114 + DUP SWAP 31 /
          IP 'N' STO 31 MOD 1
          + N SWAP Y PKD
            \>>
          \>>

          ENTER ' EASTER STO (Store the program in the 'CAL' subdirectory).

          Checksum:      # 25112d
          Bytes:         695

          Example:  Calculate the date of Easter for 1990.

          1990 ENTER EASTER

          +----------------------+
          |4:                    |
          |3:                    |
          |2:                    |
          |1:             4.15199|
          +----------------------+

          In 1990, Easter occurs on April 15.


          11  Generating Calendars


          Section 11.2  on  page  12  and  section  11.3 on page 13 contain
          programs for generating calendars.    The  calendar  programs use
          some support programs, which are given below.





                                        - 9 -






          11.1  Support Programs

          11.1.1  Calculating the Number of Days in a Month

          The  following  program  calculates the number of days in a given
          month.

          DIM (Number of Days in a Month)

                 +-----------------------+------------------------+
                 |       Arguments       |        Results         |
                 |-----------------------+------------------------|
                 | 2:  month             | 2:                     |
                 |-----------------------+------------------------|
                 | 1:  year              | 1:  # of days in month |
                 +-----------------------+------------------------+

          Program:                    Comments:

          \<< \-> M Y                 Calculate date of the
            \<< M 1 Y PKD             first day of the next
              IF M 12 <               month, incrementing
              THEN M 1 + 1 Y          the year if necessary.
              ELSE 1 1 Y 1 +          Then calculate the
              END PKD DDAYS           number of days between
            \>>                       the dates.
          \>>

          ENTER ' DIM STO (Store the program in the 'CAL' subdirectory).

          Checksum:      # 19326d
          Bytes:         129.5

          Example:  Program DIM is used in section 11.2 on page 12.

          11.1.2  Displaying Stack Contents

          Program  DSTK  takes  all  character  strings  on  the stack  and
          displays them on the display screen.

          DSTK (Display Stack Contents)

                 +-----------------------+------------------------+
                 |       Arguments       |        Results         |
                 |-----------------------+------------------------|
                 |     item 1            |                        |
                 |-----------------------+------------------------|
                 |     ...               |                        |
                 |-----------------------+------------------------|
                 | 2:  item N            | 2:                     |
                 |-----------------------+------------------------|
                 | 1:  print flag        | 1:                     |
                 +-----------------------+------------------------+




                                       - 10 -






          If print flag is 0, the  stack  contents will be displayed on the
          display screen.  If print flag  is  1, the stack contents will be
          printed on the printer.

          Program:                    Comments:

          \<< { } \-> P L
            \<< CLLCD -37 CF          Clear display screen.
          DEPTH \->LIST 'L' STO       Place stack in list.
          1 L SIZE
              FOR I L I GET I         Display each element
          DISP                        of list.
                IF P                  if print flag is 1...
                THEN L I GET
          PR1 CR DROP
                END
              NEXT
              IF P NOT                Wait for user to press
              THEN 0 WAIT             any key.
          DROP                        Remove key value from
              END                     stack.
            \>>
          \>>

          ENTER ' DSTK STO (Store the program in the 'CAL' subdirectory).

          Checksum:      # 20574d
          Bytes:         173

          Example:  Program DSTK is used in section 11.2 on page 12.

          11.1.3  Padding a Character String

          The  following program adds spaces to the left  hand  side  of  a
          character string until the string reaches the desired length.

          PAD (Pad a Character String)

                 +-----------------------+------------------------+
                 |       Arguments       |        Results         |
                 |-----------------------+------------------------|
                 | 2:  character string  | 2:                     |
                 |-----------------------+------------------------|
                 | 1:  desired length    | 1:  padded string      |
                 +-----------------------+------------------------+












                                       - 11 -






          Program:                    Comments:

          \<< \-> S W
            \<< S
              WHILE DUP SIZE          While not at desired
          W <                         length, add space to
              REPEAT " " SWAP         beginning of string.
          +
              END
            \>>
          \>>

          ENTER ' PAD STO (Store the program in the 'CAL' subdirectory).

          Checksum:      # 44762d
          Bytes:         74

          Example:  Program PAD is used in section 11.2 on page 12.

          11.2  Generating a One Month Calendar

          The following program generates a calendar for a  given  month on
          the display screen or on a printer.

          MCAL (One Month Calendar)

                 +-----------------------+------------------------+
                 |       Arguments       |        Results         |
                 |-----------------------+------------------------|
                 | 3:  month             | 3:                     |
                 |-----------------------+------------------------|
                 | 2:  year              | 2:                     |
                 |-----------------------+------------------------|
                 | 1:  print flag        | 1:                     |
                 +-----------------------+------------------------+

          Note:  the stack must be empty before MCAL is run.

          If print flag is 0, the calendar will be displayed on the display
          screen.   If print flag is 1, the calendar will be printed on the
          printer.
















                                       - 12 -






          Program:                    Comments:

          \<< "" 0 \-> M Y P S B
            \<< STD
          " SU  M TU  W TH  F SA"     Label the columns.
          M 1 Y PKD 'B' STO
          "" B DOW 1 - 3 *            Start first day under
          PAD 'S' STO 1 M Y           correct column.
          DIM                         For each day of month,
              START S B UPKD          place each day under
          DROP SWAP DROP \->STR       correct column.
          3 PAD + 'S' STO
                IF B DOW 7 ==         If Saturday, go to
                THEN S "" 'S'         next line after day
          STO                         number is displayed.
                END B 1 DATE+         Advance to next day.
          'B' STO
              NEXT S P DSTK           Display calendar
            \>>                       on display screen
          \>>                         or printer.

          ENTER ' MCAL STO (Store the program in the 'CAL' subdirectory).

          Checksum:      # 9559d
          Bytes:         320

          Example:    Generate  a calendar for January, 1990 on the display
          screen.

          BLUE CLR 1 ENTER 1990 ENTER 0 ENTER MCAL

          +----------------------+
          | SU  M TU  W TH  F SA |
          |     1  2  3  4  5  6 |
          |  7  8  9 10 11 12 13 |
          | 14 15 16 17 18 19 20 |
          | 21 22 23 24 25 26 27 |
          | 28 29 30 31          |
          +----------------------+

          11.3  Generating a One Year Calendar

          Program YCAL generates a  one  year calendar on a printer (not on
          the display screen).

          YCAL (One Year Calendar)

                 +-----------------------+------------------------+
                 |       Arguments       |        Results         |
                 |-----------------------+------------------------|
                 | 1:  year              | 1:                     |
                 +-----------------------+------------------------+

          Note:  the stack must be empty before YCAL is run.



                                       - 13 -






          Program:                    Comments:

          \<< \-> Y
            \<< 1 12                  For each month...
              FOR M M \->STR          Print month/year.
          "/" + Y \->STR + PR1
          CR DROP M Y 1 MCAL          Print calendar for
          CR CR                       current month.
              NEXT
            \>>
          \>>

          ENTER ' YCAL STO (Store the program in the 'CAL' subdirectory).

          Checksum:      # 21199d
          Bytes:         109.5

          Example:  Print a calendar for 1990.

          BLUE CLR 1990 ENTER YCAL

          The calendar will be printed on your printer.


          12  Daylight Savings Time


          The daylight savings adjustment program uses one support program,
          which is given below:

          12.1  Support Program

          SETCLK (Set Clock One Hour Forward or Backward)

                 +-----------------------+------------------------+
                 |       Arguments       |        Results         |
                 |-----------------------+------------------------|
                 | 1:  increment         | 1:                     |
                 +-----------------------+------------------------+

          Program:                    Comments:

          \<< 8192 * 3600 *           Set clock forward
          CLKADJ DATE                 or back.
          FINDALARM DELALARM          Delete alarm.
          \>>

          ENTER ' SETCLK STO (Store the program in the 'CAL' subdirectory).

          Checksum:      # 26754d
          Bytes:         56.5

          Example:  Program SETCLK is used in section 12.2 on page 14.




                                       - 14 -






          12.2  Daylight Savings Time Adjustment Program

          Unless you live  in  a  state  that does not use daylight savings
          time, you must set your clocks ahead one hour on the first Sunday
          of  April  and  set  them  back  one  hour on the last Sunday  in
          October.  The same applies to the clock in your HP 48SX, which is
          not automatically adjusted for daylight savings time.

          The following program will automatically adjust your calculator's
          clock twice a  year,  every  year.   While this program will only
          work for U.S.A. daylight savings time, it can be modified to work
          elsewhere.

          DST (Automatic Daylight Savings Time Adjustment)

                 +-----------------------+------------------------+
                 |       Arguments       |        Results         |
                 |-----------------------+------------------------|
                 | 1:                    | 1:                     |
                 +-----------------------+------------------------+

          Note:    the calculator does NOT need  to  be  on  when  daylight
          savings begins or ends in order for the clock  adjustment  to  be
          done.

































                                       - 15 -






          Program:                    Comments:

          \<< 0 0 \-> Y D
            \<< DATE 100 * FP         Get current year.
          10000 * 'Y' STO 4 1         Start on April 1 and
          Y PKD 'D' STO               search for first Sunday.
              WHILE D DOW 1 \=/
              REPEAT D 1
          DATE+ 'D' STO
              END                     If daylight savings
              IF DATE D <             has not started this
              THEN D 2                year, schedule a program
                \<< 1 CAL             to set the clock forward
          SETCLK                      one hour at 2:00 on the
                \>> 0 4 \->LIST       first Sunday of April.
          STOALARM DROP
              END 10 31 Y PKD         Start on October 31 and
          'D' STO                     search for last Sunday
              WHILE D DOW 1 \=/       backwards.
              REPEAT D -1
          DATE+ 'D' STO
              END                     If daylight savings
              IF DATE D <             has not ended this
              THEN D 2                year, schedule a program
                \<< -1 CAL            to set the clock back
          SETCLK                      one hour at 2:00 on
                \>> 0 4 \->LIST       last Sunday of October.
          STOALARM DROP
              END 1 1 Y 1 +           Schedule THIS program
          PKD 1                       to run again next year
              \<< CAL DST 0 CAL       so that the clock will
          SETCLK                      always be automatically
              \>> 0 4 \->LIST         adjusted for daylight
          STOALARM DROP               savings time.
            \>>
          \>>

          ENTER ' DST STO (Store the program in the 'CAL' subdirectory).

          Checksum:      # 59525d
          Bytes:         479

          Example:  Run the DST program.

          DST


          13  Further Reading


          The following books give methods for performing date calculations
          in   addition   to  providing  an   introduction   to   astronomy
          programming.




                                       - 16 -






          [1]            Jean    Meeus,    "Astronomical    Formulae    for
                         Calculators", 4th ed., 1988 (Willmann-Bell)

          [2]            Peter  Duffett-Smith,  "Practical  Astronomy  with
                         your   Calculator",   3rd   ed.,  1988  (Cambridge
                         University Press)


          14  About the Author


          Eric Bergman-Terrell is an author of articles about astronomy and
          computer science, as well as  the program "Astronomy Lab" for IBM
          PC compatible computers.  He can be reached at:

          Personal MicroCosms
          8547 E. Arapahoe Road Suite J-147
          Greenwood Village, CO  80112  USA







































                                       - 17 -












                                    Contents


                  1  Introduction  . . . . . . . . . . . . . . . . . 2
                  2  Conventions . . . . . . . . . . . . . . . . . . 2
                  3  Preparations  . . . . . . . . . . . . . . . . . 2
                     3.1  Date Format  . . . . . . . . . . . . . . . 3
                     3.2  A Subdirectory for the Programs  . . . . . 3
                  4  Converting Dates to Date Numbers  . . . . . . . 3
                  5  Converting Date Numbers to Dates  . . . . . . . 4
                  6  Converting Date Numbers to Julian Day Numbers . 5
                  7  Converting Julian Day Numbers to Date Numbers . 6
                  8  Limitations of Date Calculations  . . . . . . . 7
                  9  Calculating the Day of the Week . . . . . . . . 7
                  10  Calculating the Date of Easter . . . . . . . . 8
                  11  Generating Calendars . . . . . . . . . . . . . 9
                     11.1  Support Programs  . . . . . . . . . . .  10
                        11.1.1  Calculating the Number of Days in a
                                Month  . . . . . . . . . . . . . .  10
                        11.1.2  Displaying Stack Contents  . . . .  10
                        11.1.3  Padding a Character String . . . .  11
                     11.2  Generating a One Month Calendar . . . .  12
                     11.3  Generating a One Year Calendar  . . . .  13
                  12  Daylight Savings Time  . . . . . . . . . . .  14
                     12.1  Support Program . . . . . . . . . . . .  14
                     12.2  Daylight Savings Time Adjustment
                           Program . . . . . . . . . . . . . . . .  15
                  13  Further Reading  . . . . . . . . . . . . . .  16
                  14  About the Author . . . . . . . . . . . . . .  17






















                                          i


ebergman@isis.cs.du.edu (Eric Bergman-Terrell) (11/27/90)

Did anyone get "HP48SX Calendar Utilities" which I posted about a week ago?
I don't know if my postings to comp.sys.handhelds are making it beyond my
local node.  Please let me know if it made it to your part of the world!

Thanks,


Terrell

EBERBERS%yubgef51@pucc.PRINCETON.EDU (11/28/90)

      I haven't received anything like that.

            Zarko Berberski           EBERBERS@YUBGEF51.bitnet

vaps0pb@prism.gatech.edu (Phillip Bridges) (11/29/90)

I didn't get the Calender Utilities here at GT, but there is the small chance that I simply missed it.


....Phil.....

kenw@col.hp.com (Ken Wyatt) (11/30/90)

I got it ok.....