[REVIEWED 3-5-1][PATCH] Fix leap year calculation

Lionel Elie Mamane lionel at mamane.lu
Wed Feb 29 05:56:18 PST 2012


On Tue, Feb 28, 2012 at 10:10:10PM -0500, Kohei Yoshida wrote:

> From 0666b5dca1a210ce7abc61a522a59c48661fe664 Mon Sep 17 00:00:00 2001
> From: Kohei Yoshida <kohei.yoshida at suse.com>
> Date: Tue, 28 Feb 2012 22:01:52 -0500
> Subject: [PATCH] Correctly calculate leap year.

> With the old code, year 2000 would not be a leap year, but it actually
> is.  With this, Calc correctly loads cell with date value of 2000-2-29.
> ---
>  sax/source/tools/converter.cxx |    2 +-
>  1 files changed, 1 insertions(+), 1 deletions(-)
> 
> diff --git a/sax/source/tools/converter.cxx b/sax/source/tools/converter.cxx
> index eb8cc74..95f6494 100644
> --- a/sax/source/tools/converter.cxx
> +++ b/sax/source/tools/converter.cxx
> @@ -1304,7 +1304,7 @@ readDateTimeComponent(const ::rtl::OUString & rString,
>  static bool lcl_isLeapYear(const sal_uInt32 nYear)
>  {
>      return ((nYear % 4) == 0)
> -        && !(((nYear % 100) == 0) || ((nYear % 400) == 0));
> +        && (((nYear % 100) != 0) || ((nYear % 400) == 0));
>  }
>  
>  static sal_uInt16

OK for master, libreoffice-3-5, libreoffice-3-5-1.

It seems to me that what was originally intended was:

   return ((nYear % 4) == 0)
       &&  ( !((nYear % 100) == 0) || ((nYear % 400) == 0) );

That is, the bug is an switch of parenthesis and negation. Your
version is strictly equivalent and maybe a bit more readable, so good
to go.

-- 
Lionel


More information about the LibreOffice mailing list