Page 1 of 1
Just A Simple Timer
Posted: Wed Dec 18, 2024 1:46 am
by Randy Walker
I found a number of sample timers written way over my head, with structures and all kinds of unneceessary things, so I turned to Date() for a solution, real simple:
Code: Select all
Debug FormatDate("%hh:%ii:%ss", Date()) ;Just here to proof timer
seconds = 10
timer = Date() + seconds
Repeat
Delay(50)
tik = Date()
Until tik >= timer
Debug FormatDate("%hh:%ii:%ss", Date()) ;Just here to proof timer
Surpising thing -- I didn't find any solutions that required an SQL database.

Re: Just A Simple Timer
Posted: Wed Dec 18, 2024 10:11 am
by Mindphazer
Hi Randy,
I think you can post your code into the Tricks'n'Tips section, as it is fully cross platform !

Re: Just A Simple Timer
Posted: Wed Dec 18, 2024 12:43 pm
by blueb
I'm not sure about that....
Code: Select all
Procedure.s DateAsString()
aStr.s = Space(12)
GetDateFormat_(#LOCALE_USER_DEFAULT, 0, 0, 0, aStr, 12)
ProcedureReturn aStr
EndProcedure
Procedure.s TimeAsString()
aStr.s = Space(12)
GetTimeFormat_(#LOCALE_USER_DEFAULT, 0, 0, 0, aStr, 12)
ProcedureReturn aStr
EndProcedure
Debug "Localized Date/Time: " + DateAsString() + " "+ TimeAsString()
MessageRequester("Localized Date/Time: ", DateAsString() + " " + TimeAsString())
Debug "================"
Debug FormatDate("%hh:%mm:%ss", Date()) ;Just here to proof timer
seconds = 10
timer = Date() + seconds
Repeat
Delay(50)
tik = Date()
Until tik >= timer
Debug FormatDate("%hh:%mm:%ss", Date()) ;Just here to proof timer
I did a comparison, and it seems to use the time when I started my machine..
Coincidence?
Localized Date/Time: 12/18/2024 5:36:53 AM
================
05:12:10
05:12:20
Re: Just A Simple Timer
Posted: Wed Dec 18, 2024 12:53 pm
by BarryG
%mm is the month. Use
%ii for minutes.

Re: Just A Simple Timer
Posted: Wed Dec 18, 2024 1:12 pm
by Mindphazer
BarryG wrote: Wed Dec 18, 2024 12:53 pm
%mm is the month. Use
%ii for minutes.
I didn't notice it !
Though I've been tricked in the past by this mistake in one of my codes (it took me a few months to realize !)
Re: Just A Simple Timer
Posted: Wed Dec 18, 2024 6:53 pm
by Randy Walker
Mindphazer wrote: Wed Dec 18, 2024 10:11 am
Hi Randy,
I think you can post your code into the Tricks'n'Tips section, as it is fully cross platform !
Uhhh, last time I put something in Tricks'n'Tips it got moved to Questions so I figured I'm just not qualified to touch the Tricks'n'Tips section.
Looking back I guess it should be cross platform. Did you test in linux or MAC?
Re: Just A Simple Timer
Posted: Wed Dec 18, 2024 6:57 pm
by Randy Walker
BarryG wrote: Wed Dec 18, 2024 12:53 pm
%mm is the month. Use
%ii for minutes.
Thanks for pointing that out BarryG. I made the correction in my OP.

Re: Just A Simple Timer
Posted: Wed Dec 18, 2024 10:36 pm
by Mindphazer
Randy Walker wrote: Wed Dec 18, 2024 6:53 pm
Uhhh, last time I put something in Tricks'n'Tips it got moved to Questions so I figured I'm just not qualified to touch the Tricks'n'Tips section.
Looking back I guess it should be cross platform. Did you test in linux or MAC?
I tested it on my Mac, it works as expected.
I also tested it on an Ubuntu VM, and it works fine too
Re: Just A Simple Timer
Posted: Thu Dec 19, 2024 5:13 am
by Randy Walker
Mindphazer wrote: Wed Dec 18, 2024 10:36 pm
Randy Walker wrote: Wed Dec 18, 2024 6:53 pm
Looking back I guess it should be cross platform. Did you test in linux or MAC?
I tested it on my Mac, it works as expected.
I also tested it on an Ubuntu VM, and it works fine too
Glad to hear its proven cross platform. Maybe an admin can move this to tips&tricks.
Re: Just A Simple Timer
Posted: Tue Dec 24, 2024 1:54 pm
by infratec
And why not AddWindowTimer()? It is even more simple, it does not need Date()
Re: Just A Simple Timer
Posted: Tue Dec 24, 2024 10:48 pm
by Randy Walker
infratec wrote: Tue Dec 24, 2024 1:54 pm
And why not AddWindowTimer()? It is even more simple, it does not need Date()
That is an alternative option. Really not more simple if you have to create a window to use it though, is it?
AND What's wrong with using Date()?
Re: Just A Simple Timer
Posted: Thu Dec 26, 2024 12:42 am
by infratec
Using Date() needs additional calls and variables.
And your version is less exact, since Delay() can be interrupted by other events.
So 50ms can be 100ms at the end, or 1 Second can end at 1.1 seconds.
If you want delays below 1 second you need ElapsedMilliseconds() and not Date()
In most cases you are programming a windowed program, so AddWindowTimer() is the easiest and more exact way.
Or you have to use it as a blocking code which stops your program from working -> very bad if you wait 10 seconds and the user wants to quit the
program.
Or you have to implement the timer in a thread.
It could be even more complicated, always depending on what you want to achieve.
Re: Just A Simple Timer
Posted: Thu Dec 26, 2024 3:40 am
by normeus
Date() for an older PureBasic version stops working on Jan 2038