;------------------------------------------------------------------------------------------------------------------------------------------ #DefineFunction udfGetValidDateTimeStr (strYmdHms) ; The Array Version. arrIn = Arrayize (strYmdHms, ":") arrOut = Arrayize ("0000+:01+:01+:00+:00+:00", "+") intItems = ArrInfo (arrIn, 1) If intItems != 6 Then If intItems != 3 Then Return "" Switch intItems Case 6 If arrIn[5] != "" ; SS. If !IsNumber (arrIn[5]) Then Return "" arrIn[5] = Int (arrIn[5]) If arrIn[5] > 59 Then Return "" If arrIn[5] < 0 Then Return "" arrOut[5] = ":" : StrFixLeft (arrIn[5], "0", 2) EndIf If arrIn[4] != "" ; MM. If !IsNumber (arrIn[4]) Then Return "" arrIn[4] = Int (arrIn[4]) If arrIn[4] > 59 Then Return "" If arrIn[4] < 0 Then Return "" arrOut[4] = ":" : StrFixLeft (arrIn[4], "0", 2) EndIf If arrIn[3] != "" ; HH. If !IsNumber (arrIn[3]) Then Return "" arrIn[3] = Int (arrIn[3]) If arrIn[3] > 23 Then Return "" If arrIn[3] < 0 Then Return "" arrOut[3] = ":" : StrFixLeft (arrIn[3], "0", 2) EndIf Case 3 If arrIn[2] != "" ; DD. If !IsNumber (arrIn[2]) Then Return "" arrIn[2] = Int (arrIn[2]) If arrIn[2] > 31 Then Return "" If arrIn[2] < 1 Then Return "" arrOut[2] = ":" : StrFixLeft (arrIn[2], "0", 2) EndIf If arrIn[1] != "" ; MM. If !IsNumber (arrIn[1]) Then Return "" arrIn[1] = Int (arrIn[1]) If arrIn[1] > 12 Then Return "" If arrIn[1] < 1 Then Return "" arrOut[1] = ":" : StrFixLeft (arrIn[1], "0", 2) EndIf If arrIn[0] != "" ; YYYY. If !IsNumber (arrIn[0]) Then Return "" arrIn[0] = Int (arrIn[0]) If arrIn[0] > 9999 Then Return "" If arrIn[0] < 0 Then Return "" arrOut[0] = StrFixLeft (arrIn[0], "0", 4) EndIf EndSwitch strYmdHms = ArrayToStr (arrOut) ; DT-19. If intItems == 3 Then strYmdHms = StrSub (strYmdHms, 1, 10) ; DT-10. 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.20110126. ;.......................................................................................................................................... ; ; 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