Skip to content

Comment on Signed and Permanent Cookies in Rails 3

Comments

Looks like a cool feature, but I haven't yet gotten into Ruby or Rails. I do have one question, though. There is an interesting looking language feature I see: 20.years.from_now.utc. I would presume that to mean "20 years from this time in zulu". So presuming that 20.years.from_now.cdt means central daylight time, is that a different time?

To put the question a little shorter, isn't the concept of "20 years from now" independent of time zone, or am I missing something?

    irb(main):033:0> 20.years
    => 20 years               # ??????
    irb(main):030:0> 20.years.to_i
    => 631152000              # number of seconds in 20 years
    irb(main):026:0> 20.years.from_now
    => Sun Feb 10 10:14:11 +1100 2030 # AEDT
    irb(main):027:0> 20.years.from_now.utc
    => Sat Feb 09 23:14:14 UTC 2030   # UTC/GMT
Ruby Time instances have a timezone, calling Time#utc returns the instance time converted from localtime -- or whatever timezone it is -- to UTC or Greenwich Mean Time. You are correct in that it shouldn't matter which one you use in this situation though.

Your first one, commented with ??????, is an instance of a Duration object. It implements the #ago and #from_now methods, so that Integer objects aren't cluttered with methods that mean nothing to it.

http://api.rubyonrails.org/classes/ActiveSupport/Duration.ht...

It's independent of time zone. 20 years from now in any time zone will be the same "time" (in an absolute sense), but it will look different to different time zones. For example, it might look like 6:00pm for me and 5:00pm for you, but it is still the same time if we both convert our local times to utc. A Time object in ruby stores time zone, so 20.years.from_now.utc looks like this:

Sat Feb 09 23:07:46 UTC 2030

AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.