Post Reply 
days-between-dates Solver equation for 17b
10-05-2018, 04:19 PM (This post was last modified: 10-07-2018 06:37 PM by Don Shepherd.)
Post: #1
days-between-dates Solver equation for 17b
Although a Solver equation for days-between-dates on the 17b series is unnecessary because that function is included in the Time menu, I thought it would be interesting to write a Solver equation for it anyhow. This equation is based upon the DBD algorithm found here:

https://alcor.concordia.ca/~gpkatch/gdat...rithm.html

Katie Wasserman made me aware of this algorithm many years ago.

To determine the number of days between two dates, you calculate the day number for each date and then subtract. In a programming language that supports subroutines, it would be easy to calculate the number of days difference. The 17b solver, however, does not support subroutines.

I figured out a way to use the sigma function to make up for this deficiency; it loops two times, the first loop calculating the day number of the earlier date (DT1) and then calculating the day number of the later date (DT2) during the second loop. Subtraction of the two is achieved by using variable B to multiply the result of the first loop by -1 and the result of the second loop by 1. The resulting sum yields the days between the two dates.

The solver equation works fine on the 17b and 17bii. It "might" work on the 17bii+ because it does not modify menu variables, but I wouldn't guarantee it will work on the +.

This is an interesting exercise in using a loop as a substitute for a subroutine.

Format of DT1 and DT2 is mm.ddyyyy

Code:

DBD=0xL(A:DT1)xL(B:-1)+

sigma(I:1:2:1:

  0xL(D:IP(100xFP(G(A)))) x
    L(M:MOD(IP(G(A))+9:12)) x
    L(Y:1E4xFP(G(A)x100)-IDIV(G(M):10)) +

          (365xG(Y)+IDIV(G(Y):4)
          -IDIV(G(Y):100)+IDIV(G(Y):400)
          +IDIV(G(M)x306+5:10)+G(D)-1)

           xG(B)

           +0xL(A:DT2)xL(B:1)

      )
Find all posts by this user
Quote this message in a reply
10-05-2018, 05:45 PM (This post was last modified: 10-05-2018 11:06 PM by Albert Chan.)
Post: #2
RE: days-between-dates Solver equation for 17b
(10-05-2018 04:19 PM)Don Shepherd Wrote:  To determine the number of days between two dates, you calculate the day number for each date and then subtract.
In a programming language that supports subroutines, it would be easy to calculate the number of days difference.
The 17b solver, however, does not support subroutines.

I figured out a way to use the sigma function to make up for this deficiency ...

Why not just eval day number for the two dates, then subtract ?
(Not as subroutine, but run the program twice)

Edit: another benefit of exposing day number is you get days-of-the-week for free.
Example, picking day number 1 for a Monday (say, Jan 1, 1, year 1) we get this:

Today 10/05/2018 => days = 736972, days % 7 = 5 => Friday
Find all posts by this user
Quote this message in a reply
10-05-2018, 07:20 PM
Post: #3
RE: days-between-dates Solver equation for 17b
I guess it would work on the 27S too, but it already has a DDAYS function to do this.

— Ian Abbott
Find all posts by this user
Quote this message in a reply
10-05-2018, 11:25 PM
Post: #4
RE: days-between-dates Solver equation for 17b
(10-05-2018 05:45 PM)Albert Chan Wrote:  Why not just eval day number for the two dates, then subtract ?
(Not as subroutine, but run the program twice)

Thanks Albert. The equation does evaluate day number for each date, then effectively subtracts by using multiplier B=1 for the later date and B=-1 for the earlier date. There is no need to run the program (equation) twice. I would never ask a user to have to run a program twice when one time should suffice.

thanks for commenting,
Don
Find all posts by this user
Quote this message in a reply
10-06-2018, 02:06 PM (This post was last modified: 10-07-2018 02:52 PM by Albert Chan.)
Post: #5
RE: days-between-dates Solver equation for 17b
Hi, Don Shepherd

