Timezone Adjustment?

Discussion in 'Ruby' started by Lon Baker, Dec 1, 2004.

  1. Lon Baker

    Lon Baker Guest

    I am new to Ruby and am hitting a wall when it comes to "displaying"
    the date/time from a database and justing it for the a users local
    timezone in a web application.

    Is there a simple way of doing this?

    My experience in coming from the world of Java it was relatively easy.
    But I have not been able to find a easy manner of doing this in Ruby.

    Any help would be greatly appreciated.

    --
    Lon
    Lon Baker, Dec 1, 2004
    #1
    1. Advertising

  2. Lon Baker

    zunda Guest

    Hi,

    > "displaying"
    > the date/time from a database and justing it for the a users local
    > timezone in a web application.
    >
    > Is there a simple way of doing this?


    What I have been doing is to play with the environment variable TZ:

    t = Time.now
    ENV['TZ'] = 'HST'
    puts t.localtime #=> Tue Nov 30 19:25:55 HST 2004
    ENV['TZ'] = 'Japan'
    u = Time.at( t )
    puts t.localtime #=> Tue Nov 30 19:25:55 HST 2004
    puts u.localtime #=> Wed Dec 01 14:25:55 JST 2004

    It seems that we have to create a new Time object to change timezone.
    I also like to know more sofisticated ways to do this.

    Thanks,
    zunda

    __________________________________
    STOP HIV/AIDS.
    Yahoo! JAPAN Redribbon Campaign
    http://pr.mail.yahoo.co.jp/redribbon/
    zunda, Dec 1, 2004
    #2
    1. Advertising

  3. "zunda" <> schrieb im Newsbeitrag
    news:...
    > Hi,
    >
    > > "displaying"
    > > the date/time from a database and justing it for the a users local
    > > timezone in a web application.
    > >
    > > Is there a simple way of doing this?

    >
    > What I have been doing is to play with the environment variable TZ:
    >
    > t = Time.now
    > ENV['TZ'] = 'HST'
    > puts t.localtime #=> Tue Nov 30 19:25:55 HST 2004
    > ENV['TZ'] = 'Japan'
    > u = Time.at( t )
    > puts t.localtime #=> Tue Nov 30 19:25:55 HST 2004
    > puts u.localtime #=> Wed Dec 01 14:25:55 JST 2004
    >
    > It seems that we have to create a new Time object to change timezone.


    Yeah, it seems so.

    > I also like to know more sofisticated ways to do this.


    Sounds like a candidate for an RCR. Ideall we would like to have
    Time#zone= (i.e. assignment).

    It seems as if Time.utc and Time.local ignore the time zone argument (is
    this a bug?). Here are my tests:

    09:16:05 [robert.klemme]: ruby /c/temp/ruby/times.rb
    [6, 16, 10, 1, 12, 2004, 3, 336, false, "GMT+2:00"]
    [6, 16, 10, 1, 12, 2004, 3, 336, false, "GMT+1:00"]
    Wed Dec 01 10:16:06 GMT+2:00 2004
    Wed Dec 01 10:16:06 GMT+2:00 2004
    Wed Dec 01 10:16:06 UTC 2004
    GMT+2:00
    GMT+2:00
    UTC
    1101888966
    1101888966
    1101896166
    -1101888966
    0
    -7200
    09:16:06 [robert.klemme]: cat /c/temp/ruby/times.rb

    t1 = Time.now
    a = t1.to_a
    p a
    a[-1] = "GMT+1:00"
    p a
    t2 = Time.local(*a)
    t3 = Time.utc(*a)

    all = [t1,t2,t3]
    puts all
    puts all.map{|t| t.zone}
    i = all.map{|t| t.to_i}
    puts i
    i.inject(0) {|last,t| puts last-t;t}

    Kind regards

    robert
    Robert Klemme, Dec 1, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. djnex

    JChart Y axis adjustment.

    djnex, Nov 12, 2003, in forum: Java
    Replies:
    1
    Views:
    517
    Perica Milosevic
    Nov 12, 2003
  2. Bluetears76
    Replies:
    1
    Views:
    994
    Larry Barowski
    Jul 1, 2004
  3. asaf
    Replies:
    3
    Views:
    6,123
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=
    Sep 11, 2006
  4. axrock
    Replies:
    8
    Views:
    937
    Roedy Green
    Mar 25, 2009
  5. Ami
    Replies:
    5
    Views:
    264
Loading...

Share This Page