Getting Time in specified timezone/offset

Discussion in 'Ruby' started by hemant, Feb 10, 2007.

  1. hemant

    hemant Guest

    I have got following method in my code:


    def to_time
    if time? then
    Time.mktime(year,month,day_of_month,hour,minute,second,usec)
    else
    Time.mktime(year,month,day_of_month)
    end
    end

    In my class, I also have timezone/utc offset as an attribute and above
    method basically returns a Time object based on those attributes.

    But as you can see, Time.mktime is going to convert that time to local
    zone and return that value. So, is there anyway to get a Time object
    by passing above said attributes and timezone/offset in timezone
    specified. That is, no automatic conversion to utc or local zone.



    --
    gnufied
    -----------
    There was only one Road; that it was like a great river: its springs
    were at every doorstep, and every path was its tributary.
     
    hemant, Feb 10, 2007
    #1
    1. Advertising

  2. hemant

    Mike Shock Guest

    Good time-of-the-day! ;-)

    Probably you need a DateTime object, which can be constructed (as 'ri
    DateTime' says) like this:
    date_time = DateTime.new(year, month, day, hour, min, sec, offset)
    where offset is given in fractions of day: e.g. 5/24.0 for an offset of
    +5 hours from UTC or GMT.

    Here's a sample:

    require 'date'
    t1 = DateTime.new(2007, 2, 10, 12, 38, 10, 5/24.0)
    printf "%s %s %s\n", t1, t1.offset.to_s, t1.zone #
    2007-02-10T12:38:10+0500 0.208 +0500

    Cheers -
    Mike Shock

    hemant wrote:
    > I have got following method in my code:
    >
    >
    > def to_time
    > if time? then
    > Time.mktime(year,month,day_of_month,hour,minute,second,usec)
    > else
    > Time.mktime(year,month,day_of_month)
    > end
    > end
    >
    > In my class, I also have timezone/utc offset as an attribute and above
    > method basically returns a Time object based on those attributes.
    >
    > But as you can see, Time.mktime is going to convert that time to local
    > zone and return that value. So, is there anyway to get a Time object
    > by passing above said attributes and timezone/offset in timezone
    > specified. That is, no automatic conversion to utc or local zone.
    >
    >
    >
     
    Mike Shock, Feb 10, 2007
    #2
    1. Advertising

  3. Mike Shock wrote:
    > Good time-of-the-day! ;-)
    >
    > Probably you need a DateTime object, which can be constructed (as 'ri
    > DateTime' says) like this:
    > date_time = DateTime.new(year, month, day, hour, min, sec, offset)
    > where offset is given in fractions of day: e.g. 5/24.0 for an offset of
    > +5 hours from UTC or GMT.


    Won't do it :) The problem is, that we intend to actually replace stdlib
    DateTime. Time obviously does store the offset internally, as Time.local
    vs. Time.utc prove. Since we don't want to replace Time (which is merely
    a wrapper around unix-epoch) we'd like to interface with it. And if we
    can preserve the timezone-offset that'd be definately a plus.

    regards

    --
    Posted via http://www.ruby-forum.com/.
     
    Stefan Rusterholz, Feb 10, 2007
    #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. Lance Riedel

    Translated Offset to Source Offset

    Lance Riedel, Oct 14, 2003, in forum: XML
    Replies:
    2
    Views:
    502
    Patrick TJ McPhee
    Oct 15, 2003
  2. Chad

    User Timezone offset.

    Chad, Jun 8, 2007, in forum: ASP .Net
    Replies:
    1
    Views:
    379
    PlatinumBay
    Jun 9, 2007
  3. Sanjay
    Replies:
    2
    Views:
    422
    Sanjay
    Jul 18, 2007
  4. David Joseph Bonnici
    Replies:
    1
    Views:
    212
    Gunnar Hjalmarsson
    May 15, 2005
  5. Roy Smith
    Replies:
    4
    Views:
    276
    Roy Smith
    Jan 27, 2013
Loading...

Share This Page