# Simple timer logic issue

## Recommended Posts

Hi all! I am perplexed by something that should be simple. I am trying to make a small timer which will become part of a larger script, but I'm stuck on comparing the 2 times..

When you run the script, you hit OK on a message box, that's when the current system time and day of the year is saved to a variable.. when you hit OK again, the timer is stopped and the date and time are compared. I'm not using Timerinit() because I want the exact system time the timer started, and the exact system time the timer stopped. Then the time is compared and you get the difference.

This is easy as long as I am subtracting 10 seconds from 15 seconds, but what if the start timeer got second: 45 and the top timer got second: 12?

I'm sure this is cakewalk for anyone that got higher than a C in math, but I didnt.

Can anyone shed some light on this for a simpleton like myself?

```Msgbox(0,"START","Click to start timer")
\$a = timestamp()

Msgbox(0,"START","Click to stop timer")
\$b = timestamp()

compare(\$a,\$b)

Func timestamp()
Local \$stSystemTime = DllStructCreate('ushort;ushort;ushort;ushort;ushort;ushort;ushort;ushort')
DllCall('kernel32.dll', 'none', 'GetSystemTime', 'ptr', DllStructGetPtr(\$stSystemTime))
\$sMilliSeconds = StringFormat('%03d', DllStructGetData(\$stSystemTime, 8))
\$stSystemTime = 0
return @YDay & ':' & @Hour & ':' & @Min & ':' & @Sec & ':' & \$sMilliSeconds
EndFunc

Func compare(\$start,\$stop)
\$Comp_a = StringSplit(\$start,":",2)
\$Comp_b = StringSplit(\$stop,":",2)
\$DayDif  = \$comp_b[0] - \$comp_a[0]
\$HourDif = \$comp_b[1] - \$comp_a[1]
\$MinDif  = \$comp_b[2] - \$comp_a[2]
\$SecDif  = \$comp_b[3] - \$comp_a[3]
If \$comp_b[4] > \$comp_a[4] then
\$MSecDif = \$comp_b[4] - \$comp_a[4]
else
\$MSecDif = \$comp_a[4] - \$comp_b[4]
endif

Msgbox(0,"hey you, yeah you!", "Elapse Time:" & @CRLF & "Days: " & \$DayDif & @CRLF & "Hours: " & \$HourDif & @CRLF & "Minutes: " & \$MinDif & @CRLF & "Seconds: " & \$SecDif & @CRLF & "Miliseconds: " & \$MSecDif  )

endfunc```

EDIT:

Would this logic work?

;example

\$start = 45
\$stop = 12

\$val1 = \$Start - 60 ; val1 becomes 15
\$Mydiff =  \$stop + \$val1
Edited by Kovacic

C0d3 is P0etry( ͡° ͜ʖ ͡°)

##### Share on other sites

Kovacic,

Why not let AutoIt do all the work for you?

```#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
#include <Date.au3>

\$hGUI = GUICreate("Test", 500, 500)

\$cStart = GUICtrlCreateButton("Start", 10, 10, 80, 30)
\$cStop = GUICtrlCreateButton("Stop", 10, 50, 80, 30)
GUICtrlSetState(\$cStop, \$GUI_DISABLE)

GUISetState()

While 1

Switch GUIGetMsg()
Case \$GUI_EVENT_CLOSE
Exit
Case \$cStart
\$sStart = _NowCalc()
GUICtrlSetState(\$cStart, \$GUI_DISABLE)
GUICtrlSetState(\$cStop, \$GUI_ENABLE)
Case \$cStop
\$sStop = _NowCalc()
GUICtrlSetState(\$cStop, \$GUI_DISABLE)
GUICtrlSetState(\$cStart, \$GUI_ENABLE)

\$iDays = _DateDiff("D", \$sStart, \$sStop)
\$iHours = _DateDiff("h", \$sStart, \$sStop)
\$iMins = _DateDiff("n", \$sStart, \$sStop)
ConsoleWrite(\$sStart & @CRLF)
ConsoleWrite(\$sStart & @CRLF)
\$iSecs = _DateDiff("s", \$sStart, \$sStop)

MsgBox(\$MB_SYSTEMMODAL, "Time Diff:", \$iDays & " days, " & \$iHours & ":" & \$iMins & ":" & \$iSecs)

EndSwitch

WEnd```
Much easier!

M23

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

##### Share on other sites

Kovacic,

