Re: How do you handle time zones?

From: Clark Mueller (cpmuelle..ac.com)
Date: Sat Oct 06 2007 - 02:59:26 EDT

  • Next message: Miguel Arroz: "Re: How do you handle time zones?"

    I have done some experimentation with this myself just this past week.
    You can download some sample code that I wrote up to test with at:

    http://kindofworks.com/misc/TimeZoneTest.zip

    Basically, if you have a look at Application.java and Main.java, you
    can see the bulk of what's going on. Most of the NSTimestampFormatters
    in Main.java are something you would want to abstract into your
    session. The one thing I never got this example doing is to pick up
    the user's time zone from the HTTP request headers. This won't happen
    at all if you run in direct connect mode, but that wasn't being passed
    from my Apache either. Other people have alluded to doing this, and I
    expect it's an Apache config issue, but the accepted solution seems to
    be to have the user explicitly choose a time zone.

    My solution when deploying these is simply to have my users choose a
    time zone and to save it with their user data in the database. Then,
    the formatters in Session.java are initialized with that information
    when the user logs in. In the example in the zip above, the user
    chooses a time zone and it is saved in a cookie (make sure when you
    run this from your machine, you use an IP address to connect, and not
    localhost, or the cookie will not be saved correctly). Later, if the
    cookie is present, it uses the TZ value in the cookie, and if it is
    not, it asks them to choose a TZ first. If the TZ is null, at any
    time, it looks like it just defaults back to the default time zone
    (which in the sample is GMT).

    Another thing the example doesn't show is the parse time zone. If you
    have your users entering a date, you also want to call
    setDefaultParseTimeZone on your formatter (e.g.
    losAngeles
    .setDefaultParseTimeZone(NSTimeZone.timeZoneWithName("America/
    Los_Angeles"))). See NSTimestampFormatter docs for more info.

    Cheers,
    Clark

    P.S. You might get some more helpful replies on this topic if you take
    it to webobjects-dev on lists.apple.com.

    On 5 Oct 07, at 2:53 PM, Miguel Arroz wrote:

    > Hi!
    >
    > My problem now is how to know the existing time zones... I was
    > assuming that there were only 24 time zones, but that is wrong,
    > there are many more. As far as I understand, a time zone is the time
    > offset and the DST information. The JDK has tons of time zones. So,
    > question: is there somewhere a usable table with time zone names and
    > "java names" that I can use to create a reasonable time zone menu?
    >
    > Yours
    >
    > Miguel Arroz
    >
    > On 2007/10/05, at 20:27, Andrew Lindesay wrote:
    >
    >> Hello Miguel;
    >>
    >> This is how I handle it --- Store a TZ in the session. I generally
    >> achieve this by having a attribute of the timezone against the user
    >> who logged in in the database. Next, create a formatter for date
    >> and timestamps in the session with accessors in the components'
    >> superclass. Every time you want to render a date/timestamp or have
    >> a date/timestamp entry field, use the formatters which are able to
    >> transform from GMT <---> Local Time. So your application data
    >> stays in GMT and is moved to and from the local time.
    >>
    >> You can see this pattern in my LEWOStuff framework;
    >>
    >> LEWOSimpleComponent.getFormatterForDateAndTimes()
    >> LEWOSimpleComponent.getFormatterForDates()
    >>
    >> LEWOSession.getTimeZone()
    >> LEWOSession.getFormatterForDateAndTimes()
    >> LEWOSession.getFormatterForDates()
    >>
    >> cheers.
    >>
    >>> As I live in a GMT country, I usually don't think much about this,
    >>> but now I need help! :)
    >>
    >> ___
    >> Andrew Lindesay
    >> technology : www.lindesay.co.nz
    >> business : www.silvereye.co.nz
    >>
    >>
    >
    > Miguel Arroz
    > http://www.terminalapp.net
    > http://www.ipragma.com
    >
    >
    >



    This archive was generated by hypermail 2.0.0 : Sat Oct 06 2007 - 03:00:54 EDT