Results 1 to 12 of 12

Thread: VB - OnTheClock

  1. #1

    Thread Starter
    Fanatic Member doofusboy's Avatar
    Join Date
    Apr 2003
    Posts
    526

    VB - OnTheClock

    A simple app really. Just threw it together to keep track of how long I've been working on something so I knew how much to bill a client for coding time, documentation time, etc. One could always add "always on top" code should it be desired.

    Didn't make allowances for going past 24 hours because, regardless of how intense anyone is, they wouldn't need this app if they were at something that long, they'd need their head examined! [obviously the code could be amended to do that should you really need it]

    Here's the code and a screen shot (ID labels added to screenshot so you can tie back to the code; obviously actual form wouldn't have these ID's displayed):



    VB Code:
    1. '****************************************************
    2. '*
    3. '* Simple application employing the use of a Timer to
    4. '* determine what time an application started, keep track
    5. '* of the current time, and use those two pieces of info
    6. '* to determine how long the application has been running.
    7. '*
    8. '* Primary purpose for developing this application was to
    9. '* have a tool to keep track of how long I've been coding
    10. '* an application for billing purposes.
    11. '*
    12. '****************************************************
    13.  
    14. Option Explicit
    15. Dim startHour As String
    16. Dim startMinute As String
    17. Dim startSecond As String
    18. Dim startTotal As Long
    19. Dim currentHour As String
    20. Dim currentMinute As String
    21. Dim currentSecond As String
    22. Dim currentTotal As Long
    23. Dim elapsedHour As Long
    24. Dim elapsedMinute As Long
    25. Dim elapsedSecond As Long
    26. Dim elapsedTotal As Long
    27. Dim startAmPm As String
    28. Dim currentAmPm As String
    29. Dim ampmFlag As Boolean
    30.  
    31. Private Sub cmdUpdateRunTime_Click()
    32. Dim length As Long
    33.    
    34.     currentAmPm = Mid(Time, Len(Time) - 1, 2)
    35.  
    36.     'Set flag
    37.     If (startAmPm = "AM" And currentAmPm = "AM") Or (startAmPm = "PM" And currentAmPm = "PM") Then
    38.         ampmFlag = False
    39.     Else
    40.         ampmFlag = True
    41.     End If
    42.  
    43.     length = HowLong(startHour, startMinute, startSecond)
    44.     lblElapsedHours.Caption = elapsedHour
    45.     lblElapsedMinutes.Caption = elapsedMinute
    46.     lblElapsedSeconds.Caption = elapsedSecond
    47. End Sub
    48.  
    49. Private Sub Form_Load()
    50. Dim a As String
    51.  
    52.     Timer1.Interval = 1
    53.     lblStartTime.Caption = Time
    54.     startSecond = Mid$(lblStartTime.Caption, Len(lblStartTime.Caption) - 4, 2)
    55.     startMinute = Mid$(lblStartTime.Caption, Len(lblStartTime.Caption) - 7, 2)
    56.     If Len(lblStartTime.Caption) > 10 Then
    57.         startHour = Mid$(lblStartTime.Caption, Len(lblStartTime.Caption) - 10, 2)
    58.     Else
    59.         startHour = Mid$(lblStartTime.Caption, Len(lblStartTime.Caption) - 9, 1)
    60.     End If
    61.    
    62.     'Because of the way elapsed time is calculated, must keep
    63.     'track of AM/PM portion of Start time for comparison when
    64.     'elapsed time is eventually calculated
    65.     startAmPm = Mid$(lblStartTime.Caption, Len(lblStartTime.Caption) - 1, 2)
    66.  
    67. End Sub
    68.  
    69. Private Sub Timer1_Timer()
    70.     lblCurrentTime.Caption = Time
    71. End Sub
    72. Public Function HowLong(ByVal startHour As String, ByVal startMinute As String, ByVal startSecond As String) As Long
    73.  
    74. currentSecond = Mid$(Time, Len(Time) - 4, 2)
    75. currentMinute = Mid$(Time, Len(Time) - 7, 2)
    76. If Len(Time) > 10 Then
    77.     currentHour = Mid$(Time, Len(Time) - 10, 2)
    78. Else
    79.     currentHour = Mid$(Time, Len(Time) - 9, 1)
    80. End If
    81.  
    82. If ampmFlag = True And currentHour <> 12 Then
    83.     currentHour = currentHour + 12
    84. End If
    85.    
    86. startTotal = (startHour * 3600) + (startMinute * 60) + startSecond
    87. currentTotal = (currentHour * 3600) + (currentMinute * 60) + currentSecond
    88. elapsedTotal = currentTotal - startTotal
    89.  
    90. 'Application is active less than 1 minute
    91. If elapsedTotal < 60 Then
    92.     elapsedHour = 0
    93.     elapsedMinute = 0
    94.     elapsedSecond = elapsedTotal
    95. End If
    96.  
    97. 'Application is active for more than 1 minute
    98. 'but less than 1 hour
    99. If elapsedTotal >= 60 And elapsedTotal < 3600 Then
    100.     elapsedHour = 0
    101.     elapsedSecond = elapsedTotal Mod 60
    102.     elapsedMinute = (elapsedTotal - elapsedSecond) / 60
    103. End If
    104.  
    105. 'Application is active for 1 hour or more
    106. If elapsedTotal >= 3600 Then
    107.     elapsedHour = (elapsedTotal - (elapsedTotal Mod 3600)) / 3600
    108.     elapsedSecond = (elapsedTotal - (elapsedHour * 3600)) Mod 60
    109.     elapsedMinute = (elapsedTotal - (elapsedHour * 3600) - elapsedSecond) / 60
    110. End If
    111.  
    112. End Function
    Attached Images Attached Images  
    Last edited by doofusboy; May 17th, 2003 at 09:24 AM.
    Do canibals not eat clowns because they taste funny?

  2. #2
    Frenzied Member
    Join Date
    May 2003
    Location
    Sydney
    Posts
    1,123
    i do remember something from a book that would be of some concern to you, doofusboy. if memory serves right, it is from the black book of vb programming. i wont be able to put the exact sam phrase here but i can put the sense right, if you know what i mean.

    while using a timer control may be a very easy way to calculate the time interval of anything, it can also be unpredictable as it gives more preference to other processes running in the system than itself. this infers that if the interval of a timer is set for 60,000 (i.e., 1 min=60 sec), and the computer is going thru some stressful calculations like rendering some 3D pic or animation, or some games, the timer may show your time to be 1 minute but in reality it will exceed the 1 minute mark. so it is not recommended to put all your faith in the timer control if being used for long durations in stressful environments.

  3. #3

    Thread Starter
    Fanatic Member doofusboy's Avatar
    Join Date
    Apr 2003
    Posts
    526
    mebhas,

    of this I was aware, but doesn't generally apply in my case for the development work that I do. but it's a good "heads up" for those that aren't aware of it!
    Do canibals not eat clowns because they taste funny?

  4. #4
    Frenzied Member
    Join Date
    May 2003
    Location
    Sydney
    Posts
    1,123
    i am now working on a project based on your clock. i will make the project of three categories; single user, medium user and the massive user. single user, of course, for a single computer. medium user for a lan based workgroup. the massive user will be for a multiple users working with the same project from all around the world. i will have to make a separate server for the multi user support of course.

    i am telling this as the closk was your idea and i am building on it. is it okay???

  5. #5

    Thread Starter
    Fanatic Member doofusboy's Avatar
    Join Date
    Apr 2003
    Posts
    526
    Sure it's OK ! I posted it in hopes it might help someone. Glad you found a use for it !

    (keep in mind that I did NOT make allowances for this application to be running for more than 24 hours; although I'm sure it could be enhanced with logic to handle that eventuality)

    Perhaps you'll be as kind to share with us what you come up with too ?
    Last edited by doofusboy; Jun 1st, 2003 at 06:38 AM.
    Do canibals not eat clowns because they taste funny?

  6. #6
    Junior Member Bean's Avatar
    Join Date
    Apr 2003
    Location
    GA, USA
    Posts
    23

    Timer

    I think this was the paragraph you were referring to...

    From VB Black Book You shouldn’t count on a timer too closely if your task execution is dependent on exact intervals; if the system is busy executing long loops, intensive calculations, or drive, network, or port access (in which case software routinely disables the timer interrupt), your application may not get Timer events as often as the Interval property specifies. That is to say, Timer events are not guaranteed to happen exactly on time. If you need to be sure, your software should check the system clock when it needs to (using, for example, the Visual Basic Time$ function), rather than try to keep track of time internally.
    I was thinking of simply checking Now() at the beginning and end of your app usage to determine the start/end times, as you can see, this text recommends the same thing (but using Time$)...
    Is an IF...MAYBE...THEN... clause TRUE half of the time?

  7. #7
    Fanatic Member BrianHawley's Avatar
    Join Date
    Aug 2001
    Location
    Saudi Arabia
    Posts
    796
    From VB Black Book You shouldn’t count on a timer too closely if your task execution is dependent on exact intervals; if the system is busy executing long loops, intensive calculations, or drive, network, or port access (in which case software routinely disables the timer interrupt), your application may not get Timer events as often as the Interval property specifies. That is to say, Timer events are not guaranteed to happen exactly on time. If you need to be sure, your software should check the system clock when it needs to (using, for example, the Visual Basic Time$ function), rather than try to keep track of time internally.
    Don't think that is an issue here.

    Seems to me that in this case it is more important to let the application get on with its job than to have exactly accurate times - therefore this is a very appropriate use of a timer as it wil do just exactly that.
    Brian
    (Fighting with the RightToLeft bugs in VS 2005)

  8. #8
    Junior Member Bean's Avatar
    Join Date
    Apr 2003
    Location
    GA, USA
    Posts
    23

    huh

    Don't think that is an issue here....it is more important to let the application get on with its job than to have exactly accurate times - therefore this is a very appropriate use of a timer as it wil do just exactly that.

    How is it not an issue? The "job" of the app is to determine how long itself has been running...why would accurate times not be important? Why is the original program coded to determine SECONDS if this is not important?

    I am not sure WHY you would use a Timer to (accurately) determine elapsed time or to keep the current time unless you can be sure no other processes will not interfere with the Timer (but why take the chance?)
    ???????????????????????????????????????????

    IMO the system clock and a simple Datediff would do the trick with much less to-do...

    ...lol...i use a notepad to keep track of my hours (to the nearest 15 minutes...not seconds) but whatever works for ya...
    Is an IF...MAYBE...THEN... clause TRUE half of the time?

  9. #9
    PowerPoster Fox's Avatar
    Join Date
    Jan 2000
    Location
    *afk*
    Posts
    2,088
    Using the Date data type would be easier, faster and shorter. You can use the DateDiff function to compare times and Format() to output formatted time dates...

  10. #10
    Fanatic Member BrianHawley's Avatar
    Join Date
    Aug 2001
    Location
    Saudi Arabia
    Posts
    796

    Re: huh

    Originally posted by Bean
    [B]How is it not an issue? The "job" of the app is to determine how long itself has been running...
    If you re-read the original post, you will see that the intent is to measure how long he has been working on the application for billing purposes. Not something that needs great accuracy.

    Even if he WAS trying to time the app, then using a methiod which itself interferes with the timing would not be appropriate and DateDiff would be better, as you say. But for what he is doing, the timer is fine. Horses for courses.
    Brian
    (Fighting with the RightToLeft bugs in VS 2005)

  11. #11

    Thread Starter
    Fanatic Member doofusboy's Avatar
    Join Date
    Apr 2003
    Posts
    526
    WOW! Had no idea posting such a SIMPLE app would create such an uproar [operative word being 'simple' as stated in original post].

    For those concerned about accuracy, please reread the code I used. The system time IS used when measuring elapsed time. The ONLY thing the Timer control is used for in this application is to display a running current time on the form [and yes, there are other ways to do that, using a boolean for example].

    Perhaps it was the wording used in my comments at the beginning of the code that confused some of you. Sorry about that. Hope the above clarifies things for you.

    I will go back and edit the comment section of the code once I figure out less confusing wording.

    By the way, using the DateDiff function can cause some inconsistent 'rounding' problems when trying to determine how many hours, minutes, seconds have elapsed. When I tested it, it was sometimes rounding minutes when only 25 seconds had elapsed and at other times took as long as 42 seconds before the rounding issue arose.
    Last edited by doofusboy; Jun 24th, 2003 at 09:23 AM.
    Do canibals not eat clowns because they taste funny?

  12. #12

    Thread Starter
    Fanatic Member doofusboy's Avatar
    Join Date
    Apr 2003
    Posts
    526
    Just to keep everybody happy, I removed the Timer control from the form, modified the code to keep displaying the current time without the timer and modified the starting comments. Everything else still works fine.

    VB Code:
    1. '********************************************************
    2. '* Simple application to determine what time an
    3. '* application started, keep track of the current time,
    4. '* and use those two pieces of info to determine how
    5. '* long the application has been running.
    6. '*
    7. '* Primary purpose for developing this application was to
    8. '* have a tool to keep track of how long I've been coding
    9. '* an application for billing purposes.
    10. '********************************************************
    11.  
    12. Option Explicit
    13. Dim showTime As Boolean
    14. Dim startHour As String
    15. Dim startMinute As String
    16. Dim startSecond As String
    17. Dim startTotal As Long
    18. Dim currentHour As String
    19. Dim currentMinute As String
    20. Dim currentSecond As String
    21. Dim currentTotal As Long
    22. Dim elapsedHour As Long
    23. Dim elapsedMinute As Long
    24. Dim elapsedSecond As Long
    25. Dim elapsedTotal As Long
    26. Dim startAmPm As String
    27. Dim currentAmPm As String
    28. Dim ampmFlag As Boolean
    29.  
    30. Private Sub cmdUpdateRunTime_Click()
    31. Dim length As Long
    32.    
    33.     currentAmPm = Mid(Time, Len(Time) - 1, 2)
    34.  
    35.     'Set flag
    36.     If (startAmPm = "AM" And currentAmPm = "AM") Or (startAmPm = "PM" And currentAmPm = "PM") Then
    37.         ampmFlag = False
    38.     Else
    39.         ampmFlag = True
    40.     End If
    41.  
    42.     length = HowLong(startHour, startMinute, startSecond)
    43.     lblElapsedHours.Caption = elapsedHour
    44.     lblElapsedMinutes.Caption = elapsedMinute
    45.     lblElapsedSeconds.Caption = elapsedSecond
    46. End Sub
    47.  
    48. Private Sub Form_Load()
    49.  
    50.     lblStartTime.Caption = Time
    51.     startSecond = Mid$(lblStartTime.Caption, Len(lblStartTime.Caption) - 4, 2)
    52.     startMinute = Mid$(lblStartTime.Caption, Len(lblStartTime.Caption) - 7, 2)
    53.     If Len(lblStartTime.Caption) > 10 Then
    54.         startHour = Mid$(lblStartTime.Caption, Len(lblStartTime.Caption) - 10, 2)
    55.     Else
    56.         startHour = Mid$(lblStartTime.Caption, Len(lblStartTime.Caption) - 9, 1)
    57.     End If
    58.    
    59.     'Because of the way elapsed time is calculated, must keep
    60.     'track of AM/PM portion of Start time for comparison when
    61.     'elapsed time is eventually calculated
    62.     startAmPm = Mid$(lblStartTime.Caption, Len(lblStartTime.Caption) - 1, 2)
    63.     Me.Show
    64.     DisplayTime
    65.    
    66. End Sub
    67.  
    68. Public Function HowLong(ByVal startHour As String, ByVal startMinute As String, ByVal startSecond As String) As Long
    69.  
    70. currentSecond = Mid$(Time, Len(Time) - 4, 2)
    71. currentMinute = Mid$(Time, Len(Time) - 7, 2)
    72. If Len(Time) > 10 Then
    73.     currentHour = Mid$(Time, Len(Time) - 10, 2)
    74. Else
    75.     currentHour = Mid$(Time, Len(Time) - 9, 1)
    76. End If
    77.  
    78. If ampmFlag = True And currentHour <> 12 Then
    79.     currentHour = currentHour + 12
    80. End If
    81.    
    82. startTotal = (startHour * 3600) + (startMinute * 60) + startSecond
    83. currentTotal = (currentHour * 3600) + (currentMinute * 60) + currentSecond
    84. elapsedTotal = currentTotal - startTotal
    85.  
    86. 'Application is active less than 1 minute
    87. If elapsedTotal < 60 Then
    88.     elapsedHour = 0
    89.     elapsedMinute = 0
    90.     elapsedSecond = elapsedTotal
    91. End If
    92.  
    93. 'Application is active for more than 1 minute
    94. 'but less than 1 hour
    95. If elapsedTotal >= 60 And elapsedTotal < 3600 Then
    96.     elapsedHour = 0
    97.     elapsedSecond = elapsedTotal Mod 60
    98.     elapsedMinute = (elapsedTotal - elapsedSecond) / 60
    99. End If
    100.  
    101. 'Application is active for 1 hour or more
    102. If elapsedTotal >= 3600 Then
    103.     elapsedHour = (elapsedTotal - (elapsedTotal Mod 3600)) / 3600
    104.     elapsedSecond = (elapsedTotal - (elapsedHour * 3600)) Mod 60
    105.     elapsedMinute = (elapsedTotal - (elapsedHour * 3600) - elapsedSecond) / 60
    106. End If
    107.  
    108. End Function
    109.  
    110. Private Sub DisplayTime()
    111. Do While showTime = False
    112.     lblCurrentTime = Time
    113.     DoEvents
    114. Loop
    115.    
    116. End Sub
    117.  
    118. Private Sub Form_Unload(Cancel As Integer)
    119.     showTime = True
    120. End Sub
    Do canibals not eat clowns because they taste funny?

Posting Permissions

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



Click Here to Expand Forum to Full Width