[Libreoffice-commits] core.git: wizards/source
Jean-Pierre Ledure (via logerrit)
logerrit at kemper.freedesktop.org
Tue Feb 9 11:33:32 UTC 2021
wizards/source/scriptforge/SF_String.xba | 20 +++++++++++++++-----
1 file changed, 15 insertions(+), 5 deletions(-)
New commits:
commit 42535b54e32ce64b7b3d34d13f9dd1a6c0dbd5dc
Author: Jean-Pierre Ledure <jp at ledure.be>
AuthorDate: Mon Feb 8 18:21:42 2021 +0100
Commit: Jean-Pierre Ledure <jp at ledure.be>
CommitDate: Tue Feb 9 12:32:50 2021 +0100
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 at ledure.be>
Tested-by: Jenkins
Reviewed-by: Jean-Pierre Ledure <jp at ledure.be>
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 -----------------------------------------------------------------------------
More information about the Libreoffice-commits
mailing list