How to find difference in years between two dates?

thebjorn BjornSteinarFjeldPettersen at gmail.com
Sat Jul 29 03:57:48 EDT 2006


John Machin wrote:
> I don't understand. The examples that I showed went from the last day
> of a month to the last day of another month. [...]

Q1: is ((date-4days)+4days) == date?
Q2: is (((date-4days)+1month)+4days) == date+1month?

Ok, let's use Python'ish syntax (including numbering the days from 0
upwards, and backwards from -1, the last day of the month), you want
the last day of a month plus a month be the last day of the next
month. Simplistically, something like:

   month[-1] + 1 month == (month+1)[-1]               {last-to-last}

but that's obviously not the entire rule you want, unless 4-30 + 1
month == 5-31? So you would also like to have:

   month[i] + 1 month = (month+1)[i]                     {lock-step}

we'd like yesterday to be a day ago? So for suitable i:

   month[i] - 1 day == month[i-1]                      {yesterday-1}
   month[0] - 1 day == (month-1)[-1]                   {yesterday-2}

which leads to a natural definition for when tomorrow is:

   month[i] + 1 day == month[i+1]                       {tomorrow-1}
   month[-1] + 1 day == (month+1)[0]                    {tomorrow-2}

So far so good. Now let's count backwards:

   month[-1] - 1 day == month[-2]                  <by: yesterday-1>
   month[-2] - 1 day == month[-3]                  <by: yesterday-1>
   month[-3] - 1 day == month[-4]                  <by: yesterday-1>
   etc.

In other words, if you insist that the last day of the month is a well
defined concept and you want a day ago to be yesterday then month[-4],
the forth-to-last day of the month, is necessarily also well
defined. Having a well defined month[i], I'll apply your rules for
adding a month:

   month[-4] + 1 month == (month+1)[-4]           <by: last-to-last>

but you don't like this, because that means that e.g.:

   april[-4] + 1 month == may[-4]
   april[27] + 1 month == may[28]

which in addition to {lock-step}:

   april[27] + 1 month == may[27]

either gives an inconsistent, ill-formed, or FUZZY system (although
I would call it "regular" ;-)

My approach is simpler since it doesn't define addition, only
subtraction on valid dates, so if I switch to representing dates as
(month, day):

   (a, b) - (c, d) := a - c       iff b < d               {subtract}
                 else a - c - 1

{subtract} is irregular but well defined for all valid dates (it will
always give you an answer, and it's always the same answer ;-) :

   (2,29) - (1,31) == 0
   (3,1) - (1,31) == 2

I can add "day addition" and still be ok:

   (m,d) + 1 day := (m,d+1)                            {tomorrow-1}
   (m,-1) + 1 day := (m+1,0)                           {tomorrow-2}
   (m,d) - 1 day := (m,d-1)                           {yesterday-1}
   (m,0) - 1 day := (m-1,-1)                          {yesterday-2}

Now my system is well-formed and consitent, even though it is
irregular, and it will answer yes to Q1 above. I can't see a way of
adding month addition to this and stay consistent without enumerating
special cases for every month, so Q2 can't even be asked in my system.

> > You're entitled to your opinion.
>
> And you to yours :-)

Ok, I've explained why I hold mine... You care to do the same?

-- bjorn




More information about the Python-list mailing list