4 ms·
When should you use DateTime and when should you use Time?
- colinramsay 11y agoThis is superbly well explained with a great example but it doesn't quite explain why I wouldn't just use DateTime for everything? Or did I miss something?
- trebor 11y agoTL;DR, use Time except when you need to deal with historical dates in their context. This was actually explained in the last paragraph of that post. Emphasis mine. > So when should use you use DateTime in Ruby and when should you use Time? Almost certainly you'll want to use Time since your app is probably dealing with current dates and times. However, if you need to deal with dates and times in a historical context you'll want to use DateTime[...]
- Zikes 11y agoThat's not an explanation, it's a recommendation. The author does not state _why_ Time should be preferred over DateTime.
- fleitz 11y agoThey do, which is because Time supports timezones, etc.
- Semiapies 11y agoThe gist was updated.
- pixeltrix 11y agoSorry, don't like to be absolutist - updated the gist to clarify that you should use Time because of it's greater support for timezones, DST, etc.
- peterwwillis 11y agoThe only reason to use Time is for a marginal performance improvement. And it saves you typing four extra characters. Other than that, Time really sucks. http://stevenyue.com/2013/03/23/date-time-datetime-in-ruby-and-rails/ http://stevenyue.com/2013/03/23/date-time-datetime-in-ruby-a...
- peterwwillis 11y agoDateTime might also handle leap seconds, time zones, and daylight savings better in general, including future dates. I don't know Time's capabilities, but DateTime will also handle dates past 2036 on any architecture, which is useful when processing expiration dates for SSL certificates, for example. Handling of past dates and times is also important when doing log processing, data archiving, etc. If you need date/time precision, use DateTime.
- pixeltrix 11y agoNope, Time handles all those things better since Ruby 1.8.7/1.9.3 when it gained support date/times into the far future. Until then it was liable to the Year 2038 problem: http://en.wikipedia.org/wiki/Year_2038_problem http://en.wikipedia.org/wiki/Year_2038_problem
- peterwwillis 11y agoLooking into it further, apparently neither Time nor DateTime account for leap seconds (though they support a 60th second), and only the Time class seems to support daylight savings, according to the docs. So that's pretty weird.
- thwarted 11y agoWe can see the actual date of Shakespeare's death by using... The use of "actual" seems dubious in this context. The whole point of this gist is that time interpretation is relative. I like how the author makes the distinction with "solar years" later on.
- anton_gogolev 11y agoTwo obligatory links on what a strange thing "time" is: [1]: http://infiniteundo.com/post/25326999628/falsehoods-programmers-believe-about-time http://infiniteundo.com/post/25326999628/falsehoods-programm... [2]: http://infiniteundo.com/post/25509354022/more-falsehoods-programmers-believe-about-time http://infiniteundo.com/post/25509354022/more-falsehoods-pro...