Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I Convert DateTime.now to UTC in Ruby?

Tags:

ruby

If I have d = DateTime.now, how do I convert 'd' into UTC (with the appropriate date)?

like image 822
Ash Avatar asked Apr 16 '09 11:04

Ash


4 Answers

d = DateTime.now.utc

Oops!

That seems to work in Rails, but not vanilla Ruby (and of course that is what the question is asking)

d = Time.now.utc

Does work however.

Is there any reason you need to use DateTime and not Time? Time should include everything you need:

irb(main):016:0> Time.now
=> Thu Apr 16 12:40:44 +0100 2009
like image 45
DanSingerman Avatar answered Oct 16 '22 13:10

DanSingerman


DateTime.now.new_offset(0)

will work in standard Ruby (i.e. without ActiveSupport).

like image 112
Greg Campbell Avatar answered Oct 16 '22 12:10

Greg Campbell


Unfortunately, the DateTime class doesn't have the convenience methods available in the Time class to do this. You can convert any DateTime object into UTC like this:

d = DateTime.now
d.new_offset(Rational(0, 24))

You can switch back from UTC to localtime using:

d.new_offset(DateTime.now.offset)

where d is a DateTime object in UTC time. If you'd like these as convenience methods, then you can create them like this:

class DateTime
  def localtime
    new_offset(DateTime.now.offset)
  end

  def utc
    new_offset(Rational(0, 24))
  end
end

You can see this in action in the following irb session:

d = DateTime.now.new_offset(Rational(-4, 24))
 => #<DateTime: 106105391484260677/43200000000,-1/6,2299161> 
1.8.7 :185 > d.to_s
 => "2012-08-03T15:42:48-04:00" 
1.8.7 :186 > d.localtime.to_s
 => "2012-08-03T12:42:48-07:00" 
1.8.7 :187 > d.utc.to_s
 => "2012-08-03T19:42:48+00:00" 

As you can see above, the initial DateTime object has a -04:00 offset (Eastern Time). I'm in Pacific Time with a -07:00 offset. Calling localtime as described previously properly converts the DateTime object into local time. Calling utc on the object properly converts it to a UTC offset.

like image 10
Joel Watson Avatar answered Oct 16 '22 11:10

Joel Watson


Try this, works in Ruby:

DateTime.now.to_time.utc
like image 6
EEPAN Avatar answered Oct 16 '22 13:10

EEPAN