Jump to content

Date Time Range


Recommended Posts

something like that

Local $Time
While 1
    $Time = @HOUR & ":" & @MIN
    ConsoleWrite($Time & @CRLF)

    If $Time = "06:05" Then
    ;@HOUR = '6' And @MIN >= '5' Then
        Send("{[}")
        _Terminate()

    ElseIf  $Time = "07:05" Then
        ;(@HOUR >= '7' And @MIN >= '5') To (@HOUR >= '5' And @MIN >= '5') Then
        Send("{]}")
        _Terminate()
    EndIf

    Sleep(1000 * 60)

WEnd

 

Edited by ioa747

I know that I know nothing

Link to comment
Share on other sites

21 minutes ago, Nine said:

if @hour >= 6 and @hour <= 8 then ...

In one of my scripts, I tested @hour and @min differently
There was probably a reason for that, gonna think of it during lunch :D
 

; Réveil - TESTER AVANT, POUR ETRE SUR QUE WINDOWS A LES SONS SUR 'ON'
; Noter @HOUR = "hh" alors que @MIN >= "mm"

While 1
    If @HOUR = "14" And @MIN >= "00" Then ExitLoop
    Sleep(60 * 1000) ; 60 x 1s = 1min
Wend

While 1
    Beep()
    Sleep(5000) ; 5s
WEnd

 

Edited by pixelsearch
Link to comment
Share on other sites

Link to comment
Share on other sites

@ashraful089 Yes, you can do a syntax like 6 to 8, using Switch. It would like something like this:

While 1
    Sleep(10)
    If __SwitchTime(6, 8) Then
        ConsoleWrite('Doing stuff' & @CRLF)
        __DoStuff()
    EndIf
WEnd

Func __SwitchTime($iStartHour, $iEndHour)
    Switch @HOUR
        Case $iStartHour To $iEndHour
            ConsoleWrite('Switch time true' & @CRLF)
            Return True
        Case Else
            ConsoleWrite('Not yet time' & @CRLF)
    EndSwitch
EndFunc   ;==>__SwitchTime

Func __DoStuff()
;~  Send("{[}")
;~  _Terminate()
EndFunc   ;==>__DoStuff

Or just more simply:

Switch Number(@HOUR)
    Case 6 to 8
        ConsoleWrite('True' & @CRLF)
    Case Else
        ConsoleWrite('False' & @CRLF)
EndSwitch

https://www.autoitscript.com/autoit3/docs/keywords/Switch.htm

 

However I would take an approach like this if you're going to involve minutes as well:

#include <StringConstants.au3>

While 1
    Sleep(1000)

    ; If you want to check for a time that spans across a day, such as starting at 22:05 until 02:05, do it like:
    ; If Not __CheckTime('02:05', '22:05') Then
;~  If __SwitchTime(6, 8) Then
    If __CheckTime('06:05', '07:05') Then
        __DoStuff()
    EndIf
WEnd


Func __CheckTime($sStart, $sEnd)
    ; Enums just for reference with names instead of numbers
    Local Enum $eTime_Hour, $eTime_Minute

    ; Split our starting time input, in the format HOUR:MINUTE
    Local $aStart = StringSplit($sStart, ':', $STR_ENTIRESPLIT + $STR_NOCOUNT)
    If @error Then
        ConsoleWrite('Invalid format of $sStart: ' & $sStart & ', error: ' & @error & @CRLF)
        Return SetError(1, 0, False)
    EndIf

    ; Split our ending time input, in the format HOUR:MINUTE
    Local $aEnd = StringSplit($sEnd, ':', $STR_ENTIRESPLIT + $STR_NOCOUNT)
    If @error Then
        ConsoleWrite('Invalid format of $sEnd: ' & $sEnd & ', error: ' & @error & @CRLF)
        Return SetError(2, 0, False)
    EndIf

    ; Convert the times into minutes
    Local $iStart = (Int($aStart[$eTime_Hour]) * 60) + $aStart[$eTime_Minute]
    Local $iEnd = (Int($aEnd[$eTime_Hour]) * 60) + $aEnd[$eTime_Minute]

    If $iEnd < $iStart Then
        ConsoleWrite('End time (' & $sEnd & ') is before start time (' & $sEnd & ')' & @CRLF)
        Return SetError(3, 0, False)
    EndIf

;~  ConsoleWrite('$iStart: ' & $iStart & ', $iEnd: ' & $iEnd & @CRLF)

    ; Get the current time as total minutes
    Local $iCurrentTime = (Int(@HOUR) * 60) + @MIN
    If $iCurrentTime >= $iStart And $iCurrentTime <= $iEnd Then
        ; We're in the time frame specified
        Return True
    EndIf

    ; We're not in the timeframe specified
    Return False
