Welcome
Username or Email:

Password:


Missing Code




[ ]
[ ]
Online
  • Guests: 12
  • Members: 0
  • Newest Member: omjtest
  • Most ever online: 396
    Guests: 396, Members: 0 on 12 Jan : 12:51
Members Birthdays:
One birthday today, congrats!
Vaxian (17)


Next birthdays
05/21 Dalus (34)
05/21 Kizmo (37)
05/22 Skynet (32)
Contact
If you need assistance, please send an email to forum at 4hv dot org. To ensure your email is not marked as spam, please include the phrase "4hv help" in the subject line. You can also find assistance via IRC, at irc.shadowworld.net, room #hvcomm.
Support 4hv.org!
Donate:
4hv.org is hosted on a dedicated server. Unfortunately, this server costs and we rely on the help of site members to keep 4hv.org running. Please consider donating. We will place your name on the thanks list and you'll be helping to keep 4hv.org alive and free for everyone. Members whose names appear in red bold have donated recently. Green bold denotes those who have recently donated to keep the server carbon neutral.


Special Thanks To:
  • Aaron Holmes
  • Aaron Wheeler
  • Adam Horden
  • Alan Scrimgeour
  • Andre
  • Andrew Haynes
  • Anonymous000
  • asabase
  • Austin Weil
  • barney
  • Barry
  • Bert Hickman
  • Bill Kukowski
  • Blitzorn
  • Brandon Paradelas
  • Bruce Bowling
  • BubeeMike
  • Byong Park
  • Cesiumsponge
  • Chris F.
  • Chris Hooper
  • Corey Worthington
  • Derek Woodroffe
  • Dalus
  • Dan Strother
  • Daniel Davis
  • Daniel Uhrenholt
  • datasheetarchive
  • Dave Billington
  • Dave Marshall
  • David F.
  • Dennis Rogers
  • drelectrix
  • Dr. John Gudenas
  • Dr. Spark
  • E.TexasTesla
  • eastvoltresearch
  • Eirik Taylor
  • Erik Dyakov
  • Erlend^SE
  • Finn Hammer
  • Firebug24k
  • GalliumMan
  • Gary Peterson
  • George Slade
  • GhostNull
  • Gordon Mcknight
  • Graham Armitage
  • Grant
  • GreySoul
  • Henry H
  • IamSmooth
  • In memory of Leo Powning
  • Jacob Cash
  • James Howells
  • James Pawson
  • Jeff Greenfield
  • Jeff Thomas
  • Jesse Frost
  • Jim Mitchell
  • jlr134
  • Joe Mastroianni
  • John Forcina
  • John Oberg
  • John Willcutt
  • Jon Newcomb
  • klugesmith
  • Leslie Wright
  • Lutz Hoffman
  • Mads Barnkob
  • Martin King
  • Mats Karlsson
  • Matt Gibson
  • Matthew Guidry
  • mbd
  • Michael D'Angelo
  • Mikkel
  • mileswaldron
  • mister_rf
  • Neil Foster
  • Nick de Smith
  • Nick Soroka
  • nicklenorp
  • Nik
  • Norman Stanley
  • Patrick Coleman
  • Paul Brodie
  • Paul Jordan
  • Paul Montgomery
  • Ped
  • Peter Krogen
  • Peter Terren
  • PhilGood
  • Richard Feldman
  • Robert Bush
  • Royce Bailey
  • Scott Fusare
  • Scott Newman
  • smiffy
  • Stella
  • Steven Busic
  • Steve Conner
  • Steve Jones
  • Steve Ward
  • Sulaiman
  • Thomas Coyle
  • Thomas A. Wallace
  • Thomas W
  • Timo
  • Torch
  • Ulf Jonsson
  • vasil
  • Vaxian
  • vladi mazzilli
  • wastehl
  • Weston
  • William Kim
  • William N.
  • William Stehl
  • Wesley Venis
The aforementioned have contributed financially to the continuing triumph of 4hv.org. They are deserving of my most heartfelt thanks.
Forums
4hv.org :: Forums :: Computer Science
« Previous topic | Next topic »   

VB6 Month Timer Help

1 2 
Move Thread LAN_403
john bunsenburner
Mon Aug 10 2009, 11:09AM Print
john bunsenburner Registered Member #1937 Joined: Sun Jan 25 2009, 12:28PM
Location:
Posts: 53
I have been designing a program, just for fun, part of it involves counting months, simply adding 1 to a label's caption every time a month passes. The problem is the fact months tend to vary in length, and because this should be fairly accurate(and no, leap years are no problem, I will deal with those my self) at any given time i have tried to code the program in a manner in which each month is treated separately, now without further ado, I'll just give you guys the code.

Private Sub Form_Load() 
Timer1.Enabled = True 
End Sub 
Private Sub Timer1_Timer() 
Label1.Caption = (Label1.Caption + 1) 
Lbl1.Caption = (Lbl1.Caption + 1) 
If Label1.Caption = 60 Then 
Label2.Caption = (Label2.Caption + 1) 
Label1.Caption = 0 
ElseIf Label2.Caption = 60 Then 
Label3.Caption = (Label3.Caption + 1) 
Label2.Caption = 0 
ElseIf Label3.Caption = 24 Then 
Label4.Caption = (Label4.Caption + 1) 
Label3.Caption = 0 
ElseIf Lbl1.Caption = 604800 Then 
Label5.Caption = (Label5.Caption + 1) 
Lbl1.Caption = 0 
ElseIf Label4.Caption = 31 Then 
Label6.Caption = (Label6.Caption + 1) 
ElseIf Label4.Caption = 59 Then 
Label6.Caption = (Label6.Caption + 1) 
Label4.Caption = 0 
ElseIf Label4.Caption = 90 Then 
Label6.Caption = (Label6.Caption + 1) 
ElseIf Label4.Caption = 120 Then 
Label6.Caption = (Label6.Caption + 1) 
ElseIf Label4.Caption = 151 Then 
Label6.Caption = (Label6.Caption + 1) 
ElseIf Label4.Caption = 181 Then 
Label6.Caption = (Label6.Caption + 1) 
ElseIf Label4.Caption = 212 Then 
Label6.Caption = (Label6.Caption + 1) 
ElseIf Label4.Caption = 243 Then 
Label6.Caption = (Label6.Caption + 1) 
ElseIf Label4.Caption = 273 Then 
Label6.Caption = (Label6.Caption + 1) 
ElseIf Label4.Caption = 304 Then 
Label6.Caption = (Label6.Caption + 1) 
ElseIf Label4.Caption = 334 Then 
Label6.Caption = (Label6.Caption + 1) 
ElseIf Label4.Caption = 365 Then 
Label6.Caption = 0 
Label4.Caption = 0 
End If 
End Sub

Label4 displays days
Label6 months
The problem is once the day counter hits 31, the month counter goes up in seconds. Timer interval is set as 1second(1000ms). I realize the problem is caused by the day timer to sty on 31 for 24hours, thus the month timer adds 1 every second for 24 hours, this could be solved by using seconds instead of days but that would leave me with big numbers, so i ask if there is any alternative solution.

Thanks in advance!
Back to top
Nicko
Mon Aug 10 2009, 11:26AM
Nicko Registered Member #1334 Joined: Tue Feb 19 2008, 04:37PM
Location: Nr. London, UK
Posts: 615
At first glance the problem is probably that use are using "elseif" everywhere, and in timers like this you need to ripple-through the changes.
e.g.
Initialisation:
Dim nDaysInMonth as Integer = new Integer(12) {31,28,31,30,31,30,31,31,30,31,30,31}
    ' If leap year, adjust Feb
    ' (syntax depends on VB version)
    ' True if it is divisible by 4
    ' False if it is divisible by 100
    ' TRUE if it is divisble by 400
    ' intYear is the current Year as an Integer!!
If ((intYear Mod 4 = 0) And (intYear Mod 100 <> 0) Or (intYear Mod 400 = 0)) Then
    nDaysInMonth(1) = 29 / Feb is a leap year!
EndIf
Label4.Caption = 1 // First day of Month
Label6.Caption = 1 // First Month of Year
...etc...

rem note Label4.caption is always one less than correct date
Label1.Caption = (Label1.Caption + 1)  
Lbl1.Caption = (Lbl1.Caption + 1)  
If Label1.Caption = 60 Then  
    Label2.Caption = (Label2.Caption + 1)  
    Label1.Caption = 0  
