I'm using Chronic to get the last Sunday of the month of any given year. It will gladly give me the nth Sunday, but not the last.
This works, but is not what I need:
Chronic.parse('4th sunday in march', :now => Time.local(2015,1,1))
This is what I need, but doesn't work:
Chronic.parse('last sunday in march', :now => Time.local(2015,1,1))
Is there any way around this apparent limitation?
UPDATE: I'm upvoting the two answers below because they're both good, but I've already implemented this in "pure Ruby" (in 2 lines of code, besides the require 'date'
line), but I'm trying to demonstrate to management that Ruby is the right language to use to replace a Java codebase that is going away (and which had dozens of lines of code to compute this), and I told one manager that I probably could do it in one line of Ruby, and it would be readable and easy to maintain.
Chronic.parse('1 week before 1st sunday in april')
. – Shastashastranil
. – Achates7 hours before tomorrow at noon
, so I assumed the[interval] before [...]
notation would work more generally. My bad. – ShastashastraChronic
in a single line, because you just call a function. However, using that argument you are basically saying that you can call a ruby function with a single line of code.Chronic
probably need more than one line to implement this ;) – Tshombe