Jun 16, 2007

Formatting date to client timezone

Unlike a locale information that is passed by browsers via HTTP header, there is no straightforward way to retrieve the information about client timezone offset.
I also have not found any ready-to-use solutions to workaround this in web and one day when we've been posted the bug that our SMS sending times appears to be in the server timezone. I've found the solution.
The idea is pretty easy, the only way you can pass client timezone to the server is by calculating it using

var tzo = new Date().getTimezoneOffset();        


Next step - you need to pass it to the server, there is a number of ways to do it but they all have one drawback, you cannot format dates from the just loaded page, since script executes on the client. Anyway the options are: Cookie and AJAX. Cookie is easier and seems that AJAX is overkill here, that is why I've chosen cookie way.

The whole code on the client looks like
<c:if test="${sessionScope['sonoportal.timezoneOffset'] == null}">
<!--Setting TZ offset-->
<script type="text/javascript">
var tzo = new Date().getTimezoneOffset();
document.cookie = "timezoneOffset=" + escape(tzo * (-1));
</script>
</c:if>

We've also added the condition not to send cookie if we already have info about user timezone offset in the HttpSession.

Now we need to process the timezone on server and set it to the session. In our application we are using standard JSTL fmt:formatDate for formatting dates and times. Fortunately you can set default timezone in the user's HttpSession scope and it will be used authomatically. Here is the code for a Filter that do the job:

package com.sonopia.sonoportal.web.filter;

import java.io.IOException;
import java.util.TimeZone;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.jstl.core.Config;

import org.apache.commons.lang.time.DateUtils;

/**
* @author Mykola Paliyenko
*/
public class SetTimezoneOffsetFilter implements Filter {

public static final String PROPERTY_TIMEZONE_OFFEST = "sonoportal.timezoneOffset";

public static final String COOKIE_NAME = "timezoneOffset";

public void destroy() {
}

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse res = (HttpServletResponse) response;
if (req.getSession().getAttribute(PROPERTY_TIMEZONE_OFFEST) == null) {
if (req.getCookies() != null) {
for (Cookie cookie : req.getCookies()) {
if (COOKIE_NAME.equals(cookie.getName())) {
int timezoneOffsetMinutes = Integer.parseInt(cookie.getValue());
TimeZone timeZone = TimeZone.getTimeZone("GMT");
timeZone.setRawOffset(
(int) (timezoneOffsetMinutes * DateUtils.MILLIS_PER_MINUTE));
Config.set(req.getSession(), Config.FMT_TIME_ZONE, timeZone);
req.getSession().setAttribute(
PROPERTY_TIMEZONE_OFFEST, timezoneOffsetMinutes);
// removing cookie, Sun sucks as usual in their API
cookie.setMaxAge(0);
res.addCookie(cookie);
}
}
}
}
chain.doFilter(request, response);
}

public void init(FilterConfig config) throws ServletException {
}

}


Note that we are removing cookies once we get the timezone offset to reduce client/server traffic since otherwise it get send with every single request.
What remains? Declaring filter web.xml file
        <filter>
<filter-name>SetTimezoneOffsetFilter</filter-name>
<filter-class>
com.sonopia.sonoportal.web.filter.SetTimezoneOffsetFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>SetTimezoneOffsetFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>



Thats all. Now your site is one of a little set of sites that care about user's time zone.

Limitations/Drawbacks


- First served page in session will show the server times.
- If user changing timezone within session it will not be tracked by server. It is a very minor issue but still
- Additional global filter for the application

Getting started

This blog is about building feature rich application based on Java open source frameworks, problems and solution related to it. Creating our product day by day from prototype to the live system, from 4 developers to 30+, we get valuable experience that might be useful to share with community.