Time.today going away

Eric Hodel | Mon, 08 Oct 2007 22:22:32 GMT

Posted in ,

So you’ve been using Time::today for some time, and thought it was part of time.rb or ActiveSupport? Well, you’d be wrong. I was!

It turns out that rubygems/specification.rb defines Time::today, and that’s wrong. RubyGems has no business adding methods to the core when it doesn’t need to, so I’ve marked it for removal.

If you want to use Time::today in the future you’ll need to define it in your code somewhere, here is its definition:

require 'time'

def Time.today
  t = Time.now
  t - (t.to_i % 86400)
end unless Time.respond_to? :today

Go ahead and throw it in wherever you think is appropriate. I suggest you do this now.

UPDATED: Now with a better Time::today.

6 comments

Comments RSS FEED

Wow, that would of been a nasty one to figure out. Thanks for the heads up.

Ben Mabey said 23 minutes later

While i fully agree with the rationale of throwing this out of gems, i don’t think this is a good idea at this point at least not for a small dot-x release. I wouldn’t wanna be the one trying to figure this one out and there may be some not reading this blog (come to think of it, screw ‘em!). But seriously, are deprecation warnings an option?

Sebastian said about 1 hour later

@Sebastian: I’ve only marked Time::today for removal.

Removal is accomplished in three phases. Phase one is trumpeting it far and wide, where we’re at now. Phase two is warning whenever you call the to-be-removed thing. Phase three is deleting. There will be ample time between the three phases for people to adjust. (Even though they usually don’t.)

Eric Hodel said about 1 hour later

How about without parsing a string to get a Time object… (no need to require “time.rb” for Time.parse)

def Time.today
  now = now()
  mktime(now.year, now.mon, now.day)
end

Time.today # => Mon Oct 08 00:00:00 -0400 2007
dlh said about 1 hour later

Um, your updated version is just as awful.

dlh said about 1 hour later

@dlh: No, its better, nearly twice as fast as yours in 1,000,000 iterations.

parse   37.110000   0.550000  37.660000 ( 55.822070)
mktime   2.680000   0.030000   2.710000 (  3.160472)
%        1.430000   0.020000   1.450000 (  1.779844)
Eric Hodel said about 2 hours later

Comments are disabled