Why not let AutoIt do all the work for you?

```#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
#include <Date.au3>

\$hGUI = GUICreate("Test", 500, 500)

\$cStart = GUICtrlCreateButton("Start", 10, 10, 80, 30)
\$cStop = GUICtrlCreateButton("Stop", 10, 50, 80, 30)
GUICtrlSetState(\$cStop, \$GUI_DISABLE)

GUISetState()

While 1

Switch GUIGetMsg()
Case \$GUI_EVENT_CLOSE
Exit
Case \$cStart
\$sStart = _NowCalc()
GUICtrlSetState(\$cStart, \$GUI_DISABLE)
GUICtrlSetState(\$cStop, \$GUI_ENABLE)
Case \$cStop
\$sStop = _NowCalc()
GUICtrlSetState(\$cStop, \$GUI_DISABLE)
GUICtrlSetState(\$cStart, \$GUI_ENABLE)

\$iDays = _DateDiff("D", \$sStart, \$sStop)
\$iHours = _DateDiff("h", \$sStart, \$sStop)
\$iMins = _DateDiff("n", \$sStart, \$sStop)
ConsoleWrite(\$sStart & @CRLF)
ConsoleWrite(\$sStart & @CRLF)
\$iSecs = _DateDiff("s", \$sStart, \$sStop)

MsgBox(\$MB_SYSTEMMODAL, "Time Diff:", \$iDays & " days, " & \$iHours & ":" & \$iMins & ":" & \$iSecs)

EndSwitch

WEnd```
Much easier!

M23

That was what I planned originally but I need to use milliseconds, but it looks like _DateDiff only handles as small as seconds.. Good call though

Edited by Kovacic

C0d3 is P0etry( ͡° ͜ʖ ͡°)

##### Share on other sites

Kovacic,

Then we have to be creative:

```Case \$cStart
\$sStart = _NowCalc()
\$iStartMS = @MSEC
ConsoleWrite(\$sStart & "." & \$iStartMS & @CRLF)

GUICtrlSetState(\$cStart, \$GUI_DISABLE)
GUICtrlSetState(\$cStop, \$GUI_ENABLE)
Case \$cStop
\$sStop = _NowCalc()
\$iStopMS = @MSEC
ConsoleWrite(\$sStop & "." & \$iStopMS & @CRLF)

GUICtrlSetState(\$cStop, \$GUI_DISABLE)
GUICtrlSetState(\$cStart, \$GUI_ENABLE)

\$iDays = _DateDiff("D", \$sStart, \$sStop)
\$iHours = _DateDiff("h", \$sStart, \$sStop)
\$iMins = _DateDiff("n", \$sStart, \$sStop)
\$iSecs = _DateDiff("s", \$sStart, \$sStop)

; See if Secs values are the same
\$iMSecs = \$iStopMs - \$iStartMS
If StringRight(\$sStart, 2) <> StringRight(\$sStop, 2) Then
If \$iStartMS > \$iStopMs Then
\$iMSecs = 1000 - \$iStartMS + \$iStopMS
\$iSecs -= 1
EndIf
EndIf

MsgBox(\$MB_SYSTEMMODAL, "Time Diff:", \$iDays & " days, " & \$iHours & ":" & \$iMins & ":" & \$iSecs & "." & \$iMSecs)```
Checking the calculated time against the values written in the console it seems to be correct - over to you for more testing.

M23

Edit: Not quite - still working on it.

2nd Edit: Try now.

Edited by Melba23

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

##### Share on other sites

Kovacic,

Then we have to be creative:

```Case \$cStart
\$sStart = _NowCalc()
\$iStartMS = @MSEC
ConsoleWrite(\$sStart & "." & \$iStartMS & @CRLF)

GUICtrlSetState(\$cStart, \$GUI_DISABLE)
GUICtrlSetState(\$cStop, \$GUI_ENABLE)
Case \$cStop
\$sStop = _NowCalc()
\$iStopMS = @MSEC
ConsoleWrite(\$sStop & "." & \$iStopMS & @CRLF)

GUICtrlSetState(\$cStop, \$GUI_DISABLE)
GUICtrlSetState(\$cStart, \$GUI_ENABLE)

\$iDays = _DateDiff("D", \$sStart, \$sStop)
\$iHours = _DateDiff("h", \$sStart, \$sStop)
\$iMins = _DateDiff("n", \$sStart, \$sStop)
\$iSecs = _DateDiff("s", \$sStart, \$sStop)

; See if Secs values are the same
\$iMSecs = \$iStopMs - \$iStartMS
If StringRight(\$sStart, 2) <> StringRight(\$sStop, 2) Then
If \$iStartMS > \$iStopMs Then
\$iMSecs = 1000 - \$iStartMS + \$iStopMS
\$iSecs -= 1
EndIf
EndIf

MsgBox(\$MB_SYSTEMMODAL, "Time Diff:", \$iDays & " days, " & \$iHours & ":" & \$iMins & ":" & \$iSecs & "." & \$iMSecs)```
Checking the calculated time against the values written in the console it seems to be correct - over to you for more testing.

M23

Edit: Not quite - still working on it.

2nd Edit: Try now.

Looking much better, im looking at the Ms now, heres what I got while testing:

Start: 2014/07/03 12:21:54.200
Stop: 2014/07/03 12:21:55.879

Diff reported by script:  Seconds: 1   Msec: 1679

Actual diff: Seconds: 1 Msec: 679

I think my brain hurts lol

EDIT:

one of our calculations is close... im about to build a counting GUI to test the theory:

```If \$stop[1] >= \$Start[1] then
\$HourDif = \$stop[1] - \$Start[1]
else
\$HourDif =  \$Start[1] + (\$stop[1] - 60)
endif```
Edited by Kovacic

C0d3 is P0etry( ͡° ͜ʖ ͡°)

##### Share on other sites

I find that when comparing times it's much easier to convert both times to msec's subtract one from the other, and then turn that result into hh:mm:ss:ms rather than trying to do the math on the raw data.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

##### Share on other sites

I find that when comparing times it's much easier to convert both times to msec's subtract one from the other, and then turn that result into hh:mm:ss:ms rather than trying to do the math on the raw data.

that was one of my earlier thoughts, however I will need to grab the current time and day, then a later time and day, and compare the two. The only way I can break that down to Msec is if I grab the current MS since the beginning of the year and calculate since then.

C0d3 is P0etry( ͡° ͜ʖ ͡°)

##### Share on other sites

Kovacic,

A couple of glasses of good red wine later - this might be the solution (with a big shout to BrewManNH for his suggestion above):

```Case \$cStop
\$sStop = _NowCalc()
\$iStopMS = @MSEC
ConsoleWrite(\$sStop & "." & \$iStopMS & @CRLF)

GUICtrlSetState(\$cStop, \$GUI_DISABLE)
GUICtrlSetState(\$cStart, \$GUI_ENABLE)

\$iStartMs =  (Number(StringRight(\$sStart, 2)) * 1000) + \$iStartMS
\$iStopMs = (Number(StringRight(\$sStop, 2)) * 1000) + \$iStopMS
\$iDiffMs = \$iStopMs - \$iStartMS

\$iDays = _DateDiff("D", \$sStart, \$sStop)
\$iHours = _DateDiff("h", \$sStart, \$sStop)
\$iMins = _DateDiff("n", \$sStart, \$sStop)
\$iSecs = Int(\$iDiffMs / 1000)
\$iMSecs = \$iDiffMs - (\$iSecs * 1000)

ConsoleWrite(\$iSecs & "." & \$iMSecs & @CRLF)```
I cannot break it after a good few tests - your turn.

M23

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

##### Share on other sites

also this could do

