[Gambas-user] arithmetic operations on dates and times

Ian Haywood ihaywood at ...1979...
Thu Jun 9 04:54:13 CEST 2011


On Thu, Jun 9, 2011 at 12:27 PM, Kevin Fishburne
<kevinfishburne at ...1887...> wrote:
> How in the world would one do this? It's not really important that the
> client know the actual date, but it does need to know the time. The
> client should be able to add "Timer" to the game world time so it can
> keep an accurate 24-hour time while the player is playing. Basically the
> client would receive the game world time upon authentication, then look
> at "gameworldtime + Timer" to determine what time of day it was until
> the player stopped playing.
>
> I noticed that adding and subtracting to a date or time prints a
> floating point number, but multiplying or dividing complains about
> expected datatypes. Any insight or outright solutions would bring light
> and dark to the world. :)
Best suggestion is to formally convert the Date to a Float using
CFloat(...) function.
This number is the number of days since 8000BC, time is the fraction
part, so .5 for noon exactly, just before midnight will be .99 and so
on.

So your equation is

   ( CFloat(base_time) + CFloat(Now) ) * 4

You can then use CDate to turn this nuber back into a real date. BUT
my advice would be to send this number, before conversion, down the
wire to the client.

The client then calculates its own time using
      server_time + (Timer () / 86400.0)

86,400 is the number of seconds in a day, this is because Timer comes
in seconds and we want fraction-of-day to keep in line with everything
else.

This number can then be converted back to a date and displayed by the
client, using the clients own localisation setting (not the servers)
so users see the time in their own localisation, but servers and
clients don't stuff up if they are in different countries

Ian




More information about the User mailing list