CFMX applications on servers with different time zones

I came across a problem recently where I needed to deliver an application to a client in a different timezone. It seems like it shouldn't be too hard to do but CF is a bit lacking in the Timezone department.

Initially I found the excellent timezone.cfc from Paul Hastings which did a sterling job.

I created a wrapper cfc for it and with that could simple convert entire queries into the correct time zones, as well as single dates. This was fine for display (if a bit slow) but made some database functionality not work. For example the DB was still operating in Australia/Sydney days and so when doing a group by day report it would by Sydney days not Dublin days as was needed.

After a bit more hunting I discovered that MYSQL can handle time zone conversion very efficiently. The CONVERT_TZ method will do conversions on the queries coming out and values going into the DB.

To make my life a bit easier I've wrapped the functionality up into a little CFC available here.

Usage of the CFC is as follows:

  • Init the CFC with a DSN, ServerTimeZone and UserTimeZone (what will be displayed)

Then for columns and values you want changed wrap these functions around the columns name.

  • serverToUserTZ will convert values to user local timezone
  • userToServerTZ will convert values to server local timezone
SELECT #serverToUserTZ('dt_created')# as dt_created
FROM tbl_users

It runs with almost no difference in query times (but I haven't benchmarked it).

I still need to verify if the grouping by days in a different time zone works - more to follow.

Comments
well that's the first thing i've heard in a long while that actually recommends mySQL ;-)

in any case you should be aware of timzone hell:
http://www.sustainablegis.com/blog/cfg11n/index.cf...
# Posted By PaulH | 4/30/07 12:54 AM
Hi Paul,

Thanks for the link - that's exactly where I got the initial solution from. Thanks for doing the hard work.

Cheers,
Mark
# Posted By Mark Lynch | 4/30/07 7:16 AM
BlogCFC was created by Raymond Camden. This blog is running version 5.1.004.