```MsgBox(0, "START", "Click to start timer")
\$a = timestamp()

MsgBox(0, "STOP", "Click to stop timer")
\$b = timestamp()

compare(\$a, \$b)

Func timestamp()
Return @YDAY & ':' & @HOUR & ':' & @MIN & ':' & @SEC & ':' & @MSEC
EndFunc   ;==>timestamp

Func compare(\$start, \$stop)
\$Comp_a = StringSplit(\$start, ":", 2)
\$Comp_b = StringSplit(\$stop, ":", 2)
Local \$comp_c[5]

; start
\$Comp_a[0] = \$Comp_a[0] * 86400000 ; Day
\$Comp_a[1] = \$Comp_a[1] * 3600000 ; Hour
\$Comp_a[2] = \$Comp_a[2] * 60000 ; Mins
\$Comp_a[3] = \$Comp_a[3] * 1000 ; Sec

; stop
\$Comp_b[0] = \$Comp_b[0] * 86400000 ; Day
\$Comp_b[1] = \$Comp_b[1] * 3600000 ; Hour
\$Comp_b[2] = \$Comp_b[2] * 60000 ; Mins
\$Comp_b[3] = \$Comp_b[3] * 1000 ; Sec

Local \$started = \$Comp_a[0] + \$Comp_a[1] + \$Comp_a[2] + \$Comp_a[3] + \$Comp_a[4]
Local \$stopped = \$Comp_b[0] + \$Comp_b[1] + \$Comp_b[2] + \$Comp_b[3] + \$Comp_b[4]

Local \$elapsed = \$stopped - \$started ; elapsed time in milliseconds

; rebuild "days/time"
\$comp_c[0] = Int(\$elapsed / 86400000) ; days
\$elapsed -= \$comp_c[0] * 86400000
\$comp_c[1] = Int(\$elapsed / 3600000) ; hours
\$elapsed -= \$comp_c[1] * 3600000
\$comp_c[2] = Int(\$elapsed / 60000) ; mins
\$elapsed -= \$comp_c[2] * 60000
\$comp_c[3] = Int(\$elapsed / 1000) ; secs
\$elapsed -= \$comp_c[3] * 1000
\$comp_c[4] = \$elapsed ; milliseconds

Local \$duration = \$comp_c[0] & ":" & StringFormat("%02s", \$comp_c[1]) & ":" & StringFormat("%02s", \$comp_c[2]) & ":" & StringFormat("%02s", \$comp_c[3]) & ":" & StringFormat("%03s", \$comp_c[4])

MsgBox(0, \$duration, "Started at " & \$start & @CRLF & "Stopped at " & \$stop & @CRLF & "Elapse Time : " & @CRLF & " Days : " & \$comp_c[0] & @CRLF & " Hours : " & \$comp_c[1] & @CRLF & " Minutes : " & \$comp_c[2] & @CRLF & " Seconds : " & \$comp_c[3] & @CRLF & " Miliseconds : " & \$comp_c[4])
EndFunc   ;==>compare```

p.s.

is there a glass of wine for me too?

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

##### Share on other sites

Between those two answers, I think that solved it!! Thanks!!!!!!

WINE (or beer) FOR ALL!

Edited by Kovacic

C0d3 is P0etry( ͡° ͜ʖ ͡°)

##### Share on other sites

a cold beer for me, thanks

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

## Create an account

Register a new account

• ### Similar Content

