Just A Simple Timer

Windows specific forum
Randy Walker
Addict
Addict
Posts: 1059
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

Just A Simple Timer

Post 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. :lol:
Last edited by Randy Walker on Tue Dec 24, 2024 10:52 pm, edited 2 times in total.
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
User avatar
Mindphazer
Enthusiast
Enthusiast
Posts: 459
Joined: Mon Sep 10, 2012 10:41 am
Location: Savoie

Re: Just A Simple Timer

Post by Mindphazer »

Hi Randy,
I think you can post your code into the Tricks'n'Tips section, as it is fully cross platform ! :wink:
MacBook Pro 16" M4 Pro - 24 Gb - MacOS 15.4.1 - Iphone 15 Pro Max - iPad at home
...and unfortunately... Windows at work...
User avatar
blueb
Addict
Addict
Posts: 1116
Joined: Sat Apr 26, 2003 2:15 pm
Location: Cuernavaca, Mexico

Re: Just A Simple Timer

Post 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
- It was too lonely at the top.

System : PB 6.21(x64) and Win 11 Pro (x64)
Hardware: AMD Ryzen 9 5900X w/64 gigs Ram, AMD RX 6950 XT Graphics w/16gigs Mem
BarryG
Addict
Addict
Posts: 4168
Joined: Thu Apr 18, 2019 8:17 am

Re: Just A Simple Timer

Post by BarryG »

%mm is the month. Use %ii for minutes. ;)
User avatar
Mindphazer
Enthusiast
Enthusiast
Posts: 459
Joined: Mon Sep 10, 2012 10:41 am
Location: Savoie

Re: Just A Simple Timer

Post by Mindphazer »

BarryG wrote: Wed Dec 18, 2024 12:53 pm %mm is the month. Use %ii for minutes. ;)
I didn't notice it ! :D
Though I've been tricked in the past by this mistake in one of my codes (it took me a few months to realize !)
MacBook Pro 16" M4 Pro - 24 Gb - MacOS 15.4.1 - Iphone 15 Pro Max - iPad at home
...and unfortunately... Windows at work...
Randy Walker
Addict
Addict
Posts: 1059
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

Re: Just A Simple Timer

Post 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 ! :wink:
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?
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
Randy Walker
Addict
Addict
Posts: 1059
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

Re: Just A Simple Timer

Post 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. 👍
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
User avatar
Mindphazer
Enthusiast
Enthusiast
Posts: 459
Joined: Mon Sep 10, 2012 10:41 am
Location: Savoie

Re: Just A Simple Timer

Post 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
MacBook Pro 16" M4 Pro - 24 Gb - MacOS 15.4.1 - Iphone 15 Pro Max - iPad at home
...and unfortunately... Windows at work...
Randy Walker
Addict
Addict
Posts: 1059
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

Re: Just A Simple Timer

Post 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.
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
infratec
Always Here
Always Here
Posts: 7613
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Just A Simple Timer

Post by infratec »

And why not AddWindowTimer()? It is even more simple, it does not need Date()
Randy Walker
Addict
Addict
Posts: 1059
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

Re: Just A Simple Timer

Post 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()?
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
infratec
Always Here
Always Here
Posts: 7613
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Just A Simple Timer

Post 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.
normeus
Enthusiast
Enthusiast
Posts: 472
Joined: Fri Apr 20, 2012 8:09 pm
Contact:

Re: Just A Simple Timer

Post by normeus »

Date() for an older PureBasic version stops working on Jan 2038
google Translate;Makes my jokes fall flat- Fait mes blagues tombent à plat- Machte meine Witze verpuffen- Eh cumpari ci vo sunari
Post Reply