Thanks for the DBD algorithm link: https://alcor.concordia.ca/~gpkatch/gdat...rithm.html
Gary Katch also explain why it work: https://alcor.concordia.ca/~gpkatch/gdate-method.html

Based on the explanation, the day number calculation (and its inverse) are only limited by machine precision.

My old day function, using system time, only work for 1970 to 2038.
I coded above in Lua (using IEEE double). Now it can reach 24 trillion years ! Smile
Find all posts by this user
Quote this message in a reply
10-06-2018, 03:27 PM (This post was last modified: 10-06-2018 08:37 PM by Albert Chan.)
Post: #6
RE: days-between-dates Solver equation for 17b
Trivia: New Year's Day for year 400k are all Saturday
Prove:
Let y = 400k, setup so that day(1/1/1) = 1, a Monday

day(12/31/y) = 365*y + y/4 - y/100 + y/400 = 146097 y / 400 = 146097 k
Since 146097 % 7 = 0, day(12/31/y) % 7 = 0, a Sunday

Travel back time 365 days (y is leap year), 1/1/y is -365 % 7 = 6, a Saturday Smile
---

With above trivia, we can extend day range furthur:

day(m/d/y) = day(m/d/(y%400)) + 146097*floor(y/400)

Or, if no overflow issue, even simpler (without divide or modulo 400):

day(m/d/y) = day(m/d/(y%10000)) + 3652425*floor(y/10000)

For days-of-week calculation, last term can be dropped (it is divisible by 7).
Find all posts by this user
Quote this message in a reply
10-06-2018, 04:01 PM
Post: #7
RE: days-between-dates Solver equation for 17b
(10-06-2018 02:06 PM)Albert Chan Wrote:  Thanks for the DBD algorithm link: https://alcor.concordia.ca/~gpkatch/gdat...rithm.html
Gary Katch also explain why it work: https://alcor.concordia.ca/~gpkatch/gdate-method.html

Thanks Albert. That is a very good algorithm that Katie first introduced me to. It is particularly easy to implement on the HP-17b since the solver includes MOD and IDIV functions.

Don
Find all posts by this user
Quote this message in a reply
10-06-2018, 11:57 PM (This post was last modified: 10-07-2018 02:11 AM by brickviking.)
Post: #8
RE: days-between-dates Solver equation for 17b
Speaking of going back in time, do these algorithms take any notice of the 11 days lost in 1753-ish? (varies between countries from 1753 to as late as 1923).

EDIT: whoops, I meant the change from Julian to Gregorian...I should have made that clear from the get-go.

(Post 302)

Regards, BrickViking
HP-50g |Casio fx-9750G+ |Casio fx-9750GII (SH4a)
Visit this user's website Find all posts by this user
Quote this message in a reply
10-07-2018, 12:17 AM
Post: #9
RE: days-between-dates Solver equation for 17b
(10-06-2018 11:57 PM)brickviking Wrote:  Speaking of going back in time, do these algorithms take any notice of the 11 days lost in 1753-ish? (varies between countries from 1753 to as late as 1923).

(Post 302)
I don't know, but I suspect not.
Find all posts by this user
Quote this message in a reply
10-07-2018, 07:14 AM (This post was last modified: 10-07-2018 08:36 AM by Dieter.)
Post: #10
RE: days-between-dates Solver equation for 17b
(10-06-2018 11:57 PM)brickviking Wrote:  Speaking of going back in time, do these algorithms take any notice of the 11 days lost in 1753-ish? (varies between countries from 1753 to as late as 1923).

The formula calculates the day number based on the rules of the Gregorian calendar. So it works for dates since 15 October 1582 (which followed directly after 4 October). For earlier dates (i.e. until 4 October 1582) a different formula (for the Julian calendar) has to be applied.

If you want a different date for the switch between Julian and Gregorian calendar, for instance the one in 1752 when the British colonies (including the US) adopted the new system, the current formula will only work for dates since then. For earlier dates the Julian formula has to be used, just as mentioned above for dates until 4 October 1582.

