Overview

This plugin provides an implementation of the TimeZoneResolver interface, which is used to find the best TimeZone for the current user. The implementation, TimeZoneResolverDefault, tries to find the best TimeZone this way:

  • If currently not inside a HTTP request scope, it uses the default TimeZone. This default TimeZone is configurable using SpincastConfig#getDefaultTimeZone() and is "UTC" by default.
  • If a cookie specified what TimeZone to use, this TimeZone is used. The name of this cookie is configurable using SpincastConfig#getCookieNameTimeZoneId() and is "spincast_timezone" by default.

Quick Examples

  • You can inject the TimeZoneResolver component anywhere you need it, but you also have a direct access to the best TimeZone to use inside a Route Handler :

    public void myHandler(AppRequestContext context) {
        TimeZone timeZoneToUse = context.getTimeZoneToUse();
        //...
    }

Pebble extension

An extension for Pebble, the default Templating Engine, is provided. The interface of this extension is SpincastTimeZonePebbleExtension and the default implementation is SpincastTimeZonePebbleExtensionDefault.

The main function provided by this extension, "timeZoneCookie()", outputs a script that gets the timeZone of the user with javascript and stores the result in a cookie.

For example, in your HTML header:

<!doctype html>
<html>
<head>
    // ...
    
    {{ timeZoneCookie(true) }}

    // ...
</head>

If "true" is passed as a parameter to this function, "<script></script>" tags will be added around the script. If "false" they won't.

The are a couple of options for this script in the configurations of the plugin. An interesting one is "isRefreshPageAfterAddingPebbleTimeZoneCookie()": if this configuration is "true", then the page will be automatically reloaded the very first time the script sets the timeZone cookie. This allows your application to always have access to the timeZone of the user, even on the very first page he visits! For this to work well, it is suggested that you add the script quite high in the "<head>" section of your layout, before the core of the page is loaded.

Installation

If you use the spincast-default artifact and the standard Bootstrapper, this plugin is already installed by default so you have nothing to do!

If you start from scratch, using the spincast-core artifact, you can use the plugin by :

1. Adding this Maven artifact to your project:

<dependency>
    <groupId>org.spincast</groupId>
    <artifactId>spincast-plugins-timezone-resolver</artifactId>
    <version>2.2.0</version>
</dependency>

2. Installing the provided SpincastTimeZoneResolverPluginModule module to your Guice context.

Plugin class

The class implementing the SpincastPlugin interface is SpincastTimeZoneResolverPlugin.

Javadoc

Configuration

You can bind a SpincastTimeZoneResolverConfig implementation to tweak the default configurations used by this plugin. By default, the SpincastTimeZoneResolverConfigDefault class is used as the implementation.