Analog clock service for web apps

I’m nowadays working in an international company which has offices in multiple countries. I was looking for a way to get our Confluence, Enterprise Wiki, to display current time in all of our offices in one page. I didn’t find any simple solutions as lots of services just provided me a chunk of HTML that I could add to my page to create a dynamic clock but wiki syntax doesn’t really let me do that.

I’m a great fan of Google Charts API. Developers at Google have developed a really nice URL based API for the charting purposes. This was inspiration for me when I created Clock Service API for my own needs.

Clock Service is a simple servlet implementation that generates an image with the specified parameters. API can be used to define image size (s=100), timezone (tz=zone), title (t=Helsinki) and 12h/24h format (f=24). With this I can use the following URLs to produce three different clock images for three different timezones.

/clockservice/clock?tz=America/New_York&s=150&t=Atlanta&f=12
/clockservice/clock?tz=Europe/Helsinki&s=150&t=Helsinki&f=12
/clockservice/clock?tz=Australia/Sydney&s=150&t=Sydney&f=12

If you use those URLs in IMG tags then the result would look like this:

Clock (Atlanta)Clock (Helsinki)Clock (Sydney)

The images above aren’t created dynamically as I don’t have any public servlet container available.

The code behind the service is really quite simple. There are only two classes ClockServlet and Clock. Servlet implementation is only used for parsing the parameters from the request and writing the bytes from Clock.getImageData(..) to the web response.

Timezones are handled using the Java’s Calendar and TimeZone classes like this:

  1. TimeZone tz = TimeZone.getTimeZone("Europe/Helsinki");
  2. Calendar cal = Calendar.getInstance(tz);
  3. int hour = cal.get(Calendar.HOUR);
  4. int minute = cal.get(Calendar.MINUTE);

You can find the source code and compiled WAR package from the application page, here. Application is distributed under the Apache License v2.0.