Search code examples
coldfusioncoldfusion-9

How do you use DateDiff without knowing the server time?


I bit of a puzzle to solve in CF here.

You have a timestamp passed as an argument to the server. For example 2012-5-10 14:55. How would you see if 30 seconds has passed from the time given and return true or false?

Rules:

  1. You cannot use the current server time to check if 30 seconds have passed because the timestamp comes from anywhere in the world and will always be wrong.

  2. In this case you can always trust the argument timestamp passed to the server.

How would this be possible in ColdFusion 9 (if at all)?


Solution

  • Hmmm.... Your problem is that you don't know the latency. You can count 30 seconds from the time you receive the timestamp - but that could be seconds after the time stamp was created.

    You can count 30 seconds easily enough with...

    sleep(30000); //1000 miliseconds = 1 second. 30k = 30 seconds
    

    So as soon as you get the var you could "wait" for thirty seconds and then do something. But from your question it seems like you need exact 30 seconds from the time the timestamp (created on the client) was created. You probably cannot be that exact because:

    1. The 2 clocks are not in synch
    2. You cannot figure out the latency of the request.
    3. Even if you could, since you don't control the client you will have trouble guaranteeing the results because HTTP is stateless and not real time.

    If you can dictate an HTML5 browser you could use websockets for this - it's practically begging for it :) But that's the only real solution I can think of.