- | Friday, 2020-08-14T05:23:00-05:00
- Home

[ Originally pubished in Datafile, Vol 10 No 8, December 1991, page 16. ] DAY NUMBER ROUTINES USING INTEGER ARITHMETIC Craig A. Finseth (member number ???) I have seen numerous articles on routines for converting dates to day count (typically Julian) and back again. These articles have covered various HP calculators and all used the usual HP arithmetic. However, I recently had to solve this problem in a program for the 95LX in a slightly different way: using 32-bit integer arithmetic. The results are interesting. The problem has six parts which cover conversion in each direction for 360 day, 365 day, and actual calendars. GENERAL I chose the day range to be from 1 January 1583 through 31 December 9999. This carefully avoides the Gregorian / Julian calendar question for most of the world, anyways. I also chose to count days starting from 1 January 1 in the Gregorian calendar. This is not a Julian day number, but differs by a constant. The examples are written in mostly-Basic, assuming integer arithmetic. CONVERTING FROM A DAY / MONTH / YEAR TO A DAY NUMBER Input: year (1583 - 9999), month (0 - 11), day (1 - 31) Output: dayn mdays is an array of cumulative days. mdays(0) = 0, mdays(1) = 31, mdays(2) = 31 + 28, etc. This array has thirteen entries, so mdays(12) = 365. 360 day calendar. This is easy. dayn = 360 * year + 30 * month + day - 1 365 day calendar. This is easy, too. dayn = 365 * year + mdays(month) + day - 1 Actual calendar. This starts like 365 day calendar:; dayn = 365 * year + mdays(month) + day - 1 Then Jan and Feb get previous year's leap year counts. if month <= 1 then year = year - 1 dayn = dayn + year / 4 ! add leap years dayn = dayn - year / 100 ! subtract non-leap centuries dayn = dayn + year / 400 ! add back 400 years CONVERTING FROM A DAY NUMBER TO A DAY / MONTH / YEAR Output: dayn Input: year (1583 - 9999), month (0 - 11), day (1 - 31) mdays is as above. 360 day calendar. This is easy. year = dayn / 360 month = mod(dayn, 360) / 30 day = mod(dayn, 30) + 1 365 day calendar. This starts to get tricky. year = dayn / 365 dayn = mod(dayn, 365) for month = 0 to 12 step 1 if dayn < mdays(month + 1) then goto out next month out: day = dayn - mdays(month) + 1 Actually, the loop will never get month to 12. At some point before then, the cumulative days of the next month will be greater than the day number. Remember that dayn must be in the range 0 to 364 due to the mod and that mdays(12) = 365. Actual calendar. This gets REALLY tricky and is to a large extent the entire reason for the article. We have a day number in the range 0 to 3 600 000 (= 365 * 10 000). What we want to do is to divide this number by 365,2422 to get very close to the correct year (+/- 1). We must do this with integer arithmetic, so we multiply by 10 000 and divide by 3 652 422. But multiplying a day number by 10 000 exceeds a 32-bit integer (roughly 36 000 000 000 vs. about 2 100 000 000). We have to reduce the 10 000 by a factor of 20 or so in order not to overflow. If we call the orgininal number 365,2425 and so get 10 000 and 3 652 425 as the numerator and denominator, we can remove a factor of 25 and obain 400 and 146 097. We thus no longer overflow our integer. This works but is incorrect to the tune of 3 parts in (roughly) 3 000 000 or 1 in 1 000 000. As there are only about 2 500 leap year days that can foul things up, we are still close enough. Later steps will correct any error. temp = dayn * 400 temp = temp / 146097 Now the corrections start. First, make sure that we are before the correct year. In theory, we can be off by up to one year, so let's subtract two to be on the safe side. year = temp - 2 Now, we count up until we get to the correct year. month = 0; day = 1; for i = 0 to dayn step 0 i = ToDayNumber(year, month, day, "actual") if i = dayn then return ! we are done year = year + 1 next i done: year = year - 2 This loop calculates the day number for 1 January of each year until it surpasses our day number. At that point, we have to subtract one >from the year because we have, in fact, passed the correct year and another one because we incremented the year before we tested. Of course, if the day number matches exactly, the date is for 1 January and we can stop here. We now have the correct year. On to the month and day. Subtract off the day number of 1 January so that we have a number in the range 0 to 365 (if leap year). dayn = dayn - ToDayNumber(year, month, day, "actual") Check for January. if dayn < mdays(1) then day = dayn + 1 return ! done endif ! I said this was mostly-Basic Check for up to Febuary 28. if dayn < mdays(2) then month = 1 day = dayn - mdays(1) + 1 return endif So, our date must either be 29 Febuary (if we have a leap year) or some day after that. See if we are a leap year. if year / 4 = 0 and (year / 100 <> 0 or year / 400 = 0) then ! we have a leap year if dayn = mdays(2) then month = 1 day = 29 return endif dayn = dayn - 1 ! not 29 Feb, so convert it to endif ! a non-leap year day for month = 2 to 12 step 1 if dayn < mdays(month + 1) then goto out next month out: day = dayn - mdays(month) + 1 And that's it. I hope that you found this as interesting to read as I did to write (both the program and the article).

Areas

