DateTime + x.days, x.minutes and the like?

This cant be so hard but feeling rather annoyed with myself for thinking
I
can do the following:

DateTime.now + 1.day

… and getting the expected result, which would not be (but is):

ruby-1.8.7-p330 :055 > DateTime.now + 1.day
=> #<DateTime 2247-11-04T13:24:19-05:00>

Or

ruby-1.8.7-p330 :056 > DateTime.now + 120.minutes
=> #<DateTime 2030-12-31T13:25:01-05:00>

(btw, the date on my computer is correct =~ => #<DateTime
2011-04-15T13:25:17-05:00>)

Is there any easy way to do this?

I also see people saying to use Time instead of DateTime, and that it is
possible to do:

Time.now + 10.days

and get the expected output but in my case I get:

Time.now + 10.days
NoMethodError: undefined method `advance’ for #<Date 2011-04-15>

ActiveSupport is loaded though…

On 15 April 2011 19:41, David K. [email protected] wrote:

This cant be so hard but feeling rather annoyed with myself for thinking I
can do the following:

DateTime.now + 1.day

Is there any easy way to do this?

I prefer to use .since and .ago:

DateTime.now.since(1.day)

On Fri, Apr 15, 2011 at 1:50 PM, Michael P. [email protected]
wrote:

DateTime.now.since(1.day)

Hmmm, I like the idea but getting an error - I am in rails console (Ruby
1.8.7)… but in Ruby 1.9.2 the method is present. Any idea how to
aproach
this problem in 1.8.7?

ruby-1.8.7-p330 :020 > DateTime.now.since(1.day)
NoMethodError: undefined method `since’ for #<DateTime
2011-04-15T13:52:34-05:00>

ruby-1.9.2-p136 :001 > DateTime.now.since(1.day)
=> Sat, 16 Apr 2011 13:55:22 -0500

On Fri, Apr 15, 2011 at 1:56 PM, David K.
[email protected]wrote:

