Trouble with Chronic

I’ve decided to try Chronic to make it easier (hopefully) to get certain
I’ve gotten most of the way there, I’m just stuck on the syntax for the
last day of last month.

Chronic.parse(‘first day of last month’)
=> 2012-10-01

That works, but I’ve tried ‘end day of last month’, ‘final day of last
month’, ‘last day of last month’, and many others. I just can’t work out
the syntax it needs.

On Nov 29, 2012, at 01:45 , Joel P. [email protected] wrote:

the syntax it needs.
I don’t see how throwing tons of code at something so basic is (that
much) easier:

ruby19 -rdate -e ‘t =; puts t -’

To really get the point across, look at how much it does to calculate

ruby19 -rtracer -rdate -e ‘t =; puts t -’


ruby19 -rtracer -e ‘require “chronic”; Chronic.parse(“first day of
this month”) - 86400’

Thanks, that never occurred to me!
This’ll do for the moment. There are probably better ways to do it but I
don’t have time to experiment any further right now.

def get_end_of_last_month -

def get_start_of_last_month
t = -’%Y’).to_i,t.strftime(’%m’).to_i,1)

On Nov 29, 2012, at 02:28 , Joel P. [email protected] wrote:‘%Y’).to_i,t.strftime(‘%m’).to_i,1)


Your get_end_of_last_month creates 3 dates because it calls
redundantly. local variables are so cheap they’re free. Use them.

Your get_start_of_last_month reimplements get_end_of_last_month for no
reason so it too creates 3 dates, then it creates 2 strings instead of
using the API and creates ints from those strings to get your last date.
All in all: 4 dates, 2 strings, 2 ints via unnecessary string parsing.

def get_end_of_last_month # 2 dates
t =
t -

def get_start_of_last_month # 3 dates (inclusive)
t = get_end_of_last_month t.year, t.month, 1

You took the time to unfactor the efficient code I gave you. Please
instead spend that time at least using your space bar, if not writing
efficient and clean code.

For future reference, this is what I ended up working with:

def get_end_of_last_month -

def get_start_of_last_month - + 1 << 1

I’d resigned myself to using Time since the only help I could find for
working with Date assumed I had Rails. Haven’t gotten used to the
methods available yet.
I’ve always tried to avoid creating variables unless I have to, but
since these are short methods I can see the value of using local
variables. Thanks again!