basic/source/runtime/methods.cxx | 57 ++++++++++++++++++++++++++++++++------- 1 file changed, 47 insertions(+), 10 deletions(-)
New commits: commit 039705ec1a215ff83ea707e5fc642d0e63f6b48d Author: Eike Rathke <er...@redhat.com> Date: Fri Apr 28 15:37:17 2017 +0200 Resolves: tdf#106956 CDateFromIso accept also YYYY-MM-DD form This is a combination of 2 commits. Resolves: tdf#106956 CDateFromIso accept also YYYY-MM-DD form Previous implementation was over-simplified and accepted all sort of malformed input to yield some arbitrary date, including longer and shorter and not strictly numeric strings. Change-Id: I2158429aeff7431f5ec5a1c9125018a5455a4730 (cherry picked from commit cdcbdf88b7f74184b532925eaf140dbf65a2cd21) Use invalid parameter error for malformed input, tdf#106956 follow-up ... instead of invalid procedure call. Change-Id: I812f4c7041db9a116e65a24afb85164b4dd498b6 (cherry picked from commit d6fd4252bf248d2872c713a1d83817a2dc88a9d2) Reviewed-on: https://gerrit.libreoffice.org/37074 Tested-by: Jenkins <c...@libreoffice.org> Reviewed-by: Michael Stahl <mst...@redhat.com> diff --git a/basic/source/runtime/methods.cxx b/basic/source/runtime/methods.cxx index 4ac3935885c7..e3ba6bb64fde 100644 --- a/basic/source/runtime/methods.cxx +++ b/basic/source/runtime/methods.cxx @@ -2051,7 +2051,7 @@ RTLFUNC(CDateToIso) } } -// Function to convert date from ISO 8601 date format +// Function to convert date from ISO 8601 date format YYYYMMDD or YYYY-MM-DD RTLFUNC(CDateFromIso) { (void)pBasic; @@ -2059,18 +2059,55 @@ RTLFUNC(CDateFromIso) if ( rPar.Count() == 2 ) { - OUString aStr = rPar.Get(1)->GetOUString(); - const sal_Int32 iMonthStart = aStr.getLength() - 4; - OUString aYearStr = aStr.copy( 0, iMonthStart ); - OUString aMonthStr = aStr.copy( iMonthStart, 2 ); - OUString aDayStr = aStr.copy( iMonthStart+2, 2 ); - - double dDate; - if( implDateSerial( (sal_Int16)aYearStr.toInt32(), - (sal_Int16)aMonthStr.toInt32(), (sal_Int16)aDayStr.toInt32(), dDate ) ) + do { + OUString aStr = rPar.Get(1)->GetOUString(); + const sal_Int32 nLen = aStr.getLength(); + if (nLen != 8 && nLen != 10) + break; + + OUString aYearStr, aMonthStr, aDayStr; + if (nLen == 8) + { + // YYYYMMDD + if (!comphelper::string::isdigitAsciiString(aStr)) + break; + + aYearStr = aStr.copy( 0, 4 ); + aMonthStr = aStr.copy( 4, 2 ); + aDayStr = aStr.copy( 6, 2 ); + } + else + { + // YYYY-MM-DD + const sal_Int32 nSep1 = aStr.indexOf('-'); + if (nSep1 != 4) + break; + const sal_Int32 nSep2 = aStr.indexOf('-', nSep1+1); + if (nSep2 != 7) + break; + + aYearStr = aStr.copy( 0, 4 ); + aMonthStr = aStr.copy( 5, 2 ); + aDayStr = aStr.copy( 8, 2 ); + if ( !comphelper::string::isdigitAsciiString(aYearStr) || + !comphelper::string::isdigitAsciiString(aMonthStr) || + !comphelper::string::isdigitAsciiString(aDayStr)) + break; + } + + double dDate; + if (!implDateSerial( (sal_Int16)aYearStr.toInt32(), + (sal_Int16)aMonthStr.toInt32(), (sal_Int16)aDayStr.toInt32(), dDate )) + break; + rPar.Get(0)->PutDate( dDate ); + + return; } + while (false); + + SbxBase::SetError( ERRCODE_SBX_BAD_PARAMETER ); } else { _______________________________________________ Libreoffice-commits mailing list libreoffice-comm...@lists.freedesktop.org https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits