Last active
December 11, 2015 12:28
-
-
Save joshmcarthur/4600500 to your computer and use it in GitHub Desktop.
A code snippet to add #beginning_of_financial_year and #end_of_financial_year methods to Rails' DateTime class.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# spec/extensions/date_time_spec.rb | |
# PS: I know this spec code isn't particularly DRY or nice. | |
require 'spec_helper' | |
describe DateTime do | |
describe "#beginning_of_financial_year" do | |
subject do | |
DateTime.now.beginning_of_financial_year | |
end | |
context "current date is before 1 April" do | |
before do | |
DateTime.stub(:now).and_return(Date.today.change(:month => 3, :day => 30).to_datetime) | |
end | |
it "should be set to the the beginning of April" do | |
subject.day.should eq 1 | |
subject.month.should eq 4 | |
subject.hour.should eq 00 | |
subject.minute.should eq 00 | |
end | |
it "should be set to the previous year" do | |
subject.year.should eq Date.today.year - 1 | |
end | |
end | |
context "current date is after 1 April" do | |
before do | |
DateTime.stub(:now).and_return(Date.today.change(:month => 4, :day => 2).to_datetime) | |
end | |
it "should be set to the 1 April" do | |
subject.day.should eq 1 | |
subject.month.should eq 4 | |
end | |
it "should be set to the current year" do | |
subject.year.should eq Date.today.year | |
end | |
end | |
end | |
describe "#end_of_financial_year" do | |
subject do | |
DateTime.now.end_of_financial_year | |
end | |
context "current date is before 31 March" do | |
before do | |
DateTime.stub(:now).and_return(Date.today.change(:month => 3, :day => 31).to_datetime) | |
end | |
it "should be set to the 31st March" do | |
subject.day.should eq 31 | |
subject.month.should eq 3 | |
end | |
it "should be set to the current year" do | |
subject.year.should eq Date.today.year | |
end | |
end | |
context "current date is after 31 March" do | |
before do | |
DateTime.stub(:now).and_return(Date.today.change(:month => 4, :day => 1).to_datetime) | |
end | |
it "should be set to the end of the 31st of March" do | |
subject.day.should eq 31 | |
subject.month.should eq 3 | |
subject.hour.should eq 23 | |
subject.minute.should eq 59 | |
subject.second.should eq 59 | |
end | |
it "should be set to the next year" do | |
subject.year.should eq Date.today.year + 1 | |
end | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# config/initializers/date_time_extensions.rb | |
class DateTime | |
def beginning_of_financial_year | |
if self < DateTime.new(self.year, 4, 1).midnight | |
self.change(:day => 1, :month => 4, :year => self.year - 1).midnight | |
else | |
self.change(:day => 1, :month => 4).midnight | |
end | |
end | |
def end_of_financial_year | |
if self < DateTime.new(self.year, 3, 31).end_of_day | |
self.change(:day => 31, :month => 3).end_of_day | |
else | |
self.change(:day => 31, :month => 3, :year => self.year + 1).end_of_day | |
end | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment