[Libreoffice-commits] core.git: 5 commits - include/tools tools/source

Matteo Casalin matteo.casalin at yahoo.com
Wed Oct 28 13:12:56 UTC 2015


 include/tools/date.hxx             |    6 -
 tools/source/datetime/datetime.cxx |   10 +-
 tools/source/datetime/tdate.cxx    |  158 +++++++++++--------------------------
 3 files changed, 57 insertions(+), 117 deletions(-)

New commits:
commit b75bc4b5de529f27fe56beb2d86f907411fa1e17
Author: Matteo Casalin <matteo.casalin at yahoo.com>
Date:   Wed Oct 28 12:33:53 2015 +0100

    Date: avoid temporary conversions to sal_uIntPtr
    
    Change-Id: Ib52c5d1a55d17c572fd5be9a3ce36c6de7965220

diff --git a/tools/source/datetime/tdate.cxx b/tools/source/datetime/tdate.cxx
index c27f1d0..808ac48 100644
--- a/tools/source/datetime/tdate.cxx
+++ b/tools/source/datetime/tdate.cxx
@@ -133,7 +133,7 @@ static Date lcl_DaysToDate( long nDays )
     while ( bCalc );
 
     sal_uInt16 nMonth = 1;
-    while ( (sal_uIntPtr)nTempDays > ImplDaysInMonth( nMonth, nYear ) )
+    while ( nTempDays > static_cast<long>(ImplDaysInMonth( nMonth, nYear )) )
     {
         nTempDays -= ImplDaysInMonth( nMonth, nYear );
         nMonth++;
@@ -191,7 +191,7 @@ void Date::SetYear( sal_uInt16 nNewYear )
 
 DayOfWeek Date::GetDayOfWeek() const
 {
-    return (DayOfWeek)((sal_uIntPtr)(GetAsNormalizedDays()-1) % 7);
+    return static_cast<DayOfWeek>((GetAsNormalizedDays()-1) % 7);
 }
 
 sal_uInt16 Date::GetDayOfYear() const
@@ -467,10 +467,7 @@ Date operator -( const Date& rDate, long nDays )
 
 long operator -( const Date& rDate1, const Date& rDate2 )
 {
-    sal_uIntPtr nTempDays1 = rDate1.GetAsNormalizedDays();
-    sal_uIntPtr nTempDays2 = rDate2.GetAsNormalizedDays();
-
-    return nTempDays1 - nTempDays2;
+    return rDate1.GetAsNormalizedDays() - rDate2.GetAsNormalizedDays();
 }
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
commit 1d927b23dfeef3cdc270c6c94e81e08180dabe76
Author: Matteo Casalin <matteo.casalin at yahoo.com>
Date:   Wed Oct 28 12:23:23 2015 +0100

    Date: group common code
    
    Change-Id: I4d1bf6591d54621c33dc2ff0be0ecb59f1839581

diff --git a/tools/source/datetime/tdate.cxx b/tools/source/datetime/tdate.cxx
index 811a0fd..c27f1d0 100644
--- a/tools/source/datetime/tdate.cxx
+++ b/tools/source/datetime/tdate.cxx
@@ -95,18 +95,23 @@ long Date::DateToDays( sal_uInt16 nDay, sal_uInt16 nMonth, sal_uInt16 nYear )
     return nDays;
 }
 
