diff options
author | Jean-Pierre Ledure <jp@ledure.be> | 2021-02-08 18:21:42 +0100 |
---|---|---|
committer | Jean-Pierre Ledure <jp@ledure.be> | 2021-02-09 12:32:50 +0100 |
commit | 42535b54e32ce64b7b3d34d13f9dd1a6c0dbd5dc (patch) | |
tree | 2ff96267baed0f0e210c99057ec26ca47d6acab9 /wizards | |
parent | 593ab5fbedb3de3c62039affd69f3dcd30f56d8f (diff) |
ScriptForge - (SF_String) more severe check on the date validity of IsADate()
So far IsADate checked the format of the date only,
not the validity vs. leap years, days in month, etc.
Corrected with an error check on the DateSerial() builtin function
Change-Id: I3fc53ea83e0a1472b2861f256266c4422cce2580
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/110590
Tested-by: Jean-Pierre Ledure <jp@ledure.be>
Tested-by: Jenkins
Reviewed-by: Jean-Pierre Ledure <jp@ledure.be>
Diffstat (limited to 'wizards')
-rw-r--r-- | wizards/source/scriptforge/SF_String.xba | 20 |
1 files changed, 15 insertions, 5 deletions
diff --git a/wizards/source/scriptforge/SF_String.xba b/wizards/source/scriptforge/SF_String.xba index 66eb90910ba5..23010e88c750 100644 --- a/wizards/source/scriptforge/SF_String.xba +++ b/wizards/source/scriptforge/SF_String.xba @@ -719,7 +719,10 @@ Public Function IsADate(Optional ByRef InputStr As Variant _ Dim bADate As Boolean ' Return value Dim sFormat As String ' Alias for DateFormat -Dim sRegex As String ' The regex to check against the input string +Dim iYear As Integer ' Alias of year in input string +Dim iMonth As Integer ' Alias of month in input string +Dim iDay As Integer ' Alias of day in input string +Dim dDate As Date ' Date value Const cstFormat = "YYYY-MM-DD" ' Default date format Const cstFormatRegex = "(YYYY[- /.]MM[- /.]DD|MM[- /.]DD[- /.]YYYY|DD[- /.]MM[- /.]YYYY)" ' The regular expression the format must match @@ -743,10 +746,14 @@ Check: Try: If Len(InputStr) = Len(DateFormat) Then - sRegex = ReplaceStr(sFormat, Array("YYYY", "MM", "DD") _ - , Array(REGEXDATEYEAR, REGEXDATEMONTH, REGEXDATEDAY) _ - , CaseSensitive := False) - bADate = SF_String.IsRegex(InputStr, sRegex, CaseSensitive := False) + ' Extract the date components YYYY, MM, DD from the input string + iYear = CInt(Mid(InputStr, InStr(sFormat, "YYYY"), 4)) + iMonth = CInt(Mid(InputStr, InStr(sFormat, "MM"), 2)) + iDay = CInt(Mid(InputStr, InStr(sFormat, "DD"), 2)) + ' Check the validity of the date + On Local Error GoTo NotADate + dDate = DateSerial(iYear, iMonth, iDay) + bADate = True ' Statement reached only if no error End If Finally: @@ -755,6 +762,9 @@ Finally: Exit Function Catch: GoTo Finally +NotADate: + On Error GoTo 0 ' Reset the error object + GoTo Finally End Function ' ScriptForge.SF_String.IsADate REM ----------------------------------------------------------------------------- |