Hi there,
I have been trying to make a program that calculates time differences.
For some reason DBP keeps giving me a "Command out of place error" when compiling even though I am absolutely sure I need to put an Endif statement in there to close my If commands.
Here's the code - sorry it's a bit long, most of it can be ignored, just zip down to the big IF commands - there are seven open IF commands but closed with only 4 Endifs - but wierd thing is, this compiles fine!! Even though I believe I need 3 more Endifs!!
Rem Project: plant - finding time elapsed only
Rem Created: 28-Jan-13 09:51:48
Rem ***** Main Source File *****
`Set display
`set display mode 1920,1080,32
set window on
set window size 960,1080
set window position 960,0
`Make up some random dates and times
old_date$ = "29/01/13"
new_date$ = "27/01/13"
old_time$ = "10:10:05"
new_time$ = "20:10:05"
`** EXTRACT DATA FROM TEXT **
`First: we must extract hours, minutes and seconds of each time (new and old) from text data
`- Hours -
new_hr = val(left$(new_time$,2))
old_hr = val(left$(old_time$,2))
`- Minutes -
new_min = val(right$(left$(new_time$,5),2))
old_min = val(right$(left$(old_time$,5),2))
`- Seconds -
new_sec = val(right$(new_time$,2))
old_sec = val(right$(old_time$,2))
`- Days -
new_day = val(left$(new_date$,2))
old_day = val(left$(old_date$,2))
`- Months -
new_mon = val(right$(left$(new_date$,5),2))
old_mon = val(right$(left$(old_date$,5),2))
`- Years -
new_yr = val(right$(new_date$,2))
old_yr = val(right$(old_date$,2))
`** TO FIND TIME **
`This only works if the new and old times are within the SAME day - ...
IF new_day = old_day AND new_mon = old_mon AND new_yr = old_yr
`If old and new times are within the same hours...
If old_hr = new_hr
`...if old and new times are wihin same minute -> just subtract seconds to find seconds
If old_min = new_min Then SEC = new_sec - old_sec
`If NOT within same minute -> find remaining seconds until end of old minute and ADD IT to the new seconds
If old_min <> new_min Then SEC = (60 - old_sec) + new_sec
`If Seconds goes over 60, subtract 60 and add 1 minute
If SEC >= 60 then SEC = SEC - 60 : MIN = MIN + 1
`since we are within the same hour, the new minute will always be ahead of the old minute so just subtract new - old
MIN = new_min - old_min
`this doesn't really matter but anyway.. just subtract new hour from old hour and it'll be the hours (which will be zero in this situation since old hr = new hr)
HRS = new_hr - old_hr
`BUT! If old and new times are NOT within the same hours
If old_hr <> new_hr
`directly we have to find the remaining seconds in the minute and the remaining minutes in the hour
`we have to subtract one from min because think about time moving forward: when we do (60 - old sec) + new sec we are basically completing the minute which means when we have to count the remaining
`minutes we SHOULD start from one minute ahead i.e. the number of minutes goes down by 1. Same thing for hours.
SEC = (60 - old_sec) + new_sec :
MIN = (60 - old_min) + new_min - 1
HRS = new_hr - old_hr - 1
If SEC >= 60 then SEC = SEC - 60 : MIN = MIN + 1
If MIN >= 60 then MIN = MIN - 60 : HRS = HRS + 1
Endif
`This is designed to work if the new and old times are in SEPARATE days....
If new_day <> old_day OR new_mon <> old_mon OR new_yr <> old_yr
`TO Find time
HRS = (24 - old_hr) + new_hr
MIN = (60 - old_min) + new_min
SEC = (60 - old_sec) + new_sec
If SEC >= 60 then SEC = SEC - 60 : MIN = MIN + 1
If MIN >= 60 then MIN = MIN - 60 : HRS = HRS + 1
If HRS >= 24 then HRS = HRS - 24 : DDD = DDD + 1
print HRS ; ":" ; MIN ; ":" ; SEC
`** TO FIND DAYS **
`first, if start and end dates are in the same year...
If old_yr = new_yr
`if dates are in the same month (old_mon = new_mon) then just subtract the dates and that's it
If old_mon = new_mon then DDD = new_day - old_day
`but if the dates are in months that are *just* next to each other i.e. old_mon - new_mon = 1...
If old_mon - new_mon = 1
`... then we must find the days leftover from the start time (old_mon) and add it to the number of days past in the end time
`BUT! each month has different number of days, SO.. if the start time = 1 (January) we must 31 - old_day and ADD to new day. repeat this process for each month
If old_mon = 1 Then DDD = (31 - old_day) + new_day
If old_mon = 2 Then DDD = (28 - old_day) + new_day
If old_mon = 3 Then DDD = (31 - old_day) + new_day
If old_mon = 4 Then DDD = (30 - old_day) + new_day
If old_mon = 5 Then DDD = (31 - old_day) + new_day
If old_mon = 6 Then DDD = (30 - old_day) + new_day
If old_mon = 7 Then DDD = (31 - old_day) + new_day
If old_mon = 8 Then DDD = (31 - old_day) + new_day
If old_mon = 9 Then DDD = (30 - old_day) + new_day
If old_mon = 10 Then DDD = (31 - old_day) + new_day
If old_mon = 11 Then DDD = (30 - old_day) + new_day
If old_mon = 12 Then DDD = (31 - old_day) + new_day
`** MONTHS TO DAYS: if within same year **
`N.B. This is only if the months are in the SAME year...
`...first check if the months are apart by more than 1 (this shows the months are not just next to each other and indicates we need to find the number of days in these months)
If abs(new_mon - old_mon) > 1
`now we run a loop from old month PLUS 1 to the new month - 1 (this way the months in BETWEEN the 2 months will be checked, i.e. not including the new or old months)
for i = old_mon+1 to new_mon-1
`now we just check which month it is (i=2, 3, 4 etc.) and based on that month we add the corresponding number of days
`note: we don't need to include i = 1 or i = 12 because old_mon+1 can never be 1 and new_mon-1 can never be 12 (due to the fixed number of months 1 to 12)
if i = 2 then DDD = DDD + 28
if i = 3 then DDD = DDD + 31
if i = 4 then DDD = DDD + 30
if i = 5 then DDD = DDD + 31
if i = 6 then DDD = DDD + 30
if i = 7 then DDD = DDD + 31
if i = 8 then DDD = DDD + 31
if i = 9 then DDD = DDD + 31
if i = 10 then DDD = DDD + 31
if i = 11 then DDD = DDD + 30
next i
Endif
`** MONTHS TO DAYS: if years are different + no year in between **
`N.B. we must separate the situation if the dates are not within the same year or else the number of days calculated will not be accurate for some reason??
IF new_yr - old_yr = 1
`first, we must find the number of days left til end of the month for the old date (just like above). we must find this based on the total number of days for THAT month MINUS old date.
`we then just add it to the "new day" value so that we get the TOTAL number of days out of each of the start and end months
If old_mon = 1 Then DDD = (31 - old_day) + new_day
If old_mon = 2 Then DDD = (28 - old_day) + new_day
If old_mon = 3 Then DDD = (31 - old_day) + new_day
If old_mon = 4 Then DDD = (30 - old_day) + new_day
If old_mon = 5 Then DDD = (31 - old_day) + new_day
If old_mon = 6 Then DDD = (30 - old_day) + new_day
If old_mon = 7 Then DDD = (31 - old_day) + new_day
If old_mon = 8 Then DDD = (31 - old_day) + new_day
If old_mon = 9 Then DDD = (30 - old_day) + new_day
If old_mon = 10 Then DDD = (31 - old_day) + new_day
If old_mon = 11 Then DDD = (30 - old_day) + new_day
If old_mon = 12 Then DDD = (31 - old_day) + new_day
`THEN, we find the months from the old date to the end of the year then find the number of days in those months (depends on the month)
for i = (old_mon+1) to 12
if i = 2 then DDD = DDD + 28
if i = 3 then DDD = DDD + 31
if i = 4 then DDD = DDD + 30
if i = 5 then DDD = DDD + 31
if i = 6 then DDD = DDD + 30
if i = 7 then DDD = DDD + 31
if i = 8 then DDD = DDD + 31
if i = 9 then DDD = DDD + 30
if i = 10 then DDD = DDD + 31
if i = 11 then DDD = DDD + 30
if i = 12 then DDD = DDD + 31
next i
`next find the number of months from start of the next year up until the
`new date's month BUT!! we must count from 1 to the new_mon - 2 (not minus 1)
`because we DONT want to count the
for i = 1 to (new_mon-1)
if i = 1 then DDD = DDD + 31
if i = 2 then DDD = DDD + 28
if i = 3 then DDD = DDD + 31
if i = 4 then DDD = DDD + 30
if i = 5 then DDD = DDD + 31
if i = 6 then DDD = DDD + 30
if i = 7 then DDD = DDD + 31
if i = 8 then DDD = DDD + 31
if i = 9 then DDD = DDD + 30
if i = 10 then DDD = DDD + 31
if i = 11 then DDD = DDD + 30
next i
ENDIF
IF new_yr - old_yr > 1
DDD = DDD + ((new_yr - old_yr) * 365)
ENDIF
print DDD
wait key
Please help!
Thank-you
I love pizza