diff options
author | Hossein <hossein@libreoffice.org> | 2022-02-25 00:22:11 +0100 |
---|---|---|
committer | Eike Rathke <erack@redhat.com> | 2022-02-25 19:12:41 +0100 |
commit | 0308e48e46cee2f56a6239c8479d26185146d74a (patch) | |
tree | dbbbfd146029268e9bb2f6129a85d905a9d41e1e /i18npool | |
parent | 9cd4da63b6ed19b71a2475fccb4ab135f0a87873 (diff) |
tdf#145759 30.6001 -> monthDaysWithoutJanFeb
30.6001 shows month days without Jan and Feb.
According to the below link, it is calcuated as (365-31-28)/10 = 30.6
but because of a floating point bug, it was used as 30.6001 as a
workaround.
"30.6001, 25 year old hack?"
https://www.hpmuseum.org/cgi-sys/cgiwrap/hpmuseum/archv011.cgi?read=31650
The value 30.6 is used as i18nutil::monthDaysWithoutJanFeb here
instead of 30.6001. The new value is ~30.60000038 which is > 30.6, so
the calculations should be correct. In order to make sure, a unit test
is added, and part of the values are checked against the values
calculated by this website:
Julian Day and Civil Date Calculator
https://core2.gsfc.nasa.gov/time/julian.html
Change-Id: I8cc7e046514dc3de652a1c37399e351cb2b614dc
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/125813
Tested-by: Jenkins
Reviewed-by: Eike Rathke <erack@redhat.com>
Diffstat (limited to 'i18npool')
-rw-r--r-- | i18npool/CppunitTest_i18npool_test_calendar.mk | 43 | ||||
-rw-r--r-- | i18npool/Module_i18npool.mk | 1 | ||||
-rw-r--r-- | i18npool/inc/calendar_hijri.hxx | 12 | ||||
-rw-r--r-- | i18npool/qa/cppunit/test_calendar.cxx | 130 | ||||
-rw-r--r-- | i18npool/source/calendar/calendar_hijri.cxx | 8 |
5 files changed, 184 insertions, 10 deletions
diff --git a/i18npool/CppunitTest_i18npool_test_calendar.mk b/i18npool/CppunitTest_i18npool_test_calendar.mk new file mode 100644 index 000000000000..85a3b2fbb576 --- /dev/null +++ b/i18npool/CppunitTest_i18npool_test_calendar.mk @@ -0,0 +1,43 @@ +# -*- Mode: makefile-gmake; tab-width: 4; indent-tabs-mode: t; fill-column: 100 -*- +# +# This file is part of the LibreOffice project. +# +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. +# + +$(eval $(call gb_CppunitTest_CppunitTest,i18npool_test_calendar)) + +$(eval $(call gb_CppunitTest_set_include,i18npool_test_calendar,\ + $$(INCLUDE) \ + -I$(SRCDIR)/i18npool/inc \ +)) + +$(eval $(call gb_CppunitTest_use_sdk_api,i18npool_test_calendar)) + +$(eval $(call gb_CppunitTest_use_libraries,i18npool_test_calendar,\ + cppu \ + cppuhelper \ + sal \ + unotest \ + i18npool \ +)) + +$(eval $(call gb_CppunitTest_use_externals,i18npool_test_calendar,\ + icui18n \ + icuuc \ + icu_headers \ +)) + +$(eval $(call gb_CppunitTest_add_exception_objects,i18npool_test_calendar,\ + i18npool/qa/cppunit/test_calendar \ +)) + +$(eval $(call gb_CppunitTest_use_ure,i18npool_test_calendar)) + +$(eval $(call gb_CppunitTest_use_components,i18npool_test_calendar,\ + i18npool/util/i18npool \ +)) + +# vim: set noet sw=4 ts=4: diff --git a/i18npool/Module_i18npool.mk b/i18npool/Module_i18npool.mk index 271c751081da..1926fdd70d82 100644 --- a/i18npool/Module_i18npool.mk +++ b/i18npool/Module_i18npool.mk @@ -43,6 +43,7 @@ $(eval $(call gb_Module_add_check_targets,i18npool,\ CppunitTest_i18npool_test_characterclassification \ CppunitTest_i18npool_test_ordinalsuffix \ CppunitTest_i18npool_test_textsearch \ + CppunitTest_i18npool_test_calendar \ CppunitTest_i18npool_defaultnumberingprovider \ )) diff --git a/i18npool/inc/calendar_hijri.hxx b/i18npool/inc/calendar_hijri.hxx index b25c4cb0b44f..c02fa31e5d94 100644 --- a/i18npool/inc/calendar_hijri.hxx +++ b/i18npool/inc/calendar_hijri.hxx @@ -36,12 +36,12 @@ private: void mapToGregorian() override; void mapFromGregorian() override; -private: - static double NewMoon(sal_Int32 n); - static void getHijri(sal_Int32 *day, sal_Int32 *month, sal_Int32 *year); - static void ToGregorian(sal_Int32 *day, sal_Int32 *month, sal_Int32 *year); - static void getGregorianDay(sal_Int32 jd, sal_Int32 *pnDay, sal_Int32 *pnMonth, sal_Int32 *pnYear); - static sal_Int32 getJulianDay(sal_Int32 day, sal_Int32 month, sal_Int32 year); +public: + static double SAL_DLLPUBLIC_EXPORT NewMoon(sal_Int32 n); + static void SAL_DLLPUBLIC_EXPORT getHijri(sal_Int32 *day, sal_Int32 *month, sal_Int32 *year); + static void SAL_DLLPUBLIC_EXPORT ToGregorian(sal_Int32 *day, sal_Int32 *month, sal_Int32 *year); + static void SAL_DLLPUBLIC_EXPORT getGregorianDay(sal_Int32 jd, sal_Int32 *pnDay, sal_Int32 *pnMonth, sal_Int32 *pnYear); + static sal_Int32 SAL_DLLPUBLIC_EXPORT getJulianDay(sal_Int32 day, sal_Int32 month, sal_Int32 year); }; } diff --git a/i18npool/qa/cppunit/test_calendar.cxx b/i18npool/qa/cppunit/test_calendar.cxx new file mode 100644 index 000000000000..a522a4a74236 --- /dev/null +++ b/i18npool/qa/cppunit/test_calendar.cxx @@ -0,0 +1,130 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ +#include <calendar_hijri.hxx> +#include <unotest/bootstrapfixturebase.hxx> + +using namespace com::sun::star; + +class TestCalendar : public test::BootstrapFixtureBase +{ +public: + void testHijriGregorian(); + void testGetGregorianJulianDay(); + + CPPUNIT_TEST_SUITE(TestCalendar); + CPPUNIT_TEST(testHijriGregorian); + CPPUNIT_TEST(testGetGregorianJulianDay); + CPPUNIT_TEST_SUITE_END(); +}; + +void TestCalendar::testHijriGregorian() +{ + // 21-7-1443 (Hijri) == 22-2-2022 (Gregorian) + sal_Int32 day = 22, month = 2, year = 2022; + i18npool::Calendar_hijri::getHijri(&day, &month, &year); + CPPUNIT_ASSERT_EQUAL(sal_Int32(21), day); + CPPUNIT_ASSERT_EQUAL(sal_Int32(7), month); + CPPUNIT_ASSERT_EQUAL(sal_Int32(1443), year); + + i18npool::Calendar_hijri::ToGregorian(&day, &month, &year); + CPPUNIT_ASSERT_EQUAL(sal_Int32(22), day); + CPPUNIT_ASSERT_EQUAL(sal_Int32(2), month); + CPPUNIT_ASSERT_EQUAL(sal_Int32(2022), year); + + // 1-1-1 (Hijri) == 15-7-622 (Gregorian) + // NOTE: The calculated date is 15-7-622, as it was with the + // previous version of i18npool::Calendar_hijri::ToGregorian() + // but in some articles, 15-7-622 is considered the equivalent date + // https://en.wikipedia.org/wiki/622 + // This article states that 15-7-622 is correct: + // "On the Origins of the Hijrī Calendar: A Multi-Faceted Perspective + // Based on the Covenants of the Prophet and Specific Date Verification" + // https://www.mdpi.com/2077-1444/12/1/42/htm + day = 15; + month = 7; + year = 622; + i18npool::Calendar_hijri::getHijri(&day, &month, &year); + CPPUNIT_ASSERT_EQUAL(sal_Int32(1), day); + CPPUNIT_ASSERT_EQUAL(sal_Int32(1), month); + CPPUNIT_ASSERT_EQUAL(sal_Int32(1), year); + + i18npool::Calendar_hijri::ToGregorian(&day, &month, &year); + CPPUNIT_ASSERT_EQUAL(sal_Int32(15), day); + CPPUNIT_ASSERT_EQUAL(sal_Int32(7), month); + CPPUNIT_ASSERT_EQUAL(sal_Int32(622), year); + + // 1-1-100 (Hijri) == 2-8-718 (Gregorian) + // https://habibur.com/hijri/100/ + day = 2; + month = 8; + year = 718; + i18npool::Calendar_hijri::getHijri(&day, &month, &year); + CPPUNIT_ASSERT_EQUAL(sal_Int32(1), day); + CPPUNIT_ASSERT_EQUAL(sal_Int32(1), month); + CPPUNIT_ASSERT_EQUAL(sal_Int32(100), year); + + i18npool::Calendar_hijri::ToGregorian(&day, &month, &year); + CPPUNIT_ASSERT_EQUAL(sal_Int32(2), day); + CPPUNIT_ASSERT_EQUAL(sal_Int32(8), month); + CPPUNIT_ASSERT_EQUAL(sal_Int32(718), year); + + // 1-1-1000 (Hijri) == 19-10-1591 (Gregorian) + // NOTE: The calculated date is 18-10-1591, but there is inconsistency + // with this website, as it states it should be 19-10-1591 + // https://habibur.com/hijri/1000/ + day = 18; + month = 10; + year = 1591; + i18npool::Calendar_hijri::getHijri(&day, &month, &year); + CPPUNIT_ASSERT_EQUAL(sal_Int32(1), day); + CPPUNIT_ASSERT_EQUAL(sal_Int32(1), month); + CPPUNIT_ASSERT_EQUAL(sal_Int32(1000), year); + + i18npool::Calendar_hijri::ToGregorian(&day, &month, &year); + CPPUNIT_ASSERT_EQUAL(sal_Int32(18), day); + CPPUNIT_ASSERT_EQUAL(sal_Int32(10), month); + CPPUNIT_ASSERT_EQUAL(sal_Int32(1591), year); + + // 1-1-2000 (Hijri) == 7-1-2562 (Gregorian) + // NOTE: The calculated date is 7-1-2562, but there is inconsistency + // with this website, as it states it should be 8-1-2562 + // https://habibur.com/hijri/2000/ + day = 7; + month = 1; + year = 2562; + i18npool::Calendar_hijri::getHijri(&day, &month, &year); + CPPUNIT_ASSERT_EQUAL(sal_Int32(1), day); + CPPUNIT_ASSERT_EQUAL(sal_Int32(1), month); + CPPUNIT_ASSERT_EQUAL(sal_Int32(2000), year); + + i18npool::Calendar_hijri::ToGregorian(&day, &month, &year); + CPPUNIT_ASSERT_EQUAL(sal_Int32(7), day); + CPPUNIT_ASSERT_EQUAL(sal_Int32(1), month); + CPPUNIT_ASSERT_EQUAL(sal_Int32(2562), year); +} + +void TestCalendar::testGetGregorianJulianDay() +{ + // Julian day for 22-2-2022 (Gregorian) == 2459633 + // https://core2.gsfc.nasa.gov/time/julian.html + sal_Int32 lJulianDay, day = 22, month = 2, year = 2022; + lJulianDay = i18npool::Calendar_hijri::getJulianDay(day, month, year); + CPPUNIT_ASSERT_EQUAL(sal_Int32(2459633), lJulianDay); + + i18npool::Calendar_hijri::getGregorianDay(lJulianDay, &day, &month, &year); + CPPUNIT_ASSERT_EQUAL(sal_Int32(22), day); + CPPUNIT_ASSERT_EQUAL(sal_Int32(2), month); + CPPUNIT_ASSERT_EQUAL(sal_Int32(2022), year); +} + +CPPUNIT_TEST_SUITE_REGISTRATION(TestCalendar); + +CPPUNIT_PLUGIN_IMPLEMENT(); + +/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ diff --git a/i18npool/source/calendar/calendar_hijri.cxx b/i18npool/source/calendar/calendar_hijri.cxx index 3d24150f8bf8..ed64c80db68c 100644 --- a/i18npool/source/calendar/calendar_hijri.cxx +++ b/i18npool/source/calendar/calendar_hijri.cxx @@ -18,7 +18,7 @@ */ #include <sal/config.h> - +#include <i18nutil/calendar.hxx> #include <cmath> #include <stdlib.h> @@ -257,10 +257,10 @@ Calendar_hijri::getGregorianDay(sal_Int32 lJulianDay, sal_Int32 *pnDay, sal_Int3 lFactorB = lFactorA + 1524; lFactorC = static_cast<tools::Long>(6680.0 + (static_cast<float>(lFactorB - 2439870) - 122.1) / 365.25); lFactorD = static_cast<tools::Long>(365 * lFactorC + (0.25 * lFactorC)); - lFactorE = static_cast<tools::Long>((lFactorB - lFactorD) / 30.6001); + lFactorE = static_cast<tools::Long>((lFactorB - lFactorD) / i18nutil::monthDaysWithoutJanFeb); /* now, pull out the day number */ - *pnDay = lFactorB - lFactorD - static_cast<tools::Long>(30.6001 * lFactorE); + *pnDay = lFactorB - lFactorD - static_cast<tools::Long>(i18nutil::monthDaysWithoutJanFeb * lFactorE); /* ...and the month, adjusting it if necessary */ *pnMonth = lFactorE - 1; @@ -298,7 +298,7 @@ Calendar_hijri::getJulianDay(sal_Int32 day, sal_Int32 month, sal_Int32 year) jm = month + 13; } - sal_Int32 intgr = static_cast<sal_Int32>(static_cast<sal_Int32>(365.25 * jy) + static_cast<sal_Int32>(30.6001 * jm) + day + 1720995 ); + sal_Int32 intgr = static_cast<sal_Int32>(static_cast<sal_Int32>(365.25 * jy) + static_cast<sal_Int32>(i18nutil::monthDaysWithoutJanFeb * jm) + day + 1720995 ); //check for switch to Gregorian calendar double const gregcal = 15 + 31 * ( 10 + 12 * 1582 ); |