udfGetValidDateTimeStr
str udfGetValidDateTimeStr (str)
;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfGetValidDateTimeStr (strYmdHms) ; The String Version.
intItems = ItemCount (strYmdHms, ":")
If intItems != 6 Then If intItems != 3 Then Return ""
Switch intItems
Case 6
   strItem = ItemExtract (6, strYmdHms, ":") ; SS.
   If strItem != ""
      If !IsNumber (strItem) Then Return ""
      intItem = Int (strItem)
      If intItem > 59 Then Return ""
      If intItem < 0 Then Return ""
      strYmdHms = ItemReplace (StrFixLeft (strItem, "0", 2), 6, strYmdHms, ":")
   Else
      strYmdHms = ItemReplace ("00", 6, strYmdHms, ":")
   EndIf
   strItem = ItemExtract (5, strYmdHms, ":") ; MM.
   If strItem != ""
      If !IsNumber (strItem) Then Return ""
      intItem = Int (strItem)
      If intItem > 59 Then Return ""
      If intItem < 0 Then Return ""
      strYmdHms = ItemReplace (StrFixLeft (strItem, "0", 2), 5, strYmdHms, ":")
   Else
      strYmdHms = ItemReplace ("00", 5, strYmdHms, ":")
   EndIf
   strItem = ItemExtract (4, strYmdHms, ":") ; HH.
   If strItem != ""
      If !IsNumber (strItem) Then Return ""
      intItem = Int (strItem)
      If intItem > 23 Then Return ""
      If intItem < 0 Then Return ""
      strYmdHms = ItemReplace (StrFixLeft (strItem, "0", 2), 4, strYmdHms, ":")
   Else
      strYmdHms = ItemReplace ("00", 4, strYmdHms, ":")
   EndIf
Case 3
   strItem = ItemExtract (3, strYmdHms, ":") ; DD.
   If strItem != ""
      If !IsNumber (strItem) Then Return ""
      intItem = Int (strItem)
      If intItem > 31 Then Return ""
      If intItem < 1 Then Return ""
      strYmdHms = ItemReplace (StrFixLeft (strItem, "0", 2), 3, strYmdHms, ":")
   Else
      strYmdHms = ItemReplace ("01", 3, strYmdHms, ":")
   EndIf
   strItem = ItemExtract (2, strYmdHms, ":") ; MM.
   If strItem != ""
      If !IsNumber (strItem) Then Return ""
      intItem = Int (strItem)
      If intItem > 12 Then Return ""
      If intItem < 1 Then Return ""
      strYmdHms = ItemReplace (StrFixLeft (strItem, "0", 2), 2, strYmdHms, ":")
   Else
      strYmdHms = ItemReplace ("01", 2, strYmdHms, ":")
   EndIf
   strItem = ItemExtract (1, strYmdHms, ":") ; YYYY.
   If strItem != ""
      If !IsNumber (strItem) Then Return ""
      intItem = Int (strItem)
      If intItem > 9999 Then Return ""
      If intItem < 0 Then Return ""
      strYmdHms = ItemReplace (StrFixLeft (strItem, "0", 4), 1, strYmdHms, ":")
   Else
      strYmdHms = ItemReplace ("0000", 1, strYmdHms, ":")
   EndIf
EndSwitch
intLastErrorMode = ErrorMode (@OFF)
LastError ()
TimeDiff (strYmdHms, strYmdHms)
ErrorMode (intLastErrorMode)
If !!LastError () Then strYmdHms = ""
Return strYmdHms
;..........................................................................................................................................
; This UDF "udfGetValidDateTimeStr" checks a given string for valid DateTime format and valid DateTime value.
;
; The return value is either an empty string or a valid DT-10 Date or DT-19 DateTime string, depending on the given string format.
;
; This UDF respects the string formats:
;    DateTime-10 format (YYYY:MM:DD)
;    DateTime-19 format (YYYY:MM:DD:HH:MM:SS)
; Any other format will be considered as not valid.
;
; If a given string has a valid DT-10 or DT-19 colon delimited structure and contains an empty item,
; then this empty item will be automatically initialized to the smallest valid value.
; Examples:
; String "0:1:1"    will be expanded to "0000:01:01" (DT-10).
; String "0:1:1:::" will be expanded to "0000:01:01:00:00:00" (DT-19).
; String ":::::"    will be expanded to "0000:01:01:00:00:00" (DT-19).
; String "2009:02:" will be expanded to "2009:02:01" (DT-10).
;
; Note:
; Although the 4-digit year is set as system default by IntControl (41, 1, 0, 0, 0),
; WinBatch automatically expands the string "0:1:1" to "2000:01:01:00:00:00",
; due to internally 20th/21st-century rule (see manual note at IntControl 41).
;
; Therefore the function call 'TimeJulianDay ("0:1:1")' returns the value 730486 instead of value 1,
; what someone would have been expected at the first sight.
;
; This UDF will always expand the string "0:1:1" to "0000:01:01" (DT-10), so that the
; function call 'TimeJulianDay (udfGetValidDateTimeStr ("0:1:1"))' will return the correct value 1.
;
; Detlev Dalitz.20100930.
;..........................................................................................................................................
;
; Additional information.
;
; From WinBatch Webboard Forum.
; Topic:  When ErrorMode(@OFF)
; Conf:  WinBatch
; From:  tonyd
; Date:  Thursday, September 30, 2010 12:19 PM
;
; A year of '0' or '00' does not make the date invalid and the Julian date returned when using these years is also valid.
; WinBatch assumes that years in the range 50 to 99 are in the range 1950 to 1999 and years in the range 00 to 49 are
; in the current century - 2000 to 2049 ( and yes 2000 is not in the current century.)
; This is all part of the old year 2000 compliance business that most have long since forgotten.
; I don't think it is even discussed in the help file anymore, except in the IC 41 description.
; I believe the explanation still exists on the Web Site someplace but it probably should be included
; with each affected time function instead of just the ones affected by IC 41.
; Moral of the story: it is best to use only four digit years.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------


