Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2009-12

Possible bug in return value of MonthsBetween function in unit dateutils

6 messages · 4 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Funky Beast

Hi,

When trying to get the number of months between the following dates: 01-06-2009 and 01-08-2008

MonthsBetween returns: 9 months.

That is clearly wrong (fundamentally counting with my fingers yields 10 months).
Looking at the function in dateutils.inc, I copied the function implementation to do a test.
Now, if I strip away Trunc() from the function, the result is: 9.98767967. Trunc() simply strips away the decimals behind, which returns: 9.

I'm wondering if Round() should be used instead of Trunc().

Regards,
Funky Beast.

Vincent Snijders
to Funky Beast
Funky Beast wrote 16 lines
Hi, When trying to get the number of months between the following dates: 01-06-2009 and 01-08-2008 MonthsBetween returns: 9 months. That is clearly wrong (fundamentally counting with my fingers yields 10 months). Looking at the function in dateutils.inc, I copied the function implementation to do a test. Now, if I strip away Trunc() from the function, the result is: 9.98767967. Trunc() simply strips away the decimals behind, which returns: 9. I'm wondering if Round() should be used instead of Trunc().

For such easy question, I use google:
http://lmgtfy.com/?q=monthsbetween

http://www.freepascal.org/docs-html/rtl/dateutils/monthsbetween.html states that "This number is an approximation, based on an average number of days of 30.4375 per month". 9 is approximately 10, so the is behavior you see is as documented ;-)

Vincent

Funky Beast
to Vincent Snijders
Vincent Snijders wrote 33 lines
Funky Beast schreef: > Hi, > > When trying to get the number of months between the following dates: > 01-06-2009 and 01-08-2008 > > MonthsBetween returns: 9 months. > > That is clearly wrong (fundamentally counting with my fingers yields > 10 months). > Looking at the function in dateutils.inc, I copied the function > implementation to do a test. > Now, if I strip away Trunc() from the function, the result is: > 9.98767967. > Trunc() simply strips away the decimals behind, which returns: 9. > > I'm wondering if Round() should be used instead of Trunc(). > For such easy question, I use google: http://lmgtfy.com/?q=monthsbetween http://www.freepascal.org/docs-html/rtl/dateutils/monthsbetween.html states that "This number is an approximation, based on an average number of days of 30.4375 per month". 9 is approximately 10, so the is behavior you see is as documented ;-) Vincent _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal

I did read that, but I'm just wondering isn't 9.98767967 more approximate to 10 than 9?

Regards,
Funky Beast

Henry Vermaak
to Funky Beast

2009/12/10 Funky Beast :

I did read that, but I'm just wondering isn't 9.98767967 more approximate to 10 than 9?

How do you round a month? It makes sense that the function only gives full months, imo. Either way, I think this is d€lphi compatible and at least documented.

Henry

Marco van de Voort
to Henry Vermaak

In our previous episode, Henry Vermaak said:

Show quoted text 7 lines
2009/12/10 Funky Beast : > > I did read that, but I'm just wondering isn't 9.98767967 more approximate to 10 than 9? How do you round a month? It makes sense that the function only gives full months, imo. Either way, I think this is d?lphi compatible and at least documented.

Yes. Keep in mind that you wouldn't want 16 days difference being rounded up to a full month.

Funky Beast
to Marco van de Voort
Marco van de Voort wrote 13 lines
In our previous episode, Henry Vermaak said: > 2009/12/10 Funky Beast : >> I did read that, but I'm just wondering isn't 9.98767967 more approximate to 10 than 9? > How do you round a month? It makes sense that the function only gives > full months, imo. Either way, I think this is d?lphi compatible and > at least documented. Yes. Keep in mind that you wouldn't want 16 days difference being rounded up to a full month. _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal

HeHe.....

But I loose 29 days from the above when it yields 9 instead of 10. Anyway, its just my precision engineering habits kicking in. I was just consolidating our average production output from the above 2 dates and the numbers looks funny (and too positive to be true).

Regards,
Funky Beast