Django: What's the best field type to represent time elapsed?

笑着哭i 提交于 2021-02-11 08:16:12

问题


I'm need to save the time that some task took to the user to finish it.

For example, suppose you have to answer a couple of questions of an exam. If you started at 10:00 am and finish it at 11:00am I'd like to store the time it took you to finish it. In this case it'd be 1hr or 60 minutes, or 3600 seconds.

After that i'd like to display that in a pretty way. Suppose something took 527 seconds, i'd like to show: 8'47. So i'd need some template tag to do the work.

Of course i've seen TimeField, but it's using datetime.time, and I don't think it's the rigth fit for this situation.

I've also analized using just an IntegerField storing seconds (that level of detail is Ok). But maybe you guys know a better option.

Thank you!


回答1:


An integral field storing seconds is probably best for storing durations (it's easy to then plug that into a timedelta). Of course, if you think you might want to store fractions of a second in the future, you might want to consider making it a float field instead.

You could also subclass either IntegerField or FloatField and make a version that automatically translates the contents to and from a timedelta automatically... or use one that someone's created already.



来源:https://stackoverflow.com/questions/11478798/django-whats-the-best-field-type-to-represent-time-elapsed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!