Incorrect date with ruby ​​Date.today and DateTime.now

I installed ruby-1.8.6-p383 with RVM. System Ruby - 1.9.1_p378-1

I get the wrong date from Date.today and DateTime.now when using ruby ​​1.8 .. While Time.now is correct:

irb(main):002:0> DateTime.now.to_s => "2126--1-10618T11:23:43+00:00" irb(main):004:0> Date.today.to_s => "2126--1-10618" irb(main):005:0> Time.now => Thu Jan 28 11:55:27 +0000 2010 

Everything is fine if I switch to ruby ​​1.9:

 irb(main):003:0> DateTime.now.to_s => "2010-01-28T11:58:51+00:00" irb(main):004:0> Date.today.to_s => "2010-01-28" irb(main):005:0> Time.now => 2010-01-28 11:59:05 +0000 

Any advice on how to get a DateTime to work properly in ruby ​​1.8 would be greatly appreciated!

+7
date ruby datetime rvm
source share
3 answers

I came across a similar problem and got 3 different solutions which I tried 2 of them and both worked. Read more about this here:

http://www.ruby-forum.com/topic/210647

Yoke

+3
source share

The returned string seems very broken: i) year 2126, ii) there are two hyphens between year and month, and ii) day 10618.

What platform are you working on? What time zone are you using (example means you are in GMT)?

I'm on WinXP and Ruby 1.8.6-p111 gives the same result in all three cases.

Edit: I did some digging in the Ruby source code ( Date and DateTime defined in lib/date.rb ).

It turns out that Date.today calls Time.now.__send__(:to_date)... , so maybe you should try calling Time.now.__send__(:to_date).to_s in irb and compare the result with Time.now.to_s to narrow down the problem.

Date.today also calls .new_start(...) on the created instance of Date , so maybe this will ruin the date in your case.

+1
source share

I'm not sure if you are running locally or on a server, but I think Ruby can accept a server date, which may be in a different time zone than you, and therefore looks different.

-one
source share

All Articles