ruby-1.8.7-p330 :020 > DateTime.now.since(1.day)
NoMethodError: undefined method `since’ for #<DateTime
2011-04-15T13:52:34-05:00>

ruby-1.9.2-p136 :001 > DateTime.now.since(1.day)
=> Sat, 16 Apr 2011 13:55:22 -0500

Which version of ActiveSupport are you running? I have ruby 1.8.7
installed
and ActiveSupport 2.3.11. I was able to run your first examples and get
the
correct output as well as run Micheal’s examples and get the correct
output.

DateTime.now
=> Sat, 16 Apr 2011 01:00:29 -0500
DateTime.now + 1.day
=> Sun, 17 Apr 2011 01:00:32 -0500
DateTime.now.since(1.day)
=> Sun, 17 Apr 2011 01:00:34 -0500

B.

On Sat, Apr 16, 2011 at 4:37 AM, David K.
[email protected]wrote:

On 15 April 2011 19:41, David K. [email protected] wrote:
DateTime.now.since(1.day)
ruby-1.9.2-p136 :001 > DateTime.now.since(1.day)
lock, which I assume is reliable)… on one side I am glad that perhaps
ruby-1.8.7-p330 :002 > DateTime.now + 1.day
DateTime.now.since(1.day)
=> Sun, 17 Apr 2011 01:00:34 -0500

Am still perplexed by this output (ruby 1.8.7-p330 on 64bit/Snow
Leopard):

ruby-1.8.7-p330 :001 > DateTime.now
=> #<DateTime 2011-04-18T10:35:18-05:00>
ruby-1.8.7-p330 :002 > DateTime.now + 1.day
=> #<DateTime 2247-11-07T10:35:21-05:00>

I was thinking that maybe it was due to upgrading to Snow Leopard but I
reinstalled ruby and my gemset and not success, could that be a
plausible
explanation and I am missing something? Anyone have any other idea?
Really
bothering me that I am getting one result and others on list getting
different.

On Mon, Apr 18, 2011 at 10:38 AM, David K.
[email protected]wrote:

Is there any easy way to do this?
ruby-1.8.7-p330 :020 > DateTime.now.since(1.day)
correct output.
guess I should probably drop my gemset and reinstall and see what
DateTime.now

I wonder if it is the patch level that is the issue. I’m not using p330.
This is my version:

ruby 1.8.7 (2009-06-12 patchlevel 174) [universal-darwin10.0]

To the group: Is anyone else running p330 and having this issue?

B.

On Sat, Apr 16, 2011 at 1:01 AM, Bryan C.
[email protected]wrote:

1.8.7)… but in Ruby 1.9.2 the method is present. Any idea how to aproach
Which version of ActiveSupport are you running? I have ruby 1.8.7 installed
and ActiveSupport 2.3.11. I was able to run your first examples and get the
correct output as well as run Micheal’s examples and get the correct
output.

Hey Brian, thanks… so I am on activesupport (= 3.0.6) (from my
gemfile
lock, which I assume is reliable)… on one side I am glad that perhaps
something is wrong with my environment because I have resorted to doing
things like: Time.now + 1.day.seconds to get certain outcomes (gave up
on
datetime).

So you are on Rails 2, right? I wonder if either is a difference bet R2
and
R3 but I think Michael was using Ruby 1.9.2 and I assumed Rails 3. I
guess I
should probably drop my gemset and reinstall and see what happens…

ruby-1.8.7-p330 :001 > DateTime.now
=> #<DateTime 2011-04-16T04:32:20-05:00>
ruby-1.8.7-p330 :002 > DateTime.now + 1.day
=> #<DateTime 2247-11-05T04:32:27-05:00>

On 18 April 2011 16:38, David K. [email protected] wrote:

Am still perplexed by this output (ruby 1.8.7-p330 on 64bit/Snow Leopard):

ruby-1.8.7-p330 :001 > DateTime.now
=> #<DateTime 2011-04-18T10:35:18-05:00>
ruby-1.8.7-p330 :002 > DateTime.now + 1.day
=> #<DateTime 2247-11-07T10:35:21-05:00>

I wonder if it’s something along the lines of the value of
DateTime.now expressed as an integer added to 1.day expressed as an
integer (86400 seconds maybe) converted back to a DateTime - giving a
date wayyy in the future… :-/

What happens when you do:
DateTime.now + 1.second
or
DateTime.now + 1.minute
etc?

?:-/
'fraid I can’t help more than this wild speculation though, because I
have no access to a Mac to test it on.

ruby 1.8.7 (2009-06-12 patchlevel 174) [universal-darwin10.0]

To the group: Is anyone else running p330 and having this issue?

Not p330, but p334… doesn’t show the problem.

ruby-1.8.7-p334 :036 > DateTime.now
=> Mon, 18 Apr 2011 09:32:07 -0700
ruby-1.8.7-p334 :037 > DateTime.now + 1.day
=> Tue, 19 Apr 2011 09:32:18 -0700

ruby:
interpreter: “ruby”
version: “1.8.7”
date: “2011-02-18”
platform: “x86_64-darwin10.6.0”
patchlevel: “2011-02-18 patchlevel 334”
full_version: “ruby 1.8.7 (2011-02-18 patchlevel 334)
[x86_64-darwin10.6.0]”

On Mon, Apr 18, 2011 at 1:11 PM, Michael P. [email protected]
wrote:

DateTime.now expressed as an integer added to 1.day expressed as an
integer (86400 seconds maybe) converted back to a DateTime - giving a
date wayyy in the future… :-/

What happens when you do:
DateTime.now + 1.second
or
DateTime.now + 1.minute
etc?

Yeah, I am pending to play around with this a bit later, as I am also
recalling that ruby 1.9.2 is fine on my computer, so at least try
another
wipe of the 1.8.7. As well see if anything is weird on the config, since
as
to your question, it looks like you are on to something:

ruby-1.8.7-p330 :005 > dt = DateTime.now
=> #<DateTime 2011-04-18T13:23:45-05:00>
ruby-1.8.7-p330 :006 > dt + 1.second
=> #<DateTime 2011-04-19T13:23:45-05:00>
ruby-1.8.7-p330 :007 > dt + 1.minute
=> #<DateTime 2011-06-17T13:23:45-05:00>

Because also if I do the following we are right on:

ruby-1.8.7-p330 :008 > dt + 1
=> #<DateTime 2011-04-19T13:23:45-05:00>

date wayyy in the future… :-/
=> #<DateTime 2011-04-18T13:23:45-05:00>
Hi all, so a little more data if it sparks any further input, I appreciate
everyone’s input so far:
DateTime from hell · GitHub

So, if anyone has made it this far, I guess my next question would be as to how
I can confirm whether active support (in particular the file mentioned above is
getting loaded). Actually if Phillip, or someone else (running Ruby 1.8.7-p334 and
Rails 3.0.7 ideally on Snow Leopard mac or close) to this set up can send me their
output of DateTime.now.methods.sort, it would at least confirm things.

ruby 1.8.7, rails 2.3.8…
http://pastefree.pjkh.com/pastes/155

ruby 1.9.2, rails 3.0.7
http://pastefree.pjkh.com/pastes/156

ruby 1.8.7, rails 3.0.7
http://pastefree.pjkh.com/pastes/157

-philip

On Tue, Apr 19, 2011 at 1:08 PM, Philip H. [email protected]
wrote:

integer (86400 seconds maybe) converted back to a DateTime - giving a
recalling that ruby 1.9.2 is fine on my computer, so at least try another
Because also if I do the following we are right on:
2247). It has to be something to do with my environment as Phillip Halstrom
ruby-1.8.7-p334 :003 > DateTime.now.since(1.day)
1.8.7/Rails 3.0.7 and 1.9.2/Rails 3.0.3:

ruby 1.8.7, rails 2.3.8…
http://pastefree.pjkh.com/pastes/155

ruby 1.9.2, rails 3.0.7
http://pastefree.pjkh.com/pastes/156

ruby 1.8.7, rails 3.0.7
http://pastefree.pjkh.com/pastes/157

Thanks Phillip, well this one confirms my environment has something
wrong
and is pretty clearly not something wrong with either Rails or Ruby.
Well
for now I think I am just going to work the project under 1.9.2 and then
move it back to 1.8.7 for the client/production, not ideal but nor is
creating vm or rebuilding my laptop at the moment.

http://pastefree.pjkh.com/pastes/157

Thanks Phillip, well this one confirms my environment has something wrong and is
pretty clearly not something wrong with either Rails or Ruby. Well for now I think
I am just going to work the project under 1.9.2 and then move it back to 1.8.7 for
the client/production, not ideal but nor is creating vm or rebuilding my laptop at
the moment.

Or install RVM and ditch the system ruby entirely?? I’ll admit I don’t
remember if you’ve already gone that direction or not…

On Tue, Apr 19, 2011 at 1:25 PM, Philip H. [email protected]
wrote:

move it back to 1.8.7 for the client/production, not ideal but nor is
creating vm or rebuilding my laptop at the moment.

Or install RVM and ditch the system ruby entirely?? I’ll admit I don’t
remember if you’ve already gone that direction or not…

Yeah, thats what’s scary is that I have been using rvm the whole way
through. Actually I just discovered that I have a dependency on an
engine
built in 1.8.7 so I guess I am going to have to do something to my
environ…

On Mon, Apr 18, 2011 at 1:26 PM, David K.
[email protected]wrote:

ruby-1.8.7-p330 :002 > DateTime.now + 1.day
DateTime.now + 1.minute
ruby-1.8.7-p330 :006 > dt + 1.second
=> #<DateTime 2011-04-19T13:23:45-05:00>
ruby-1.8.7-p330 :007 > dt + 1.minute
=> #<DateTime 2011-06-17T13:23:45-05:00>

Because also if I do the following we are right on:

ruby-1.8.7-p330 :008 > dt + 1
=> #<DateTime 2011-04-19T13:23:45-05:00>

Hi all, so a little more data if it sparks any further input, I
appreciate
everyone’s input so far:

I installed fresh ruby 1.8.7-p334 and set up my app and end up with the
same
problem as above (DateTime.now + 1.day brings a result with the year
2247).
It has to be something to do with my environment as Phillip Halstrom
above
has the same version of ruby and has a good result… I hate to rebuild
my
mac for such a lame issue but I am starting to think about it or to move
this project to a vm.

What is weird is this: I am poking around
active_support/core_ext/date_time/calculations.rb and see the methods
such
as #since, #advance, #past?, etc. available, yet I am getting an error
doing
what Michael P. suggested above, as well as ‘no method found’ on
the
other methods:

ruby-1.8.7-p334 :003 > DateTime.now.since(1.day)
NoMethodError: undefined method `since’ for #<DateTime
2011-04-19T12:09:42-05:00

