What is software architecture? What is the role of a software architect? How do you define software architecture? How do you share software architecture? How do you deliver software architecture?

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

That reminds me of a time when we needed to receive data from a company in the US (we're in the UK). They wanted to send dates in their local time, with no time zone at all. I had to explain them that I would have no way to convert that dates into GMT (unless I assumed they were sending it in a specific time zone), and I wouldn't know when they changed to/from summertime. They had no idea about time zones, even when they live in a country with 4 time zones and summertime.

Re: Time zones

That's exactly the type of thing I'm talking about. How did you solve it?

Re: Time zones

I have a similar challenge with a globally deployed telemetry+gps system. Part of the information is relayed to us from a partner in a mixture of GMT, Server timezone, and information timezone (where the transmitter is.) It's a nightmare of assumptions is some cases. All of that is due to the partner not storing the times in an unified timezone and/or not including time timezone information with the time stamps. My current approach is to record the timezone of each deployed transmitter and store all the time stamps in GMT. The database fields are actually named something like ReceivedOnUTC, EventTimeUTC, etc...

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

Imagine: you are developing a distributed system. You have remote, data hungry clients and server clusters spread geographicaly.
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

You could (probably) build some logic into a software or hardware router to do this. I guess it all depends on how much context the client is sending over and how easy it is to access. A software routing component (either on the client or server) would be the easiest implement.

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

We are facing the same issue with our web application .Our servers run on MST and store dates to database in MST . The problem does not creep in when users are doing actions that creates date as our client is capable of converting date to user's time zone .Problem is with the processing that is happening on server side which is in MST regardless of which timezone the user is in .So when a non MST users says give me the last day's data ,on the server side it will be interpreted and processed in MST i:e last day would be MST's last day ...How do I handle such scenarios

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

Hey Thanks for replying so quickly .We have our servers running on MST and we store our dates in MST.So whenever we process dates we mean MST.Now we want our processing to be timezone sensitive.One of the options I could think of and which you I could think of and which you have mentioned is knowing user's timezone and applying it while processing dates.But I also want to have my servers running on GMT and store GMT .I am not completely sure on the advantages of using GMT but I know it is a normal practice followed.I also wanted to ask you does the industry follow some best practices to solve dates.I surfed on the net and saw very few people discussing this issue .I was amazed as this is a problem faced by everybody .....Thanks in advance ...would appreciate a reply from you...

Re: Time zones

forgot to mention that on server side we use Java programming language .

Add a comment Send a TrackBack
Software architecture for developers