diff options
author | Eike Rathke <erack@redhat.com> | 2023-06-20 15:07:59 +0200 |
---|---|---|
committer | Eike Rathke <erack@redhat.com> | 2023-06-20 16:52:23 +0200 |
commit | d3d77d99e3e6f05b232923edc2ea23e513a721d5 (patch) | |
tree | 5ee7854674b23430a5019e86e0d96f7d3406af5f /tools | |
parent | 1ff817faad79952e6271bab178d41cae2a0d9ecd (diff) |
Use tools::Duration in friend double operator-() if DateTime has Time set
Change-Id: Iff3e2aad596fdfd1530c46cdeff28b44e5142517
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/153327
Reviewed-by: Eike Rathke <erack@redhat.com>
Tested-by: Jenkins
Diffstat (limited to 'tools')
-rw-r--r-- | tools/source/datetime/datetime.cxx | 15 |
1 files changed, 5 insertions, 10 deletions
diff --git a/tools/source/datetime/datetime.cxx b/tools/source/datetime/datetime.cxx index ec2c53673b17..bb4c1ff173a5 100644 --- a/tools/source/datetime/datetime.cxx +++ b/tools/source/datetime/datetime.cxx @@ -196,18 +196,13 @@ DateTime operator +( const DateTime& rDateTime, double fTimeInDays ) double operator -( 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 |