Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DateTime.now or Time.now?

Which of the two should I be using in my Rails application:

DateTime.now or Time.now 

Is there any harm in using both in the application?

Can there ever be any differences between the two in case of the above (now) example? (On my current system they are both showing the same time)

like image 873
Zabba Avatar asked Apr 08 '11 20:04

Zabba


2 Answers

Use (Date)Time.current instead of (Date)Time.now

Rails extends the Time and DateTime objects, and includes the current property for retrieving the time the Rails environment is set to (default = UTC), as opposed to the server time (Could be anything).

This is critical- You should always be working in UTC time except when converting between timezones for user input or display- but many production systems are not UTC by default. (e.g. Heroku is set to PST (GMT -8))

See article here

like image 71
Yarin Avatar answered Sep 24 '22 08:09

Yarin


In reference to Time.now (not DateTime.now):

The object created will be created using the resolution available on your system clock, and so may include fractional seconds.

a = Time.new      #=> Wed Apr 09 08:56:03 CDT 2003 b = Time.new      #=> Wed Apr 09 08:56:03 CDT 2003 a == b            #=> false "%.6f" % a.to_f   #=> "1049896563.230740" "%.6f" % b.to_f   #=> "1049896563.231466" 
like image 43
slandau Avatar answered Sep 24 '22 08:09

slandau