Then I decided to check the methods for DateTime.now, in my bad ruby
1.8.7-p334 / rails 3.0.7, vs my working ruby 1.9.2-p136 / rails 3.0.3.
As
you see on the gist below the list of methods are very different. I am
wondering if there is a disconnect in how rails is loading in my 1.8.7

because at initial glance, it looks to me like the DateTime class I am
looking at in active_support is not being loaded as the methods it
specifies
are not there. Following are outputs of the methods for DateTime::now
for my
1.8.7/Rails 3.0.7 and 1.9.2/Rails 3.0.3:

So, if anyone has made it this far, I guess my next question would be as
to
how I can confirm whether active support (in particular the file
mentioned
above is getting loaded). Actually if Phillip, or someone else (running
Ruby
1.8.7-p334 and Rails 3.0.7 ideally on Snow Leopard mac or close) to this
set
up can send me their output of DateTime.now.methods.sort, it would at
least
confirm things.

Thanks!

David

On Tue, Apr 19, 2011 at 1:36 PM, David K.
[email protected]wrote:

Yeah, thats what’s scary is that I have been using rvm the whole way
through. Actually I just discovered that I have a dependency on an engine
built in 1.8.7 so I guess I am going to have to do something to my
environ…

I GOT IT!!! And I did it by trying to change to ruby 1.9.2 and
remembering
that I have an engine dependency which uses 1.8.7… and when I
commented
the code below I get a good DateTime object. Looks like (have to prove
it
after lunch), that what is below overrides the application config and as
such something important does not happen. Thanks everyone for the help
and
sorry it is such a lame answer…