In simple words: there are two formulas, one for the rules according to the Julian calendar and another one for those of the Gregorian calendar. It's you choice when to switch from one to the other. The earliest possible date is 4...15 Oct 1582 because before that the new Gregorian system was not yet defined.

Further reading (with both Julian and Gregorian formulas): Wikibooks (in German).
Note: the Julian Day Number (JDN) and the value g() calculated by the formula in this thread differ by a constant offset of 1721120 (which is not required for date differences). Also the JDNs in the Wikibook formula may have a fractional part that represents the time, here 0,5 means midnight.

Edit: for simple date differences only the g() formula is required. This turns a Gregorian date into a day number. For the Julian calendar the equivalent function j() is:

Code:
function j(y,m,d)
m = (m + 9) % 12
y = y - m/10
return 365*y + y/4 + (m*306 + 5)/10 + d - 3

As usual, all "/" are integer divisions.

Check:
g(1582,10,15) = 578041
j (1582,10, 4 ) = 578040

After y has been adjusted, the difference between both calendar systems is  2 – y/100 + y/400 days. Or y/100 – y/400 – 2 days, if you prefer positive numbers.
That's the 11 days for a calendar switch in 1752, or 13 days today.

Dieter
Find all posts by this user
Quote this message in a reply
10-07-2018, 01:35 PM
Post: #11
RE: days-between-dates Solver equation for 17b
Some interesting facts about the calendar ...

Why Julius calendar had leap year every third year ?
Why August "stole" a day from February ?
After New Year's day, the year might not change ...

https://stevemorse.org/juliancalendar/julian.htm
Find all posts by this user
Quote this message in a reply
10-08-2018, 06:40 AM (This post was last modified: 10-08-2018 06:47 AM by brickviking.)
Post: #12
RE: days-between-dates Solver equation for 17b
(10-07-2018 01:35 PM)Albert Chan Wrote:  Some interesting facts about the calendar ...

(1) Why Julius calendar had leap year every third year ?
(2) Why August "stole" a day from February ?
(3) After New Year's day, the year might not change ...

https://stevemorse.org/juliancalendar/julian.htm

  1. To the best of my knowledge, the Julian calendar system ended up with leap years every four years (thank Augustus for that), regardless of if the century could be divided by four or not. This is what caused the gradual drift (an extra three leap days per four centuries doesn't sound like a lot) of Easter, compared with when the nearest full moon was; the liturgical calendar is lunar-based, so a discrepancy like this was eventually seen to in 1582 and countries eventually converted over.
  2. No idea, I don't know how that was handled. I'm a bit hazy on the finer details of the Julian calendar.
  3. Given that the year actually started in March (not January like currently), this was not really considered an issue. They simply incremented the year in March (I believe).


(Post 303)

Regards, BrickViking
HP-50g |Casio fx-9750G+ |Casio fx-9750GII (SH4a)
Visit this user's website Find all posts by this user
Quote this message in a reply
10-08-2018, 07:29 AM (This post was last modified: 10-08-2018 07:32 AM by Dieter.)
Post: #13
RE: days-between-dates Solver equation for 17b
(10-08-2018 06:40 AM)brickviking Wrote:  To the best of my knowledge, the Julian calendar system ended up with leap years every four years (thank Augustus for that), regardless of if the century could be divided by four or not.

Regardless of the century being divisible by 100 (or 400) or not. Yes, this was the rule, but in the early years it was applied incorrectly: instead of a leap year after three common years it was a leap year every three years. It took several decades until the correct rule was in use.

(10-08-2018 06:40 AM)brickviking Wrote:  Given that the year actually started in March (not January like currently), this was not really considered an issue. They simply incremented the year in March (I believe).

You can still see that in the names of the months: September, October, November and December are derived from the Latin septem = seven, octo = eight, novem = nine and decem = ten.

Dieter
Find all posts by this user
Quote this message in a reply
10-08-2018, 09:44 AM (This post was last modified: 10-08-2018 09:45 AM by StephenG1CMZ.)
Post: #14
RE: days-between-dates Solver equation for 17b
I had been told that the reason December was called Dec although it was the 12th month now was because Julius added a month, and then Augustus copied him and added another month - not that it was because years used to start in March.
If both are true, December should be called October.

Stephen Lewkowicz (G1CMZ)
https://my.numworks.com/python/steveg1cmz
Visit this user's website Find all posts by this user
Quote this message in a reply
10-08-2018, 11:01 AM
Post: #15
RE: days-between-dates Solver equation for 17b
(10-08-2018 09:44 AM)StephenG1CMZ Wrote:  I had been told that the reason December was called Dec although it was the 12th month now was because Julius added a month, and then Augustus copied him and added another month - not that it was because years used to start in March.
If both are true, December should be called October.

Nobody added a month: quintilis became iulius (July) and sextilis became augustus (August).
And maybe a day was added to augustus (taking it from february) to make it equal to iulius (uncertain).

Hereabout the year started in march, since then comes spring, and a new beginning.

Greetings,
    Massimo

-+×÷ ↔ left is right and right is wrong
Visit this user's website Find all posts by this user
Quote this message in a reply
10-08-2018, 01:23 PM (This post was last modified: 10-08-2018 01:26 PM by Albert Chan.)
Post: #16
RE: days-between-dates Solver equation for 17b
(10-08-2018 09:44 AM)StephenG1CMZ Wrote:  I had been told that the reason December was called Dec although it was the 12th month now was because Julius added a month, and then Augustus copied him and added another month - not that it was because years used to start in March.
If both are true, December should be called October.

Based on https://stevemorse.org/juliancalendar/julian.htm

There were indeed a time with ten months per year. (Romulus calendar, 753 BC)
Later, Ianuarius, Februarius, Intercalarius (leap year month) were added (Numa calendar, 713 BC)

Originally, the months added were after December, until around 450 BC.
Numa calendar were shifted, with December as last month, just like today.
Find all posts by this user
Quote this message in a reply
10-08-2018, 02:22 PM (This post was last modified: 10-08-2018 02:23 PM by Thomas Okken.)
Post: #17
RE: days-between-dates Solver equation for 17b
(10-08-2018 11:01 AM)Massimo Gnerucci Wrote:  And maybe a day was added to augustus (taking it from february) to make it equal to iulius (uncertain).

Uncertain? I always thought that this was known for certain, but I'll admit I'm no expert and not familiar with any primary sources. At least it's plausible because it explains the break in the pattern of alternating 31- and 30-day months, if you assume that August / Sextilis used to be 30 days, September 31, October 30, November 31, and December 30. Making August 31 days, and changing September through December in order to restore the alternation as much as possible, meant that one day had to be stolen from somewhere, since three months were changed from 30 days to 31, while only two months were changed from 31 days to 30. So February, which was already the odd one out at 29 days, ended up with only 28. If this isn't rigorously documented, I guess that's because the calendar that was being modified, the Julian calendar, hadn't been in force for all that long, so it wasn't like they were messing with ancient tradition -- Caesar had taken care of that already.
Visit this user's website Find all posts by this user
Quote this message in a reply
10-08-2018, 03:16 PM (This post was last modified: 10-08-2018 03:17 PM by Massimo Gnerucci.)
Post: #18
RE: days-between-dates Solver equation for 17b
(10-08-2018 02:22 PM)Thomas Okken Wrote:  
(10-08-2018 11:01 AM)Massimo Gnerucci Wrote:  And maybe a day was added to augustus (taking it from february) to make it equal to iulius (uncertain).

Uncertain? I always thought that this was known for certain, but I'll admit I'm no expert and not familiar with any primary sources. At least it's plausible because it explains the break in the pattern of alternating 31- and 30-day months, if you assume that August / Sextilis used to be 30 days, September 31, October 30, November 31, and December 30. Making August 31 days, and changing September through December in order to restore the alternation as much as possible, meant that one day had to be stolen from somewhere, since three months were changed from 30 days to 31, while only two months were changed from 31 days to 30. So February, which was already the odd one out at 29 days, ended up with only 28. If this isn't rigorously documented, I guess that's because the calendar that was being modified, the Julian calendar, hadn't been in force for all that long, so it wasn't like they were messing with ancient tradition -- Caesar had taken care of that already.

Plausible it came from February, I concur. But it seems the only source is Johannes de Sacrobosco's Computus.

Greetings,
    Massimo

-+×÷ ↔ left is right and right is wrong
Visit this user's website Find all posts by this user
Quote this message in a reply
10-08-2018, 08:46 PM (This post was last modified: 10-08-2018 08:56 PM by Albert Chan.)
Post: #19
RE: days-between-dates Solver equation for 17b
(10-07-2018 07:14 AM)Dieter Wrote:  Check:
g(1582,10,15) = 578041
j (1582,10, 4 ) = 578040

That's the 11 days for a calendar switch in 1752, or 13 days today.

Since Julian calendar "leap" faster, above two calendar systems must match in the past.

Above 10 days gap implied 15th century, two systems are 9 days apart.
Every 400 years to the past, two calendars are 3 days closer ...

Thus, two calendar match in the 15 - 9/3 * 4 = 3rd century A.D.

--> Trivia: From 3/1/0200 to 2/28/0300, g(date) == j(date)
--> Year 201 to 299 (99 years), Julian calendar match Gregorian.

Of course, this is just arithmetic. Back then, Gregorian calendar did not exist Big Grin
---

We can use above trivia to quickly find out days apart for X century:
X = 3,4,5,6, 7,8,9,10, ... => days apart = 0,1,1,2, 3,4,4,5, ...

Y = X - 3 (note: treat March 1, 100*(X-1) as start of century X)
Days apart = 3 * floor(Y/4) + floor((Y % 4) * 2/3 + 0.5)

For year 2018, Y = X - 3 = 21 - 3 = 18
Days apart = 3 * floor(18/4) + floor(11/6) = 3*4 + 1 = 13 (match above quote)

For Julian Day Number 0 (Julian date: Jan 1, 4713 B.C.)
year = 1 - 4713 = -4712
Y = X - 3 = -47 - 3 = -50
Days apart = 3 * floor(-50/4) + floor(11/6) = -39 + 1 = -38

So, JDN of 0 correspond to Gregorian date of Nov 24, 4714 B.C (shorted 7+31 days)
Find all posts by this user
Quote this message in a reply
10-08-2018, 09:13 PM
Post: #20
RE: days-between-dates Solver equation for 17b
(10-08-2018 08:46 PM)Albert Chan Wrote:  We can use above trivia to quickly find out days apart for X century:
X = 3,4,5,6, 7,8,9,10, ... => days apart = 0,1,1,2, 3,4,4,5, ...

Y = X - 3 (note: treat March 1, 100*(X-1) as start of century X)
Days apart = 3 * floor(Y/4) + floor((Y % 4) * 2/3 + 0.5)

For year 2018, Y = X - 3 = 21 - 3 = 18
Days apart = 3 * floor(18/4) + floor(11/6) = 3*4 + 1 = 13 (match above quote)

For Julian Day Number 0 (Julian date: Jan 1, 4713 B.C.)
year = 1 - 4713 = -4712
Y = X - 3 = -47 - 3 = -50
Days apart = 3 * floor(-50/4) + floor(11/6) = -39 + 1 = -38

Excuse me if I don't get the point here, but... what's wrong with the shorter and less complicated standard formula for the Julian-Gregorian difference?

Quote:...y/100 – y/400 – 2 days

So for year –4712 that's simply –48 – (–12) – 2 = –38.

Just asking. ;-)

Dieter
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)