summaryrefslogtreecommitdiff
path: root/wizards
diff options
context:
space:
mode:
authorJean-Pierre Ledure <jp@ledure.be>2021-02-08 18:21:42 +0100
committerJean-Pierre Ledure <jp@ledure.be>2021-02-09 12:32:50 +0100
commit42535b54e32ce64b7b3d34d13f9dd1a6c0dbd5dc (patch)
tree2ff96267baed0f0e210c99057ec26ca47d6acab9 /wizards
parent593ab5fbedb3de3c62039affd69f3dcd30f56d8f (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.xba20
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 &apos; Return value
Dim sFormat As String &apos; Alias for DateFormat
-Dim sRegex As String &apos; The regex to check against the input string
+Dim iYear As Integer &apos; Alias of year in input string
+Dim iMonth As Integer &apos; Alias of month in input string
+Dim iDay As Integer &apos; Alias of day in input string
+Dim dDate As Date &apos; Date value
Const cstFormat = &quot;YYYY-MM-DD&quot; &apos; Default date format
Const cstFormatRegex = &quot;(YYYY[- /.]MM[- /.]DD|MM[- /.]DD[- /.]YYYY|DD[- /.]MM[- /.]YYYY)&quot;
&apos; The regular expression the format must match
@@ -743,10 +746,14 @@ Check:
Try:
If Len(InputStr) = Len(DateFormat) Then
- sRegex = ReplaceStr(sFormat, Array(&quot;YYYY&quot;, &quot;MM&quot;, &quot;DD&quot;) _
- , Array(REGEXDATEYEAR, REGEXDATEMONTH, REGEXDATEDAY) _
- , CaseSensitive := False)
- bADate = SF_String.IsRegex(InputStr, sRegex, CaseSensitive := False)
+ &apos; Extract the date components YYYY, MM, DD from the input string
+ iYear = CInt(Mid(InputStr, InStr(sFormat, &quot;YYYY&quot;), 4))
+ iMonth = CInt(Mid(InputStr, InStr(sFormat, &quot;MM&quot;), 2))
+ iDay = CInt(Mid(InputStr, InStr(sFormat, &quot;DD&quot;), 2))
+ &apos; Check the validity of the date
+ On Local Error GoTo NotADate
+ dDate = DateSerial(iYear, iMonth, iDay)
+ bADate = True &apos; Statement reached only if no error
End If
Finally:
@@ -755,6 +762,9 @@ Finally:
Exit Function
Catch:
GoTo Finally
+NotADate:
+ On Error GoTo 0 &apos; Reset the error object
+ GoTo Finally
End Function &apos; ScriptForge.SF_String.IsADate
REM -----------------------------------------------------------------------------