Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added Date#at_time to give a specific time on a specific date.

  • Loading branch information...
commit 1243fe1f35cc3c8f288d0b9411d12c49af5dd705 1 parent 3f14ca4
@ymendel ymendel authored
Showing with 74 additions and 2 deletions.
  1. +3 −2 lib/timely.rb
  2. +13 −0 lib/timely/date.rb
  3. +58 −0 spec/date_spec.rb
View
5 lib/timely.rb
@@ -2,5 +2,6 @@
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
module Timely
-
-end
+end
+
+require 'timely/date'
View
13 lib/timely/date.rb
@@ -0,0 +1,13 @@
+module Timely
+ module Date
+ def at_time(hour = nil, minute = nil, second = nil)
+ Time.local(year, month, day, hour, minute, second)
+ end
+
+ alias_method :at, :at_time
+ end
+end
+
+class Date
+ include Timely::Date
+end
View
58 spec/date_spec.rb
@@ -0,0 +1,58 @@
+require File.dirname(__FILE__) + '/spec_helper.rb'
+
+describe Date do
+ before :each do
+ @date = Date.today - 5
+
+ @hour = 1
+ @minute = 2
+ @second = 3
+ end
+
+ it 'should give a time on that date' do
+ @date.should respond_to(:at_time)
+ end
+
+ describe 'giving a time on that date' do
+ it 'should accept hour, minute, and second' do
+ lambda { @date.at_time(@hour, @minute, @second) }.should_not raise_error(ArgumentError)
+ end
+
+ it 'should accept hour and minute' do
+ lambda { @date.at_time(@hour, @minute) }.should_not raise_error(ArgumentError)
+ end
+
+ it 'should accept hour' do
+ lambda { @date.at_time(@hour) }.should_not raise_error(ArgumentError)
+ end
+
+ it 'should accept no arguments' do
+ lambda { @date.at_time }.should_not raise_error(ArgumentError)
+ end
+
+ it 'should return a time for the given hour, minute, and second if all three are specified' do
+ expected = Time.local(@date.year, @date.month, @date.day, @hour, @minute, @second)
+ @date.at_time(@hour, @minute, @second).should == expected
+ end
+
+ it 'should default second to 0 if unspecified' do
+ expected = Time.local(@date.year, @date.month, @date.day, @hour, @minute, 0)
+ @date.at_time(@hour, @minute).should == expected
+ end
+
+ it 'should default minute to 0 if unspecified' do
+ expected = Time.local(@date.year, @date.month, @date.day, @hour, 0, 0)
+ @date.at_time(@hour).should == expected
+ end
+
+ it 'should default hour to 0 if unspecified' do
+ expected = Time.local(@date.year, @date.month, @date.day, 0, 0, 0)
+ @date.at_time.should == expected
+ end
+ end
+
+ it "should provide 'at' as an alias" do
+ expected = expected = Time.local(@date.year, @date.month, @date.day, @hour, @minute, @second)
+ @date.at(@hour, @minute, @second).should == expected
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.