(35S) calendar and date functions - Printable Version +- HP Forums (https://www.hpmuseum.org/forum) +-- Forum: HP Software Libraries (/forum-10.html) +--- Forum: General Software Library (/forum-13.html) +--- Thread: (35S) calendar and date functions (/thread-2956.html) |
(35S) calendar and date functions - Dieter - 01-28-2015 08:24 PM Since date functions and calendar programs seem to be en vogue these days, here is my contribution for the HP35s. It is essentially a small program that converts between common calendar dates and Julian day numbers. This allows for all essential date related functions:
You can now imagine that JDNs make date related calculations very easy - as they are plain integers you can use simple addition and subtraction. Dividing a JDN by 7 yields a remainder that stands for the weekday of that date. Here is a program that does all this. Just to be sure: of course RPN mode is assumed. And be sure not to confuse x<>y (exchange stack registers x and y) and x<> Y (exchange x and variable Y). Code: J001 LBL J Entering dates Any day is entered resp. displayed as follows:
Code: 25 Dec 2014 <=> 25,122014 This is what you see in FIX 6 display mode. You may also use ALL, but since trailing zeroes are ommited, dates like 1 Jul 1980 will appear as 1,07198 or 15 Aug 2000 will be displayed as 15.082. The program uses Flag 0 - please see below at the section on using a proleptic Julian calendar. The following examples assume that Flag 0 is cleared. Date calculations The program converts dates both from and to Julian day numbers (JDNs). Any integer input is considered a JDN, and everything with a fractional part is assumed to be a date in dd.mmyyyy format. The program returns two results: the JDN resp. date in X as well as the corresponding weekday in Y. Here 1 = Monday ... 7 = Sunday. So the general usage is as follows: Code: input keys output Examples Determine the weekday and Julian day number of 4 July 1979 Code: 4,071979 [XEQ] J [ENTER] 3 // Wednesday Now add 1000 days to this... Code: 1000 [+] 2445059 // JDN 1000 days later ...and convert back to day, month and year: Code: [XEQ] J [ENTER] 2 // Tuesday How many days have elapsed between 1 Jan 1900 and 31 Dec 1999? Code: 1,011900 [XEQ] J [ENTER] 1 // Monday Some useful application samples Of course you can also add a few little routines that do these calculations for you. For instance here is a DAYS+ routine that adds a number of days to a given date: Code: A001 LBL A Or use another short routine that calculates the difference between two dates: Code: D001 LBL D And finally, the following routine determines the day number within a year (many thanks to Wolfgang for the idea of adding this :-)): Code: N001 LBL N Forcing a proleptic Julian calendar Unless flag 0 is set, the program assumes that the Julian calendar was used until 4 Oct 1582 as stated in Inter gravissimas by Gregory XIII. Using this date for the switch from Julian to Gregorian is common practice. However, many regions in the world changed later, some not before the 20th century. Setting Flag 0 now enables a proleptic Julian calendar, i.e. it assumes this calendar was valid for any given date. This allows some useful applications. 1. Converting between Julian and Gregorian calendar The October Revolution started on 7 Nov 1917. What date was this in the Julian calender used those days in Russia? Code: [FLAGS] [CF] 0 // just to be sure (date is Gregorian) Johann Sebastian Bach was born on 21 March 1685 according to the Julian calendar which was still valid in his region. Later the Gregorian calendar was adopted. On which day may Bach have celebrated his birthday after the switch? Code: [FLAGS] [SF] 0 // assume Julian calendar 2. Adjusting the transition between Julian and Gregorian calendar The original program assumes the Julian calendar was valid until Thu 4 Oct 1582 which was directly followed by Fri 15 Oct 1582 as the first day of the Gregorian calendar. This is hardcoded by the constant 2299160 in line J040 and J072. It is the JDN of the last day of the Julian calendar, i.e. the JDN of 4 Oct 1582. If you live in a region where the Gregorian calendar was adopted later, you may want to reflect your local situation by using a different transition point. For instance, Britain and its colonies (including the US) switched after 2 Sep 1752 (followed by 14 Sep 1752). Adjusting the program accordingly is easy: Code: [FLAGS] [SF] 0 // assume Julian calendar Now you can replace the constant 2299160 in line J040 and J072 with 2361221 and the transition will occur on 2/14 Sep 1752. After the change you will get the following results: Code: [FLAGS] [CF] 0 // back to automatic calendar switching Now try an undefined date between these two: Code: 9,091752 [XEQ] J [ENTER] NONEXISTENT // error: date does not exist Using the unmodified program, you can try the same with 4 Oct 1582 (adding a day will yield 15 Oct 1582) and an invalid date like 10 Oct 1582 (will return 2299156 and 2299166). Checking dates The program does not perform any checks to make sure the user entered a valid date, i.e. you may enter 35 Mar 1975 without an error message although March has only 31 days. In this case the program will return the JDN for 4 Apr 1975. This can be used for a simple validity test: simply convert a date to its JDN and back again. If the result matches the original entry, the date was valid. Code: 31,091975 [XEQ] J [ENTER] 3 // Wednesday As usual, any comments, suggestions and corrections are welcome. ;-) Dieter RE: (35S) calendar and date functions - Matt Agajanian - 03-09-2018 09:55 PM Thanks (as usual), Dieter for such an extensive resource! RE: (35S) calendar and date functions - ErikN - 03-27-2019 04:59 PM This is a great program! It's the first program I entered into my new 35s, and it works like a charm. Thank you for sharing! |