Selaa lähdekoodia

HPCC-22791 Std.Date: Supply default day number when parsing a string

When parsing a string into a Std.Date.Date_t value, there is no requirement to supply a parse format any part of a date (year, month, or day).  When a parse format is missing from one of those parts, a sensible default value needs to be provided.  All other parts of both Std.Date.Date_t and Std.Date.Time_t had default values, but day number within a date did not.

Signed-off-by: Dan S. Camper <dan.camper@lexisnexisrisk.com>
Dan S. Camper 5 vuotta sitten
vanhempi
commit
14d36a7f7d

+ 1 - 2
ecllibrary/std/Date.ecl

@@ -621,8 +621,7 @@ EXPORT Seconds_t SecondsFromDateTimeRec(DateTime_rec datetime, BOOLEAN is_local_
 
 
 /**
- * Converts a string to a Date_t using the relevant string format.  The resulting
- * date must be representable within the Gregorian calendar after the year 1600.
+ * Converts a string to a Date_t using the relevant string format.
  *
  * @param date_text     The string to be converted.
  * @param format        The format of the input string.

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

@@ -23,6 +23,8 @@ EXPORT TestFormat := MODULE
     ASSERT(Date.FromStringToDate('1December1', '%d%b%Y') = 00011201, CONST);
     ASSERT(Date.FromStringToDate('1970-02-01', '%Y-%m-%d') = 19700201, CONST);
     ASSERT(Date.FromStringToDate('', '%Y-%m-%d') = 0, CONST); // HPCC-16780; Invalid input date
+    ASSERT(Date.FromStringToDate('', '') = 19000101, CONST); // HPCC-22791; Supply default day
+    ASSERT(Date.FromStringToDate('201909', '%Y%m') = 20190901, CONST); // HPCC-22791; Supply default day
 
     ASSERT(Date.FromStringToTime('12:34:56', '%H:%M:%S') = 123456, CONST);
 

+ 2 - 0
plugins/stringlib/stringlib.cpp

@@ -1357,6 +1357,7 @@ STRINGLIB_API unsigned STRINGLIB_CALL slStringToDate(size32_t lenS, const char *
 {
     struct tm tm;
     memset(&tm, 0, sizeof(tm));
+    tm.tm_mday = 1;
     if (simple_strptime(lenS, s, fmtin, &tm))
         return makeDate(tm);
     return 0;
@@ -1382,6 +1383,7 @@ STRINGLIB_API unsigned STRINGLIB_CALL slMatchDate(size32_t lenS, const char * s,
         const char * curFormat = formats+off;
         
         memset(&tm, 0, sizeof(tm));
+        tm.tm_mday = 1;
         if (simple_strptime(lenS, s, curFormat, &tm))
             return makeDate(tm);
         off += strlen(curFormat) + 1;