24

In Rails, I'm a little confused on the guidance between when to use DateTime.now.utc and Time.current. There seem to be differing opinions inside the framework about which is best, particularly in different versions.

It looks like DateTime.now.utc produces a timestamp with a UTC offset of zero, while Time.current.utc produces a timestamp with a time zone of UTC. That seems like a subtle distinction but it's pretty important in many cases (e.g. DST calculations).

When should you use DateTime.now.utc, and when should you use Time.current.utc? Is there any reason to use DateTime.now.utc instead of Time.current.utc?

John Feminella
  • 303,634
  • 46
  • 339
  • 357
  • Can you give an example of where it makes a difference? Given that UTC doesn't have any daylight saving transitions, surely it only makes a difference when you start using other time zones... (I don't know Ruby, but I've done quite a bit of date/time stuff, so with more background I may be able to help.) – Jon Skeet Oct 05 '12 at 14:45
  • @JonSkeet: I don't know if it makes a difference. (That's why I'm asking which one I should be using!) `DateTime.now.utc` first invokes `DateTime.now`, and then reverses the UTC offset to get to "UTC" time. So notice that it doesn't translate between _time zones_, but rather between _offsets_. Could that be problematic in edge cases, e.g., the infamous 1927-1928 example in Shanghai? – John Feminella Oct 05 '12 at 14:47
  • @JonSkeet: Unless you're actually in UTC, then `DateTime.now` and `Time.current` both produce a local time in something other than UTC. In other words, they seem to differ in their approaches about how they convert to UTC -- one does it by applying an offset straightforwardly; the other does it by actually redoing the time zone calculations in the new time zone of UTC. – John Feminella Oct 05 '12 at 14:52
  • Both seem to be somewhat broken, to be honest - getting the current UTC time via the local time leads to ambiguity, unless `Time.current` remembers its original offset from UTC as well... – Jon Skeet Oct 05 '12 at 15:27

1 Answers1

58

I think you should use .current instead of .now.

The difference of .current and .now is .now use the server's timezone, while .current use what the Rails environment is set to. If it's not set, then .current will be same as .now.

Time.current

Returns Time.zone.now when Time.zone or config.time_zone are set, otherwise just returns Time.now.

DateTime.current

Returns Time.zone.now.to_datetime when Time.zone or config.time_zone are set, otherwise returns Time.now.to_datetime.

whitehat101
  • 2,428
  • 23
  • 18
xdazz
  • 158,678
  • 38
  • 247
  • 274
  • how about Date.current and Date.today are these same? – ajahongir Sep 17 '13 at 18:08
  • 1
    What is the difference between `Time.now.in_time_zone` and `Time.current` ? – Benjamin Crouzier Mar 24 '14 at 11:14
  • @pinouchon They are the same, both return instance of `ActiveSupport::TimeWithZone`. – xdazz Aug 05 '14 at 06:10
  • 1
    When is Time.zone ever _not_ set in Rails? I have commented out `config.time_zone` and I still get Time.zone equal to 'UTC' as it apparently sets that by default. So then, what is the use of using `Time.current` over `Time.zone.now`? – Magne Aug 17 '17 at 19:44
  • 1
    Just a pointer that it's DateTime.current not Datetime.current as in the above explanation. In case anyone is stuck there. +1 on using DateTime.current over other options however. – timpwbaker Oct 02 '17 at 12:27