How to customize scheduled events in a calendar - ruby-on-rails

I am looking schema for calendar events which includes weekly, monthly and daily events particularly for random days in a week.
In a rails application, I used simple_calendar gem to generate monthly, weekly calendar. Is it possible to configure event creation for say Mon, Wed, Fri between 10-11 AM, if so how should I do that using the simple_calendar gem?
Currently my database schema look like the following,
Schema for calender event
name string
description string
start_date datetime
end_date datetime
duration string
is_scheduled boolean
scheduled_type string (monthly, daily, weekly)

From the README:
Simple Calendar is designed to do one thing really really well: render a calendar.
This is not the right tool to use for creating/tracking/scheduling events. Maybe take a look at something like Montrose or IceCube

Related

How to handle a DateTIme style like Saturdays at 01:05 (JST)

I'm scraping a site to get the date and time a episode comes out on. So what i'm getting is Saturdays at 01:05 (JST) which is the time in japan. I have a field in my episodes table called broadcast and it has a DateTime format.
What is the best way to handle this and to change the time zone to (pst)?
Assuming you're literally scraping the string Saturdays at 01:05 (JST) off of the website, there are a few things to consider.
First, a single DateTime field is not really going to be able to represent a recurring time, like you have. Gems like IceCube were created to handle the complexities of recurring, scheduled dates. But you can get by with one DateTime field, under the assumption that it stores one instance of the broadcast and all other broadcasts are at the same time and day every week.
Now, you need to parse the string you're scraping into that date. The easiest way to do this is with the Chronic gem, which takes all sorts of English-worded time representations and turns it into a date. With your date, you need to unpluralize "Saturdays" so that Chronic knows to just find the next Saturday:
require 'chronic'
string = "Saturdays at 1:30 (JST)"
broadcast = Chronic.parse(string.sub('days', 'day'))
The broadcast variable now holds a DateTime value which you can store directly into the database. Don't worry about the time zone -- Chronic returned a Time value with zone information, so Rails will store it into your database in UTC, and convert it back out to your server timezone whenever you load it.
If you do want to convert the time to a specific zone ever, you can do so easily:
pst_time = broadcast.in_time_zone('Pacific Time (US & Canada)')

How to store timezone info in rails