EndIf
If Label2.Caption = 60 Then  
    Label3.Caption = (Label3.Caption + 1)  
    Label2.Caption = 0  
EndIf
If Label3.Caption = 24 Then  
    Label4.Caption = (Label4.Caption + 1)  
    Label3.Caption = 0  
EndIf
If Label4.Caption > nDaysInMonth(Label6.Caption - 1)  Then 
    Label6.Caption = Label6.Caption + 1
    Label4.Caption = 1 // First day of next month
EndIf
If Label6.Caption > 12  Then 
    ' ...its another year
    Label6.Caption = 1
EndIf
...etc.
Also, there are 86400 seconds in a day (not sure what magic numbers you are using or even why).

HTH
Back to top
LithiumLord
Mon Aug 10 2009, 11:30AM
LithiumLord Registered Member #1739 Joined: Fri Oct 03 2008, 10:05AM
Location: Moscow, Russia
Posts: 261
woah-woah-woah! Hold on, are you making just a counter that, once started, counts full running time?
Private Sub Timer1_Timer()
Static Init As Variant, InitDay As Integer, InitMonth As Integer, InitYear As Integer
If Init = "" Then
Init = Time
InitDay = Day(Date)
InitMonth = Month(Date)
InitYear = Year(Date)
End If
Label1.Caption = "Running: " & Year(Date) - InitYear & " Y " & Month(Date) - InitMonth & " M " & Day(Date) - InitDay & " D " & Format(Time - Init, "HH:MM:SS")
End Sub
Back to top
john bunsenburner
Mon Aug 10 2009, 07:42PM
john bunsenburner Registered Member #1937 Joined: Sun Jan 25 2009, 12:28PM
Location:
Posts: 53
Hey thanks for the help, I ajusted the code so that only the month part uses else ifs, then i read this...604800 is seconds in a week.
Yes im making a counter that simply counts seconds,minutes,hours,days,weeks, months and years from the time it is started, this will then be used for the rest of the program.

I didn't know VB had a built in system clock, it would of spared me lots of work, but now where i started it this way, might as well continue similarly.

Really the code you supplied will need close studying by me, so i might ask dumb questions later on, or use a, far less efficeint meathod of doing it, if I do it, might as well do it my self.

If we foget about leap years, how would the code look(the Dim statement is new to me, i understand what is being done, but not exactly how)

I greatly appriciate your help, and ask you to bare with me until I manage to grasp hold of VB. Maybe i'll come up wiht something intresting for you guys some day.

Again thanks a bunch, and see you round!
Back to top
LithiumLord
Mon Aug 10 2009, 11:47PM
LithiumLord Registered Member #1739 Joined: Fri Oct 03 2008, 10:05AM
Location: Moscow, Russia
Posts: 261
Sry I left that "d" var as I expected to use it but never did :P
Anyway, for a meter that's independant of the system time tweaks and that uses a fixed week/month/whatever length as is common for exact measurements, also has an mS accuracy:
'initialize the variables
Dim t As Single, pt As Single, Days As Integer
Const MaxIncrement = 100 'sanity limit ;)

Private Sub Form_Load()
'initial setup
pt = Timer
End Sub

Private Sub Timer1_timer()
'midnight
If pt > Timer Then pt = 86400 - pt
'increment
If Timer - pt < MaxIncrement And Timer - pt > 0 Then
t = t + Timer - pt
Else
t = t + Timer1.Interval / 1000
End If
'sync time
pt = Timer
'all the output code should be ran after this fast bit
'here we go with a sample code, splitting our single time accumulator in two to increase the capacity:
Days = Days + Int(t / 86400)
t = t - Int(t / 86400) * 86400
'this sets the program duty limit to the max integer days, that's kinda much ;)
'No need to keep splitting as the max day length is 86400 seconds which fits the Single type var's capacity
'btw - this splitting method is a good example of how it works - even with a few days increment it will still work out ;)

