Showing results 1 to 8 of 8

Thread: compare time variable with system time

  1. #1
    flajoso Gast

    Default compare time variable with system time

    [i've started this Post in General Support]

    Hi all,

    my VBA skills are not very nice.. so i would like for some help, if possible.
    I'm trying to compare the actual system time with an interval time between two variables. I tryed it with 3 variables (2 for the interval and 1 to check if it is in the interval or not) and works fine. Now i'm trying to check with system time with "Format(now)" like an another user suggested me. The problem is that i cannot compare because of the variable types..

    Public Sub LeftClickDown_teste(obElem As Element)

    Dim mytime1 As Variable
    Dim mytime2 As Variable
    Dim mytime3 As Variable
    Dim state As Variable
    Dim teste As Time



    Set mytime1 = thisProject.Variables.Item("time1")
    Set mytime2 = thisProject.Variables.Item("time2")
    Set mytime3 = thisProject.Variables.Item("time3")
    Set state = thisProject.Variables.Item("timestate")


    'actual time
    teste = (Format(Now, "hh:mm"))

    ' this was a test with 3 internal variables
    'If mytime3.Value < mytime2.Value And mytime3.Value > mytime1.Value Then

    If teste < mytime2.Value And teste > mytime1.Value Then

    state.Value = "1"
    MsgBox ("TRUE") ' for debugging
    Else
    'MsgBox ("FALSE")
    state.Value = "0"
    End If


    'Debug.Print state.StatusString
    'state.Value = "1"



    End Sub

  2. #2
    robertf Gast

    Default Re: compare time variable with system time

    Hello flajoso!

    Please also post some samples of the values within the variables.
    Are these string variables or numerical variables?

    It is not that easy to compare times stored as strings, they have to be converted to values, either the ">", "<" ... can not work!

    If you post some more details, i can make a small sample...

  3. #3
    flajoso Gast

    Default Re: compare time variable with system time

    Hi robertf,
    thank you for your reply.
    I think i know what the problem is. Vairables time1,time2 and time3 are Internal TIME variables in Zenon, and i can't "see" them as TIME variables in VBA, i "see" them as integers.

    What i want to do is the following:

    I have two times to be defined by the user: time1 (start time) and time2 (endtime). Every time the system time is between this interval, a bit should be ON.

    startime = 13:30
    endtime = 15:00

    if system_time > startime and system_time< endtime then

    ...

    Public Sub LeftClickDown_teste(obElem As Element)
    
    Dim mytime1 As Variable
    Dim mytime2 As Variable
    'Dim mytime3 As Variable
    Dim state As Variable
    Dim now_time_str As string
    Dim mytime1_str as String
    Dim mytime2_str as String
    
    
    Set mytime1 = thisProject.Variables.Item("time1")
    Set mytime2 = thisProject.Variables.Item("time2")
    Set mytime3 = thisProject.Variables.Item("time3")
    Set state = thisProject.Variables.Item("timestate")
    
    
    'actual time
    now_time_str = (Format(Now, "hh:mm"))
    
    ' i tryed to convert all the values to strings..
    mytime1_str = CStr(mytime1.Value)
    mytime2_str = CStr(mytime2.Value)
    
    'and then compare it like TimeValues.. the problem is that mytime.value is not in TIME format, it looks like an integer
    
    If TimeValue(now_time_str) < TimeValue(mytime2_str) And TimeValue(now_time_str)  > TimeValue(mytime1_str) Then
    
    state.Value = "1"
    MsgBox ("TRUE") ' for debugging
    Else
    'MsgBox ("FALSE")
    state.Value = "0"
    End If
    
    
    End Sub


    Thanks in advance!

    Best regards,

  4. #4
    robertf Gast

    Default Re: compare time variable with system time

    Hello again!

    If the Time in Variable "Time1" is 12:00 what value is within the Variable?

    12:00 = 43200 seconds ?
    12:00 = 720 minutes ?
    12:00 = 1200 ???

    There are several possibilities to convert, but to make a sample I need more detailed information!

    I know what you want to do, but I need some sample values. Either I am not able to do the converting correctly!

    It would be easy to debug the code, but therefor i would need your constellation...
    (project, data, ...)

  5. #5
    flajoso Gast

    Default Re: compare time variable with system time

    Hi,

    in fact 12:00 = 43200 seconds, thats the value within the variable. I guess that i will have to convert it!

    Check my 'constellation'!

    Thanks!
    Attached Files Attached Files

  6. #6
    robertf Gast

    Default Re: compare time variable with system time

    Hello!

    You can try the following sample...

    In this sample the seconds are compared...

    Public Sub CompareTimes()
      Dim varTime1 As Variable
      Dim varTime2 As Variable
      Dim varTime3 As Variable
      Dim varState As Variable
      Dim actTime As Integer
      
      Set varTime1 = thisProject.Variables.Item("Time1")
      Set varTime2 = thisProject.Variables.Item("Time2")
      Set varTime3 = thisProject.Variables.Item("Time3")
      Set varState = thisProject.Variables.Item("TimeState")
      
      'check if variables are advised!
      If varTime1.IsOnline = False Or varTime2.IsOnline = False Or varTime3.IsOnline = False Then
         'get seconds of actual time!
        actTime = (Hour(Now) * 3600) + (Minute(Now) * 60)
        'compare actual time with values from variables
        If actTime < varTime2.Value And actTime > varTime1.Value Then
          varState.Value = 1
        Else
          varState.Value = 0
        End If
      Else
        'Variables are not online, value is not present
        'needed to avoid errors on reading, if variables are not advised!
      End If
      
    End Sub

  7. #7
    flajoso Gast

    Default Re: compare time variable with system time

    WORKS!

    i just made small changes:
    -the actTime from Integer to Long
    -the .isOnline from False to True... right? I advised them checking DDE active.

    Thank you for your help!

    Best regards,

  8. #8
    robertf Gast

    Default Re: compare time variable with system time

    Hello flajoso!

    You are right, sorry for the mistakes, i have done it very quickly

    But I see you understood my sample

Similar Threads

  1. Real-time performancce of STRATON
    By prasadkh in forum straton / zenon Logic
    Replies: 4
    Last Post: 12th December 2013, 12:31
  2. system time
    By flajoso in forum zenon Supervisor
    Replies: 2
    Last Post: 19th August 2008, 15:07
  3. How to have cumulative alarms time in report
    By fabios in forum zenon Supervisor
    Replies: 6
    Last Post: 21st May 2008, 10:08
  4. Local Day Time in format DINT
    By markusw in forum straton / zenon Logic
    Replies: 0
    Last Post: 12th October 2007, 09:53
  5. Value, Time-Stamp and status with allocations
    By markclemens in forum zenon Supervisor
    Replies: 0
    Last Post: 14th September 2007, 07:57

Posting Rules

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •