Reputation: 343
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)?
Upvotes: 0
Views: 150
Reputation: 7522
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)')
Upvotes: 1