initializer “my_engine.load_config” do |app|

config_file = “#{Rails.root}/config/config.yml”

if File.exists?(config_file)

::APP_CONFIG =

YAML.load_file(config_file)[Rails.env].symbolize_keys!

ActionMailer::Base.smtp_settings = APP_CONFIG[:smtp]

ActionMailer::Base.default(APP_CONFIG[:mailer_defaults])

else

puts "No config.yml file found, run 'rails g

my_engine:install’"

end

end

On Tue, Apr 19, 2011 at 2:13 PM, David K.
[email protected]wrote:

mentioned above is getting loaded). Actually if Phillip, or someone else

::APP_CONFIG =

YAML.load_file(config_file)[Rails.env].symbolize_keys!

ActionMailer::Base.smtp_settings = APP_CONFIG[:smtp]

ActionMailer::Base.default(APP_CONFIG[:mailer_defaults])

else

puts “No config.yml file found, run ‘rails g my_engine:install’”

end

end

More info for posteriority if this ends up helping anyone: the actual
conflict was the gem ‘home_run’ which was not referenced in my project
but
in the engine it depends on, unbenownst to me.

Once the dependency was removed from the engine, DateTime began to
function
correctly. I suspect that this problem might have to do with my local
environment as home_run creates native extensions. I will try to post
back
here with final results if and when I rebuild or have a new mac, but
until
then we just removed the gem.