Rails. How to store time of day (for schedule)?

前端 未结 8 744
被撕碎了的回忆
被撕碎了的回忆 2020-12-13 02:25

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

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

I was think

相关标签:
8条回答
  • 2020-12-13 02:43

    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 =)

    0 讨论(0)
  • 2020-12-13 02:43

    From the SQLite 3 website,

    "SQLite does not have a storage class set aside for storing dates and/or times. Instead, the built-in Date And Time Functions of SQLite are capable of storing dates and times as TEXT, REAL, or INTEGER values:

    TEXT as ISO8601 strings ("YYYY-MM-DD HH:MM:SS.SSS"). REAL as Julian day numbers, the number of days since noon in Greenwich on November 24, 4714 B.C. according to the proleptic Gregorian calendar. INTEGER as Unix Time, the number of seconds since 1970-01-01 00:00:00 UTC. Applications can chose to store dates and times in any of these formats and freely convert between formats using the built-in date and time functions."

    You can then manipulate the values using the Date and Time functions outlined here.

    0 讨论(0)
  • 2020-12-13 02:46

    If you're using Postgresql you can use a time column type which is just the time of day and no date. You can then query

    Event.where("start_time > '10:00:00' and end_time < '12:00:00'")
    

    Maybe MySQL has something similar

    0 讨论(0)
  • 2020-12-13 02:48

    I would store the starting hour and the duration within the database, using two integer columns.

    By retrieving both values, you could convert the starting hour as in (assuming that you know the day already:

    # assuming date is the date of the day, datetime will hold the start time
    datetime = date.change({:hour => your_stored_hour_value , :min => 0 , :sec => 0 })
    
    # calculating the end time
    end_time = datetime + your_stored_duration.seconds
    

    Otherwise, hava a look at Chronic. The gem makes handling time a little bit easier. Note that the changemethod is part of rails, and not available in plain Ruby.

    The documentation on DateTime for plain Ruby can be found here.

    Also, whatever you do, don't start storing your dates/time in 12-hour format, you can use I18nin Rails to convert the time:

    I18n.l Time.now, :format => "%I.%m %p",  :locale => :"en"
    I18n.l Time.now + 12.hours, :format => "%I.%m %p",  :locale => :"en"
    

    You can also get from this notation, that you can store you duration in hours, if you want, you can then convert them rather easily by:

    your_stored_value.hours
    

    if stored as an integer, that is.

    0 讨论(0)
  • 2020-12-13 02:52

    Suggestion:

    Don’t worry about a specific datatype for that. A simple solution would be:

    1. In the database, add an integer type column for start_time and another for end_time. Each will store the number of minutes since midnight.
      Ex: 8:30am would be stored as 510 (8*60+30)

    2. In the form, create a select field (dropdown) that displays all available times in time format:
      Ex.: 10am, 10:30am and so on.
      But the actual field values that get saved in the database are their integer equivalents:
      Ex: 600, 630 and so on (following the example above)

    0 讨论(0)
  • 2020-12-13 02:54

    I assume you are using some kind of database for this. If you are using MySQL or Postgresql, you can use the datetime column type, which Ruby/Rails will automatically convert to/from a Time object when reading/writing to the database. I'm not sure if sqlite has something similar, but I imagine it probably does.

    0 讨论(0)
提交回复
热议问题