What is the best type of column to record amount of time?

Asked

Viewed 164 times

1

We usually use Datetime to record a specific moment. But what if I want to record that an event lasted half an hour (00:30) or an hour and fifteen (01:15)?

I thought to convert to decimal, so the input of 1:15 by the user would have to be converted to 1.25 (one plus a quarter of an hour) or 00:30 to 0.5.

Is this the best way or is there something more appropriate?

I’m using Rubyonrails and Postgressql.

  • Man, I think in this case, the ideal would be you working with minutes. When you need them, convert into hours and Alz. Hugs.

2 answers

5


Hard to say a better one for all cases. If your event is guaranteed to last less than 24 hours you can use a column with the type time. If you can last longer, use timestamp. Postgresql has the interval which may not be worth it for the space it spends too much and we hardly need what it offers the most. http://www.postgresql.org/docs/9.3/static/datatype-datetime.html

You can also use int converting everything to second, but using a type of floating point, a decimal, I think worse, unless you want to display the duration always decimally.

  • The guy time is what best suits me, even for Rails already have a field time_field which does not let the user enter invalid hours.

  • @Andrey, in fact, is nothing more than an input of the type "time"

2

The type in question also depends on how this information will be used, if it is tarifaction the best would be to record as integer working in the smallest unit (seconds for example) because in theory it would facilitate calculation , the display in the format of hour:minute : second can be easily mounted. If it is merely informational types like TIME (if any)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.