In my rails application,users can create questions and publish that, anybody from any country can response for that.
We are designing database structure for that. so planning to get user timezone using some js and while answering converting that time and to store in a separate column(tz_created_at).
so in created at the date will be stored in utc format, and in another column say tz_created_at the datetime will be stored as user's timezone converted time. (ie) in created_at column i have
irb(main):013:0> DateTime.now.utc => Sun, 30 Mar 2014 18:54:46 +0000
And in tz_created_at column i have
irb(main):015:0> DateTime.now
+ 6.hours(from user's timezone) => Mon, 31 Mar 2014 06:58:31 +0600
we are using sunspot solr to show some statistics in response over the time period.
so while querying for responses for a particular question i will search in tz_created_at column. is this approach is correct . please correct me if i am wrong
Instead of doing that, you probably want to store the datetime as UTC, then store the user's timezone in another column. By doing that you can always change the timezone if you need to without affecting the true time (e.g. the user moves country). When you display a time to the user you can easily convert the time to their zone to display it.
If you're a subscriber of railscasts there's a video on it there, but if you're not a subscriber there's an older one that's free to watch that will give you a general idea of best practices.

Accounting for daylight savings in rails webapp and iCal

Right, this is a bit confusing for me, so I'm going to try and explain from the top!
I have a rails web app. It's an internal company app and will only be used in the UK.
One of the things the app does is manage meetings.
Meetings have a date & time when they start. There's a date/time picker on the form which allows the user to pick the date & time the meeting is for. I save this date AS IS into the database. All meetings last 2 hours, so the end time is simply start + 2 hours.
Example:
2013-06-23 6:45PM in the form is stored in the db as 2013-06-23 18:45:00
2013-12-23 6.45pm in the form is stored in the db as 2013-12-23 18:45:00
Note that the first date is during Daylight Savings (BST) and the second is during GMT. I don't actually care whether it is GMT or BST: the meeting happens at that time, absolutely.
Inside the rails webapp, I simply print out the exact date & time from the DB - formatted nicely, of course!
Now, at some point I send an email to the organiser of the meeting, and the person they're meeting with. This email tells them the the date & time of the meeting etc, and also includes an iCal (.ics) file for them to put into their (Outlook usually, but also Apple or gmail) calendar.
The issue I am having is that (using the above examples) Outlook shows the meetings like this:
Meeting #1: Start: 23/06/2013 7:45pm, End: 23/06/2013 9:45pm
Meeting #2: Start: 23/12/2013 6:45pm, End: 23/12/2013 8:45pm
Note that it has adjusted the first one because of the BST/GMT thing.
The text of the .ics file contains this code:
Meeting #1:
BEGIN:VCALENDAR
...
DTEND:20130623T204500Z
DTSTART:20130623T184500Z
...
END:VCALENDAR
Meeting #2:
BEGIN:VCALENDAR
...
DTEND:20131223T204500Z
DTSTART:20131223T184500Z
...
END:VCALENDAR
So I am encoding the dates/times using the Z timezone (UTC). I understand this is why Outlook mis converting the UTC time into the BST time for #1 and leaving #2 alone (because GMT == UTC)
My question is: how do I stop this happening? I want the time the meeting is scheduled for to be the absolute, actual time, regardless of GMT/BST: 6:45pm
Should I be storing the date-times as UTC in the DB? How would this be done (I assume it would apply to all dates, not just meeting start dates). And how to re-convert them back into the actual datetime when I display them in the webapp?
Extra:
I have an entry in my initializers/time_formats.rb like this:
:ical => "%Y%m%dT%H%M00Z"
So dates come out like "20130623T184500Z". I use this when building the ics. And this I think is the issue - if the date/time is during BST I don't want to be using Z, but something else?
Your problem is your date/time format. You have:
DTSTART:20130623T184500Z
in your .ics file and this corresponds to 19:45 BST (as British summer time is UTC+1).
There are a few things you should do. First, you can simply remove the 'Z' from the end of your dates. This means that the times inherit the timezone of the calendar, or the underlying application.
This will work assuming that the machines which are running Outlook are all in the Europe/London timezone. If not, or if you want to be a bit safer, you should also specify the following after your BEGIN: VCALENDAR line:
X-WR-TIMEZONE:Europe/London
This specifies the default timezone for all dates which are not specified explicitly.
Finally, if this does not work for any reason then you need to define your datetimes explicitly. First you need to add a timezone definition for Europe/London to the calendar. The info you need is available at http://www.tzurl.org/zoneinfo-outlook/Europe/London.ics. Then you need to ensure that all datetimes are of the format:
DTSTART;TZID=Europe/London:20130623T184500
This last approach is the best, as it means that if your requirements expand to other timezones you will be able to handle them relatively easily.
Sorry to answer this myself, but in case anyone else runs into this here's what I found was the cause of my particular issue. Note that the answer above re timezones also makes sense!
My rails app is storing UTC datetimes in the DB (as is default)
But, it also thought it's own timezone was UTC, which also seems to be the default.
The upshot of that is essentially it was storing local dates, local to UTC anyway. Changing the app to know it was sitting in Europe/London made it so the dates in the DB are all now accurately UTC (meaning, they're an hour off if I'm currently in BST)
I can now use the Z datetime format in iCals, and outlook and the rails app both convert the UTC date back into the actual datetime for the viewing-user's locale (Europe/London for everyone at the moment). This is what I wanted.

Events with multiple dates do I use has_and_belongs_to_many

Users can create multiple day events.
So an event can have multiple dates, including a start time and end time for the date.
So an event might have these dates:
July 1 2013 9:00am - 5:00pm
July 2 2013 10:00am - 6:00pm
July 7 2013 1:00pm - 5:30pm
So the event table would store the event id, event name, the type of event, address fields for where the event will be held, etc. But how do I model the multiple dates for the event? I could have a separate event_dates table, and that table would have multiple rows for each event:
id event_date event_start_time event_end_time
event1 July 1 2013 9:00am 5:00pm
event1 July 2 2013 10:00am 6:00pm
event1 July 7 2013 1:00pm 5:30pm
But I would also like to query for events occurring on a day (regardless of time), so users could find events in their area that occur on certain days (they might visit multiple events that day).
So how would I model this? Would I use has_and_belongs_to_many, where one event has multiple dates, and one date has multiple events?
In the view, when saving the event, I guess I need to save the event data to the events table, and also save the date/time combination to the other table. But to save the date/time combination to the other table, I need the id for the event that was just saved. So in one form I have a dependency such that the first save must succeed (save event), for the second save to proceed (save date/time using new event id).
Thanks for your help in advance.
I would suggest you set up Event to have many Sessions. Then each session can specify a date/time range for that session.
As far as querying for a particular day goes, you can use a join and a little Rails magic like so: (assuming rails 3.2)
class Event
has_many :sessions
def self.on_day(date)
joins(:sessions).where("session.start < ? AND session.end > ?", date.end_of_day, date.beginning_of_day)
end
have a look at closure_tree gem (https://github.com/mceachen/closure_tree) wich allow you to use acts_as_tree and a hierarchy for your events...
Cheers

Rails app - how to enter events in local time zone

I am working on a Rails app that displays a sports schedule (in a basic table). Each game/event is a db table row. The customer wants all the events to display in chronological order but to also display the time in the event's local timezone.
How can I add a timezone selector to the New action? So that when the customer enters events they can select the event's timezone and input the event's time in that timezone.
Here is what the schedule would look like:
Event 1 7:00 pm EST
Event 2 5:00 pm PST
Event 3 7:00 pm PST
I personally think this is confusing, but it's what they want. Thanks in advance.
Geoff Buesing wrote a great primer on Rails support of time zones back when 2.1 was released.
Okay I am not so sure I understood your question directly but here is my line of thinking there is a library I passed along called tzinfo which is supposed to give Rails the ability to work with timezones so my guess is do the following build a list for a select box (wikipedia has a good complete list) when the user selects the timezone send it to the server which will calculate the appropriate timings now and send back (maybe depending on your architecture you could send only the added hours that should be added or subtracted)

Resources