Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a Time object with specific time in Ruby 1.8.7?

Tags:

ruby

In Ruby 1.9.2 we can do:

Time.new(2008,6,21, 13,30,0, "+09:00")

How to do the same in Ruby 1.8.7?

like image 360
Zabba Avatar asked Feb 21 '11 20:02

Zabba


2 Answers

Depending on your needs, you can use Time.utc, Time.gm (a synonym for Time.utc), or Time.local. All three take arguments to set specific time and date.

http://www.ruby-doc.org/core-1.8.7/classes/Time.html

like image 178
Mike Yockey Avatar answered Oct 12 '22 15:10

Mike Yockey


If I understand the question correctly, you are trying to set the time zone of a time by passing it the +09:00 offset. In Ruby 1.8.7, the only time zones you can use are your local (system) time, or UTC/GMT.

What you can do is create a new time, equivalent to the time you desire, but in UTC instead of UTC+9:

ruby-1.8.7-p302 :052 > Time.parse("2008-06-21 13:30:00 UTC") - 9*3600
 => Sat Jun 21 04:30:00 UTC 2008

Which is the same time as:

ruby-1.9.2-p0 :003 > Time.new(2008,6,21, 13,30,0, "+09:00").utc
 => 2008-06-21 04:30:00 UTC 
like image 41
Jamie Forrest Avatar answered Oct 12 '22 13:10

Jamie Forrest