summaryrefslogtreecommitdiff
path: root/basic
diff options
context:
space:
mode:
authorEike Rathke <erack@redhat.com>2017-04-28 15:37:17 +0200
committerAndras Timar <andras.timar@collabora.com>2017-05-04 12:48:32 +0200
commit9a4fc49db63ee4ff6783e0d059c1eacdab9c794d (patch)
treea46d27e0cc45bb50b7f97b3a7404288f8f9d5f3e /basic
parentc79f6cb82c205eab9f154c0ed5ffcdb247078556 (diff)
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 <ci@libreoffice.org> Reviewed-by: Michael Stahl <mstahl@redhat.com> (cherry picked from commit 039705ec1a215ff83ea707e5fc642d0e63f6b48d)
Diffstat (limited to 'basic')
-rw-r--r--basic/source/runtime/methods.cxx57
1 files changed, 47 insertions, 10 deletions
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
{