-static void DaysToDate( long nDays,
-                        sal_uInt16& rDay, sal_uInt16& rMonth, sal_uInt16& rYear )
+static Date lcl_DaysToDate( long nDays )
 {
+    if ( nDays >= MAX_DAYS )
+        return Date( 31, 12, 9999 );
+
+    if ( nDays <= 0 )
+        return Date( 1, 0, 0 );
+
     long    nTempDays;
     long    i = 0;
     bool    bCalc;
 
+    sal_uInt16 nYear;
     do
     {
-        nTempDays = (long)nDays;
-        rYear = (sal_uInt16)((nTempDays / 365) - i);
-        nTempDays -= ImpYearToDays(rYear);
+        nYear = (sal_uInt16)((nDays / 365) - i);
+        nTempDays = nDays - ImpYearToDays(nYear);
         bCalc = false;
         if ( nTempDays < 1 )
         {
@@ -117,7 +122,7 @@ static void DaysToDate( long nDays,
         {
             if ( nTempDays > 365 )
             {
-                if ( (nTempDays != 366) || !ImpIsLeapYear( rYear ) )
+                if ( (nTempDays != 366) || !ImpIsLeapYear( nYear ) )
                 {
                     i--;
                     bCalc = true;
@@ -127,13 +132,14 @@ static void DaysToDate( long nDays,
     }
     while ( bCalc );
 
-    rMonth = 1;
-    while ( (sal_uIntPtr)nTempDays > ImplDaysInMonth( rMonth, rYear ) )
+    sal_uInt16 nMonth = 1;
+    while ( (sal_uIntPtr)nTempDays > ImplDaysInMonth( nMonth, nYear ) )
     {
-        nTempDays -= ImplDaysInMonth( rMonth, rYear );
-        rMonth++;
+        nTempDays -= ImplDaysInMonth( nMonth, nYear );
+        nMonth++;
     }
-    rDay = (sal_uInt16)nTempDays;
+
+    return Date( static_cast<sal_uInt16>(nTempDays), nMonth, nYear );
 }
 
 Date::Date( DateInitSystem )
@@ -278,11 +284,7 @@ sal_uInt16 Date::GetWeekOfYear( DayOfWeek eStartDay,
                 long nTempDays = GetAsNormalizedDays();
 
                 nTempDays +=  6 - (GetDayOfWeek()+(7-(short)eStartDay)) % 7;
-                sal_uInt16  nDay;
-                sal_uInt16  nMonth;
-                sal_uInt16  nYear;
-                DaysToDate( nTempDays, nDay, nMonth, nYear );
-                nWeek = Date( nDay, nMonth, nYear ).GetWeekOfYear( eStartDay, nMinimumNumberOfDaysInWeek );
+                nWeek = lcl_DaysToDate( nTempDays ).GetWeekOfYear( eStartDay, nMinimumNumberOfDaysInWeek );
             }
         }
     }
@@ -409,84 +411,29 @@ bool Date::Normalize( sal_uInt16 & rDay, sal_uInt16 & rMonth, sal_uInt16 & rYear
 
 Date& Date::operator +=( long nDays )
 {
-    sal_uInt16  nDay;
-    sal_uInt16  nMonth;
-    sal_uInt16  nYear;
-
-    if (nDays == 0)
-        return *this;
-
-    long nTempDays = GetAsNormalizedDays();
-
-    nTempDays += nDays;
-    if ( nTempDays > MAX_DAYS )
-        setDateFromDMY( 31, 12, 9999 );
-    else if ( nTempDays <= 0 )
-        setDateFromDMY( 1, 100, 0 );
-    else
-    {
-        DaysToDate( nTempDays, nDay, nMonth, nYear );
-        setDateFromDMY( nDay, nMonth, nYear );
-    }
+    if (nDays != 0)
+        *this = lcl_DaysToDate( GetAsNormalizedDays() + nDays );
 
     return *this;
 }
 
 Date& Date::operator -=( long nDays )
 {
-    sal_uInt16  nDay;
-    sal_uInt16  nMonth;
-    sal_uInt16  nYear;
-
-    if (nDays == 0)
-        return *this;
-
-    long nTempDays = GetAsNormalizedDays();
-
-    nTempDays -= nDays;
-    if ( nTempDays > MAX_DAYS )
-        setDateFromDMY( 31, 12, 9999 );
-    else if ( nTempDays <= 0 )
-        setDateFromDMY( 1, 100, 0 );
-    else
-    {
-        DaysToDate( nTempDays, nDay, nMonth, nYear );
-        setDateFromDMY( nDay, nMonth, nYear );
-    }
+    if (nDays != 0)
+        *this = lcl_DaysToDate( GetAsNormalizedDays() - nDays );
 
     return *this;
 }
 
 Date& Date::operator ++()
 {
-    sal_uInt16  nDay;
-    sal_uInt16  nMonth;
-    sal_uInt16  nYear;
-    long nTempDays = GetAsNormalizedDays();
-
-    if ( nTempDays < MAX_DAYS )
-    {
-        nTempDays++;
-        DaysToDate( nTempDays, nDay, nMonth, nYear );
-        setDateFromDMY( nDay, nMonth, nYear );
-    }
-
+    *this = lcl_DaysToDate( GetAsNormalizedDays() + 1 );
     return *this;
 }
 
 Date& Date::operator --()
 {
-    sal_uInt16  nDay;
-    sal_uInt16  nMonth;
-    sal_uInt16  nYear;
-    long nTempDays = GetAsNormalizedDays();
-
-    if ( nTempDays > 1 )
-    {
-        nTempDays--;
-        DaysToDate( nTempDays, nDay, nMonth, nYear );
-        setDateFromDMY( nDay, nMonth, nYear );
-    }
+    *this = lcl_DaysToDate( GetAsNormalizedDays() - 1 );
     return *this;
 }
 
commit a3de32acc06cfcf9bb343a29c4b9854c92645f70
Author: Matteo Casalin <matteo.casalin at yahoo.com>
Date:   Wed Oct 28 11:23:36 2015 +0100

    Rename (private) Date::init and reuse it
    
    Change-Id: I3123876860cf6cce1e16c8f516f3d08fa7e15d83

diff --git a/include/tools/date.hxx b/include/tools/date.hxx
index 22cec9a..ffb425c 100644
--- a/include/tools/date.hxx
+++ b/include/tools/date.hxx
@@ -32,7 +32,7 @@ class TOOLS_DLLPUBLIC SAL_WARN_UNUSED Date
 {
 private:
     sal_uInt32      nDate;
-    void            init( sal_uInt16 nDay, sal_uInt16 nMonth, sal_uInt16 nYear )
+    void            setDateFromDMY( sal_uInt16 nDay, sal_uInt16 nMonth, sal_uInt16 nYear )
                         { nDate = (   sal_uInt32( nDay   % 100 ) ) +
                                   ( ( sal_uInt32( nMonth % 100 ) ) * 100 ) +
                                   ( ( sal_uInt32( nYear  % 10000 ) ) * 10000); }
@@ -56,11 +56,11 @@ public:
                     Date( const Date& rDate )
                         { nDate = rDate.nDate; }
                     Date( sal_uInt16 nDay, sal_uInt16 nMonth, sal_uInt16 nYear )
-                        { init(nDay, nMonth, nYear); }
+                        { setDateFromDMY(nDay, nMonth, nYear); }
                     Date( const css::util::Date& _rDate )
                     {
                         SAL_WARN_IF(_rDate.Year < 0, "tools.datetime", "Negative year in css::util::Date to ::Date conversion");
-                        init(_rDate.Day, _rDate.Month, _rDate.Year);
+                        setDateFromDMY(_rDate.Day, _rDate.Month, _rDate.Year);
                     }
                     Date( const css::util::DateTime& _rDateTime );
 
diff --git a/tools/source/datetime/tdate.cxx b/tools/source/datetime/tdate.cxx
index b957441..811a0fd 100644
--- a/tools/source/datetime/tdate.cxx
+++ b/tools/source/datetime/tdate.cxx
@@ -143,9 +143,7 @@ Date::Date( DateInitSystem )
     GetLocalTime( &aDateTime );
 
     // Combine to date
-    nDate = ((sal_uIntPtr)aDateTime.wDay) +
-            (((sal_uIntPtr)aDateTime.wMonth)*100) +
-            (((sal_uIntPtr)aDateTime.wYear)*10000);
+    setDateFromDMY( aDateTime.wDay, aDateTime.wMonth, aDateTime.wYear );
 #else
     time_t     nTmpTime;
     struct tm aTime;
@@ -156,42 +154,33 @@ Date::Date( DateInitSystem )
     // compute date
     if ( localtime_r( &nTmpTime, &aTime ) )
     {
-        nDate = ((sal_uIntPtr)aTime.tm_mday) +
-                (((sal_uIntPtr)(aTime.tm_mon+1))*100) +
-                (((sal_uIntPtr)(aTime.tm_year+1900))*10000);
+        setDateFromDMY( static_cast<sal_uInt16>(aTime.tm_mday),
+            static_cast<sal_uInt16>(aTime.tm_mon+1),
+            static_cast<sal_uInt16>(aTime.tm_year+1900) );
     }
     else
-        nDate = 1 + 100 + (((sal_uIntPtr)1900)*10000);
+        setDateFromDMY( 1, 100, 1900 );
 #endif
 }
 
 Date::Date( const ::com::sun::star::util::DateTime& rDateTime )
 {
-  init( rDateTime.Day, rDateTime.Month, rDateTime.Year );
+    setDateFromDMY( rDateTime.Day, rDateTime.Month, rDateTime.Year );
 }
 
 void Date::SetDay( sal_uInt16 nNewDay )
 {
-    sal_uIntPtr  nMonth  = GetMonth();
-    sal_uIntPtr  nYear   = GetYear();
-
-    nDate = ((sal_uIntPtr)(nNewDay%100)) + (nMonth*100) + (nYear*10000);
+    setDateFromDMY( nNewDay, GetMonth(), GetYear() );
 }
 
 void Date::SetMonth( sal_uInt16 nNewMonth )
 {
-    sal_uIntPtr  nDay    = GetDay();
-    sal_uIntPtr  nYear   = GetYear();
-
-    nDate = nDay + (((sal_uIntPtr)(nNewMonth%100))*100) + (nYear*10000);
+    setDateFromDMY( GetDay(), nNewMonth, GetYear() );
 }
 
 void Date::SetYear( sal_uInt16 nNewYear )
 {
-    sal_uIntPtr  nDay   = GetDay();
-    sal_uIntPtr  nMonth = GetMonth();
-
-    nDate = nDay + (nMonth*100) + (((sal_uIntPtr)(nNewYear%10000))*10000);
+    setDateFromDMY( GetDay(), GetMonth(), nNewYear );
 }
 
 DayOfWeek Date::GetDayOfWeek() const
@@ -364,9 +353,7 @@ bool Date::Normalize()
     if (!Normalize( nDay, nMonth, nYear))
         return false;
 
-    SetDay( nDay);
-    SetMonth( nMonth);
-    SetYear( nYear);
+    setDateFromDMY( nDay, nMonth, nYear );
 
     return true;
 }
@@ -433,13 +420,13 @@ Date& Date::operator +=( long nDays )
 
     nTempDays += nDays;
     if ( nTempDays > MAX_DAYS )
-        nDate = 31 + (12*100) + (((sal_uIntPtr)9999)*10000);
+        setDateFromDMY( 31, 12, 9999 );
     else if ( nTempDays <= 0 )
-        nDate = 1 + 100;
+        setDateFromDMY( 1, 100, 0 );
     else
     {
         DaysToDate( nTempDays, nDay, nMonth, nYear );
-        nDate = ((sal_uIntPtr)nDay) + (((sal_uIntPtr)nMonth)*100) + (((sal_uIntPtr)nYear)*10000);
+        setDateFromDMY( nDay, nMonth, nYear );
     }
 
     return *this;
@@ -458,13 +445,13 @@ Date& Date::operator -=( long nDays )
 
     nTempDays -= nDays;
     if ( nTempDays > MAX_DAYS )
-        nDate = 31 + (12*100) + (((sal_uIntPtr)9999)*10000);
+        setDateFromDMY( 31, 12, 9999 );
     else if ( nTempDays <= 0 )
-        nDate = 1 + 100;
+        setDateFromDMY( 1, 100, 0 );
     else
     {
         DaysToDate( nTempDays, nDay, nMonth, nYear );
-        nDate = ((sal_uIntPtr)nDay) + (((sal_uIntPtr)nMonth)*100) + (((sal_uIntPtr)nYear)*10000);
+        setDateFromDMY( nDay, nMonth, nYear );
     }
 
     return *this;
@@ -481,7 +468,7 @@ Date& Date::operator ++()
     {
         nTempDays++;
         DaysToDate( nTempDays, nDay, nMonth, nYear );
-        nDate = ((sal_uIntPtr)nDay) + (((sal_uIntPtr)nMonth)*100) + (((sal_uIntPtr)nYear)*10000);
+        setDateFromDMY( nDay, nMonth, nYear );
     }
 
     return *this;
@@ -498,7 +485,7 @@ Date& Date::operator --()
     {
         nTempDays--;
         DaysToDate( nTempDays, nDay, nMonth, nYear );
-        nDate = ((sal_uIntPtr)nDay) + (((sal_uIntPtr)nMonth)*100) + (((sal_uIntPtr)nYear)*10000);
+        setDateFromDMY( nDay, nMonth, nYear );
     }
     return *this;
 }
commit 24c28ac7b08848f80001d6e69595804d3ab64b47
Author: Matteo Casalin <matteo.casalin at yahoo.com>
Date:   Tue Oct 27 23:14:31 2015 +0100

    Date: use support function for mapping years to days
    
    and reduce scope for other local support functions.
    
    Change-Id: Id0d6b9a04ff67620f6dca39d62443a3f191d75ee

diff --git a/tools/source/datetime/tdate.cxx b/tools/source/datetime/tdate.cxx
index b970d51..b957441 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<long>(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 )
         {
commit 8e369625a442324d5a639fefc2481d421cd9b706
Author: Matteo Casalin <matteo.casalin at yahoo.com>
Date:   Tue Oct 27 22:32:55 2015 +0100

    Avoid intermediate cast to sal_uIntPtr
    
    Change-Id: Ia7b5f519e381318f1d1cd6d0f982d22e8bbdffa7

diff --git a/tools/source/datetime/datetime.cxx b/tools/source/datetime/datetime.cxx
index 8399415..b56d20c 100644
--- a/tools/source/datetime/datetime.cxx
+++ b/tools/source/datetime/datetime.cxx
@@ -265,10 +265,12 @@ DateTime DateTime::CreateFromWin32FileDateTime( const sal_uInt32 & rLower, const
     Date _aDate(
         (sal_uInt16)( nDays + 1 ), nMonths,
         sal::static_int_cast< sal_uInt16 >(nYears + 1601) );
-    tools::Time _aTime( sal_uIntPtr( ( aTime / ( a100nPerSecond * 60 * 60 ) ) % sal_Int64( 24 ) ),
-                 sal_uIntPtr( ( aTime / ( a100nPerSecond * 60 ) )      % sal_Int64( 60 ) ),
-                 sal_uIntPtr( ( aTime / ( a100nPerSecond ) )           % sal_Int64( 60 ) ),
-                 (aTime % a100nPerSecond) * 100 );
+    tools::Time _aTime(
+        static_cast<sal_uInt32>( ( aTime / ( a100nPerSecond * 60 * 60 ) ) % sal_Int64( 24 ) ),
+        static_cast<sal_uInt32>( ( aTime / ( a100nPerSecond * 60 ) )      % sal_Int64( 60 ) ),
+        static_cast<sal_uInt32>( ( aTime / ( a100nPerSecond ) )           % sal_Int64( 60 ) ),
+        static_cast<sal_uInt64>(aTime % a100nPerSecond) * 100
+    );
 
     return DateTime( _aDate, _aTime );
 }


More information about the Libreoffice-commits mailing list