Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Getting Time in specified timezone/offset

Reply
Thread Tools

Getting Time in specified timezone/offset

 
 
hemant
Guest
Posts: n/a
 
      02-10-2007
I have got following method in my code:


def to_time
if time? then
Time.mktime(year,month,day_of_month,hour,minute,se cond,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.

 
Reply With Quote
 
 
 
 
Mike Shock
Guest
Posts: n/a
 
      02-10-2007
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,se cond,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.
>
>
>



 
Reply With Quote
 
 
 
 
Stefan Rusterholz
Guest
Posts: n/a
 
      02-10-2007
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/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Getting error error: 26 - Error Locating Server/Instance Specified juvi ASP .Net 3 01-22-2009 12:05 PM
Getting method parameters in specified order Chintana Wilamuna Ruby 8 12-14-2007 11:46 PM
Is time.time() < time.time() always true? flamesrock Python 8 11-24-2006 06:51 AM
getting character(s) of a string at specified positions. Matt Roth Ruby 3 11-28-2005 02:17 PM
Understand why I am getting a "Specified cast is not valid" error =?Utf-8?B?Q2hhcmxlcw==?= ASP .Net 8 05-04-2004 04:06 PM



Advertisments