Create an "internal" clock

BlitzMax Forums/BlitzMax Beginners Area/Create an "internal" clock

Queller(Posted 2008) [#1]
How best could I create an "internal" clock? By internal, I mean take the RTC ticks, but allow flexible time definitions such as allow the internal time to be ahead or behind the real current time, and also allow for things like a "day" being defined as 32 hours instead of 24 for example. I would also like to be able to use hooks to trigger events at different points in a "day". May I have some pointers?


Queller(Posted 2008) [#2]
Not that anyone seems to care, but here is what I cobbled.



And a sample




tonyg(Posted 2008) [#3]
Might be too late but I would have a clock type where day, hour , min and secs are specified in terms of ms.
If you want a 32 hour day then set day to 32*min_ms*sec_ms.
My triggers would then check the ms counter from which the time is calculated.


Queller(Posted 2008) [#4]
Hmmm... is a good suggestion. Thanks!


Vilu(Posted 2008) [#5]
Queller, I'm in the process of further developing your type to support months, weekdays, leap years and adding/substracting an arbitrary amount of time units. Unless there already is such a thing out there...? :)


Queller(Posted 2008) [#6]
Not as far as a quick search reveals. Those are exactly the features I was planning on adding too...


Vilu(Posted 2008) [#7]
Well, as you'll probably beat me to it (I'm quite busy with my day job), care to share the results...? ;)


Queller(Posted 2008) [#8]
tonyg, would your suggestion be based on using Millisecs() every loop, or something? That seems like an ugly way to track milliseconds. If I use SetTimer(1000) and base everything off of millisecond ticks as above, the clock hangs after a few seconds... [edit] never mind, i have the millisecond resolution under control [/edit]


Vilu(Posted 2008) [#9]
Also remember the dreaded 26-day (or something) wrap-around of the millisecs counter. Not that I expect anyone running the computer 26 days straight and playing my game without rebooting, but that's always a possibility, especially when used in game servers.

For that reason I feel using a real timer is a bit more foolproof, although you can implement additional checks for detecting the millisecs wrap.


Queller(Posted 2008) [#10]
I was unaware of such a problem... is that because the memory holding the MilliSecs() data counter gets filled up and has to reset? In any case, I have circumvented it by not using MilliSecs() :P


Vilu(Posted 2008) [#11]
Yes, the signed integer(!) holding the Millisecs value overflows after hitting the 2 billion (2,147,483,648) mark and wraps around to negative 2 billion, and starts counting towards zero from there. Do a forum search for "negative millisecs" for details and some useful hints to cope with it.

I had to deal with it when coding a server-side monitoring application with BlitzMax at work. The only thing causing some grief is the moment it wraps around. Otherwise doing a simple previousValue - Millisecs() comparison works with negative values as well.


Queller(Posted 2008) [#12]
You use BlitzMax at your place of employment? What I would do to be paid to program in BlitzMax... !!! Anyway, I'll have some decent code to post soon.


Vilu(Posted 2008) [#13]
It's really no different from programming with Python, Perl, Progress or C#, which I also do at work. It's the matter of choosing the right tool for the right job. Every piece of software I do are all in-house apps for the company I work for, so I get to choose the language.

The reason for choosing Blitz for the job was the requirement for a Win32+Linux platform server-client GUI app which very few programming languages do in a "rapid application development" -way. Blitz is god-sent in some situations. =)

In any case, I look forward to your custom calendar type. It's for Ananta (my game project), not for work, provided you release it as public domain ;)


Queller(Posted 2008) [#14]
Vilu, have you seen Brucey's awesome DateTime Module? It may be just what you are looking for.


Vilu(Posted 2008) [#15]
Yeah, I stumbled on it but it seems a bit too bloated for my purposes. Also I really need a ticking clock with a variable tick rate, which Brucey's module most likely doesn't offer. I've thought to take a closer look at it at some point and see if it can be easily controlled by an external timer.