From 24c28ac7b08848f80001d6e69595804d3ab64b47 Mon Sep 17 00:00:00 2001 From: Matteo Casalin Date: Tue, 27 Oct 2015 23:14:31 +0100 Subject: Date: use support function for mapping years to days and reduce scope for other local support functions. Change-Id: Id0d6b9a04ff67620f6dca39d62443a3f191d75ee --- tools/source/datetime/tdate.cxx | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) (limited to 'tools/source') diff --git a/tools/source/datetime/tdate.cxx b/tools/source/datetime/tdate.cxx index b970d51da5ba..b957441864f1 100644 --- a/tools/source/datetime/tdate.cxx +++ b/tools/source/datetime/tdate.cxx @@ -31,6 +31,15 @@ static const sal_uInt16 aDaysInMonth[12] = { 31, 28, 31, 30, 31, 30, #define MAX_DAYS 3636532 +namespace +{ + +inline long ImpYearToDays( sal_uInt16 nYear ) +{ + const long nYr(static_cast(nYear) - 1); + return nYr*365 + nYr/4 - nYr/100 + nYr/400; +} + inline bool ImpIsLeapYear( sal_uInt16 nYear ) { return ( ( ((nYear % 4) == 0) && ((nYear % 100) != 0) ) || @@ -51,6 +60,8 @@ inline sal_uInt16 ImplDaysInMonth( sal_uInt16 nMonth, sal_uInt16 nYear ) } } +} + // static sal_uInt16 Date::GetDaysInMonth( sal_uInt16 nMonth, sal_uInt16 nYear ) { @@ -75,12 +86,9 @@ long Date::GetAsNormalizedDays() const long Date::DateToDays( sal_uInt16 nDay, sal_uInt16 nMonth, sal_uInt16 nYear ) { - long nDays; - Normalize( nDay, nMonth, nYear); - nDays = ((sal_uIntPtr)nYear-1) * 365; - nDays += ((nYear-1) / 4) - ((nYear-1) / 100) + ((nYear-1) / 400); + long nDays = ImpYearToDays(nYear); for( sal_uInt16 i = 1; i < nMonth; i++ ) nDays += ImplDaysInMonth(i,nYear); nDays += nDay; @@ -98,8 +106,7 @@ static void DaysToDate( long nDays, { nTempDays = (long)nDays; rYear = (sal_uInt16)((nTempDays / 365) - i); - nTempDays -= ((sal_uIntPtr)rYear-1) * 365; - nTempDays -= ((rYear-1) / 4) - ((rYear-1) / 100) + ((rYear-1) / 400); + nTempDays -= ImpYearToDays(rYear); bCalc = false; if ( nTempDays < 1 ) { -- cgit