Distance of time in days - ruby-on-rails-3

In rails 3 there is distance_of_time_in_words which works fine, but I would like to tweak its output a bit.
The thing is that if the dates are exactly the same it will say "Less Than a day" I would like it to show "Today" instead.
Is that possible?

I wrote a gem called "date-casually" that monkey-patches the Date class with a "casual" method:
Date.today.casual
#=> 'today'
(Date.today + 1).casual
#=> 'tomorrow'
(Date.today - 1).casual
#=> 'yesterday'
Hope it helps:
http://rubygems.org/gems/date-casually

Related

How to write query in Ruby to check certain time object is less than 1 hour or 3 hour

I have a Trip model in ruby which has start_at column and i need to get Trip object if start_at less than 1 hour or 3 hour (only i should get trip object when start_at is < 1hr or < 3hr not for < 2hr, < 4hr, ..... etc).
Note: I have Cron job which runs for every 15.minutes to get trip object as explained above.
Example: Assume i have a trip which start_at = 9:00 am and current time is 10:00 am i should get that trip object. Same goes for 3 hour also (start_at = 9:00 am and current time is 12:00 am)
^^ Except above two cases i should not get trip object, need to get only for less than 1hr or 3hr
This is what i tried
Trip.where("start_at < ? ", 1.hour.ago)
But above query returning trip object even if start_at < 2 hours ago, 4 hours ago, ..... blah blah
I am new to Ruby any help would be appreciated.
Thanks!
Trip.where(start_at: 2.hours.ago..1.hour.ago).or(Trip.where(start_at: 4.hours.ago..3.hour.ago))
You could use or query, or use array value:
Trip.where(start_at: [2.hours.ago..1.hour.ago, 4.hours.ago..3.hour.ago])

Rails 3.1 - Getting results between start and end dates?

I have an event listing site where i'm currently listing events occuring today by doing:
def load_today_events
#today_events = Event.find(:all, :conditions => ['start_date =?', Date.today])
end
This works perfectly well.
I've recently introduced an end_date for events spanning more than one day.
Obviously, with the above code once the start_date has passed the event disappears from my listing.
I may be having a mental block here, but I can't for the life of me figure out how to show events that's start date has passed but end date has not.
I think I want something along the lines of:
find events where date.today is equal to or between start_date or end_date
Any ideas how best to approach this?
Not sure if i understand this correctly, but here are some examples of what you can do with dates.
In the example below, current_date is a postgresql function for the today date. This would match the events that the current_date is bigger or equal than start_date and less than end_date.
#today_events = Event.where("current_date >= ? and current_date <= ?", start_date, end_date)
Another approach could be like this:
#today_events = Event.where("start_date >= ? and end_date <= ?", start_date, end_date)
This example shows events that are from a current date interval.
This is the basics, so i hope you can apply to your own problem.

date select showing days before and after given date

I want to have a drop down on a form that shows in plain English "1 day before, 2 days before" etc a given date as well as "1 days after, 2 days after".
How can I do this with Rails date_select?
Thanks
Ok I am creating my own as follows but am a bit stuck:
<%= select_tag(:board,options_for_select([["On the Birthday", 0], ["1 Day Before", -1], ["Two Days Before", -2], ["Three Days Before", -3]]), { :class => "default" }) %>
Now what I want to do is to show the number of days the user has saved. So how can I program or set the select so it knows which value to select.
I have the delivery date saved in a table and can access it as follows:
#board.deliver_on
Thanks.
Put simply: you can't do it with a date_select. You would need to build it using either the select or collection_select helpers and work out the values for the dates yourself. There is no helper in Rails to do that as far as I am aware.

How to use conditions in SQLite (like if-statements, etc.)

I have a table in a database with one column containing dates and another one containing scores. Basically, what I want to do is grab the best score in a given week.
Weeks can start on any given day (From Friday to Thursday, for instance), and that is defined by the user.
Here is what I have so far:
SELECT MAX(Series), DATE(DATE(Date, 'weekday 0'), '-7 days') dateStartOfWeek FROM SeriesScores
WHERE Season = '2010-2011'
AND dateStartOfWeek = '2010-08-29'
GROUP BY DateStartOfWeek
Where Series is the column containing the scores and Date is the (badly) named actual date.
The problem with this query is that it works for every day except for the day the week is supposed to be starting on.
For example: 2010-08-29 is a Sunday and in this example, I'm trying to find on which date the Sunday of the given week is. My function works for every day of that week except for 2010-08-29 (Sunday) since it tries to find the next day that is a Sunday (itself in this case). To compensate for that, I go back 7 days to get the correct Sunday, which creates the error for the already correct Sunday since this one doesn't need to go back 7 days or else it is one week off.
I figured I could solve this problem easily using Java, but I want to see how it should be done using SQL instead.
My solution (I don't even know if it can be done), would be to check if date and dateStartOfWeek are the same. If they are, don't substract 7 days from the date. If they're not, do as I did in my example. I don't know how to use conditions such as this one in SQL, though, and this is where I need help.
Thanks a lot in advance!
I think you need to use CASE operator - see http://sqlite.awardspace.info/syntax/sqlitepg09.htm
EDIT - try:
SELECT MAX(Series), CASE WHEN STRFTIME ( '%w', Date ) = 0 THEN DATE(Date, 'weekday 0') ELSE DATE(DATE(Date, 'weekday 0'), '-7 days') END AS dateStartOfWeek FROM SeriesScores
WHERE Season = '2010-2011'
AND dateStartOfWeek = '2010-08-29'
GROUP BY DateStartOfWeek
see http://www.sqlite.org/lang_datefunc.html

Rails 3 Finding day of week

I'm trying to make my own calendar because i cant seem to get any help with event calendar so what im trying to do is Display the day of the week ie.. January 1 2011 is a Saturday is there an easy command to get that day or do i need something else
You can also get the string, like "Wednesday", using time.strftime("%A")
Can't you use time.wday, 0 = sunday and so on
Actively using in Rails 4 - should in most other versions as well...
Both of these options will give you the day string (eg. "Saturday") for the date specified:
Specific date:
Date.new(2011, 1, 1).strftime('%A') # returns "Saturday"
Today:
Date.today.strftime('%A')
If you're attempting to write your own calendar from scratch and want to write a function to do day lookup, you might want to check out Conway's Doomsday Algorithm, which is an interesting method for determining the day of the week on any given date. Otherwise the standard time class has a wday method which returns a number from 0-6 (0 is Sunday, 1 is Monday, etc).
We can use Time.now.utc.wday to get day of week without considering zone.
No need to use the Time class. date.cwday will return 1 for Monday, 2 for Tuesday etc.