Time Calculation Extension
July 27th, 2007 -
Time.now.end_of_yearTime.now.first_of_the_year (or) Time.now.the_new_yearTime.now.end_of_weekTime.now.next_quarterTime.now.end_of_quarter
I’ve been finding these pretty handy and so I’ve added them to a plugin to extend the Time class. You can grab the plugin here:
http://svn.robertrevans.com/plugins/calc_extension/
Enjoy!
Comments
Robert
July 28th, 2007
Eric: Why would I do that? I like how the syntax reads.
Your first example has been fixed, a simple syntax error.
The second example you cite isn’t for the beginning of the current year, but was written for the beginning of the follow year. So, yes Time.now.first_of_the_year should equal Time.now.beginning_of_year + 1.year.
Time.now.next_quarter using + 3.months or month_since(3) is pretty much the same thing.
Open up irb and try out Time.now + 1.months and continue to add months and you’ll see the day never changes, as the months do.
Eric (again)
July 29th, 2007
I hope you didn’t take it bad, my tone could have been somewhat offensive…. sorry!
1) Correct me if I’m wrong, but hasn’t december always 31 days, regardless of the current year?
2) Maybe you should call it beginning_of_next_year ?
3) No, it’s not! 3.months are always 90.days, while months_since(3) could be 92.days, 91.days or …, depending on the current month.
(Actually, I just read http://dev.rubyonrails.org/changeset/5940, maybe we just dont use the same rails -v… do you run rails-edge?)
Robert
July 29th, 2007
No worries, Eric.
1. As I said, it was a syntax error on my part and it has been fixed.
2. Perhaps renaming it for the sake of confusion would be better – good point.
3. The *.months has been fixed and is currently in edge and the soon rails 2.0. So yes, it is pretty much the same thing. Also, my readme file does point out this plugin is for edge rails.
Commenting has been turned off.
Eric
July 28th, 2007
Hi! Why would you do that???
Time.local(2007,6).end_of_year == Time.local(2007,7).end_of_year => false
Also, shouldn’t Time#first_of_the_year==Time#beginning_of_year instead of Time#first_of_the_year==Time#beginning_of_year+1.year?
Finally, i think def next_quarter beginning_of_quarter.months_since(3) end would be more accurate, because: >> Time.now.next_quarter => Sat Sep 29 00:00:00 +0200 2007 but should be: >> Time.now.beginning_of_quarter.months_since(3) => Mon Oct 01 00:00:00 +0200 2007
Best regards, Eric