; Test.

strResult11 = udfGetValidDateTimeStr (TimeYmdHms ())           ; Current DateTime string.
strResult12 = udfGetValidDateTimeStr ("2009:02:01")            ; "2009:02:01".
strResult13 = udfGetValidDateTimeStr ("2009:02:01:00:00:00")   ; "2009:02:01:00:00:00".
strResult14 = udfGetValidDateTimeStr ("2009:02:01:23:59:59")   ; "2009:02:01:23:59:59".
strResult15 = udfGetValidDateTimeStr ("2009:2:1")              ; "2009:02:01".
strResult16 = udfGetValidDateTimeStr ("2009:2:1:0:0:0")        ; "2009:02:01:00:00:00".

strResult21 = udfGetValidDateTimeStr ("0000:01:01:00:00:00")   ; "0000:01:01:00:00:00" Minimum DateTime.
strResult22 = udfGetValidDateTimeStr ("9999:12:31:23:59:59")   ; "9999:12:31:23:59:59" Maximum DateTime.

strResult31 = udfGetValidDateTimeStr ("2009:02:01:00:00:60")   ; "".
strResult32 = udfGetValidDateTimeStr ("2009:02:01:00:60:00")   ; "".
strResult33 = udfGetValidDateTimeStr ("2009:02:01:24:00:00")   ; "".
strResult34 = udfGetValidDateTimeStr ("2009:00:00:00:00:00")   ; "".
strResult35 = udfGetValidDateTimeStr ("2009:02:00:00:00:00")   ; "".
strResult36 = udfGetValidDateTimeStr ("2009:00:01:00:00:00")   ; "".

strResult41 = udfGetValidDateTimeStr ("2008:02:29:00:00:00")   ; "2008:02:29:00:00:00" Leapday.
strResult42 = udfGetValidDateTimeStr ("2008:02:30:00:00:00")   ; "".
strResult43 = udfGetValidDateTimeStr ("2009:02:29:00:00:00")   ; "".
strResult44 = udfGetValidDateTimeStr ("2009:01:32:00:00:00")   ; "".

strResult51 = udfGetValidDateTimeStr ("0000:00:00:00:00:00")   ; "".
strResult52 = udfGetValidDateTimeStr ("10000:01:01:00:00:00")  ; "".
strResult53 = udfGetValidDateTimeStr ("yyyy:mm:dd:hh:mm:ss")   ; "".
strResult54 = udfGetValidDateTimeStr ("1111:-2:*3:/4:+5:-1")   ; "".
strResult55 = udfGetValidDateTimeStr ("1111:-2:*3")            ; "".

strResult61 = udfGetValidDateTimeStr ("::")                    ; "0000:01:01".
strResult62 = udfGetValidDateTimeStr (":::::")                 ; "0000:01:01:00:00:00".
strResult63 = udfGetValidDateTimeStr ("0:1:1")                 ; "0000:01:01".
strResult64 = udfGetValidDateTimeStr ("0:1:1:::")              ; "0000:01:01:00:00:00".
strResult65 = udfGetValidDateTimeStr ("2009:2::::")            ; "2009:02:01:00:00:00".
strResult66 = udfGetValidDateTimeStr (":::::59")               ; "0000:01:01:00:00:59".
strResult67 = udfGetValidDateTimeStr (":::::60")               ; "".
strResult68 = udfGetValidDateTimeStr ("::31:::")               ; "0000:01:31:00:00:00".
strResult69 = udfGetValidDateTimeStr ("::32:::")               ; "".

intResult11 = TimeJulianDay ("0:1:1")                          ; 730486.
intResult12 = TimeJulianDay (udfGetValidDateTimeStr ("0:1:1")) ; 1.

Exit