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

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 a quarter (01:15)?

I thought of converting to decimal, so the input of 01:15 by the user would have to be converted to 1.25 (one plus one 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.

    
asked by anonymous 17.07.2014 / 14:26

2 answers

5

Hard to say the best for all cases. If your event is guaranteed to last less than 24 hours you can use a column with type time . If it can take longer, use timestamp . PostgreSQL has interval that may not be worth it for the space it spends the most and we hardly need what it offers the most. link

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

    
17.07.2014 / 14:35
2

The type in question also depends on how this information will be used, if it is charging, the best would be to write as an integer working on the smallest unit (seconds for example). be easily assembled. If it is merely informative types like TIME (if any)

    
17.07.2014 / 15:14