From 9950e50a0f304362f569d4dc0bd9ca30b7b6291d Mon Sep 17 00:00:00 2001 From: Eike Rathke Date: Tue, 13 Nov 2018 15:04:02 +0100 Subject: Set error also in non-UNO/UCB case file date failures, tdf#121337 follow-up And do not attempt to format an odd date in case of failure. Change-Id: I82e93f9e473f42735b6a7e7b634b14ee7f09941d Reviewed-on: https://gerrit.libreoffice.org/63331 Reviewed-by: Eike Rathke Tested-by: Jenkins --- basic/source/runtime/methods.cxx | 83 ++++++++++++++++++++++++++-------------- 1 file changed, 55 insertions(+), 28 deletions(-) (limited to 'basic') diff --git a/basic/source/runtime/methods.cxx b/basic/source/runtime/methods.cxx index b2f639e1a6c4..84202c8d4e45 100644 --- a/basic/source/runtime/methods.cxx +++ b/basic/source/runtime/methods.cxx @@ -3035,43 +3035,70 @@ void SbRtl_FileDateTime(StarBASIC *, SbxArray & rPar, bool) } else { - DirectoryItem aItem; - DirectoryItem::get( getFullPath( aPath ), aItem ); - FileStatus aFileStatus( osl_FileStatus_Mask_ModifyTime ); - aItem.getFileStatus( aFileStatus ); - TimeValue aTimeVal = aFileStatus.getModifyTime(); - oslDateTime aDT; - osl_getDateTimeFromTimeValue( &aTimeVal, &aDT ); + bool bSuccess = false; + do + { + DirectoryItem aItem; + if (DirectoryItem::get( getFullPath( aPath ), aItem ) != FileBase::E_None) + break; - aTime = tools::Time( aDT.Hours, aDT.Minutes, aDT.Seconds, aDT.NanoSeconds ); - aDate = Date( aDT.Day, aDT.Month, aDT.Year ); - } + FileStatus aFileStatus( osl_FileStatus_Mask_ModifyTime ); + if (aItem.getFileStatus( aFileStatus ) != FileBase::E_None) + break; - double fSerial = aDate.IsEmpty() ? 0 : static_cast(GetDayDiff( aDate )); - long nSeconds = aTime.GetHour(); - nSeconds *= 3600; - nSeconds += aTime.GetMin() * 60; - nSeconds += aTime.GetSec(); - double nDays = static_cast(nSeconds) / (24.0*3600.0); - fSerial += nDays; + TimeValue aTimeVal = aFileStatus.getModifyTime(); + oslDateTime aDT; + if (!osl_getDateTimeFromTimeValue( &aTimeVal, &aDT )) + // Strictly spoken this is not an i/o error but some other failure. + break; - Color* pCol; + aTime = tools::Time( aDT.Hours, aDT.Minutes, aDT.Seconds, aDT.NanoSeconds ); + aDate = Date( aDT.Day, aDT.Month, aDT.Year ); + bSuccess = true; + } + while(false); - std::shared_ptr pFormatter; - sal_uInt32 nIndex; - if( GetSbData()->pInst ) + if (!bSuccess) + StarBASIC::Error( ERRCODE_IO_GENERAL ); + } + + // An empty date shall not result in a formatted null-date (1899-12-30 + // or 1900-01-01) or even worse -0001-12-03 or some such due to how + // GetDayDiff() treats things. There should be an error set in this + // case anyway because of a missing file or other error above, but.. so + // do not even bother to use the number formatter. + OUString aRes; + if (aDate.IsEmpty()) { - pFormatter = GetSbData()->pInst->GetNumberFormatter(); - nIndex = GetSbData()->pInst->GetStdDateTimeIdx(); + aRes = "0000-00-00 00:00:00"; } else { - sal_uInt32 n; - pFormatter = SbiInstance::PrepareNumberFormatter( n, n, nIndex ); - } + double fSerial = static_cast(GetDayDiff( aDate )); + long nSeconds = aTime.GetHour(); + nSeconds *= 3600; + nSeconds += aTime.GetMin() * 60; + nSeconds += aTime.GetSec(); + double nDays = static_cast(nSeconds) / (24.0*3600.0); + fSerial += nDays; - OUString aRes; - pFormatter->GetOutputString( fSerial, nIndex, aRes, &pCol ); + Color* pCol; + + std::shared_ptr pFormatter; + sal_uInt32 nIndex; + if( GetSbData()->pInst ) + { + pFormatter = GetSbData()->pInst->GetNumberFormatter(); + nIndex = GetSbData()->pInst->GetStdDateTimeIdx(); + } + else + { + sal_uInt32 n; + pFormatter = SbiInstance::PrepareNumberFormatter( n, n, nIndex ); + } + + pFormatter->GetOutputString( fSerial, nIndex, aRes, &pCol ); + } rPar.Get(0)->PutString( aRes ); } } -- cgit