Monday, August 17, 2009

Display Server Date Dilemma

Had this case recently where the client wanted to display their local time on the banner of their webpage. First I didn't think much of it, just required a single line of php to print out the server time (server is set to the same timezone as the client).

The server though is a VPS and the site is complex enough and busy engough to be under a bit of load, so we make use of Drupal's page caching, e-accellerator and also memcache. With that trifector running (and 1GB of RAM) the VPS handles things quite smoothly.

Displaying the time though threw a spanner in the works. Unfortunately, time continues to change, which means that it doesn't like being cached. If we're displaying the time accuracy down to the minute then Drupal's page cache would need to be refreshed every 60 seconds. Drupal's cron is set to run once an hour at the moment and considering that it does a few other tasks like checking for module updates and indexing, I don't think running the cron every minute would help us reduce load.

So I started looking for a few other options and came up with the following ideas based on moving the function to the client's browser to process:

1. Javascript could display the date using the appropriate offset and the client's clock. This relies on the client having the time (and location) set correctly on their own machine.

2. Javascript could use ajax to get a non-cached version of the date, either from our server or a separate time server. This would be accurate but would double the number of requests/responses needed.

3. Javascript could use the page response header timestamp to display the server time. I looked into this one and found that there is indeed a fileCreatedDate header, but it's not accessible to javascript in all browsers.

So at the moment, option 1 seems like the best solution to this dilemma. I'm not happy with it though, I don't like relying on the general public to have their computers configured correctly.

If anyone else has any other creative solutions to this one, I'd love to hear them.

No comments:

Post a Comment