- hp01a (HP-01A)
- hp6s (HP6S)
- hp6ssolar (HP6S Solar)
- hp8s (HP8s)
- hp9g (HP9G)
- hp9s (HP9S)
- hp10a (HP-10A)
- hp10b (HP-10B)
- hp10bii (HP10BII)
- hp10c (HP-10C)
- hp10s (HP10s)
- hp11c (HP-11C)
- hp12bii (HP-12BII)
- hp12c (HP-12C)
- hp12cpa (HP12CPA)
- hp12cpt (HP12CPt)
- hp14b (HP14B)
- hp15c (HP-15C)
- hp16c (HP-16C)
- hp17b (HP-17B)
- hp17bii (HP17BII)
- hp17bii+ (HP17BII+)
- hp18c (HP-18C)
- hp19b (HP-19B)
- hp19bii (HP19BII)
- hp19bii+ (HP19BII+)
- hp19c (HP-19C)
- hp20s (HP-20S)
- hp21a (HP-21A)
- hp21s (HP-21S)
- hp22a (HP-22A)
- hp22s (HP-22S)
- hp25a (HP-25A)
- hp25c (HP-25C)
- hp27a (HP-27A)
- hp27s (HP-27S)
- hp28c (HP-28C)
- hp28s (HP-28S)
- hp29c (HP-29C)
- hp30s (HP30S)
- hp31e (HP-31E)
- hp32e (HP-32E)
- hp32s (HP-32S)
- hp32sii (HP32SII)
- hp33c (HP-33C)
- hp33e (HP-33E)
- hp33s (HP33S)
- hp34c (HP-34C)
- hp35a (HP-35A)
- hp35s (HP35s)
- hp37e (HP-37E)
- hp38c (HP-38C)
- hp38e (HP-38E)
- hp38g (HP38G)
- hp38g (HP38G+)
- hp39g (HP39G)
- hp39g+ (HP39G+)
- hp39gs (HP39GS)
- hp40g (HP40G)
- hp40gs (HP40GS)
- hp40g+ (HP40G+)
- hp41c (HP-41C)
- hp41cv (HP-41CV)
- hp41cx (HP-41CX)
- hp41opt1 (HP-41OPT1)
- hp42s (HP-42S)
- hp45a (HP-45A)
- hp46a (HP-46A)
- hp48g (HP48G)
- hp48gii (HP48GII)
- hp48gx (HP48GX)
- hp48g+ (HP48G+)
- hp48s (HP48S)
- hp48sx (HP48SX)
- hp49g (HP49G)
- hp49g+ (HP49G+)
- hp50g (HP50G)
- hp55a (HP-55A)
- hp56a (HP-56A)
- hp65a (HP-65A)
- hp66a (HP-66A)
- hp67a (HP-67A)
- hp70a (HP-70A)
- hp71b (HP-71B)
- hp75c (HP-75C)
- hp75d (HP-75D)
- hp80a (HP-80A)
- hp81a (HP-81A)
- hp83a (HP-83A)
- hp83f (HP-83F)
- hp85a (HP-85A)
- hp85b (HP-85B)
- hp85f (HP-85F)
- hp86a (HP-86A)
- hp86b (HP-86B)
- hp87a (HP-87A)
- hp87xm (HP-87XM)
- hp91a (HP-91A)
- hp92a (HP-92A)
- hp94d (HP-94D)
- hp94e (HP-94E)
- hp94f (HP-94F)
- hp95c (HP-95C)
- hp95lx (HP95LX)
- hp95lx1mb (HP95LX1MB)
- hp97a (HP-97A)
- hp97s (HP-97S)
- hp100lx (HP100LX)
- hp100lx2mb (HP100LX2MB)

General

- Bibliography and addresses
- HP48S* \-notation
- HP Handheld Users' Conferences
- Date of introduction
- Evolution chart
- Various feature summaries
- Information file overview
- Information summary
- HP Journal articles
- Displaying 7-segment letters
- LIF file formats
- Product numbers llist
- Names and code names
- Math problems
- Jeff Brown's additional Q&A
- RPN introduction
- Serial numbers
- Series summaries
- Solver comparison
- Blank template
- A timeline of the "firsts"
- Timing tests
- HP48SX virus
- "wall of fame."
- Wlodek Mier-Jedrzejowicz articles

Craig's Articles

- Anatomy of an Application
- An ApLet for the 38G
- The Collect-94 Development System (PDF) Datafile, Vol 11 No 3
- The Collect-94 Language (PDF) Datafile, Vol 11 No 5
- Day Number Routines Using Integer Arithmetic
- Freyja - An Emacs for the HP95LX (PDF) Datafile, Vol 11 No1
- Gotos on the HP48SX (PDF) Datafile, Vol 10 No 8
- The HP-94: A Posthumous Review
- The HP-94 Story - Audax Corporation (PDF) Datafile, Vol 11 No 6
- Initial Impressions of the Aurora FN 1000 Financial Manager
- An Inside View of HHUC-95
- Notes on the CHIP Meeting
- Notes on the 1992 HP Handheld User's Conference
- A Phase-of-the-Moon Program for the 32SII
- Some Thoughts on the HP-95LX
- Other Article List

Last modified Saturday, 2012-02-25T17:29:03-06:00.