English 中文(简体)
Wrong date with ruby Date.today and DateTime.now
原标题:

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

I m getting the wrong date from Date.today and DateTime.now when using ruby 1.8.. Whereas 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

All is well 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 DateTime to work properly in ruby 1.8 would be most appreciated!

最佳回答

I stumbled upon similar problem and got 3 different solutions, which i tried 2 of and both worked. Read more about it from here:

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

Jarmo

问题回答

The returned string seems to be very broken: i) the year is 2126, ii) there s two hyphens between the year and the month, and ii) the day is 10618.

What platform are you on? What timezone are you on (the example implies you re 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 are defined in lib/date.rb).

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

Date.today also calls .new_start(...) on the created Date instance so maybe that s messing up the date in your case.

Im not sure if you are running things locally, or on the server, but I think ruby might be taking the server date, which may be in a different time zone than you are, and thus, appear differently.





相关问题
Mysql compaire two dates from datetime?

I was try to measure what is faster and what should be the best way to compare two dates, from datetime record in MySql db. There are several approaches how to grab a date from date time, but I was ...

iPhone Date Picker rolls over to 2010 on December 27th?

So I implemented a UIDatepicker in one of my applications for scheduling and autodialing teleconferences... everything is pretty much ready to go except, while testing I noticed that when the date ...

Convert date Python

I have MMDDYY dates, i.e. today is 111609 How do I convert this to 11/16/2009, in Python?

specifying date format when using $form->inputs() in CakePHP

I am wondering if there is a way to specify the date format in the forms created using CakePHP s $form->inputs(); Please note that this is not the individual $form->input() but instead $form->inputs() ...

NSDateFormat, super simple! Where am I screwing up?

Ok, this is really simple, maybe I m a getting a bit burnt out, but seems like it should work, Query XML feed, put out date string, format, display in a cell. The issue is I m a getting a NULL ...

sqlite writing a date into an email

I am exporting a date value from sqlite and placing it into an email. The date appears like this 279498721.322872 I am using Objective C in an Iphone App. Does anyone know how to make this export ...

热门标签