Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add Time kernel extensions spec

  • Loading branch information...
commit 63952323f2253a2ad7706848dec422c79e2b70bc 1 parent b435d20
@johnnyshields johnnyshields authored
Showing with 60 additions and 3 deletions.
  1. +3 −3 lib/by_star/kernel/time.rb
  2. +57 −0 spec/unit/kernel_time_spec.rb
View
6 lib/by_star/kernel/time.rb
@@ -18,17 +18,17 @@ def beginning_of_weekend
end
def end_of_weekend
- beginning_of_weekend + 60.hours
+ (beginning_of_weekend + 59.hours).end_of_hour
end
# A "Fortnight" is defined as a two week period, with the first fortnight of the
# year beginning on 1st January.
def beginning_of_fortnight
- beginning_of_year + ((self - beginning_of_year) / 2.weeks) * 2.weeks
+ beginning_of_year + ((self - beginning_of_year) / 2.weeks).to_i * 2.weeks
end
def end_of_fortnight
- (beginning_of_fortnight + 2.weeks).end_of_day
+ (beginning_of_fortnight + 13.days).end_of_day
end
# A "Calendar Month" is defined as a month as it appears on a calendar, including days
View
57 spec/unit/kernel_time_spec.rb
@@ -0,0 +1,57 @@
+require 'spec_helper'
+
+describe Time do
+
+ describe 'weekend' do
+
+ (0..6).each do |n|
+ context "Monday plus #{n} days" do
+ subject { Time.parse('2014-01-06') + n.days }
+ its(:beginning_of_weekend){ should eq Time.parse('2014-01-10 15:00') }
+ its(:end_of_weekend){ should eq Time.parse('2014-01-13 02:00').end_of_hour }
+ end
+ end
+ end
+
+ describe 'fortnight' do
+
+ context 'first day of year' do
+ subject { Time.parse '2014-01-01' }
+ its(:beginning_of_fortnight){ should eq Time.parse('2014-01-01') }
+ its(:end_of_fortnight){ should eq Time.parse('2014-01-14').end_of_day }
+ end
+
+ context 'second fortnight of year' do
+ subject { Time.parse '2014-01-16' }
+ its(:beginning_of_fortnight){ should eq Time.parse('2014-01-15') }
+ its(:end_of_fortnight){ should eq Time.parse('2014-01-28').end_of_day }
+ end
+
+ context 'middle of year' do
+ subject { Time.parse '2014-06-13' }
+ its(:beginning_of_fortnight){ should eq Time.parse('2014-06-04') }
+ its(:end_of_fortnight){ should eq Time.parse('2014-06-17').end_of_day }
+ end
+
+ context 'last day of year' do
+ subject { Time.parse '2014-12-31' }
+ its(:beginning_of_fortnight){ should eq Time.parse('2014-12-31') }
+ its(:end_of_fortnight){ should eq Time.parse('2015-01-13').end_of_day }
+ end
+ end
+
+ describe 'calendar_month' do
+
+ subject { Time.parse '2014-01-01' }
+
+ context 'week begins Monday' do
+ its(:beginning_of_calendar_month){ should eq Time.parse('2013-12-30') }
+ its(:end_of_calendar_month){ should eq Time.parse('2014-02-02').end_of_day }
+ end
+
+ context 'week begins Sunday' do
+ it { subject.beginning_of_calendar_month(:sunday).should eq Time.parse('2013-12-29') }
+ it { subject.end_of_calendar_month(:sunday).should eq Time.parse('2014-02-01').end_of_day }
+ end
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.