Forum: Ruby Trouble with Chronic

Posted by Joel Pearson (virtuoso)
on 2012-11-29 10:44
I've decided to try Chronic to make it easier (hopefully) to get certain
dates.
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.
Posted by Ryan Davis (Guest)
on 2012-11-29 11:10
(Received via mailing list)
On Nov 29, 2012, at 01:45 , Joel Pearson <lists@ruby-forum.com> 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 = Date.today; puts t - t.day'

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

  ruby19 -rtracer -rdate -e 't = Date.today; puts t - t.day'

vs:

  ruby19 -rtracer -e 'require "chronic"; Chronic.parse("first day of 
this month") - 86400'
Posted by Joel Pearson (virtuoso)
on 2012-11-29 11:28
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
  Date.today - Date.today.day
end

def get_start_of_last_month
  t = Date.today - Date.today.day
  Date.new(t.strftime('%Y').to_i,t.strftime('%m').to_i,1)
end
Posted by Ryan Davis (Guest)
on 2012-11-29 22:35
(Received via mailing list)
On Nov 29, 2012, at 02:28 , Joel Pearson <lists@ruby-forum.com> wrote:

>  Date.new(t.strftime('%Y').to_i,t.strftime('%m').to_i,1)
> end


*sigh*

Your get_end_of_last_month creates 3 dates because it calls Date.today 
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 = Date.today
    t - t.day
  end

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

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.
Posted by Joel Pearson (virtuoso)
on 2012-11-30 00:14
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!
Posted by Joel Pearson (virtuoso)
on 2012-12-18 10:59
For future reference, this is what I ended up working with:

def get_end_of_last_month
  Date.today - Date.today.day
end

def get_start_of_last_month
  Date.today - Date.today.mday + 1 << 1
end
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.