You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Angelo Chen <an...@yahoo.com.hk> on 2009/02/17 23:46:55 UTC

T5: OutputLocaleNumber

Hi,

I use  http://wiki.apache.org/tapestry/Tapestry5OutputLocaleNumber
OutputLocaleNumber  to format a date for display, it works, but it is simply
based on the date in the server, is there a way to display according to the
viewer's time zone? any tips? 

Ac
-- 
View this message in context: http://www.nabble.com/T5%3A-OutputLocaleNumber-tp22068067p22068067.html
Sent from the Tapestry - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: T5: OutputLocaleNumber

Posted by Julian Wood <wo...@ucalgary.ca>.
On our login page, we add some javascript to set a cookie which  
contains the timezone.

     @AfterRender
     public void setClientTimeZoneCookie() {
         StringBuilder sb = new StringBuilder();
         sb.append("var expires = new Date();\n");
         sb.append("expires.setTime(expires.getTime() +  
30*24*60*60*1000);\n");
         sb.append("document.cookie = 'TimeZoneOffset=' + new  
Date().getTimezoneOffset() * -1 + \";expires=\" +  
expires.toGMTString() + \";path=/\";");
         renderSupport.addScript(sb.toString());
     }

All of our pages extend BasePage, and implements this method:

     @Inject
     private CookieSource cookieSource;

     public TimeZone getClientTimeZone() {
         Cookie[] cookies = cookieSource.getCookies();
         for (Cookie cookie : cookies) {
             if (cookie.getName().equals("TimeZoneOffset")) {
                 // this will be something like -420 for MST (note  
javascript is backwards to java and almost all other timezone maps)
                 // that is in minutes (i.e., 7 hours)
                 String tzOffset = cookie.getValue();
                 TimeZone tz;
                 try {
                     // here we don't get DST
                     int offset = Integer.parseInt(tzOffset) / 60;
                     String timezoneString = offset >= 0 ? "GMT+" +  
offset : "GMT" + offset;
                     tz = TimeZone.getTimeZone(timezoneString);
                 } catch (NumberFormatException e) {
                     logger.warn("Can't determine client timezone  
(using UTC): " + tzOffset);
                     tz = TimeZone.getDefault(); // UTC
                 }
                 return tz;
             }
         }
         return TimeZone.getDefault();
     }

Now on any page, you can easily render a datetime using the viewer's  
timezone.

             SimpleDateFormat dateFormat = new  
SimpleDateFormat(DATE_FORMAT);
             dateFormat.setTimeZone(clientTimeZone);
             dateFormat.format(event.getTime());


HTH,

J

On Feb 17, 2009, at 3:46 PM, Angelo Chen wrote:

>
> Hi,
>
> I use  http://wiki.apache.org/tapestry/Tapestry5OutputLocaleNumber
> OutputLocaleNumber  to format a date for display, it works, but it  
> is simply
> based on the date in the server, is there a way to display according  
> to the
> viewer's time zone? any tips?
>
> Ac
> -- 
> View this message in context: http://www.nabble.com/T5%3A-OutputLocaleNumber-tp22068067p22068067.html
> Sent from the Tapestry - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org