Time zones
Every project, every architecture
When I start a new project, I can be sure that there are always a number of common tasks that need to happen. These include defining document templates, the source code structure, a build file, the development environment, the continuous integration environment, common frameworks/libraries, etc. These are known quantities and they are something that I typically plan for. One aspect of a system that always catches me out, however, is time zones.
From a Java perspective, time zones are actually pretty easy to deal with. The built-in date API might not be that intuitive, but at least it's fairly easy to get to grips with. For example, if I want to be sure that I represent local time in London, I can use the following code to create a Calendar.
Calendar cal = Calendar.getInstance(
TimeZone.getTimeZone("Europe/London"));
Provided that you remember to explicitly specify a time zone whenever you are creating or formatting dates, you should have very few problems. Of course, it's all very well actually knowing this, but time zone management is always something that I overlook until something starts misbehaving. Typical triggers include :
- Deploying the system to a server running in a different time zone (e.g. usually GMT all year round).
- Working with a physically separated team.
- Daylight saving changes during the project.
As an architect though, you have to look at things from a wider perspective than just the application tier, and this is when it can start to get tricky. For example, what happens when your application tier and database are running in different time zones? Do you ensure the application tier sends all date/times in a specific time zone (e.g. GMT) or rely on the database defaults? What if your database vendor allows time zone information to be stored alongside any date/times, do you use this feature? What if they don't? What happens if clients are connecting to your application from all over the world and sending you dates?
And it doesn't stop at the implementation of the system itself. If you're planning to deploy your system across multiple sites or countries, what do you do then? What if that deployment is active-passive; should you plan to test the DR and BCP environments?
Time zone management seems like a pretty trivial detail, but it can easily have wider implications and has played a part in every project and every architecture that I've been involved in for the last couple of years. As we build more and more systems that break down traditional geographical barriers, projects need to have somebody to look after this type of detail. For me, that's part of the architect's role.
Re: Time zones
Re: Time zones
Re: Time zones
I must admit I get a perverse pleasure out of this sort of thing - it's a tricky problem involving very simple maths! Plus I find the Java date APIs fairly natural if not ideal.
The side of things that naturally involves time zones I've typically found easier, such as creation/modification dates - it's just a case of capturing the originating time zone. Sure, you've got to decide whether the time zone is that of the client or any one of your tiers. Just be wary of mixing them (eg, column or O/R tool defaults).
Those problems that relate to business rules have often been far less straightforward (for me), such as settlement dates involving different time zones. In such cases there doesn't seem to be a time zone that will suit both parties so an artificial one has to be introduced (such as UTC). It feels slightly unsatisfactory but probably better than storing as a datatype other than date in my opinion.
Re: Time zones
Can the client time zone information be used to help with supporting locality of request? So that when your client is in London its request is satisfied by the cluster in Europe rather than China [assuming corporate WAN for the moment]
Re: Time zones
Whether you use the client's local time zone or one assigned to the client "session" by its local server will depend on your environment and requirements (can you trust your client to be using the correct time zone?). Including this information in the client session context may indeed help you distribute its operations across various regions, however, and may well be a good reason to avoid using the server's time zone in your business rules.
Re: Time zones
Re: Time zones
You're right that "yesterday" is a locale-specific concept. Therefore any service that depends on that concept has to either assume a particular timezone or be provided with one.
If you have some query, say getYesterdaysData() then you should probably overload it to take a timezone (eg, the user's timezone in this case). Work out what yesterday is in that timezone (ie, a start and end date) and convert that to your system's timezone (ie, MST) - use that to construct your database query.
A more specific answer will depend on what programming language you're using and perhaps how you're storing the dates in the database.
Re: Time zones











