
AdamUL
-
Posts
715 -
Joined
-
Last visited
-
Days Won
3
Reputation Activity
-
AdamUL got a reaction from RichardL in Time and Date Conversion UDF
Here is a UDF that I created when I had to do a large project working with dates and times, and calculations with them. I need to convert the time from 12 hr time with "AM" and "PM", and sometimes with "a" and "p", to 24 hr time for calculations, and convert the time back to 12 hr when needed. I also needed to convert "MM/DD/YYYY" to "YYYY/MM/DD" for use in calculations as well, and convert back as well.
For details on each function, please look at the function headers.
Look at Melba23's more flexible Date_Time_Convert UDF. I use that UDF more now than this one, but I still use "IsBetween" functions in this UDF.
DateTimeConvert.au3
#include-once #include <Date.au3> #AutoIt3Wrapper_AU3Check_Parameters= -d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 #AutoIt3Wrapper_AU3Check_Stop_OnWarning=Y ; #INDEX# ======================================================================================================================= ; Title .........: Time and Date Conversion Library ; AutoIt Version : 3.3.6++ ; UDF Version ...: 1.1 ; Language ......: English ; Description ...: Converts time between 12 hr and 24 hr with other time and date related functions. ; Dll ...........: ; Author(s) .....: Adam Lawrence (AdamUL) ; Email .........: ; Modified.......: ; Contributors ..: ; Resources .....: ; Remarks .......: ; =============================================================================================================================== ; #CURRENT# ===================================================================================================================== ;_IsTime12Hr ;_IsTime24Hr ;_Time12HrTo24Hr ;_Time24HrTo12Hr ;_IsCalcDate ;_IsStandardDate ;_IsDateAndTime ;_DateStandardToCalcDate ;_DateCalcToStandardDate ;_IsBetweenTimes ;_IsBetweenDatesTimes ;_IsBetweenDatesTimesLite ; =============================================================================================================================== ; #FUNCTION# ==================================================================================================================== ; Name ..........: _IsTime12Hr ; Description ...: Checks to see if a time string is in the 12 hr (AM/PM) format. ; Syntax ........: _IsTime12Hr($sTime) ; Parameters ....: $sTime - A string value in time format. ; Return values .: Success - True ; Failure - False, sets @error to: ; |0 - String is not in 12 hr time format. ; |1 - Invalid time format string. ; Author ........: Adam Lawrence (AdamUL) ; Modified ......: ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: No ; =============================================================================================================================== Func _IsTime12Hr($sTime) If StringRegExp($sTime, "^(1[0-2]|[1-9]):([0-5]\d):?([0-5]\d)?(?-i:\h*)(?i)([ap]m?)$") Then Return True If @error Then Return SetError(1, 0, False) Return False EndFunc ; #FUNCTION# ==================================================================================================================== ; Name ..........: _IsTime24Hr ; Description ...: Checks to see if a time string is in the 24 hr format. ; Syntax ........: _IsTime24Hr($sTime) ; Parameters ....: $sTime - A string value in time format. ; Return values .: Success - True ; Failure - False, sets @error to: ; |0 - String is not in 24 hr time format. ; |1 - Invalid time format string. ; Author ........: Adam Lawrence (AdamUL) ; Modified ......: ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: No ; =============================================================================================================================== Func _IsTime24Hr($sTime) If StringRegExp($sTime, "^([01]?\d|2[0-3]):([0-5]\d):?([0-5]\d)?$") Then Return True If @error Then Return SetError(1, 0, False) Return False EndFunc ; #FUNCTION# ==================================================================================================================== ; Name ..........: _Time12HrTo24Hr ; Description ...: Convert 12 hr (AM/PM) time string to a 24 hr time string. ; Syntax ........: _Time12HrTo24Hr($sTime[, $fDisplaySecs = True[, $fHourLeadingZero = False]]) ; Parameters ....: $sTime - "hh:mm:ss AM/PM" time format. ; $fDisplaySecs - [optional] A boolean value to display seconds values. Default is True. ; $fHourLeadingZero - [optional] A boolean value to pad leading zero to single digit hours. Default is False. ; Return values .: Success - A string value in "hh:mm:ss" 24 hr time string. ; Failure - "", sets @error to: ; |1 - Invalid time format string ; Author ........: Adam Lawrence (AdamUL) ; Modified ......: ; Remarks .......: ; Related .......: _Time24HrTo12Hr ; Link ..........: ; Example .......: _Time12HrTo24Hr("12:30AM"), _Time12HrTo24Hr("1:30:45 PM"), _Time12HrTo24Hr("12:30 pm") ; =============================================================================================================================== Func _Time12HrTo24Hr($sTime, $fDisplaySecs = True, $fHourLeadingZero = False) Local $aTime = StringRegExp($sTime, "^(1[0-2]|[1-9]):([0-5]\d):?([0-5]\d)?(?-i:\h*)(?i)([ap]m?)$", 1) If @error Then Return SetError(1, 0, "") Local $sHour = $aTime[0] Local $sMins = $aTime[1] Local $sSecs = $aTime[2] Local $sAMPM = $aTime[3] $sHour = Mod($sHour, 12) If StringInStr($sAMPM, "p") Then $sHour += 12 If $fHourLeadingZero And Number($sHour) < 10 And StringLen($sHour) = 1 Then $sHour = "0" & $sHour If $fDisplaySecs And $sSecs = "" Then $sSecs = "00" If $fDisplaySecs Then Return $sHour & ":" & $sMins & ":" & $sSecs Return $sHour & ":" & $sMins EndFunc ; #FUNCTION# ==================================================================================================================== ; Name ..........: _Time24HrTo12Hr ; Description ...: Convert 24 hr time string to a 12 hr (AM/PM) time string. ; Syntax ........: _Time24HrTo12Hr($sTime[, $fDisplaySecs = True[, $fHourLeadingZero = False[, $sAMPMDelim = " "]]]) ; Parameters ....: $sTime - A string value in "hh:mm:ss" time format. ; $fDisplaySecs - [optional] A boolean value to display seconds values. Default is True. ; $fHourLeadingZero - [optional] A boolean value to pad leading zero to single digit hours. Default is False. ; $sAMPMDelim - [optional] A string value delimiter to seperate AM/PM from the numeric time. Default is " ". ; Return values .: Success - "hh:mm:ss AM/PM" time format. ; Failure - "", sets @error to: ; |1 - Invalid time format string. ; Author ........: Adam Lawrence (AdamUL) ; Modified ......: ; Remarks .......: ; Related .......: _Time12HrTo24Hr ; Link ..........: ; Example .......: _Time24HrTo12Hr("0:30"), _Time24HrTo12Hr("15:45:36"), _Time24HrTo12Hr("5:36") ; =============================================================================================================================== Func _Time24HrTo12Hr($sTime, $fDisplaySecs = True, $fHourLeadingZero = False, $sAMPMDelim = " ") Local $aTime = StringRegExp($sTime, "^([01]?\d|2[0-3]):([0-5]\d):?([0-5]\d)?$", 1) If @error Then Return SetError(1, 0, "") If UBound($aTime) = 2 Then ReDim $aTime[3] Local $sHour = $aTime[0] Local $sMins = $aTime[1] Local $sSecs = $aTime[2] Local $sAMPM = "" Switch $sHour Case 0 $sHour = 12 $sAMPM = "AM" Case 1 To 11 $sAMPM = "AM" Case 12 $sAMPM = "PM" Case 13 To 23 $sHour = $sHour - 12 $sAMPM = "PM" Case Else EndSwitch If $fHourLeadingZero And Number($sHour) < 10 And StringLen($sHour) = 1 Then $sHour = "0" & $sHour If Not $fHourLeadingZero And Number($sHour) < 10 And StringLen($sHour) = 2 Then $sHour = Number($sHour) If $fDisplaySecs And $sSecs = "" Then $sSecs = "00" If $fDisplaySecs Then Return $sHour & ":" & $sMins & ":" & $sSecs & $sAMPMDelim & $sAMPM Return $sHour & ":" & $sMins & $sAMPMDelim & $sAMPM EndFunc ; #FUNCTION# ==================================================================================================================== ; Name ..........: _IsCalcDate ; Description ...: Checks to see if a date is in a format for calculations. ; Syntax ........: _IsCalcDate($sDate) ; Parameters ....: $sDate - A string value date format. ; Return values .: Success - True ; Failure - False, sets @error to 1. ; Author ........: Adam Lawrence (AdamUL) ; Modified ......: ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: No ; =============================================================================================================================== Func _IsCalcDate($sDate) If StringRegExp($sDate, "^(\d{4})/(\d{1,2})/(\d{1,2})$") Then Return True If @error Then Return SetError(1, 0, False) Return False EndFunc ; #FUNCTION# ==================================================================================================================== ; Name ..........: _IsStandardDate ; Description ...: Checks to see if a date is in a standard date format, "MM/DD/YYYY". ; Syntax ........: _IsStandardDate($sDate) ; Parameters ....: $sDate - A string value in date format. ; Return values .: Success - True ; Failure - False, sets @error to 1. ; Author ........: Adam Lawrence (AdamUL) ; Modified ......: ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: No ; =============================================================================================================================== Func _IsStandardDate($sDate) If StringRegExp($sDate, "^(\d{1,2})/(\d{1,2})/(\d{4})$") Then Return True If @error Then Return SetError(1, 0, False) Return False EndFunc ; #FUNCTION# ==================================================================================================================== ; Name ..........: _IsDateAndTime ; Description ...: Checks to see if a string is in a date and time format. ; Syntax ........: _IsDateAndTime($sDateTime) ; Parameters ....: $sDateTime - A string value in date and time format. ; Return values .: Success - True ; Failure - False, sets @error to 1. ; Author ........: Adam Lawrence (AdamUL) ; Modified ......: ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: No ; =============================================================================================================================== Func _IsDateAndTime($sDateTime) Local $sRegEx = "^((?:\d{1,2}/\d{1,2}/\d{4})|(?:\d{4}/\d{1,2}/\d{1,2}))?(?-i:\h*)?((?:1[0-2]|[1-9]):(?:[0-5]\d):?(?:[0-5]\d)?(?-i:\h*)(?i:[ap]m?)|(?:[01]?\d|2[0-3]):(?:[0-5]\d):?(?:[0-5]\d)?)$" If StringRegExp($sDateTime, $sRegEx) Then Return True If @error Then Return SetError(1, 0, False) Return False EndFunc ; #FUNCTION# ==================================================================================================================== ; Name ..........: _DateStandardToCalcDate ; Description ...: Convert a date from "MM/DD/YYYY" to "YYYY/MM/DD" format to use in date calculations. ; Syntax ........: _DateStandardToCalcDate($sDate) ; Parameters ....: $sDate - A string value in "MM/DD/YYYY" format. ; Return values .: Success - Calc date string. ; Failure - "", sets @error to: ; |1 - Invalid date format. ; Author ........: Adam Lawrence (AdamUL) ; Modified ......: ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: No ; =============================================================================================================================== Func _DateStandardToCalcDate($sDate) If Not StringRegExp($sDate, "^(\d{1,2})/(\d{1,2})/(\d{4})$") Then Return SetError(1, 0, "") If @error Then Return SetError(1, 0, "") Local $sDateNew = StringRegExpReplace($sDate, "(\d{2})/(\d{2})/(\d{4})", "$3/$1/$2") $sDateNew = StringRegExpReplace($sDateNew, "(\d{2})/(\d)/(\d{4})", "$3/$1/0$2") $sDateNew = StringRegExpReplace($sDateNew, "(\d)/(\d{2})/(\d{4})", "$3/0$1/$2") $sDateNew = StringRegExpReplace($sDateNew, "(\d)/(\d)/(\d{4})", "$3/0$1/0$2") Return $sDateNew EndFunc ; #FUNCTION# ==================================================================================================================== ; Name ..........: _DateCalcToStandardDate ; Description ...: Convert a date from "YYYY/MM/DD" to "MM/DD/YYYY" format. ; Syntax ........: _DateCalcToStandardDate($sDate) ; Parameters ....: $sDate - A string value in "YYYY/MM/DD" format. ; Return values .: Success - Standard date string ; Failure - "", sets @error to: ; |1 - Invalid date format. ; Author ........: Adam Lawrence (AdamUL) ; Modified ......: ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: No ; =============================================================================================================================== Func _DateCalcToStandardDate($sDate) Local $aDate = StringRegExp($sDate, "^(\d{4})/(\d{1,2})/(\d{1,2})$", 1) If @error Then Return SetError(1, 0, "") Local $sYear = $aDate[0] Local $sMonth = $aDate[1] Local $sDay = $aDate[2] Return Number($sMonth) & "/" & Number($sDay) & "/" & $sYear EndFunc Func _DateTimeStandardToCalcDateTime($sDateTime) Local $sRegEx = "^((?:\d{1,2}/\d{1,2}/\d{4})|(?:\d{4}/\d{1,2}/\d{1,2}))?(?-i:\h*)?((?:1[0-2]|[1-9]):(?:[0-5]\d):?(?:[0-5]\d)?(?-i:\h*)(?i:[ap]m?)|(?:[01]?\d|2[0-3]):(?:[0-5]\d):?(?:[0-5]\d)?)$" Local $aDateTime = StringRegExp($sDateTime, $sRegEx, 1) If @error Then Return SetError(1, 1, "") Local $sDate = $aDateTime[0] Local $sTime = $aDateTime[1] If _IsStandardDate($sDate) Then $sDate = _DateStandardToCalcDate($sDate) If @error Then Return SetError(2, 1, "") EndIf If Not _IsCalcDate($sDate) Then Return SetError(2, 2, "") If _IsTime12Hr($sTime) Then $sTime = _Time12HrTo24Hr($sTime) If @error Then Return SetError(3, 1, "") EndIf If Not _IsTime24Hr($sTime) Then Return SetError(3, 2, "") Return $sDate & " " & $sTime EndFunc Func _DateTimeCalcToStandardDateTime($sDateTime) Local $sRegEx = "^((?:\d{1,2}/\d{1,2}/\d{4})|(?:\d{4}/\d{1,2}/\d{1,2}))?(?-i:\h*)?((?:1[0-2]|[1-9]):(?:[0-5]\d):?(?:[0-5]\d)?(?-i:\h*)(?i:[ap]m?)|(?:[01]?\d|2[0-3]):(?:[0-5]\d):?(?:[0-5]\d)?)$" Local $aDateTime = StringRegExp($sDateTime, $sRegEx, 1) If @error Then Return SetError(1, 1, "") Local $sDate = $aDateTime[0] Local $sTime = $aDateTime[1] If _IsCalcDate($sDate) Then $sDate = _DateCalcToStandardDate($sDate) If @error Then Return SetError(2, 1, "") EndIf If Not _IsStandardDate($sDate) Then Return SetError(2, 2, "") If _IsTime24Hr($sTime) Then $sTime = _Time24HrTo12Hr($sTime) If @error Then Return SetError(3, 1, "") EndIf If Not _IsTime12Hr($sTime) Then Return SetError(3, 2, "") Return $sDate & " " & $sTime EndFunc ; #FUNCTION# ==================================================================================================================== ; Name ..........: _IsBetweenTimes ; Description ...: Test a time to see if it is between the Start Time and the End Time in a 24 hour day. ; Syntax ........: _IsBetweenTimes($sTestTime, $sStartTime, $sEndTime) ; Parameters ....: $sTestTime - A string value time to test, in 12 hr or 24 hr format. ; $sStartTime - A string value start Time, must be before End Time in 12 hr or 24 hr format. ; $sEndTime - A string value end Time, must be after Start Time in 12 hr or 24 hr format. ; Return values .: Success - True ; Failure - False, sets @error to: ; |0 - Not between times. ; |1 - Invalid 12 Hr format. ; |2 - Invalid 24 Hr format. ; |3 - Invalid time string. ; |4 - End Time before Start Time. ; Author ........: Adam Lawrence (AdamUL) ; Modified ......: ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: No ; =============================================================================================================================== Func _IsBetweenTimes($sTestTime, $sStartTime, $sEndTime) If _IsTime12Hr($sTestTime) Then $sTestTime = _Time12HrTo24Hr($sTestTime) If @error Then Return SetError(1, 1, False) EndIf If _IsTime12Hr($sStartTime) Then $sStartTime = _Time12HrTo24Hr($sStartTime) If @error Then Return SetError(1, 2, False) EndIf If _IsTime12Hr($sEndTime) Then $sEndTime = _Time12HrTo24Hr($sEndTime) If @error Then Return SetError(1, 3, False) EndIf If Not _IsTime24Hr($sTestTime) Then Return SetError(2, 1, False) If Not _IsTime24Hr($sStartTime) Then Return SetError(2, 2, False) If Not _IsTime24Hr($sEndTime) Then Return SetError(2, 3, False) $sTestTime = StringReplace(StringStripWS($sTestTime, 8), ":", "") If @error Or @extended > 2 Then Return SetError(3, 1, False) If @extended = 1 Then $sTestTime &= "00" Local $iTestTime = Number($sTestTime) $sStartTime = StringReplace(StringStripWS($sStartTime, 8), ":", "") If @error Or @extended > 2 Then Return SetError(3, 2, False) If @extended = 1 Then $sStartTime &= "00" Local $iStartTime = Number($sStartTime) $sEndTime = StringReplace(StringStripWS($sEndTime, 8), ":", "") If @error Or @extended > 2 Then Return SetError(3, 3, False) If @extended = 1 Then $sEndTime &= "00" Local $iEndTime = Number($sEndTime) If $iEndTime < $iStartTime Then Return SetError(4, 0, False) If $iTestTime >= $iStartTime And $iTestTime <= $iEndTime Then Return True Return False EndFunc ; #FUNCTION# ==================================================================================================================== ; Name ..........: _IsBetweenDatesTimes ; Description ...: Test a time to see if it is between the Start Date and Time and the End Date and Time. ; Syntax ........: _IsBetweenDatesTimes($sTestDateTime, $sStartDateTime, $sEndDateTime) ; Parameters ....: $sTestDateTime - A string value, Date and Time to test, in 12 hr or 24 hr format "YYYY/MM/DD[ HH:MM:SS]". ; $sStartDateTime - A string value, Start Date and Time, must be before End Date and Time in 12 hr or 24 hr format "YYYY/MM/DD[ HH:MM:SS]". ; $sEndDateTime - A string value, End Date and Time, must be after Start Date and Time in 12 hr or 24 hr format "YYYY/MM/DD[ HH:MM:SS]". ; Return values .: Success - True ; Failure - False, sets @error to: ; |1 - Invalid date format. ; |2 - Error Converting to Calc date. ; |3 - Invalid time format. ; |4 - Invalid $sTestDateTime ; |5 - Invalid $sStartDateTime ; |6 - Invalid $sEndDateTime ; Author ........: Adam Lawrence (AdamUL) ; Modified ......: ; Remarks .......: ; Related .......: _IsBetweenDatesTimesLite, _IsBetweenTimes ; Link ..........: ; Example .......: No ; =============================================================================================================================== Func _IsBetweenDatesTimes($sTestDateTime, $sStartDateTime, $sEndDateTime) Local $sRegEx = "^((?:\d{1,2}/\d{1,2}/\d{4})|(?:\d{4}/\d{1,2}/\d{1,2}))?(?-i:\h*)?((?:1[0-2]|[1-9]):(?:[0-5]\d):?(?:[0-5]\d)?(?-i:\h*)(?i:[ap]m?)|(?:[01]?\d|2[0-3]):(?:[0-5]\d):?(?:[0-5]\d)?)$" Local $aTestDateTime = StringRegExp($sTestDateTime, $sRegEx, 1) If @error Then Return SetError(1, 1, False) Local $sTestDate = $aTestDateTime[0] Local $sTestTime = $aTestDateTime[1] Local $aStartDateTime = StringRegExp($sStartDateTime, $sRegEx, 1) If @error Then Return SetError(1, 2, False) Local $sStartDate = $aStartDateTime[0] Local $sStartTime = $aStartDateTime[1] Local $aEndDateTime = StringRegExp($sEndDateTime, $sRegEx, 1) If @error Then Return SetError(1, 3, False) Local $sEndDate = $aEndDateTime[0] Local $sEndTime = $aEndDateTime[1] Select Case $sTestDate = "" And $sStartDate = "" And $sEndDate = "" $sTestDate = _NowCalcDate() $sStartDate = $sTestDate $sEndDate = $sTestDate Case $sTestDate <> "" And $sStartDate <> "" And $sEndDate <> "" Case $sTestDate = "" And $sStartDate <> "" And $sEndDate <> "" ContinueCase Case $sTestDate <> "" And $sStartDate = "" And $sEndDate <> "" ContinueCase Case $sTestDate <> "" And $sStartDate <> "" And $sEndDate = "" ContinueCase Case Else Return SetError(1, 4, False) EndSelect If _IsStandardDate($sTestDate) Then $sTestDate = _DateStandardToCalcDate($sTestDate) If @error Then Return SetError(2, 1, False) EndIf If _IsStandardDate($sStartDate) Then $sStartDate = _DateStandardToCalcDate($sStartDate) If @error Then Return SetError(2, 2, False) EndIf If _IsStandardDate($sEndDate) Then $sEndDate = _DateStandardToCalcDate($sEndDate) If @error Then Return SetError(2, 3, False) EndIf If Not _IsCalcDate($sTestDate) Then Return SetError(3, 1, False) If Not _IsCalcDate($sStartDate) Then Return SetError(3, 2, False) If Not _IsCalcDate($sEndDate) Then Return SetError(3, 3, False) $sTestDate = $sTestDate & " " $sStartDate = $sStartDate & " " $sEndDate = $sEndDate & " " If _IsTime12Hr($sTestTime) Then $sTestTime = _Time12HrTo24Hr($sTestTime) If @error Then Return SetError(2, 4, False) EndIf If _IsTime12Hr($sStartTime) Then $sStartTime = _Time12HrTo24Hr($sStartTime) If @error Then Return SetError(2, 5, False) EndIf If _IsTime12Hr($sEndTime) Then $sEndTime = _Time12HrTo24Hr($sEndTime) If @error Then Return SetError(2, 6, False) EndIf If Not _IsTime24Hr($sTestTime) Then Return SetError(3, 4, False) If Not _IsTime24Hr($sStartTime) Then Return SetError(3, 5, False) If Not _IsTime24Hr($sEndTime) Then Return SetError(3, 6, False) $sTestDateTime = $sTestDate & $sTestTime $sStartDateTime = $sStartDate & $sStartTime $sEndDateTime = $sEndDate & $sEndTime Local $sStartTestDateTimeDiff = _DateDiff("s", $sStartDateTime, $sTestDateTime) Switch @error Case 2 Return SetError(5, @error, False) Case 3 Return SetError(4, @error, False) EndSwitch Local $sEndTestDateTimeDiff = _DateDiff("s", $sTestDateTime, $sEndDateTime) Switch @error Case 2 Return SetError(4, @error, False) Case 3 Return SetError(6, @error, False) EndSwitch If $sStartTestDateTimeDiff >= 0 And $sEndTestDateTimeDiff >= 0 Then Return True Return False EndFunc ; #FUNCTION# ==================================================================================================================== ; Name ..........: _IsBetweenDatesTimesLite ; Description ...: Test a time to see if it is between the Start Date and Time and the End Date and Time. ; Syntax ........: _IsBetweenDatesTimesLite($sTestDateTime, $sStartDateTime, $sEndDateTime) ; Parameters ....: $sTestDateTime - A string value, Date and Time to test, in 24 hr format "YYYY/MM/DD[ HH:MM:SS]". ; $sStartDateTime - A string value, Start Date and Time, must be before End Date and Time in 24 hr format "YYYY/MM/DD[ HH:MM:SS]". ; $sEndDateTime - A string value, End Date and Time, must be after Start Date and Time in 24 hr format "YYYY/MM/DD[ HH:MM:SS]". ; Return values .: Success - True ; Failure - False, sets @error to: ; |1 - Invalid sTestDateTime. ; |2 - Invalid $sStartDateTime. ; |3 - Invalid $sEndDateTime. ; Author ........: Adam Lawrence (AdamUL) ; Modified ......: ; Remarks .......: Faster return than the _IsBetweenDatesTimes function, but more retricted on the format of the date and time entered. ; Related .......: _IsBetweenDatesTimes, _IsBetweenTimes ; Link ..........: ; Example .......: No ; =============================================================================================================================== Func _IsBetweenDatesTimesLite($sTestDateTime, $sStartDateTime, $sEndDateTime) Local $sStartTestDateTimeDiff = _DateDiff("s", $sStartDateTime, $sTestDateTime) Switch @error Case 2 Return SetError(2, @error, False) Case 3 Return SetError(1, @error, False) EndSwitch Local $sEndTestDateTimeDiff = _DateDiff("s", $sTestDateTime, $sEndDateTime) Switch @error Case 2 Return SetError(1, @error, False) Case 3 Return SetError(3, @error, False) EndSwitch If $sStartTestDateTimeDiff >= 0 And $sEndTestDateTimeDiff >= 0 Then Return True Return False EndFunc I hope others find this UDF useful.
In response to BrewManNH suggestion about date formats, here are some functions created by guinness that could be useful to use with this UDF in relation to US/UK date format: _UKToUSDate() and _USToUKDate()
Lupo73 updated and added a function to this thread, _DateTimeStandard, to convert date and time to a format than can be used with this UDF.
Edit: Removed extra @error values listed in _Time12HrTo24Hr header.
Edit 2: Corrected header for _IsBetweenDatesTimesLite for $sEndDateTime.
Edit 3: Added link to guinness' _UKToUSDate() and _USToUKDate() functions.
Edit 4: Added link to Lupo73's _DateTimeStandard function.
Edit 5: Forum update stripped out in regexes, added file for download.
Adam
-
AdamUL got a reaction from ahmeddzcom in Multiple statements on one line?
Not explicitly, but there are workarounds. Example.
;Declaring Variables Global $a = 1, $b = 2, $c = $a + $b ConsoleWrite($c & @CRLF) ;Using Operators with Functions, must be saved to a variable. (For demonstration purposes only.) $Test = MsgBox(0, "Test 1", "This is a test.") + MsgBox(0, "Test 2", "This is a test.") - MsgBox(0, "Test 3", "This is a test.") ;Using a Function with Operators. (For demonstration purposes only.) _MultilineExec(MsgBox(0, "Test 4", "This is a test.") + MsgBox(0, "Test 5", "This is a test.") - MsgBox(0, "Test 6", "This is a test.")) Func _MultilineExec($sParameter) Return $sParameter EndFunc
Adam
-
AdamUL got a reaction from wongshing1439 in RunAs not fully elevated (UAC issue)
Here is a workaround for dealing with RunAs and RunAsWait and the UAC Admin Token. This uses re-execution to elevate the script and allow the Admin part of the script to run. After the admin part runs, it reverts back to the not admin part. Example script is below.
#include <MsgBoxConstants.au3> Global $sAdminUser = "USERNAME" Global $sAdminPassword = "PASSWORD" Global $sDomain = @ComputerName Global $iLogOnFlag = 0 Global $sParameters = "" ;Run as the Admin account. If @UserName <> $sAdminUser And Not IsAdmin() Then $sParameters = "" If Not @Compiled Then $sParameters = ' "' & @ScriptFullPath & '"' EndIf ;Use RunAsWait to run as AdminUser, to continue the script as the user that started it, and to wait for the Admin part to Finish. RunAsWait($sAdminUser, $sDomain, $sAdminPassword, $iLogOnFlag, @AutoItExe & $sParameters) If @error Then Exit MsgBox($MB_ICONERROR + $MB_TOPMOST, "ERROR!", "Unable to run under administrator account.") EndIf ;Request the Admin Token for the Admin account in Windows Vista and Higher. If @UserName = $sAdminUser And Not IsAdmin() And Not StringRegExp(@OSVersion, "_(XP|200(0|3))") Then $sParameters = "" If Not @Compiled Then $sParameters = '"' & @ScriptFullPath & '"' EndIf ;Use ShellExecuteWait to run as AdminUser with Admin Token, to wait for the Admin part of the script to finish, and then to exit. ShellExecuteWait(@AutoItExe, $sParameters, "", "runas") If @error Then Exit MsgBox($MB_ICONERROR + $MB_TOPMOST, "ERROR!", "Unable to elevate to Admin due to UAC.") Exit EndIf MsgBox($MB_ICONINFORMATION, @UserName, "Is " & (IsAdmin() ? "" : "Not " ) & "Admin") ;Example Global $sDrive = EnvGet("systemdrive") ;Admin part of script. If IsAdmin() Then MsgBox ($MB_OK, "Admin Run Test", "Run Admin part of script and then exit to run as user who started the script.") ;Example Run($sDrive & "\vendor\vendor.exe") ;Wait for Vendor Version Control to close Do Sleep(100) Until Not WinExists("Vendor Version Control") ;Wait for VendorAppLauncher to exist ProcessWait("VendorAppLauncher.exe") ;Close the Vendor Launcher as Admin ProcessClose("VendorAppLauncher.exe") ;Exit to finish Admin part of script. Exit EndIf ;Put rest of the non Admin part of script here. ;Re-open Vendor Launcher as user Run($sDrive & "\Vendor\VendorAppLauncher.exe", $sDrive & "\Vendor")
Adam
-
AdamUL got a reaction from v4nandu in unable to open snipping tool using run
Another way, if you want or need to run 32-bit is to turn off file system redirection.
#include <WinAPIFiles.au3> ;Turn off redirection for a 32-bit script on 64-bit system. If @OSArch = "X64" And Not @AutoItX64 Then _WinAPI_Wow64EnableWow64FsRedirection(False) Global $sFile = "C:\WINDOWS\system32\SnippingTool.exe" If FileExists($sFile) Then Run($sFile) Else MsgBox(0,'File Error', "The Windows snipping tool was not found") EndIf
Adam
-
AdamUL got a reaction from PoojaKrishna in Time and Date Conversion UDF
Here is a UDF that I created when I had to do a large project working with dates and times, and calculations with them. I need to convert the time from 12 hr time with "AM" and "PM", and sometimes with "a" and "p", to 24 hr time for calculations, and convert the time back to 12 hr when needed. I also needed to convert "MM/DD/YYYY" to "YYYY/MM/DD" for use in calculations as well, and convert back as well.
For details on each function, please look at the function headers.
Look at Melba23's more flexible Date_Time_Convert UDF. I use that UDF more now than this one, but I still use "IsBetween" functions in this UDF.
DateTimeConvert.au3
#include-once #include <Date.au3> #AutoIt3Wrapper_AU3Check_Parameters= -d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 #AutoIt3Wrapper_AU3Check_Stop_OnWarning=Y ; #INDEX# ======================================================================================================================= ; Title .........: Time and Date Conversion Library ; AutoIt Version : 3.3.6++ ; UDF Version ...: 1.1 ; Language ......: English ; Description ...: Converts time between 12 hr and 24 hr with other time and date related functions. ; Dll ...........: ; Author(s) .....: Adam Lawrence (AdamUL) ; Email .........: ; Modified.......: ; Contributors ..: ; Resources .....: ; Remarks .......: ; =============================================================================================================================== ; #CURRENT# ===================================================================================================================== ;_IsTime12Hr ;_IsTime24Hr ;_Time12HrTo24Hr ;_Time24HrTo12Hr ;_IsCalcDate ;_IsStandardDate ;_IsDateAndTime ;_DateStandardToCalcDate ;_DateCalcToStandardDate ;_IsBetweenTimes ;_IsBetweenDatesTimes ;_IsBetweenDatesTimesLite ; =============================================================================================================================== ; #FUNCTION# ==================================================================================================================== ; Name ..........: _IsTime12Hr ; Description ...: Checks to see if a time string is in the 12 hr (AM/PM) format. ; Syntax ........: _IsTime12Hr($sTime) ; Parameters ....: $sTime - A string value in time format. ; Return values .: Success - True ; Failure - False, sets @error to: ; |0 - String is not in 12 hr time format. ; |1 - Invalid time format string. ; Author ........: Adam Lawrence (AdamUL) ; Modified ......: ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: No ; =============================================================================================================================== Func _IsTime12Hr($sTime) If StringRegExp($sTime, "^(1[0-2]|[1-9]):([0-5]\d):?([0-5]\d)?(?-i:\h*)(?i)([ap]m?)$") Then Return True If @error Then Return SetError(1, 0, False) Return False EndFunc ; #FUNCTION# ==================================================================================================================== ; Name ..........: _IsTime24Hr ; Description ...: Checks to see if a time string is in the 24 hr format. ; Syntax ........: _IsTime24Hr($sTime) ; Parameters ....: $sTime - A string value in time format. ; Return values .: Success - True ; Failure - False, sets @error to: ; |0 - String is not in 24 hr time format. ; |1 - Invalid time format string. ; Author ........: Adam Lawrence (AdamUL) ; Modified ......: ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: No ; =============================================================================================================================== Func _IsTime24Hr($sTime) If StringRegExp($sTime, "^([01]?\d|2[0-3]):([0-5]\d):?([0-5]\d)?$") Then Return True If @error Then Return SetError(1, 0, False) Return False EndFunc ; #FUNCTION# ==================================================================================================================== ; Name ..........: _Time12HrTo24Hr ; Description ...: Convert 12 hr (AM/PM) time string to a 24 hr time string. ; Syntax ........: _Time12HrTo24Hr($sTime[, $fDisplaySecs = True[, $fHourLeadingZero = False]]) ; Parameters ....: $sTime - "hh:mm:ss AM/PM" time format. ; $fDisplaySecs - [optional] A boolean value to display seconds values. Default is True. ; $fHourLeadingZero - [optional] A boolean value to pad leading zero to single digit hours. Default is False. ; Return values .: Success - A string value in "hh:mm:ss" 24 hr time string. ; Failure - "", sets @error to: ; |1 - Invalid time format string ; Author ........: Adam Lawrence (AdamUL) ; Modified ......: ; Remarks .......: ; Related .......: _Time24HrTo12Hr ; Link ..........: ; Example .......: _Time12HrTo24Hr("12:30AM"), _Time12HrTo24Hr("1:30:45 PM"), _Time12HrTo24Hr("12:30 pm") ; =============================================================================================================================== Func _Time12HrTo24Hr($sTime, $fDisplaySecs = True, $fHourLeadingZero = False) Local $aTime = StringRegExp($sTime, "^(1[0-2]|[1-9]):([0-5]\d):?([0-5]\d)?(?-i:\h*)(?i)([ap]m?)$", 1) If @error Then Return SetError(1, 0, "") Local $sHour = $aTime[0] Local $sMins = $aTime[1] Local $sSecs = $aTime[2] Local $sAMPM = $aTime[3] $sHour = Mod($sHour, 12) If StringInStr($sAMPM, "p") Then $sHour += 12 If $fHourLeadingZero And Number($sHour) < 10 And StringLen($sHour) = 1 Then $sHour = "0" & $sHour If $fDisplaySecs And $sSecs = "" Then $sSecs = "00" If $fDisplaySecs Then Return $sHour & ":" & $sMins & ":" & $sSecs Return $sHour & ":" & $sMins EndFunc ; #FUNCTION# ==================================================================================================================== ; Name ..........: _Time24HrTo12Hr ; Description ...: Convert 24 hr time string to a 12 hr (AM/PM) time string. ; Syntax ........: _Time24HrTo12Hr($sTime[, $fDisplaySecs = True[, $fHourLeadingZero = False[, $sAMPMDelim = " "]]]) ; Parameters ....: $sTime - A string value in "hh:mm:ss" time format. ; $fDisplaySecs - [optional] A boolean value to display seconds values. Default is True. ; $fHourLeadingZero - [optional] A boolean value to pad leading zero to single digit hours. Default is False. ; $sAMPMDelim - [optional] A string value delimiter to seperate AM/PM from the numeric time. Default is " ". ; Return values .: Success - "hh:mm:ss AM/PM" time format. ; Failure - "", sets @error to: ; |1 - Invalid time format string. ; Author ........: Adam Lawrence (AdamUL) ; Modified ......: ; Remarks .......: ; Related .......: _Time12HrTo24Hr ; Link ..........: ; Example .......: _Time24HrTo12Hr("0:30"), _Time24HrTo12Hr("15:45:36"), _Time24HrTo12Hr("5:36") ; =============================================================================================================================== Func _Time24HrTo12Hr($sTime, $fDisplaySecs = True, $fHourLeadingZero = False, $sAMPMDelim = " ") Local $aTime = StringRegExp($sTime, "^([01]?\d|2[0-3]):([0-5]\d):?([0-5]\d)?$", 1) If @error Then Return SetError(1, 0, "") If UBound($aTime) = 2 Then ReDim $aTime[3] Local $sHour = $aTime[0] Local $sMins = $aTime[1] Local $sSecs = $aTime[2] Local $sAMPM = "" Switch $sHour Case 0 $sHour = 12 $sAMPM = "AM" Case 1 To 11 $sAMPM = "AM" Case 12 $sAMPM = "PM" Case 13 To 23 $sHour = $sHour - 12 $sAMPM = "PM" Case Else EndSwitch If $fHourLeadingZero And Number($sHour) < 10 And StringLen($sHour) = 1 Then $sHour = "0" & $sHour If Not $fHourLeadingZero And Number($sHour) < 10 And StringLen($sHour) = 2 Then $sHour = Number($sHour) If $fDisplaySecs And $sSecs = "" Then $sSecs = "00" If $fDisplaySecs Then Return $sHour & ":" & $sMins & ":" & $sSecs & $sAMPMDelim & $sAMPM Return $sHour & ":" & $sMins & $sAMPMDelim & $sAMPM EndFunc ; #FUNCTION# ==================================================================================================================== ; Name ..........: _IsCalcDate ; Description ...: Checks to see if a date is in a format for calculations. ; Syntax ........: _IsCalcDate($sDate) ; Parameters ....: $sDate - A string value date format. ; Return values .: Success - True ; Failure - False, sets @error to 1. ; Author ........: Adam Lawrence (AdamUL) ; Modified ......: ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: No ; =============================================================================================================================== Func _IsCalcDate($sDate) If StringRegExp($sDate, "^(\d{4})/(\d{1,2})/(\d{1,2})$") Then Return True If @error Then Return SetError(1, 0, False) Return False EndFunc ; #FUNCTION# ==================================================================================================================== ; Name ..........: _IsStandardDate ; Description ...: Checks to see if a date is in a standard date format, "MM/DD/YYYY". ; Syntax ........: _IsStandardDate($sDate) ; Parameters ....: $sDate - A string value in date format. ; Return values .: Success - True ; Failure - False, sets @error to 1. ; Author ........: Adam Lawrence (AdamUL) ; Modified ......: ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: No ; =============================================================================================================================== Func _IsStandardDate($sDate) If StringRegExp($sDate, "^(\d{1,2})/(\d{1,2})/(\d{4})$") Then Return True If @error Then Return SetError(1, 0, False) Return False EndFunc ; #FUNCTION# ==================================================================================================================== ; Name ..........: _IsDateAndTime ; Description ...: Checks to see if a string is in a date and time format. ; Syntax ........: _IsDateAndTime($sDateTime) ; Parameters ....: $sDateTime - A string value in date and time format. ; Return values .: Success - True ; Failure - False, sets @error to 1. ; Author ........: Adam Lawrence (AdamUL) ; Modified ......: ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: No ; =============================================================================================================================== Func _IsDateAndTime($sDateTime) Local $sRegEx = "^((?:\d{1,2}/\d{1,2}/\d{4})|(?:\d{4}/\d{1,2}/\d{1,2}))?(?-i:\h*)?((?:1[0-2]|[1-9]):(?:[0-5]\d):?(?:[0-5]\d)?(?-i:\h*)(?i:[ap]m?)|(?:[01]?\d|2[0-3]):(?:[0-5]\d):?(?:[0-5]\d)?)$" If StringRegExp($sDateTime, $sRegEx) Then Return True If @error Then Return SetError(1, 0, False) Return False EndFunc ; #FUNCTION# ==================================================================================================================== ; Name ..........: _DateStandardToCalcDate ; Description ...: Convert a date from "MM/DD/YYYY" to "YYYY/MM/DD" format to use in date calculations. ; Syntax ........: _DateStandardToCalcDate($sDate) ; Parameters ....: $sDate - A string value in "MM/DD/YYYY" format. ; Return values .: Success - Calc date string. ; Failure - "", sets @error to: ; |1 - Invalid date format. ; Author ........: Adam Lawrence (AdamUL) ; Modified ......: ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: No ; =============================================================================================================================== Func _DateStandardToCalcDate($sDate) If Not StringRegExp($sDate, "^(\d{1,2})/(\d{1,2})/(\d{4})$") Then Return SetError(1, 0, "") If @error Then Return SetError(1, 0, "") Local $sDateNew = StringRegExpReplace($sDate, "(\d{2})/(\d{2})/(\d{4})", "$3/$1/$2") $sDateNew = StringRegExpReplace($sDateNew, "(\d{2})/(\d)/(\d{4})", "$3/$1/0$2") $sDateNew = StringRegExpReplace($sDateNew, "(\d)/(\d{2})/(\d{4})", "$3/0$1/$2") $sDateNew = StringRegExpReplace($sDateNew, "(\d)/(\d)/(\d{4})", "$3/0$1/0$2") Return $sDateNew EndFunc ; #FUNCTION# ==================================================================================================================== ; Name ..........: _DateCalcToStandardDate ; Description ...: Convert a date from "YYYY/MM/DD" to "MM/DD/YYYY" format. ; Syntax ........: _DateCalcToStandardDate($sDate) ; Parameters ....: $sDate - A string value in "YYYY/MM/DD" format. ; Return values .: Success - Standard date string ; Failure - "", sets @error to: ; |1 - Invalid date format. ; Author ........: Adam Lawrence (AdamUL) ; Modified ......: ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: No ; =============================================================================================================================== Func _DateCalcToStandardDate($sDate) Local $aDate = StringRegExp($sDate, "^(\d{4})/(\d{1,2})/(\d{1,2})$", 1) If @error Then Return SetError(1, 0, "") Local $sYear = $aDate[0] Local $sMonth = $aDate[1] Local $sDay = $aDate[2] Return Number($sMonth) & "/" & Number($sDay) & "/" & $sYear EndFunc Func _DateTimeStandardToCalcDateTime($sDateTime) Local $sRegEx = "^((?:\d{1,2}/\d{1,2}/\d{4})|(?:\d{4}/\d{1,2}/\d{1,2}))?(?-i:\h*)?((?:1[0-2]|[1-9]):(?:[0-5]\d):?(?:[0-5]\d)?(?-i:\h*)(?i:[ap]m?)|(?:[01]?\d|2[0-3]):(?:[0-5]\d):?(?:[0-5]\d)?)$" Local $aDateTime = StringRegExp($sDateTime, $sRegEx, 1) If @error Then Return SetError(1, 1, "") Local $sDate = $aDateTime[0] Local $sTime = $aDateTime[1] If _IsStandardDate($sDate) Then $sDate = _DateStandardToCalcDate($sDate) If @error Then Return SetError(2, 1, "") EndIf If Not _IsCalcDate($sDate) Then Return SetError(2, 2, "") If _IsTime12Hr($sTime) Then $sTime = _Time12HrTo24Hr($sTime) If @error Then Return SetError(3, 1, "") EndIf If Not _IsTime24Hr($sTime) Then Return SetError(3, 2, "") Return $sDate & " " & $sTime EndFunc Func _DateTimeCalcToStandardDateTime($sDateTime) Local $sRegEx = "^((?:\d{1,2}/\d{1,2}/\d{4})|(?:\d{4}/\d{1,2}/\d{1,2}))?(?-i:\h*)?((?:1[0-2]|[1-9]):(?:[0-5]\d):?(?:[0-5]\d)?(?-i:\h*)(?i:[ap]m?)|(?:[01]?\d|2[0-3]):(?:[0-5]\d):?(?:[0-5]\d)?)$" Local $aDateTime = StringRegExp($sDateTime, $sRegEx, 1) If @error Then Return SetError(1, 1, "") Local $sDate = $aDateTime[0] Local $sTime = $aDateTime[1] If _IsCalcDate($sDate) Then $sDate = _DateCalcToStandardDate($sDate) If @error Then Return SetError(2, 1, "") EndIf If Not _IsStandardDate($sDate) Then Return SetError(2, 2, "") If _IsTime24Hr($sTime) Then $sTime = _Time24HrTo12Hr($sTime) If @error Then Return SetError(3, 1, "") EndIf If Not _IsTime12Hr($sTime) Then Return SetError(3, 2, "") Return $sDate & " " & $sTime EndFunc ; #FUNCTION# ==================================================================================================================== ; Name ..........: _IsBetweenTimes ; Description ...: Test a time to see if it is between the Start Time and the End Time in a 24 hour day. ; Syntax ........: _IsBetweenTimes($sTestTime, $sStartTime, $sEndTime) ; Parameters ....: $sTestTime - A string value time to test, in 12 hr or 24 hr format. ; $sStartTime - A string value start Time, must be before End Time in 12 hr or 24 hr format. ; $sEndTime - A string value end Time, must be after Start Time in 12 hr or 24 hr format. ; Return values .: Success - True ; Failure - False, sets @error to: ; |0 - Not between times. ; |1 - Invalid 12 Hr format. ; |2 - Invalid 24 Hr format. ; |3 - Invalid time string. ; |4 - End Time before Start Time. ; Author ........: Adam Lawrence (AdamUL) ; Modified ......: ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: No ; =============================================================================================================================== Func _IsBetweenTimes($sTestTime, $sStartTime, $sEndTime) If _IsTime12Hr($sTestTime) Then $sTestTime = _Time12HrTo24Hr($sTestTime) If @error Then Return SetError(1, 1, False) EndIf If _IsTime12Hr($sStartTime) Then $sStartTime = _Time12HrTo24Hr($sStartTime) If @error Then Return SetError(1, 2, False) EndIf If _IsTime12Hr($sEndTime) Then $sEndTime = _Time12HrTo24Hr($sEndTime) If @error Then Return SetError(1, 3, False) EndIf If Not _IsTime24Hr($sTestTime) Then Return SetError(2, 1, False) If Not _IsTime24Hr($sStartTime) Then Return SetError(2, 2, False) If Not _IsTime24Hr($sEndTime) Then Return SetError(2, 3, False) $sTestTime = StringReplace(StringStripWS($sTestTime, 8), ":", "") If @error Or @extended > 2 Then Return SetError(3, 1, False) If @extended = 1 Then $sTestTime &= "00" Local $iTestTime = Number($sTestTime) $sStartTime = StringReplace(StringStripWS($sStartTime, 8), ":", "") If @error Or @extended > 2 Then Return SetError(3, 2, False) If @extended = 1 Then $sStartTime &= "00" Local $iStartTime = Number($sStartTime) $sEndTime = StringReplace(StringStripWS($sEndTime, 8), ":", "") If @error Or @extended > 2 Then Return SetError(3, 3, False) If @extended = 1 Then $sEndTime &= "00" Local $iEndTime = Number($sEndTime) If $iEndTime < $iStartTime Then Return SetError(4, 0, False) If $iTestTime >= $iStartTime And $iTestTime <= $iEndTime Then Return True Return False EndFunc ; #FUNCTION# ==================================================================================================================== ; Name ..........: _IsBetweenDatesTimes ; Description ...: Test a time to see if it is between the Start Date and Time and the End Date and Time. ; Syntax ........: _IsBetweenDatesTimes($sTestDateTime, $sStartDateTime, $sEndDateTime) ; Parameters ....: $sTestDateTime - A string value, Date and Time to test, in 12 hr or 24 hr format "YYYY/MM/DD[ HH:MM:SS]". ; $sStartDateTime - A string value, Start Date and Time, must be before End Date and Time in 12 hr or 24 hr format "YYYY/MM/DD[ HH:MM:SS]". ; $sEndDateTime - A string value, End Date and Time, must be after Start Date and Time in 12 hr or 24 hr format "YYYY/MM/DD[ HH:MM:SS]". ; Return values .: Success - True ; Failure - False, sets @error to: ; |1 - Invalid date format. ; |2 - Error Converting to Calc date. ; |3 - Invalid time format. ; |4 - Invalid $sTestDateTime ; |5 - Invalid $sStartDateTime ; |6 - Invalid $sEndDateTime ; Author ........: Adam Lawrence (AdamUL) ; Modified ......: ; Remarks .......: ; Related .......: _IsBetweenDatesTimesLite, _IsBetweenTimes ; Link ..........: ; Example .......: No ; =============================================================================================================================== Func _IsBetweenDatesTimes($sTestDateTime, $sStartDateTime, $sEndDateTime) Local $sRegEx = "^((?:\d{1,2}/\d{1,2}/\d{4})|(?:\d{4}/\d{1,2}/\d{1,2}))?(?-i:\h*)?((?:1[0-2]|[1-9]):(?:[0-5]\d):?(?:[0-5]\d)?(?-i:\h*)(?i:[ap]m?)|(?:[01]?\d|2[0-3]):(?:[0-5]\d):?(?:[0-5]\d)?)$" Local $aTestDateTime = StringRegExp($sTestDateTime, $sRegEx, 1) If @error Then Return SetError(1, 1, False) Local $sTestDate = $aTestDateTime[0] Local $sTestTime = $aTestDateTime[1] Local $aStartDateTime = StringRegExp($sStartDateTime, $sRegEx, 1) If @error Then Return SetError(1, 2, False) Local $sStartDate = $aStartDateTime[0] Local $sStartTime = $aStartDateTime[1] Local $aEndDateTime = StringRegExp($sEndDateTime, $sRegEx, 1) If @error Then Return SetError(1, 3, False) Local $sEndDate = $aEndDateTime[0] Local $sEndTime = $aEndDateTime[1] Select Case $sTestDate = "" And $sStartDate = "" And $sEndDate = "" $sTestDate = _NowCalcDate() $sStartDate = $sTestDate $sEndDate = $sTestDate Case $sTestDate <> "" And $sStartDate <> "" And $sEndDate <> "" Case $sTestDate = "" And $sStartDate <> "" And $sEndDate <> "" ContinueCase Case $sTestDate <> "" And $sStartDate = "" And $sEndDate <> "" ContinueCase Case $sTestDate <> "" And $sStartDate <> "" And $sEndDate = "" ContinueCase Case Else Return SetError(1, 4, False) EndSelect If _IsStandardDate($sTestDate) Then $sTestDate = _DateStandardToCalcDate($sTestDate) If @error Then Return SetError(2, 1, False) EndIf If _IsStandardDate($sStartDate) Then $sStartDate = _DateStandardToCalcDate($sStartDate) If @error Then Return SetError(2, 2, False) EndIf If _IsStandardDate($sEndDate) Then $sEndDate = _DateStandardToCalcDate($sEndDate) If @error Then Return SetError(2, 3, False) EndIf If Not _IsCalcDate($sTestDate) Then Return SetError(3, 1, False) If Not _IsCalcDate($sStartDate) Then Return SetError(3, 2, False) If Not _IsCalcDate($sEndDate) Then Return SetError(3, 3, False) $sTestDate = $sTestDate & " " $sStartDate = $sStartDate & " " $sEndDate = $sEndDate & " " If _IsTime12Hr($sTestTime) Then $sTestTime = _Time12HrTo24Hr($sTestTime) If @error Then Return SetError(2, 4, False) EndIf If _IsTime12Hr($sStartTime) Then $sStartTime = _Time12HrTo24Hr($sStartTime) If @error Then Return SetError(2, 5, False) EndIf If _IsTime12Hr($sEndTime) Then $sEndTime = _Time12HrTo24Hr($sEndTime) If @error Then Return SetError(2, 6, False) EndIf If Not _IsTime24Hr($sTestTime) Then Return SetError(3, 4, False) If Not _IsTime24Hr($sStartTime) Then Return SetError(3, 5, False) If Not _IsTime24Hr($sEndTime) Then Return SetError(3, 6, False) $sTestDateTime = $sTestDate & $sTestTime $sStartDateTime = $sStartDate & $sStartTime $sEndDateTime = $sEndDate & $sEndTime Local $sStartTestDateTimeDiff = _DateDiff("s", $sStartDateTime, $sTestDateTime) Switch @error Case 2 Return SetError(5, @error, False) Case 3 Return SetError(4, @error, False) EndSwitch Local $sEndTestDateTimeDiff = _DateDiff("s", $sTestDateTime, $sEndDateTime) Switch @error Case 2 Return SetError(4, @error, False) Case 3 Return SetError(6, @error, False) EndSwitch If $sStartTestDateTimeDiff >= 0 And $sEndTestDateTimeDiff >= 0 Then Return True Return False EndFunc ; #FUNCTION# ==================================================================================================================== ; Name ..........: _IsBetweenDatesTimesLite ; Description ...: Test a time to see if it is between the Start Date and Time and the End Date and Time. ; Syntax ........: _IsBetweenDatesTimesLite($sTestDateTime, $sStartDateTime, $sEndDateTime) ; Parameters ....: $sTestDateTime - A string value, Date and Time to test, in 24 hr format "YYYY/MM/DD[ HH:MM:SS]". ; $sStartDateTime - A string value, Start Date and Time, must be before End Date and Time in 24 hr format "YYYY/MM/DD[ HH:MM:SS]". ; $sEndDateTime - A string value, End Date and Time, must be after Start Date and Time in 24 hr format "YYYY/MM/DD[ HH:MM:SS]". ; Return values .: Success - True ; Failure - False, sets @error to: ; |1 - Invalid sTestDateTime. ; |2 - Invalid $sStartDateTime. ; |3 - Invalid $sEndDateTime. ; Author ........: Adam Lawrence (AdamUL) ; Modified ......: ; Remarks .......: Faster return than the _IsBetweenDatesTimes function, but more retricted on the format of the date and time entered. ; Related .......: _IsBetweenDatesTimes, _IsBetweenTimes ; Link ..........: ; Example .......: No ; =============================================================================================================================== Func _IsBetweenDatesTimesLite($sTestDateTime, $sStartDateTime, $sEndDateTime) Local $sStartTestDateTimeDiff = _DateDiff("s", $sStartDateTime, $sTestDateTime) Switch @error Case 2 Return SetError(2, @error, False) Case 3 Return SetError(1, @error, False) EndSwitch Local $sEndTestDateTimeDiff = _DateDiff("s", $sTestDateTime, $sEndDateTime) Switch @error Case 2 Return SetError(1, @error, False) Case 3 Return SetError(3, @error, False) EndSwitch If $sStartTestDateTimeDiff >= 0 And $sEndTestDateTimeDiff >= 0 Then Return True Return False EndFunc I hope others find this UDF useful.
In response to BrewManNH suggestion about date formats, here are some functions created by guinness that could be useful to use with this UDF in relation to US/UK date format: _UKToUSDate() and _USToUKDate()
Lupo73 updated and added a function to this thread, _DateTimeStandard, to convert date and time to a format than can be used with this UDF.
Edit: Removed extra @error values listed in _Time12HrTo24Hr header.
Edit 2: Corrected header for _IsBetweenDatesTimesLite for $sEndDateTime.
Edit 3: Added link to guinness' _UKToUSDate() and _USToUKDate() functions.
Edit 4: Added link to Lupo73's _DateTimeStandard function.
Edit 5: Forum update stripped out in regexes, added file for download.
Adam
-
AdamUL got a reaction from hudsonhock in RunAs
For your remote users, are you going to store the network credentials in the install script? Also, RunAs does not give you full admin rights (Admin Token), even if the user has admin right on the PC. You have to use a workaround of re-execution. Here is an example script.
Global $sAdminUser = "USERNAME" Global $sAdminPassword = "PASSWORD" Global $sDomain = "AD" Global $iLogOnFlag = 0 Global $sParameters = "" ;Elevate with the Admin account. If @UserName <> $sAdminUser And Not IsAdmin() Then $sParameters = "" If Not @Compiled Then $sParameters = ' "' & @ScriptFullPath & '"' EndIf If RunAs($sAdminUser, $sDomain, $sAdminPassword, $iLogOnFlag, @AutoItExe & $sParameters) Then Exit Else Exit MsgBox(16 + 262144, "ERROR!", "Unable to run under administrator account.") EndIf EndIf ;Run with Admin Token in Windows Vista and Higher. If @UserName = $sAdminUser And Not IsAdmin() And Not StringRegExp(@OSVersion, "_(XP|200(0|3))") Then $sParameters = "" If Not @Compiled Then $sParameters = '"' & @ScriptFullPath & '"' EndIf If ShellExecute(@AutoItExe, $sParameters, "", "runas") Then Exit Else Exit MsgBox(16 + 262144, "ERROR!", "Unable to elevate to Admin due to UAC.") EndIf EndIf ;Put rest of the script here. MsgBox(16, $sAdminUser, IsAdmin()) ;ExampleAdam
-
AdamUL got a reaction from Colduction in [Solved] SQLite - SQLite3.dll Can't be Loaded
You are getting the error due to running 32-bit AutoIt. It needs to be run in as 64-bit. Add the following to the top of your script, and it will work, at least for me it does.
#AutoIt3Wrapper_UseX64=Y ;(Y/N) Use AutoIt3_x64 or Aut2Exe_x64. Default=N #include <SQLite.au3> #include <SQLite.dll.au3> _SQLite_Startup(@ScriptDir & "\Config\sqlite3_x64.dll", False, 1) If @error Then MsgBox(16, "SQLite Error", "SQLite3.dll Can't be Loaded! - " & $__g_hPrintCallback_SQLite & @CRLF & @CRLF & "Exiting application / Zavolej Honzovi") Exit -1 EndIf
Adam
-
AdamUL got a reaction from TheXman in Unlock a local user account - Windows 10
You could also use the Local Account UDF.
Example below.
#RequireAdmin #include <LocalAccount.au3> Global $sUserName = "Admin" _AccountDisableProperty($sUserName, $ADS_UF_LOCKOUT) If @error Then ConsoleWrite(@error & @CRLF)
Adam
-
AdamUL got a reaction from Earthshine in Unlock a local user account - Windows 10
You could also use the Local Account UDF.
Example below.
#RequireAdmin #include <LocalAccount.au3> Global $sUserName = "Admin" _AccountDisableProperty($sUserName, $ADS_UF_LOCKOUT) If @error Then ConsoleWrite(@error & @CRLF)
Adam
-
AdamUL got a reaction from robertocm in Retrieve info from google sheets - (Moved)
A little bit of googling, and I found GSpread.NET. I have never used it, so I cannot say how good it is. You may be able to use it with the Excel UDF, but I'm not certain.
Adam
-
AdamUL got a reaction from BigDaddyO in Document frickin huge scripts that are not mine
You could try using Tidy with the /gd switch to generate docs for each test script. The report is a text file, but it might be something you could work with.
https://www.autoitscript.com/autoit3/scite/docs/SciTE4AutoIt3/Tidy.html
Adam
-
AdamUL got a reaction from FrancescoDiMuro in Document frickin huge scripts that are not mine
You could try using Tidy with the /gd switch to generate docs for each test script. The report is a text file, but it might be something you could work with.
https://www.autoitscript.com/autoit3/scite/docs/SciTE4AutoIt3/Tidy.html
Adam
-
AdamUL reacted to Subz in File and printer sharing for Microsoft networks
You can use something like to enable/disable File and Printer Sharing for Microsoft Networks for all network adapters.
#RequireAdmin _FilePrintSharing() Func _FilePrintSharing($_bEnable = True) Switch $_bEnable Case False ;~ Disable File and Printer Sharing for Microsoft Networks RunWait('PowerShell -Command "& {Get-NetAdapter | Disable-NetAdapterBinding -DisplayName ' & "'File and Printer Sharing for Microsoft Networks'" & '}"', "", @SW_HIDE) Case True ;~ Enable File and Printer Sharing for Microsoft Networks RunWait('PowerShell -Command "& {Get-NetAdapter | Enable-NetAdapterBinding -DisplayName ' & "'File and Printer Sharing for Microsoft Networks'" & '}"', "", @SW_HIDE) EndSwitch EndFunc
-
AdamUL got a reaction from Nine in Folder watcher doesn't work when folder has more than a few files
This is not a bug, but a feature. https://www.autoitscript.com/autoit3/docs/intro/running.htm#CommandLine
Adam
-
AdamUL got a reaction from Subz in File and printer sharing for Microsoft networks
Here is a function that I use to enable file and print sharing on Windows 7 and Windows 10 PC.
#RequireAdmin #include <Constants.au3> #include <WinAPIFiles.au3> Global Const $sSystemDir = @WindowsDir & "\System32" ;@SystemDir returns @WindowsDir & "\SYSWOW64" with a 32 bit script. Func _EnableFileAndPrintSharing() ;Enable File and Print Sharing. ;Disable x86 redirection mechanism for a 32-bit script. If @OSArch = "X64" And Not @AutoItX64 Then _WinAPI_Wow64EnableWow64FsRedirection(False) Local $iPIDNetsh = Run('netsh advfirewall firewall set rule group="File and Printer Sharing" new enable=yes', $sSystemDir, @SW_HIDE, $STDERR_MERGED) ;Enable File and Print Sharing ;Enable x86 redirection mechanism for a 32-bit script. If @OSArch = "X64" And Not @AutoItX64 Then _WinAPI_Wow64EnableWow64FsRedirection(True) ProcessWaitClose($iPIDNetsh) Local $sNetshOutput = StringStripWS(StdoutRead($iPIDNetsh), $STR_STRIPLEADING + $STR_STRIPTRAILING) If StringRegExp($sNetshOutput, "Updated \d+ rule\(s\)\.\s*Ok\.") Then Return True Return SetError(1, 0, False) EndFunc ;==>_EnableFileAndPrintSharing
Adam
-
AdamUL reacted to Subz in RegWrite is not working properly on Windows 10 64bit
Group Policy would still be the best option imho:
It can be managed centrally You can apply it only to particular AD User group, for example we apply these settings to our Kiosk, Citrix and RDS users, but don't want this being applied to domain admin accounts, system account or support accounts on those systems. If those users log on to a company desktop/laptop these settings aren't applied. My 2cents
-
AdamUL reacted to water in OutlookEX UDF - Help & Support (IV)
Just as I pressed the "Submit Reply" button I found the solution.
Prepend the recipient to check with an "=" for a strict resolution. Means:
$aRecipient = _OL_ItemRecipientCheck($oOutlook, "=" & $aGroups[$iGroupIndex][1]) ; <== Issue Here _OL_ItemRecipientCheck returning False, and not returning AddressEntry object.
-
AdamUL reacted to TheSaint in TeraCopy Timer
TeraCopy Timer updated to v3.3. See first post.
(v3.3)
The STOP button now has a red area around it when Shutdown is enabled, and its screen position is now recorded & restored.
More Splash are shown for Calculating and Recalculating.
(v3.2)
All relevant controls are now disabled until Job processing has finished.
The STOP button now has additional information in regard to 'Stop Monitoring'.
P.S. By the way, 'Stop Monitoring', also means stop controlling.
-
AdamUL got a reaction from Musashi in Editor problem - (Moved)
Look in the SciTE Help under Contents tab -> SciTE4AutoIt3 -> Abbreviations List for a nicely organized list.
Adam
-
AdamUL got a reaction from Davidowicza in Editor problem - (Moved)
Look in the SciTE Help under Contents tab -> SciTE4AutoIt3 -> Abbreviations List for a nicely organized list.
Adam
-
AdamUL reacted to water in Creating a "brushed up" Task Scheduler UDF?
Examples (The query needs the latest version of the UDF (1.2.1.0) which I'm going to release quite soon):
; Enable/Disable the Registered Task Global $aProperties[] = ["TASK|Enabled|xxxx"] ; Replace xxxx with True or False to enable or disable the task _TS_TaskPropertiesSet($oTask, $aProperties) ; Query the Registered Task $aTaskProperties = _TS_TaskPropertiesGet($oService, $oTask, 1, False, "TASK", "Enabled") MsgBox(0, "Query Task", "Task is enabled = " & $aTaskproperties[0][2])
-
AdamUL reacted to water in Creating a "brushed up" Task Scheduler UDF?
I'm thinking about a function that would return the full error message text when you pass the error number (e.g. from @error) and the function name (default is the name of the last called function).
Means:
#include <TaskScheduler.au3> _TS_Open() $sErrorMessage = _TS_ErrorMessage(1) ; Returns the text for error 1 of function _TS_Open: Error creating the COM error handler. @extended is set to the error code returned by _TS_ErrorNotify $sErrorMessage = _TS_ErrorMessage(2, "_TS_ActionCreate") ; Returns the text for error 2 of function _TS_ActionCreate: Error creating the COM error handler. @extended is set to the error code returned by _TS_ErrorNotify What do you think?
-
AdamUL got a reaction from Werty in Unable to interact with 64 program
You are using ShellExecuteWait. The script is not going to do anything until the mmc.exe is closed. Use ShellExecute instead, and WinWaitActive.
Adam
-
AdamUL got a reaction from FrancescoDiMuro in Creating a "brushed up" Task Scheduler UDF?
@water Your welcome, glad I could help.
@Professor_Bernd Here are some examples of _TS_Open that I used to connect to the different computers.
;Connect to a computer in AD where the current user is in a the local Admininstrators group ;on that computer, via domain or local group, or directly. Use AD computer name. Global $oService = _TS_Open("COMPUTERNAME") ;Connect to a computer in AD where the entered user (AdminUser) is an AD user, and is in a the ;local Admininstrators group on that computer, via domain or local group, or directly. Use AD computer name. Global $oService = _TS_Open("COMPUTERNAME", "AdminUser", "AD", "Password") ;Connect to a computer in AD where the entered local user (Administrator) is NOT an AD user, ;and is in a the local Admininstrators group on that computer, via local group, or directly. Use AD computer name. Global $oService = _TS_Open("COMPUTERNAME", "Administrator", ".", "Password") ;Non-domain computer where the entered local user (Administrator) is in a the local Admininstrators group ;on that computer. Use IP address or DNS name to connect. Global $oService = _TS_Open("192.168.0.1", "Administrator", ".", "Password")
Adam
-
AdamUL got a reaction from Musashi in Creating a "brushed up" Task Scheduler UDF?
@water Your welcome, glad I could help.
@Professor_Bernd Here are some examples of _TS_Open that I used to connect to the different computers.
;Connect to a computer in AD where the current user is in a the local Admininstrators group ;on that computer, via domain or local group, or directly. Use AD computer name. Global $oService = _TS_Open("COMPUTERNAME") ;Connect to a computer in AD where the entered user (AdminUser) is an AD user, and is in a the ;local Admininstrators group on that computer, via domain or local group, or directly. Use AD computer name. Global $oService = _TS_Open("COMPUTERNAME", "AdminUser", "AD", "Password") ;Connect to a computer in AD where the entered local user (Administrator) is NOT an AD user, ;and is in a the local Admininstrators group on that computer, via local group, or directly. Use AD computer name. Global $oService = _TS_Open("COMPUTERNAME", "Administrator", ".", "Password") ;Non-domain computer where the entered local user (Administrator) is in a the local Admininstrators group ;on that computer. Use IP address or DNS name to connect. Global $oService = _TS_Open("192.168.0.1", "Administrator", ".", "Password")
Adam