Sfoglia il codice sorgente

Merge pull request #8341 from dcamper/6.0.0-std-date-ConvertFormatMultiple

HPCC-15165 Check for invalid date formats within Std.Date.ConvertDateFormatMultiple

Reviewed-By: Richard Chapman <rchapman@hpccsystems.com>
Richard Chapman 9 anni fa
parent
commit
4863562a9a
2 ha cambiato i file con 10 aggiunte e 3 eliminazioni
  1. 8 3
      ecllibrary/std/Date.ecl
  2. 2 0
      ecllibrary/teststd/Date/TestFormat.ecl

+ 8 - 3
ecllibrary/std/Date.ecl

@@ -601,7 +601,7 @@ EXPORT Seconds_t SecondsFromDateTimeRec(DateTime_rec datetime, BOOLEAN is_local_
  * @param date_text     The string to be converted.
  * @param format        The format of the input string.
  *                      (See documentation for strftime)
- * @return              The date that was matched in the string.  Returns 0 if failed to match 
+ * @return              The date that was matched in the string.  Returns 0 if failed to match
  *                      or if the date components match but the result is an invalid date.
  *
  * Supported characters:
@@ -799,8 +799,13 @@ EXPORT STRING ConvertTimeFormat(STRING time_text, VARSTRING from_format='%H%M%S'
  * @return              The converted string, or blank if it failed to match the format.
  */
 
-EXPORT STRING ConvertDateFormatMultiple(STRING date_text, SET OF VARSTRING from_formats, VARSTRING to_format='%Y%m%d') :=
-    DateToString(MatchDateString(date_text, from_formats), to_format);
+EXPORT STRING ConvertDateFormatMultiple(STRING date_text, SET OF VARSTRING from_formats, VARSTRING to_format='%Y%m%d') := FUNCTION
+    matchResult := MatchDateString(date_text, from_formats);
+
+    reformatResult := IF(matchResult = (Date_t)0, '', DateToString(matchResult, to_format));
+
+    RETURN reformatResult;
+END;
 
 
 /**

+ 2 - 0
ecllibrary/teststd/Date/TestFormat.ecl

@@ -65,6 +65,8 @@ EXPORT TestFormat := MODULE
 
     ASSERT(Date.ConvertDateFormatMultiple('1/31/2011',DateFormats,'%Y-%m-%d') = '2011-01-31');
 
+    ASSERT(Date.ConvertDateFormatMultiple('',DateFormats,'%Y-%m-%d') = '');
+
     ASSERT(Date.ConvertTimeFormatMultiple('123456',TimeFormats,'%H:%M:%S') = '12:34:56');
 
     ASSERT(TRUE)