eugeniodepalo
eugeniodepalo

Reputation: 801

What is the correct type I should use to store a date up to the minute?

I want to store the date of an event in my database, but I want to do so without storing informations about seconds or anything smaller than seconds. Using Rails, in my migration I have the option to create a date column or a datetime column, the first one of which is too less accurate, and the second one is too much (up to the second and less). Which type should I choose to store such a date? Currently I'm using datetime and setting the seconds to a fixed value (e.g. 0) manually each time some date is set in the model.

Something like this:

self.date ||= Time.now.change(:sec => 0)

Am I totally out of track? Should I just use an integer field for each component of the date instead? (year, month, day, etc...) Or is datetime the correct type but I'm not understanding the purpose of it? (I think it's meant for timestamps and such things where seconds matter)

Upvotes: 1

Views: 196

Answers (1)

Denis de Bernardy
Denis de Bernardy

Reputation: 78463

datetime is the correct type. And be sure to store it without time zone at time zone UTC:

http://derickrethans.nl/storing-date-time-in-database.html

At your option, use an SQL trigger to round your date to the minute on insert/update. It'll simplify your ruby code.

Upvotes: 1

Related Questions