summaryrefslogtreecommitdiff
path: root/tools/source/datetime
diff options
context:
space:
mode:
Diffstat (limited to 'tools/source/datetime')
-rw-r--r--tools/source/datetime/datetime.cxx117
-rw-r--r--tools/source/datetime/duration.cxx328
-rw-r--r--tools/source/datetime/tdate.cxx241
-rw-r--r--tools/source/datetime/ttime.cxx25
4 files changed, 426 insertions, 285 deletions
diff --git a/tools/source/datetime/datetime.cxx b/tools/source/datetime/datetime.cxx
index 00790ff78dd4..6f9dea26c6e8 100644
--- a/tools/source/datetime/datetime.cxx
+++ b/tools/source/datetime/datetime.cxx
@@ -17,6 +17,7 @@
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <tools/datetime.hxx>
+#include <tools/duration.hxx>
#include <rtl/math.hxx>
#include <sal/log.hxx>
@@ -94,32 +95,37 @@ sal_Int64 DateTime::GetSecFromDateTime( const Date& rDate ) const
}
}
-DateTime& DateTime::operator +=( const tools::Time& rTime )
+void DateTime::NormalizeTimeRemainderAndApply( tools::Time& rTime )
{
- tools::Time aTime = *this;
- aTime += rTime;
- sal_uInt16 nHours = aTime.GetHour();
- if ( aTime.GetTime() > 0 )
+ sal_uInt16 nHours = rTime.GetHour();
+ if ( rTime.GetTime() > 0 )
{
- while ( nHours >= 24 )
+ if (nHours >= 24)
{
- Date::operator++();
- nHours -= 24;
+ AddDays( nHours / 24 );
+ nHours %= 24;
+ rTime.SetHour( nHours );
}
- aTime.SetHour( nHours );
}
- else if ( aTime.GetTime() != 0 )
+ else if ( rTime.GetTime() != 0 )
{
- while ( nHours >= 24 )
+ if (nHours >= 24)
{
- Date::operator--();
- nHours -= 24;
+ AddDays( -static_cast<sal_Int32>(nHours) / 24 );
+ nHours %= 24;
+ rTime.SetHour( nHours );
}
Date::operator--();
- aTime = Time( 24, 0, 0 )+aTime;
+ rTime = Time( 24, 0, 0 ) + rTime;
}
- tools::Time::operator=( aTime );
+ tools::Time::operator=( rTime );
+}
+DateTime& DateTime::operator +=( const tools::Time& rTime )
+{
+ tools::Time aTime = *this;
+ aTime += rTime;
+ NormalizeTimeRemainderAndApply(aTime);
return *this;
}
@@ -127,28 +133,14 @@ DateTime& DateTime::operator -=( const tools::Time& rTime )
{
tools::Time aTime = *this;
aTime -= rTime;
- sal_uInt16 nHours = aTime.GetHour();
- if ( aTime.GetTime() > 0 )
- {
- while ( nHours >= 24 )
- {
- Date::operator++();
- nHours -= 24;
- }
- aTime.SetHour( nHours );
- }
- else if ( aTime.GetTime() != 0 )
- {
- while ( nHours >= 24 )
- {
- Date::operator--();
- nHours -= 24;
- }
- Date::operator--();
- aTime = Time( 24, 0, 0 )+aTime;
- }
- tools::Time::operator=( aTime );
+ NormalizeTimeRemainderAndApply(aTime);
+ return *this;
+}
+DateTime& DateTime::operator +=( const tools::Duration& rDuration )
+{
+ AddDays(rDuration.GetDays());
+ operator+=(rDuration.GetTime());
return *this;
}
@@ -180,27 +172,19 @@ DateTime operator -( const DateTime& rDateTime, const tools::Time& rTime )
return aDateTime;
}
+DateTime operator +( const DateTime& rDateTime, const tools::Duration& rDuration )
+{
+ DateTime aDateTime(rDateTime);
+ aDateTime.AddDays( rDuration.GetDays());
+ aDateTime += rDuration.GetTime();
+ return aDateTime;
+}
+
void DateTime::AddTime( double fTimeInDays )
{
- double fInt, fFrac;
- if ( fTimeInDays < 0.0 )
- {
- fInt = ::rtl::math::approxCeil( fTimeInDays );
- fFrac = fInt <= fTimeInDays ? 0.0 : fTimeInDays - fInt;
- }
- else
- {
- fInt = ::rtl::math::approxFloor( fTimeInDays );
- fFrac = fInt >= fTimeInDays ? 0.0 : fTimeInDays - fInt;
- }
- AddDays( sal_Int32(fInt) ); // full days
- if ( fFrac )
- {
- tools::Time aTime(0); // default ctor calls system time, we don't need that
- fFrac *= ::tools::Time::nanoSecPerDay; // time expressed in nanoseconds
- aTime.MakeTimeFromNS( static_cast<sal_Int64>(fFrac) ); // method handles negative ns
- operator+=( aTime );
- }
+ // Use Duration to diminish floating point accuracy errors.
+ tools::Duration aDuration(fTimeInDays);
+ operator+=(aDuration);
}
DateTime operator +( const DateTime& rDateTime, double fTimeInDays )
@@ -210,20 +194,21 @@ DateTime operator +( const DateTime& rDateTime, double fTimeInDays )
return aDateTime;
}
-double operator -( const DateTime& rDateTime1, const DateTime& rDateTime2 )
+tools::Duration operator -( const DateTime& rDateTime1, const DateTime& rDateTime2 )
+{
+ return tools::Duration( rDateTime2, rDateTime1);
+}
+
+// static
+double DateTime::Sub( const DateTime& rDateTime1, const DateTime& rDateTime2 )
{
- sal_Int32 nDays = static_cast<const Date&>(rDateTime1)
- - static_cast<const Date&>(rDateTime2);
- sal_Int64 nTime = rDateTime1.GetNSFromTime() - rDateTime2.GetNSFromTime();
- if ( nTime )
+ if (static_cast<const tools::Time&>(rDateTime1) != static_cast<const tools::Time&>(rDateTime2))
{
- double fTime = double(nTime);
- fTime /= ::tools::Time::nanoSecPerDay; // convert from nanoseconds to fraction
- if ( nDays < 0 && fTime > 0.0 )
- fTime = 1.0 - fTime;
- return double(nDays) + fTime;
+ // Use Duration to diminish floating point accuracy errors.
+ const tools::Duration aDuration( rDateTime2, rDateTime1);
+ return aDuration.GetInDays();
}
- return double(nDays);
+ return static_cast<const Date&>(rDateTime1) - static_cast<const Date&>(rDateTime2);
}
void DateTime::GetWin32FileDateTime( sal_uInt32 & rLower, sal_uInt32 & rUpper ) const
diff --git a/tools/source/datetime/duration.cxx b/tools/source/datetime/duration.cxx
new file mode 100644
index 000000000000..a655f016a1bc
--- /dev/null
+++ b/tools/source/datetime/duration.cxx
@@ -0,0 +1,328 @@
+/* -*- 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 <tools/duration.hxx>
+#include <tools/datetime.hxx>
+#include <rtl/math.hxx>
+#include <o3tl/safeint.hxx>
+#include <cmath>
+
+namespace tools
+{
+Duration::Duration(const ::DateTime& rStart, const ::DateTime& rEnd)
+ : mnDays(static_cast<const Date&>(rEnd) - static_cast<const Date&>(rStart))
+{
+ SetTimeDiff(rStart, rEnd);
+}
+
+Duration::Duration(const Time& rStart, const Time& rEnd)
+{
+ const sal_uInt16 nStartHour = rStart.GetHour();
+ const sal_uInt16 nEndHour = rEnd.GetHour();
+ if (nStartHour >= 24 || nEndHour >= 24)
+ {
+ Time aEnd(rEnd);
+ if (nEndHour >= 24)
+ {
+ mnDays = (nEndHour / 24) * (aEnd.GetTime() < 0 ? -1 : 1);
+ aEnd.SetHour(nEndHour % 24);
+ }
+ Time aStart(rStart);
+ if (nStartHour >= 24)
+ {
+ mnDays -= (nStartHour / 24) * (aStart.GetTime() < 0 ? -1 : 1);
+ aStart.SetHour(nStartHour % 24);
+ }
+ SetTimeDiff(aStart, aEnd);
+ }
+ else
+ {
+ SetTimeDiff(rStart, rEnd);
+ }
+}
+
+Duration::Duration(double fTimeInDays, sal_uInt64 nAccuracyEpsilonNanoseconds)
+{
+ assert(nAccuracyEpsilonNanoseconds <= Time::nanoSecPerSec - 1);
+ double fInt, fFrac;
+ if (fTimeInDays < 0.0)
+ {
+ fInt = ::rtl::math::approxCeil(fTimeInDays);
+ fFrac = fInt <= fTimeInDays ? 0.0 : fTimeInDays - fInt;
+ }
+ else
+ {
+ fInt = ::rtl::math::approxFloor(fTimeInDays);
+ fFrac = fInt >= fTimeInDays ? 0.0 : fTimeInDays - fInt;
+ }
+ mnDays = static_cast<sal_Int32>(fInt);
+ if (fFrac)
+ {
+ fFrac *= Time::nanoSecPerDay;
+ fFrac = ::rtl::math::approxFloor(fFrac);
+ sal_Int64 nNS = static_cast<sal_Int64>(fFrac);
+ const sal_Int64 nN = nNS % Time::nanoSecPerSec;
+ if (nN)
+ {
+ const sal_uInt64 nA = std::abs(nN);
+ if (nA <= nAccuracyEpsilonNanoseconds)
+ nNS -= (nNS < 0) ? -nN : nN;
+ else if (nA >= Time::nanoSecPerSec - nAccuracyEpsilonNanoseconds)
+ {
+ const sal_Int64 nD = Time::nanoSecPerSec - nA;
+ nNS += (nNS < 0) ? -nD : nD;
+ if (std::abs(nNS) >= Time::nanoSecPerDay)
+ {
+ mnDays += nNS / Time::nanoSecPerDay;
+ nNS %= Time::nanoSecPerDay;
+ }
+ }
+ }
+ maTime.MakeTimeFromNS(nNS);
+ assert(mnDays == 0 || maTime.GetTime() == 0 || (mnDays < 0) == (nNS < 0));
+ }
+}
+
+Duration::Duration(sal_Int32 nDays, const Time& rTime)
+ : mnDays(nDays)
+{
+ assert(nDays == 0 || rTime.GetTime() == 0 || (nDays < 0) == (rTime.GetTime() < 0));
+ Normalize(rTime.GetHour(), rTime.GetMin(), rTime.GetSec(), rTime.GetNanoSec(),
+ ((nDays < 0) || (rTime.GetTime() < 0)));
+}
+
+Duration::Duration(sal_Int32 nDays, sal_uInt32 nHours, sal_uInt32 nMinutes, sal_uInt32 nSeconds,
+ sal_uInt64 nNanoseconds)
+ : mnDays(nDays)
+{
+ Normalize(nHours, nMinutes, nSeconds, nNanoseconds, nDays < 0);
+}
+
+Duration::Duration(sal_Int32 nDays, sal_Int64 nTime)
+ : maTime(nTime)
+ , mnDays(nDays)
+{
+}
+
+void Duration::Normalize(sal_uInt64 nHours, sal_uInt64 nMinutes, sal_uInt64 nSeconds,
+ sal_uInt64 nNanoseconds, bool bNegative)
+{
+ if (nNanoseconds >= Time::nanoSecPerSec)
+ {
+ nSeconds += nNanoseconds / Time::nanoSecPerSec;
+ nNanoseconds %= Time::nanoSecPerSec;
+ }
+ if (nSeconds >= Time::secondPerMinute)
+ {
+ nMinutes += nSeconds / Time::secondPerMinute;
+ nSeconds %= Time::secondPerMinute;
+ }
+ if (nMinutes >= Time::minutePerHour)
+ {
+ nHours += nMinutes / Time::minutePerHour;
+ nMinutes %= Time::minutePerHour;
+ }
+ if (nHours >= Time::hourPerDay)
+ {
+ sal_Int64 nDiff = nHours / Time::hourPerDay;
+ nHours %= Time::hourPerDay;
+ bool bOverflow = false;
+ if (bNegative)
+ {
+ nDiff = -nDiff;
+ bOverflow = (nDiff < SAL_MIN_INT32);
+ bOverflow |= o3tl::checked_add(mnDays, static_cast<sal_Int32>(nDiff), mnDays);
+ if (bOverflow)
+ mnDays = SAL_MIN_INT32;
+ }
+ else
+ {
+ bOverflow = (nDiff > SAL_MAX_INT32);
+ bOverflow |= o3tl::checked_add(mnDays, static_cast<sal_Int32>(nDiff), mnDays);
+ if (bOverflow)
+ mnDays = SAL_MAX_INT32;
+ }
+ assert(!bOverflow);
+ if (bOverflow)
+ {
+ nHours = Time::hourPerDay - 1;
+ nMinutes = Time::minutePerHour - 1;
+ nSeconds = Time::secondPerMinute - 1;
+ nNanoseconds = Time::nanoSecPerSec - 1;
+ }
+ }
+ maTime = Time(nHours, nMinutes, nSeconds, nNanoseconds);
+ if (bNegative)
+ maTime = -maTime;
+ assert(mnDays == 0 || maTime.GetTime() == 0 || (mnDays < 0) == (maTime.GetTime() < 0));
+}
+
+void Duration::ApplyTime(sal_Int64 nNS)
+{
+ if (mnDays > 0 && nNS < 0)
+ {
+ --mnDays;
+ nNS = Time::nanoSecPerDay + nNS;
+ }
+ else if (mnDays < 0 && nNS > 0)
+ {
+ ++mnDays;
+ nNS = -Time::nanoSecPerDay + nNS;
+ }
+ maTime.MakeTimeFromNS(nNS);
+ assert(mnDays == 0 || maTime.GetTime() == 0 || (mnDays < 0) == (maTime.GetTime() < 0));
+}
+
+void Duration::SetTimeDiff(const Time& rStart, const Time& rEnd)
+{
+ const sal_Int64 nNS = rEnd.GetNSFromTime() - rStart.GetNSFromTime();
+ ApplyTime(nNS);
+}
+
+Duration Duration::operator-() const
+{
+ Duration aD(-mnDays, -maTime.GetTime());
+ return aD;
+}
+
+Duration& Duration::Add(const Duration& rDuration, bool& rbOverflow)
+{
+ rbOverflow = o3tl::checked_add(mnDays, rDuration.mnDays, mnDays);
+ // Duration is always normalized, time values >= 24h don't occur.
+ sal_Int64 nNS = maTime.GetNSFromTime() + rDuration.maTime.GetNSFromTime();
+ if (nNS < -Time::nanoSecPerDay)
+ {
+ rbOverflow |= o3tl::checked_sub(mnDays, sal_Int32(1), mnDays);
+ nNS += Time::nanoSecPerDay;
+ }
+ else if (nNS > Time::nanoSecPerDay)
+ {
+ rbOverflow |= o3tl::checked_add(mnDays, sal_Int32(1), mnDays);
+ nNS -= Time::nanoSecPerDay;
+ }
+ ApplyTime(nNS);
+ return *this;
+}
+
+Duration Duration::Mult(sal_Int32 nMult, bool& rbOverflow) const
+{
+ // First try a simple calculation in nanoseconds.
+ bool bBadNS = false;
+ sal_Int64 nNS;
+ sal_Int64 nDays;
+ if (o3tl::checked_multiply(static_cast<sal_Int64>(mnDays), static_cast<sal_Int64>(nMult), nDays)
+ || o3tl::checked_multiply(nDays, Time::nanoSecPerDay, nDays)
+ || o3tl::checked_multiply(maTime.GetNSFromTime(), static_cast<sal_Int64>(nMult), nNS)
+ || o3tl::checked_add(nDays, nNS, nNS))
+ {
+ bBadNS = rbOverflow = true;
+ }
+ else
+ {
+ const sal_Int64 nD = nNS / Time::nanoSecPerDay;
+ if (nD < SAL_MIN_INT32 || SAL_MAX_INT32 < nD)
+ rbOverflow = true;
+ else
+ {
+ rbOverflow = false;
+ nNS -= nD * Time::nanoSecPerDay;
+ Duration aD(static_cast<sal_Int32>(nD), 0);
+ aD.ApplyTime(nNS);
+ return aD;
+ }
+ }
+ if (bBadNS)
+ {
+ // Simple calculation in overall nanoseconds overflowed, try with
+ // individual components.
+ const sal_uInt64 nMult64 = (nMult < 0) ? -nMult : nMult;
+ do
+ {
+ sal_uInt64 nN;
+ if (o3tl::checked_multiply(static_cast<sal_uInt64>(maTime.GetNanoSec()), nMult64, nN))
+ break;
+ sal_uInt64 nS;
+ if (o3tl::checked_multiply(static_cast<sal_uInt64>(maTime.GetSec()), nMult64, nS))
+ break;
+ sal_uInt64 nM;
+ if (o3tl::checked_multiply(static_cast<sal_uInt64>(maTime.GetMin()), nMult64, nM))
+ break;
+ sal_uInt64 nH;
+ if (o3tl::checked_multiply(static_cast<sal_uInt64>(maTime.GetHour()), nMult64, nH))
+ break;
+ sal_uInt64 nD;
+ if (o3tl::checked_multiply(
+ mnDays < 0 ? static_cast<sal_uInt64>(-static_cast<sal_Int64>(mnDays))
+ : static_cast<sal_uInt64>(mnDays),
+ nMult64, nD))
+ break;
+ if (nN > Time::nanoSecPerSec)
+ {
+ const sal_uInt64 nC = nN / Time::nanoSecPerSec;
+ if (o3tl::checked_add(nS, nC, nS))
+ break;
+ nN -= nC * Time::nanoSecPerSec;
+ }
+ if (nS > Time::secondPerMinute)
+ {
+ const sal_uInt64 nC = nS / Time::secondPerMinute;
+ if (o3tl::checked_add(nM, nC, nM))
+ break;
+ nS -= nC * Time::secondPerMinute;
+ }
+ if (nM > Time::minutePerHour)
+ {
+ const sal_uInt64 nC = nM / Time::minutePerHour;
+ if (o3tl::checked_add(nH, nC, nH))
+ break;
+ nM -= nC * Time::minutePerHour;
+ }
+ if (nH > Time::hourPerDay)
+ {
+ const sal_uInt64 nC = nH / Time::hourPerDay;
+ if (o3tl::checked_add(nD, nC, nD))
+ break;
+ nH -= nC * Time::hourPerDay;
+ }
+ if (IsNegative() ? (static_cast<sal_uInt64>(SAL_MAX_INT32) + 1) < nD
+ || -static_cast<sal_Int64>(nD) < SAL_MIN_INT32
+ : SAL_MAX_INT32 < nD)
+ break;
+
+ rbOverflow = false;
+ Time aTime(nH, nM, nS, nN);
+ if (IsNegative() == (nMult < 0))
+ {
+ Duration aD(nD, aTime.GetTime());
+ return aD;
+ }
+ else
+ {
+ Duration aD(-static_cast<sal_Int64>(nD), -aTime.GetTime());
+ return aD;
+ }
+ } while (false);
+ }
+ assert(rbOverflow);
+ if (IsNegative() == (nMult < 0))
+ {
+ Duration aD(SAL_MAX_INT32, 0);
+ aD.ApplyTime(Time::nanoSecPerDay - 1);
+ return aD;
+ }
+ else
+ {
+ Duration aD(SAL_MIN_INT32, 0);
+ aD.ApplyTime(-(Time::nanoSecPerDay - 1));
+ return aD;
+ }
+}
+};
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
diff --git a/tools/source/datetime/tdate.cxx b/tools/source/datetime/tdate.cxx
index 979611333813..e20add430353 100644
--- a/tools/source/datetime/tdate.cxx
+++ b/tools/source/datetime/tdate.cxx
@@ -21,25 +21,7 @@
#include <com/sun/star/util/DateTime.hpp>
#include <systemdatetime.hxx>
-
-const sal_uInt16 aDaysInMonth[12] = { 31, 28, 31, 30, 31, 30,
- 31, 31, 30, 31, 30, 31 };
-
-// Once upon a time the number of days we internally handled was limited to
-// MAX_DAYS 3636532. That changed with a full 16-bit year.
-// Assuming the first valid positive date in a proleptic Gregorian calendar is
-// 0001-01-01, this resulted in an end date of 9957-06-26.
-// Hence we documented that years up to and including 9956 are handled.
-/* XXX: it is unclear history why this value was chosen, the representable
- * 9999-12-31 would be 3652060 days from 0001-01-01. Even 9998-12-31 to
- * distinguish from a maximum possible date would be 3651695.
- * There is connectivity/source/commontools/dbconversion.cxx that still has the
- * same value to calculate with css::util::Date */
-/* XXX can that dbconversion cope with years > 9999 or negative years at all?
- * Database fields may be limited to positive 4 digits. */
-
-const sal_Int32 MIN_DAYS = -11968265; // -32768-01-01
-const sal_Int32 MAX_DAYS = 11967900; // 32767-12-31
+#include <comphelper/date.hxx>
namespace
{
@@ -47,54 +29,6 @@ namespace
const sal_Int16 kYearMax = SAL_MAX_INT16;
const sal_Int16 kYearMin = SAL_MIN_INT16;
-// Days until start of year from zero, so month and day of month can be added.
-// year 1 => 0 days, year 2 => 365 days, ...
-// year -1 => -366 days, year -2 => -731 days, ...
-sal_Int32 ImpYearToDays( sal_Int16 nYear )
-{
- assert( nYear != 0 );
- sal_Int32 nOffset;
- sal_Int32 nYr;
- if (nYear < 0)
- {
- nOffset = -366;
- nYr = nYear + 1;
- }
- else
- {
- nOffset = 0;
- nYr = nYear - 1;
- }
- return nOffset + nYr*365 + nYr/4 - nYr/100 + nYr/400;
-}
-
-bool ImpIsLeapYear( sal_Int16 nYear )
-{
- // Leap years BCE are -1, -5, -9, ...
- // See
- // https://en.wikipedia.org/wiki/Proleptic_Gregorian_calendar#Usage
- // https://en.wikipedia.org/wiki/0_(year)#History_of_astronomical_usage
- assert( nYear != 0 );
- if (nYear < 0)
- nYear = -nYear - 1;
- return ( ( ((nYear % 4) == 0) && ((nYear % 100) != 0) ) ||
- ( (nYear % 400) == 0 ) );
-}
-
-// All callers must have sanitized or normalized month and year values!
-sal_uInt16 ImplDaysInMonth( sal_uInt16 nMonth, sal_Int16 nYear )
-{
- if ( nMonth != 2 )
- return aDaysInMonth[nMonth-1];
- else
- {
- if (ImpIsLeapYear(nYear))
- return aDaysInMonth[nMonth-1] + 1;
- else
- return aDaysInMonth[nMonth-1];
- }
-}
-
}
void Date::setDateFromDMY( sal_uInt16 nDay, sal_uInt16 nMonth, sal_Int16 nYear )
@@ -130,7 +64,7 @@ sal_uInt16 Date::GetDaysInMonth( sal_uInt16 nMonth, sal_Int16 nYear )
nMonth = 1;
else if (12 < nMonth)
nMonth = 12;
- return ImplDaysInMonth( nMonth, nYear);
+ return comphelper::date::getDaysInMonth( nMonth, nYear);
}
sal_Int32 Date::GetAsNormalizedDays() const
@@ -138,71 +72,33 @@ sal_Int32 Date::GetAsNormalizedDays() const
// This is a very common datum we often calculate from.
if (mnDate == 18991230) // 1899-12-30
{
- assert(DateToDays( GetDay(), GetMonth(), GetYear() ) == 693594);
+#ifndef NDEBUG
+ static sal_Int32 nDays = DateToDays( GetDay(), GetMonth(), GetYear());
+ assert(nDays == 693594);
+#endif
return 693594;
}
- return DateToDays( GetDay(), GetMonth(), GetYear() );
+ // Not calling comphelper::date::convertDateToDaysNormalizing() here just
+ // avoids a second check on null-date handling like above.
+ sal_uInt16 nDay = GetDay();
+ sal_uInt16 nMonth = GetMonth();
+ sal_Int16 nYear = GetYear();
+ comphelper::date::normalize( nDay, nMonth, nYear);
+ return comphelper::date::convertDateToDays( nDay, nMonth, nYear);
}
sal_Int32 Date::DateToDays( sal_uInt16 nDay, sal_uInt16 nMonth, sal_Int16 nYear )
{
- Normalize( nDay, nMonth, nYear);
-
- sal_Int32 nDays = ImpYearToDays(nYear);
- for( sal_uInt16 i = 1; i < nMonth; i++ )
- nDays += ImplDaysInMonth(i,nYear);
- nDays += nDay;
- return nDays;
+ return comphelper::date::convertDateToDaysNormalizing( nDay, nMonth, nYear);
}
static Date lcl_DaysToDate( sal_Int32 nDays )
{
- if ( nDays <= MIN_DAYS )
- return Date( 1, 1, kYearMin );
- if ( nDays >= MAX_DAYS )
- return Date( 31, 12, kYearMax );
-
- // Day 0 is -0001-12-31, day 1 is 0001-01-01
- const sal_Int16 nSign = (nDays <= 0 ? -1 : 1);
- sal_Int32 nTempDays;
- sal_Int32 i = 0;
- bool bCalc;
-
+ sal_uInt16 nDay;
+ sal_uInt16 nMonth;
sal_Int16 nYear;
- do
- {
- nYear = static_cast<sal_Int16>((nDays / 365) - (i * nSign));
- if (nYear == 0)
- nYear = nSign;
- nTempDays = nDays - ImpYearToDays(nYear);
- bCalc = false;
- if ( nTempDays < 1 )
- {
- i += nSign;
- bCalc = true;
- }
- else
- {
- if ( nTempDays > 365 )
- {
- if ( (nTempDays != 366) || !ImpIsLeapYear( nYear ) )
- {
- i -= nSign;
- bCalc = true;
- }
- }
- }
- }
- while ( bCalc );
-
- sal_uInt16 nMonth = 1;
- while ( nTempDays > ImplDaysInMonth( nMonth, nYear ) )
- {
- nTempDays -= ImplDaysInMonth( nMonth, nYear );
- ++nMonth;
- }
-
- return Date( static_cast<sal_uInt16>(nTempDays), nMonth, nYear );
+ comphelper::date::convertDaysToDate( nDays, nDay, nMonth, nYear);
+ return Date( nDay, nMonth, nYear);
}
Date::Date( DateInitSystem )
@@ -269,7 +165,7 @@ void Date::AddYears( sal_Int16 nAddYears )
}
SetYear( nYear );
- if (GetMonth() == 2 && GetDay() == 29 && !ImpIsLeapYear( nYear))
+ if (GetMonth() == 2 && GetDay() == 29 && !comphelper::date::isLeapYear( nYear))
SetDay(28);
}
@@ -306,7 +202,7 @@ sal_uInt16 Date::GetDayOfYear() const
Normalize( nDay, nMonth, nYear);
for( sal_uInt16 i = 1; i < nMonth; i++ )
- nDay += ::ImplDaysInMonth( i, nYear );
+ nDay += comphelper::date::getDaysInMonth( i, nYear );
return nDay;
}
@@ -403,13 +299,13 @@ sal_uInt16 Date::GetDaysInMonth() const
sal_Int16 nYear = GetYear();
Normalize( nDay, nMonth, nYear);
- return ImplDaysInMonth( nMonth, nYear );
+ return comphelper::date::getDaysInMonth( nMonth, nYear );
}
bool Date::IsLeapYear() const
{
sal_Int16 nYear = GetYear();
- return ImpIsLeapYear( nYear );
+ return comphelper::date::isLeapYear( nYear );
}
bool Date::IsValidAndGregorian() const
@@ -420,7 +316,7 @@ bool Date::IsValidAndGregorian() const
if ( !nMonth || (nMonth > 12) )
return false;
- if ( !nDay || (nDay > ImplDaysInMonth( nMonth, nYear )) )
+ if ( !nDay || (nDay > comphelper::date::getDaysInMonth( nMonth, nYear )) )
return false;
else if ( nYear <= 1582 )
{
@@ -437,19 +333,13 @@ bool Date::IsValidAndGregorian() const
bool Date::IsValidDate() const
{
- return IsValidDate( GetDay(), GetMonth(), GetYear());
+ return comphelper::date::isValidDate( GetDay(), GetMonth(), GetYear());
}
//static
bool Date::IsValidDate( sal_uInt16 nDay, sal_uInt16 nMonth, sal_Int16 nYear )
{
- if (nYear == 0)
- return false;
- if ( !nMonth || (nMonth > 12) )
- return false;
- if ( !nDay || (nDay > ImplDaysInMonth( nMonth, nYear )) )
- return false;
- return true;
+ return comphelper::date::isValidDate( nDay, nMonth, nYear);
}
bool Date::IsEndOfMonth() const
@@ -460,7 +350,8 @@ bool Date::IsEndOfMonth() const
//static
bool Date::IsEndOfMonth(sal_uInt16 nDay, sal_uInt16 nMonth, sal_Int16 nYear)
{
- return IsValidDate(nDay, nMonth, nYear) && ImplDaysInMonth(nMonth, nYear) == nDay;
+ return comphelper::date::isValidDate(nDay, nMonth, nYear)
+ && comphelper::date::getDaysInMonth(nMonth, nYear) == nDay;
}
void Date::Normalize()
@@ -478,83 +369,7 @@ void Date::Normalize()
//static
bool Date::Normalize( sal_uInt16 & rDay, sal_uInt16 & rMonth, sal_Int16 & rYear )
{
- if (IsValidDate( rDay, rMonth, rYear))
- return false;
-
- if (rDay == 0 && rMonth == 0 && rYear == 0)
- return false; // empty date
-
- if (rDay == 0)
- {
- if (rMonth == 0)
- ; // nothing, handled below
- else
- --rMonth;
- // Last day of month is determined at the end.
- }
-
- if (rMonth > 12)
- {
- rYear += rMonth / 12;
- rMonth = rMonth % 12;
- if (rYear == 0)
- rYear = 1;
- }
- if (rMonth == 0)
- {
- --rYear;
- if (rYear == 0)
- rYear = -1;
- rMonth = 12;
- }
-
- if (rYear < 0)
- {
- sal_uInt16 nDays;
- while (rDay > (nDays = ImplDaysInMonth( rMonth, rYear)))
- {
- rDay -= nDays;
- if (rMonth > 1)
- --rMonth;
- else
- {
- if (rYear == kYearMin)
- {
- rDay = 1;
- rMonth = 1;
- return true;
- }
- --rYear;
- rMonth = 12;
- }
- }
- }
- else
- {
- sal_uInt16 nDays;
- while (rDay > (nDays = ImplDaysInMonth( rMonth, rYear)))
- {
- rDay -= nDays;
- if (rMonth < 12)
- ++rMonth;
- else
- {
- if (rYear == kYearMax)
- {
- rDay = 31;
- rMonth = 12;
- return true;
- }
- ++rYear;
- rMonth = 1;
- }
- }
- }
-
- if (rDay == 0)
- rDay = ImplDaysInMonth( rMonth, rYear);
-
- return true;
+ return comphelper::date::normalize( rDay, rMonth, rYear);
}
void Date::AddDays( sal_Int32 nDays )
diff --git a/tools/source/datetime/ttime.cxx b/tools/source/datetime/ttime.cxx
index c6c89c934886..fcfa2e080e99 100644
--- a/tools/source/datetime/ttime.cxx
+++ b/tools/source/datetime/ttime.cxx
@@ -125,6 +125,18 @@ void tools::Time::init( sal_uInt32 nHour, sal_uInt32 nMin, sal_uInt32 nSec, sal_
nHour += nMin / minInHour;
nMin %= minInHour;
+ // 922337 * HOUR_MASK = 9223370000000000000 largest possible value, 922338
+ // would be -9223364073709551616.
+ assert(HOUR_MASK * nHour >= 0 && "use tools::Duration with days instead!");
+ if (HOUR_MASK * nHour < 0)
+ nHour = 922337;
+
+ // But as is, GetHour() retrieves only sal_uInt16. Though retrieving in
+ // nanoseconds or milliseconds might be possible this is all crap.
+ assert(nHour <= SAL_MAX_UINT16 && "use tools::Duration with days instead!");
+ if (nHour > SAL_MAX_UINT16)
+ nHour = SAL_MAX_UINT16;
+
// construct time
nTime = nNanoSec +
nSec * SEC_MASK +
@@ -412,17 +424,17 @@ Time tools::Time::GetUTCOffset()
{
nTime = time( nullptr );
localtime_r( &nTime, &aTM );
- sal_Int32 nLocalTime = mktime( &aTM );
+ auto nLocalTime = mktime( &aTM );
#if defined(__sun)
// Solaris gmtime_r() seems not to handle daylight saving time
// flags correctly
- nUTC = nLocalTime + ( aTM.tm_isdst == 0 ? timezone : altzone );
+ auto nUTC = nLocalTime + ( aTM.tm_isdst == 0 ? timezone : altzone );
#elif defined( LINUX )
// Linux mktime() seems not to handle tm_isdst correctly
- sal_Int32 nUTC = nLocalTime - aTM.tm_gmtoff;
+ auto nUTC = nLocalTime - aTM.tm_gmtoff;
#else
gmtime_r( &nTime, &aTM );
- sal_Int32 nUTC = mktime( &aTM );
+ auto nUTC = mktime( &aTM );
#endif
nCacheTicks = nTicks;
nCacheSecOffset = (nLocalTime-nUTC) / 60;
@@ -477,11 +489,12 @@ sal_uInt64 tools::Time::GetMonotonicTicks()
#if defined(_POSIX_TIMERS)
struct timespec currentTime;
clock_gettime( CLOCK_MONOTONIC, &currentTime );
- nMicroSeconds = currentTime.tv_sec * 1000 * 1000 + currentTime.tv_nsec / 1000;
+ nMicroSeconds
+ = static_cast<sal_uInt64>(currentTime.tv_sec) * 1000 * 1000 + currentTime.tv_nsec / 1000;
#else
struct timeval currentTime;
gettimeofday( &currentTime, nullptr );
- nMicroSeconds = currentTime.tv_sec * 1000 * 1000 + currentTime.tv_usec;
+ nMicroSeconds = static_cast<sal_uInt64>(currentTime.tv_sec) * 1000 * 1000 + currentTime.tv_usec;
#endif
#endif // __MACH__
return nMicroSeconds;