EndFunc   ;==>__CheckTime

; $iEndHour is optional, so as long as it's after the start hour in the same day, it'll return true
Func __SwitchTime($iStartHour, $iEndHour = 24)
    Switch @HOUR
        Case $iStartHour To $iEndHour
            ConsoleWrite('Switch time true' & @CRLF)
            Return True
        Case Else
            ConsoleWrite('Not yet time' & @CRLF)
    EndSwitch
EndFunc   ;==>__SwitchTime


Func __DoStuff()
    ConsoleWrite('Doing stuff' & @CRLF)
;~  Send("{[}")
;~  _Terminate()
EndFunc   ;==>__DoStuff

 

Edited by mistersquirrle
Updated code with some comments

We ought not to misbehave, but we should look as though we could.

Link to comment
Share on other sites

it can be expressed like this :)

Local $Time
While 1
    $Time = Int(@HOUR) & "." & int(@MIN)
    ;ConsoleWrite($Time & @CRLF)

    Switch $Time
        Case 6.5 To 7.5
            Send("{[}")
            _Terminate()

        Case 7.6 To 17.5
            Send("{]}")
            _Terminate()

    EndSwitch
    
    Sleep(10)

WEnd

 

Edited by ioa747

I know that I know nothing

Link to comment
Share on other sites

7 hours ago, ioa747 said:

it can be expressed like this

No, you're "expressing" something else!

Hint: try running your program when @MIN is "05" and "50". Is there a difference? Why?

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

2 hours ago, jchd said:

Hint: try running your program when @MIN is "05" and "50". Is there a difference? Why?

sorry for the bad expressions but my English comes from Greek via google translate.
I don't understand what exactly you mean by 'is there a difference?'  what should i expect? that 05 and 50 would give me the same result?

where is my mistake?  :huh2:

Please explain me.
Thank you

; https://www.autoitscript.com/forum/topic/209595-date-time-range/?do=findComment&comment=1512676

Local $Time
While 1
    $Time = Int(@HOUR) & "." & int(@MIN)
    ;ConsoleWrite($Time & @CRLF)

    Switch $Time
        Case 6.5 To 7.5
            ;Send("{[}")
            _Terminate()

        Case 7.6 To 17.5
            ;Send("{]}")
            _Terminate()

    EndSwitch

    Sleep(1000 * 60)

WEnd

Func _Terminate()
    ConsoleWrite($Time & @CRLF)
EndFunc

I am also attaching the results of the console

Spoiler

15.48
15.49
15.50
15.51
15.52
15.53
15.54
15.55
15.56
15.57
15.58
15.59
16.0
16.1
16.2
16.3
16.4
16.5
16.6
16.7
16.8
16.9
16.10
16.11
16.12

 

I know that I know nothing

Link to comment
Share on other sites

What @jchd means is what's the difference between 16.5 (hr = 16 and mn = 05) and 16.50 (hr = 15 and mn = 50) from your example above ?  None. In your script it is interpreted the same way.  The issue is that you convert the string (@MIN) into an integer before you concatenate the whole thing into a string.  Why? Since the end result is a string.

But then your switch use a float (which means that your string is reconverted into a number), so both 16:05 and 16:50 are identical.

Local $sTime = int("16") & "." & int("05") ; hr = 16 and mn = 05
Switch $sTime
  Case 15 to 16.5
    ConsoleWrite("bw 15 - 16.5" & @CRLF)
EndSwitch

Local $sTime = int("16") & "." & int("50") ; hr = 16 and mn = 50
Switch $sTime
  Case 15 to 16.5
    ConsoleWrite("bw 15 - 16.5" & @CRLF)
EndSwitch

To correctly use your approach, you should have done this :

Local $nTime = Number(@HOUR & "." & @MIN) ; my current time is 10:20
ConsoleWrite($nTime & @CRLF)
Switch $nTime
  Case 10 to 10.2
    ConsoleWrite("bw 10 - 10.2" & @CRLF)
EndSwitch

 

Link to comment
Share on other sites

After correction!  :D

Local $Time
While 1
    $Time = Number(@HOUR & "." & @MIN)
    ;ConsoleWrite($Time & @CRLF)

    Switch $Time
        Case 6.05 To 7.05
            ;Send("{[}")
            _Terminate()

        Case 7.06 To 17.05
            ;Send("{]}")
            _Terminate()

    EndSwitch

    Sleep(10)

WEnd

 

Edited by ioa747

I know that I know nothing

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...