'Now, let's do some output examples. This is a simple one:
Label1.Caption = Days & " Days " & Format(Int(t) / 86400, "HH:MM:SS") & "." & Int((t - Int(t)) * 1000)
'Here we use that 86400 divider in order to be able to use the Format function right, and the rightmost bit is for mS display
'ok, now I'll show you how you can split the time using fixed month, week and year length
Label2.Caption = Int(Days / 8)
Label3.Caption = Days - Label2.Caption * 8
Label4 = Int(t / 3600)
Label5 = Int((t - Label4 * 3600) / 60)
Label6 = t - Label5 * 60 - Label4 * 3600
End Sub
'Kinda easy, huh? Now for the fun stuff:
Private Sub Command1_Click()
MsgBox Int(Days / 8) & " Weeks" & vbNewLine & Days - Int(Days / 8) * 8 & " Days" & vbNewLine & Format(Int(t) / 86400, "HH:MM:SS") & "." & Int((t - Int(t)) * 1000), vbInformation, "Running time"
End Sub
This needs a form with a timer with any interval, labels and a command button, the MaxIncrement constant at the beginning is used for the system time setup: should a time setup happen it checks if the incremention value which has a very high accuracy as just a few directives are ran in between the points gets above the set value and if so, it uses the timer's interval value instead of the measured delay. The midnight-point compensation is there btw. It is set in seconds and represents the max allowed increment.
Back to top
john bunsenburner
Tue Aug 11 2009, 08:29AM
john bunsenburner Registered Member #1937 Joined: Sun Jan 25 2009, 12:28PM
Location:
Posts: 53
Wow, thanks alot mate, this looks great, and somewhat simple. Ill go about understaning all of it, then i will use it. Thanks alot, your a great help.


I am a little confused at the meanign of pt and t
Also why are days/8 in the end?
Back to top
LithiumLord
Tue Aug 11 2009, 10:05AM
LithiumLord Registered Member #1739 Joined: Fri Oct 03 2008, 10:05AM
Location: Moscow, Russia
Posts: 261
I got a bit messed myself, of course /7 (lol, night-time coding :)). That's an example of a weekly counter, that uses the first division to count weeks and then multiplies it back (it's an integer now, see? ;)) and trims the needed number of days from the counter to count the remaining days that do not form a full week.

t is the accumulator, and pt is the delta.
Back to top
Steve Conner
Tue Aug 11 2009, 11:34AM
Steve Conner Registered Member #30 Joined: Fri Feb 03 2006, 10:52AM
Location: Glasgow, Scotland
Posts: 6706
The solution to the original problem (months count up in seconds) is to keep a record of what each variable was the last time you checked it, and then do something like the following for each test:

If (day is 31 AND day was not 31 the last time I checked)
then do stuff
else do nothing

Of course it's much easier to use the system clock as LithiumLord points out :) It already knows leap years, the numbers of days in each month and so on.
Back to top
john bunsenburner
Tue Aug 11 2009, 08:51PM
john bunsenburner Registered Member #1937 Joined: Sun Jan 25 2009, 12:28PM
Location:
Posts: 53
Does this solve the problem:

Back to top
LithiumLord
Tue Aug 11 2009, 10:12PM
LithiumLord Registered Member #1739 Joined: Fri Oct 03 2008, 10:05AM
Location: Moscow, Russia
Posts: 261
Well, I see whole three downsides in the code shown. First of all, there's no point in using control properties as shared vars if you can just declare a few shared vars ;) Then - there's no reason to use such a distributed code as you can just implement sequential splitting as in my version, eg via that Int/division trick. And next - the code execution takes time, and it will make the delays inaccurate, also keep in mind there's no reason in using different year/month length as we are making a differential measurement and therefore need no calendar dependancy - or it would be mess like in physics a light year would be different for different years ;)
Back to top
1 2 

Moderator(s): Chris Russell, Noelle, Alex, Tesladownunder, Dave Marshall, Dave Billington, Bjørn, Steve Conner, Wolfram, Kizmo, Mads Barnkob

Go to:

Powered by e107 Forum System
 
Legal Information
This site is powered by e107, which is released under the GNU GPL License. All work on this site, except where otherwise noted, is licensed under a Creative Commons Attribution-ShareAlike 2.5 License. By submitting any information to this site, you agree that anything submitted will be so licensed. Please read our Disclaimer and Policies page for information on your rights and responsibilities regarding this site.