导轨.如何存储一天中的时间(用于计划)? [英] Rails. How to store time of day (for schedule)?

查看:36
本文介绍了导轨.如何存储一天中的时间(用于计划)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个跟踪学校课程的应用.

I'm writing an app that keeps track of school classes.

我需要存储时间表.例如:周一至周五 8:am-11am.

I need to store the schedule. For example: Monday-Friday from 8:am-11am.

我正在考虑使用一个简单的字符串列,但稍后我将需要进行时间计算.

I was thinking about using a simple string column but I'm going to need to make time calculations later.

比如我需要存储一个8am的表示,比如start_at:8am end_at:11am

For example, I need to store a representation of 8am, such as start_at:8am end_at:11am

那么我应该如何存储时间?我应该使用什么数据类型?我应该存储开始时间和秒数或分钟数,然后从那里计算?或者有更简单的方法吗?

So how should I store the time? What datatype should I use? Should I store start time and number of seconds or minutes and then calculate from there? or is there an easier way?

我使用 MySQL 进行生产,使用 SQLite 进行开发.

I use MySQL for production and SQLite for development.

推荐答案

我最近做了一个应用程序来解决这个问题.我决定在一个简单的营业时间模型中从午夜开始在几秒钟内存储 open_at 和 closed_at.ActiveSupport 包括这个方便的助手,用于查找自午夜以来的时间(以秒为单位):

I made an app recently that had to tackle this problem. I decided to store open_at and closed_at in seconds from midnight in a simple business hour model. ActiveSupport includes this handy helper for finding out the time in seconds since midnight:

Time.now.seconds_since_midnight

这样我就可以做一个简单的查询来确定场地是否开放:

This way I can do a simple query to find out if a venue is open:

BusinessHour.where("open_at > ? and close_at < ?", Time.now.seconds_since_midnight, Time.now.seconds_since_midnight)

如有任何改进此功能的提示,我们将不胜感激 =)

Any tips for making this better would be appreciated =)

这篇关于导轨.如何存储一天中的时间(用于计划)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