• By Inpho
Hi All,
I intend on keeping custom functions/UDFs (works in progress) here; if anyone wants to use any code, feel free.
String functions:
#AutoIt3Wrapper_AU3Check_Parameters=-d -w- 1 -w 2 -w 3 -w 4 -w 5 -w 6 #include-once ; #FUNCTION# ==================================================================================================================== ; Name ..........: _DateTimeGet ; Description ...: Returns the date and time formatted for use in sortable filenames, logs, listviews, etc. ; Syntax ........: _DateTimeGet(iType = 1[, \$bHumanFormat = False]) ; Parameters ....: \$iType - [optional] an integer value. Default is 1. ; 1 - Date and time in file-friendly format; 20190115_113756 ; 2 - Date in file-friendly format; 20190115 ; 3 - Time in file friendly format; 113756 ; \$bHumanFormat - [optional] a boolean value. Default is False. ; True - Includes slashes in the date and colons in the time with a space inbetween ; False - No slashes or colons included with an underscore inbetween ; Return values .: Success - String ; Failure - Sets @error to non-zero and returns an empty string ; Author ........: Sam Coates ; =============================================================================================================================== Func _DateTimeGet(\$iType = 1, \$bHumanFormat = False) If \$iType < 1 Or \$iType > 3 Then Return (SetError(-1, 0, "")) ;; Param1: ;; 1 = Date and time in file friendly format: 20190115_113756 ;; 2 = Date in file friendly format: 20190115 ;; 3 = Time in file friendly format: 113756 ;; Param2: ;; True = Use human-readable format: 15/01/2019 11:37:56 Local \$sTime = @HOUR & ":" & @MIN & ":" & @SEC Local \$sDate = @MDAY & "/" & @MON & "/" & @YEAR If \$iType = 1 Then If \$bHumanFormat = False Then \$sTime = StringReplace(\$sTime, ":", "") \$sDate = StringReplace(\$sDate, "/", "") \$sDate = StringTrimLeft(\$sDate, 4) & StringMid(\$sDate, 3, 2) & StringLeft(\$sDate, 2) Return (\$sDate & "_" & \$sTime) Else Return (\$sDate & " " & \$sTime) EndIf ElseIf \$iType = 2 Then If \$bHumanFormat = False Then \$sDate = StringReplace(\$sDate, "/", "") \$sDate = StringTrimLeft(\$sDate, 4) & StringMid(\$sDate, 3, 2) & StringLeft(\$sDate, 2) EndIf Return (\$sDate) ElseIf \$iType = 3 Then If \$bHumanFormat = False Then \$sTime = StringReplace(\$sTime, "/", "") EndIf Return (\$sTime) EndIf EndFunc ;==>_DateTimeGet ; #FUNCTION# ==================================================================================================================== ; Name ..........: _FileToFileExtension ; Description ...: Returns a file extension from a filename/FQPN (Fully Qualified Path Name) ; Syntax ........: _FileToFileExtension(\$sPath) ; Parameters ....: \$sPath - a string value. ; Return values .: Success - String ; Failure - Empty string as returned from StringTrimLeft() ; Author ........: Sam Coates ; =============================================================================================================================== Func _FileToFileExtension(\$sPath) Return (StringTrimLeft(\$sPath, StringInStr(\$sPath, ".", 0, -1))) EndFunc ;==>_FileToFileExtension ; #FUNCTION# ==================================================================================================================== ; Name ..........: _FileToFileName ; Description ...: Returns a filename from a FQPN (Fully Qualified Path Name) ; Syntax ........: _FileToFileName(\$sPath[, \$bIncludeExtension = True]) ; Parameters ....: \$sPath - a string value. ; \$bIncludeExtension - [optional] a boolean value. Default is True. ; Return values .: Success - String ; Failure - Empty string as returned from StringLeft() ; Author ........: Sam Coates ; =============================================================================================================================== Func _FileToFileName(\$sPath, \$bIncludeExtension = True) Local \$sReturn = StringTrimLeft(\$sPath, StringInStr(\$sPath, "\", 0, -1)) If \$bIncludeExtension = False Then \$sReturn = StringLeft(\$sReturn, StringInStr(\$sReturn, ".", 0, -1) - 1) Return (\$sReturn) EndFunc ;==>_FileToFileName ; #FUNCTION# ==================================================================================================================== ; Name ..........: _FileToFilePath ; Description ...: Returns a folder path from a FQPN (Fully Qualified Path Name) ; Syntax ........: _FileToFilePath(\$sPath) ; Parameters ....: \$sPath - a string value. ; Return values .: Success - String ; Failure - Empty string as returned from StringLeft() ; Author ........: Sam Coates ; =============================================================================================================================== Func _FileToFilePath(\$sPath) Return (StringLeft(\$sPath, StringInStr(\$sPath, "\", 0, -1) - 1)) EndFunc ;==>_FileToFilePath ; #FUNCTION# ==================================================================================================================== ; Name ..........: _StringLeft ; Description ...: Searches for a string inside a string, then removes everything on the right of that string ; Syntax ........: _StringLeft(\$sString, \$sRemove[, \$iCaseSense = 0, \$iOccurrence = 1]) ; Parameters ....: \$sString - a string value. The string to search inside. ; \$sRemove - a string value. The string to search for. ; \$iCaseSense - an integer value. Flag to indicate if the operations should be case sensitive. ; \$iOccurrence - an integer value. Which occurrence of the substring to find in the string. Use a ; negative occurrence to search from the right side. ; Return values .: Success - String ; Failure - Empty string as returned from StringLeft() ; Author ........: Sam Coates ; =============================================================================================================================== Func _StringLeft(\$sString, \$sRemove, \$iCaseSense = 0, \$iOccurrence = 1) Return (StringLeft(\$sString, StringInStr(\$sString, \$sRemove, \$iCaseSense, \$iOccurrence) - 1)) EndFunc ;==>_StringLeft ; #FUNCTION# ==================================================================================================================== ; Name ..........: _StringRandom ; Description ...: Returns a string of random characters ; Syntax ........: _StringRandom(\$iAmount[, \$iType = 1]) ; Parameters ....: \$iAmount - an integer value. Length of returned string ; \$iType - [optional] an integer value. Default is 1. ; 1 - Return digits (0-9) ; 2 - Return hexadecimal (0-9, A - F) ; 3 - Return Alphanumeric upper (0-9, A - Z) ; 4 - Return Alphanumeric (0-9, A - Z, a - z) ; 5 - Return Alpha upper (A - Z) ; 6 - Return Alpha (A - Z, a - z) ; Return values .: Success - String ; Failure - Empty string and @error flag as follows: ; @error : 1 - \$iAmount is not a positive integer ; 2 - \$iType is out of bounds ; Author ........: Sam Coates ; =============================================================================================================================== Func _StringRandom(\$iAmount, \$iType = 1) If \$iAmount < 1 Or IsInt(\$iAmount) = 0 Then Return (SetError(-1, 0, "")) Local \$sString = "" Local \$iRandomLow = 1, \$iRandomHigh = 62 #Tidy_Off Local Static \$aCharId[63] = [0, Chr(48), Chr(49), Chr(50), Chr(51), Chr(52), Chr(53), Chr(54), Chr(55), Chr(56), Chr(57), Chr(65), Chr(66), Chr(67), _ Chr(68), Chr(69), Chr(70), Chr(71), Chr(72), Chr(73), Chr(74), Chr(75), Chr(76), Chr(77), Chr(78), Chr(79), Chr(80), _ Chr(81), Chr(82), Chr(83), Chr(84), Chr(85), Chr(86), Chr(87), Chr(88), Chr(89), Chr(90), Chr(97), Chr(98), Chr(99), _ Chr(100), Chr(101), Chr(102), Chr(103), Chr(104), Chr(105), Chr(106), Chr(107), Chr(108), Chr(109), Chr(110), Chr(111), _ Chr(112), Chr(113), Chr(114), Chr(115), Chr(116), Chr(117), Chr(118), Chr(119), Chr(120), Chr(121), Chr(122)] #Tidy_On If \$iType = 1 Then ;; digits: 1 - 10 \$iRandomHigh = 10 ElseIf \$iType = 2 Then ;; hexadecimal: 1 - 16 \$iRandomHigh = 16 ElseIf \$iType = 3 Then ;; alnumupper: 1 - 36 \$iRandomHigh = 36 ElseIf \$iType = 4 Then ;; alnum: 1 - 62 \$iRandomHigh = 62 ElseIf \$iType = 5 Then ;; alphaupper: 11 - 36 \$iRandomLow = 11 \$iRandomHigh = 36 ElseIf \$iType = 6 Then ;; alpha: 11 = 62 \$iRandomLow = 11 \$iRandomHigh = 62 Else Return (SetError(-2, 0, "")) EndIf For \$i = 1 To \$iAmount \$sString &= \$aCharId[Random(\$iRandomLow, \$iRandomHigh, 1)] ;; append string with corresponding random character from ascii array Next Return (\$sString) EndFunc ;==>_StringRandom ; #FUNCTION# ==================================================================================================================== ; Name ..........: _StringTrimLeft ; Description ...: Searches for a string inside a string, then removes everything on the left of that string ; Syntax ........: _StringTrimLeft(\$sString, \$sRemove[, \$iCaseSense = 0, \$iOccurrence = 1]) ; Parameters ....: \$sString - a string value. The string to search inside. ; \$sRemove - a string value. The string to search for. ; \$iCaseSense - an integer value. Flag to indicate if the operations should be case sensitive. ; \$iOccurrence - an integer value. Which occurrence of the substring to find in the string. Use a ; negative occurrence to search from the right side. ; Return values .: Success - String ; Failure - Empty string as returned from StringTrimLeft() ; Author ........: Sam Coates ; =============================================================================================================================== Func _StringTrimLeft(\$sString, \$sRemove, \$iCaseSense = 0, \$iOccurrence = 1) Return (StringTrimLeft(\$sString, StringInStr(\$sString, \$sRemove, \$iCaseSense, \$iOccurrence) + StringLen(\$sRemove) - 1)) EndFunc ;==>_StringTrimLeft Examples:
ConsoleWrite(_StringRandom(100, 6) & @CRLF) ConsoleWrite(_StringTrimLeft("C:\Windows\System32\cmd.exe", "C:\Windows\System32\") & @CRLF) ConsoleWrite(_StringLeft("C:\Windows\System32\cmd.exe", "cmd.exe") & @CRLF) ConsoleWrite(_FileToFileName("C:\Windows\System32\cmd.exe") & @CRLF) ConsoleWrite(_FileToFilePath("C:\Windows\System32\cmd.exe") & @CRLF) ConsoleWrite(_FileToFileExtension("C:\Windows\System32\cmd.exe") & @CRLF) ConsoleWrite(_StringRandom(6, 4) & "-" & _StringRandom(4, 4) & "-" & _StringRandom(4, 4) & "-" & _StringRandom(4, 4) & "-" & _StringRandom(6, 4)& @CRLF)

• I was looking for something like this, in the forum and my HDD. Bad hair day. Anyway,
Here is the code
• By Simpel
Hi,
I wanted to use _GetLogonTime() by guinness (https://www.autoitscript.com/forum/topic/19370-autoit-wrappers/?do=findComment&comment=942069).
#include <Date.au3> #include <Array.au3> ConsoleWrite(_GetLogonTime() & @LF) Func _GetLogonTime(\$sUserName = @UserName, \$sComputerName = @ComputerName) ; Idea by trancexx: http://www.autoitscript.com/forum/topic/113611-if-isadmin-not-detected-as-admin/ Local \$aRet = DllCall("netapi32.dll", "long", "NetUserGetInfo", "wstr", \$sComputerName, "wstr", \$sUserName, "dword", 11, "ptr*", 0) _ArrayDisplay(\$aRet) If @error Or \$aRet[0] Then Return SetError(1, 0, False) Local \$sHours = DllStructGetData(DllStructCreate("ptr;ptr;ptr;ptr;dword;dword;dword;ptr;ptr;dword;dword;dword;dword;ptr;dword;ptr;dword;dword;byte;dword", \$aRet[4]), 18) DllCall("netapi32.dll", "long", "NetApiBufferFree", "ptr", \$aRet[4]) Return _DateAdd("h", "-" & \$sHours, _NowCalc()) EndFunc ;==>_GetLogonTime It always returns false.
I displayed \$aRet:
[0]|2221
[1]|mycomputer
[2]|myuser
[3]|11
[4]|0x0000000000000000
\$aRet[4] shouldn't be 0x0000000000000000 right? Any help?

• By Skeletor
Hey All,
I'm trying to set the date using a variable.
Basically, I set the date into the input box, then I change the input box, say, I change the year.
Then I set what I typed into the input box into the Date Picker.

This is a demo code.
#include <ButtonConstants.au3> ;Start GUI includes #include <EditConstants.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> #include <DateTimeConstants.au3> #include <GUIConstantsEx.au3> #include <GuiDateTimePicker.au3> #Region ### START Koda GUI section ### Form= \$Form1 = GUICreate("Form1", 615, 438, 192, 124) \$Date1 = GUICtrlCreateDate("2019/02/02 23:16:26", 80, 64, 186, 21, \$DTS_SHORTDATEFORMAT) \$Input1 = GUICtrlCreateInput("Input1", 80, 152, 185, 21) \$Button1 = GUICtrlCreateButton("Set data", 176, 96, 75, 25) \$Button2 = GUICtrlCreateButton("Read from input", 176, 200, 75, 25) GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### While 1 \$nMsg = GUIGetMsg() Switch \$nMsg Case \$GUI_EVENT_CLOSE Exit Case \$Button1 \$Read = GUICtrlRead(\$Date1) GUICtrlSetData(\$Input1, \$Read) MsgBox(-1, "", \$Read) Case \$Button2 \$Read = GUICtrlRead(\$Input1) \$New_date = StringReplace(\$Read, "/", "") \$DAY = StringLeft(\$New_date, 2) \$MON = StringMid(\$New_date, 4, 3) \$YEAR = StringRight(\$New_date, 4) MsgBox(-1, "", \$DAY & \$MON & \$YEAR) ;_GUICtrlDTP_SetFormat(\$hWndDate, "yyyy/MM/dd") \$DateFormate = (\$DAY & " " & \$MON & " " & \$YEAR) \$DTM_SETFORMAT_ = 0x1032 ; \$DTM_SETFORMATW GUICtrlSendMsg(\$Date1, \$DateFormate, 0, "MM/dd/yyyy") EndSwitch WEnd
• By Chimp
Simple and generalpurpose stopwatch function

the GUI interface is not a "finished product" but a simple example to show how to use the function.
; --- simple example of use --- #include <GUIConstantsEx.au3> \$Form1_1 = GUICreate("Stopwatch", 170, 70);, 100, 100) \$LapsedTime = GUICtrlCreateLabel(" 00:00:00.0", 10, 10, 200, 25) GUICtrlSetTip(-1, "Lapsed time") GUICtrlSetFont(-1, 18) \$Button1 = GUICtrlCreateButton("halt/go", 10, 40, 50, 20) ; go / pause GUICtrlSetTip(-1, "Pause/Unpause counting") \$Button2 = GUICtrlCreateButton("clear", 60, 40, 50, 20) ; reset GUICtrlSetTip(-1, "Clears counter and stops") \$Button3 = GUICtrlCreateButton("restart", 110, 40, 50, 20) ; restart GUICtrlSetTip(-1, "Clears counter and restarts") GUISetState(@SW_SHOW) Stopwatch(2) ; reset to 0 and stops While 1 \$x = Int(Stopwatch() / 100) \$nMsg = GUIGetMsg() Switch \$nMsg Case \$GUI_EVENT_CLOSE Exit Case \$Button1 ; toggle status (go <-> pause) Stopwatch() ; this reads counter and sets @extended according to actual \$paused status Stopwatch(@extended) ; if is paused (@extended = 1) then unpause [Stopwatch(1)] ; if is unpaused (@extended = 0) then pause [Stopwatch(0)] Case \$Button2 ; reset to 0 and stop counter Stopwatch(2) Case \$Button3 ; reset to 0 and restart counter Stopwatch(3) EndSwitch If \$x <> Int(Stopwatch() / 100) Then \$totsec = Int(Stopwatch() / 1000) ; ms to sec \$hr = Int(\$totsec / 3600) ; hours \$mn = Int((\$totsec - (\$hr * 3600)) / 60) ; minutes \$sc = Int((\$totsec - (\$hr * 3600) - (\$mn * 60))) ; seconds \$tn = Int((Int(Stopwatch() / 100) - (\$hr * 36000) - (\$mn * 600) - (\$sc * 10))) ; tenths of a second GUICtrlSetData(\$LapsedTime, " " & StringFormat("%02s", \$hr) & ":" & StringFormat("%02s", \$mn) & ":" & StringFormat("%02s", \$sc) & "." & StringFormat("%01s", \$tn)) EndIf WEnd ; --- end of example --- ; #FUNCTION# (snippet) ========================================================================================================== ; Name...........: Stopwatch ; Description ...: returns the number of milliseconds counted (according to actions requests by caller) ; Syntax.........: Stopwatch([\$action]) ; Parameters ....: \$action: 0 - pause counting ; 1 - resume counting ; 2 - reset counter to 0 and stops ; 3 - reset counter to 0 and start counting ; 4 - (default) just ruturns actual counting value and status (status in @extended) ; ; Return values .: number of milliseconds in counter ; @extended contains paused status (1 or 0) ; 1 if stopwatch is in pause ; 0 if stopwatch is counting ; ; Author ........: Chimp ; Modified.......: ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: yes ; =============================================================================================================================== Func Stopwatch(\$ToggleTo = 4) Static Local \$Paused = True Static Local \$Stopwatch = 0 Static Local \$TotalTime = 0 Switch \$ToggleTo Case 0 ; pause counter If \$Paused Then SetExtended(\$Paused) ; \$Paused status Return \$TotalTime ; already paused, just return current \$TotalTime Else \$TotalTime += TimerDiff(\$Stopwatch) \$Paused = True SetExtended(\$Paused) Return \$TotalTime EndIf Case 1 ; unpause counter If \$Paused Then \$Stopwatch = TimerInit() \$Paused = False SetExtended(\$Paused) Return \$TotalTime Else SetExtended(\$Paused) Return \$TotalTime + TimerDiff(\$Stopwatch) EndIf Case 2 ; reset to 0 and pause \$Paused = True \$TotalTime = 0 SetExtended(\$Paused) Return \$TotalTime Case 3 ; reset to 0 and restart \$Paused = False \$TotalTime = 0 \$Stopwatch = TimerInit() SetExtended(\$Paused) Return \$TotalTime Case 4 ; return status SetExtended(\$Paused) If \$Paused Then Return \$TotalTime Else Return \$TotalTime + TimerDiff(\$Stopwatch) EndIf EndSwitch EndFunc ;==>Stopwatch P.S.
If you need a finished stopwatch with a nice gui and more functions, take a look to this one by AZJIO
×

• Wiki

• Back

• #### Beta

• Git
• FAQ
• Our Picks
×
• Create New...