Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
added beginning_of_hour support to core_ext calculations for Time and…
… DateTime
  • Loading branch information
mjtko committed May 4, 2012
1 parent 7c7fb3a commit 145cc69
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 0 deletions.
Expand Up @@ -81,6 +81,17 @@ def end_of_day
change(:hour => 23, :min => 59, :sec => 59)
end

# Returns a new DateTime representing the start of the hour (hh:00:00)
def beginning_of_hour
change(:min => 0)
end
alias :at_beginning_of_hour :beginning_of_hour

# Returns a new DateTime representing the end of the hour (hh:59:59)
def end_of_hour
change(:min => 59, :sec => 59)
end

# 1.9.3 defines + and - on DateTime, < 1.9.3 do not.
if DateTime.public_instance_methods(false).include?(:+)
def plus_with_duration(other) #:nodoc:
Expand Down
15 changes: 15 additions & 0 deletions activesupport/lib/active_support/core_ext/time/calculations.rb
Expand Up @@ -218,6 +218,21 @@ def end_of_day
change(:hour => 23, :min => 59, :sec => 59, :usec => 999999.999)
end

# Returns a new Time representing the start of the hour (x:00)
def beginning_of_hour
change(:min => 0)
end
alias :at_beginning_of_hour :beginning_of_hour

# Returns a new Time representing the end of the hour, x:59:59.999999 (.999999999 in ruby1.9)
def end_of_hour
change(
:min => 59,
:sec => 59,
:usec => 999999.999
)
end

# Returns a new Time representing the start of the month (1st of the month, 0:00)
def beginning_of_month
#self - ((self.mday-1).days + self.seconds_since_midnight)
Expand Down
8 changes: 8 additions & 0 deletions activesupport/test/core_ext/date_time_ext_test.rb
Expand Up @@ -91,6 +91,14 @@ def test_end_of_day
assert_equal DateTime.civil(2005,2,4,23,59,59), DateTime.civil(2005,2,4,10,10,10).end_of_day
end

def test_beginning_of_hour
assert_equal DateTime.civil(2005,2,4,19,0,0), DateTime.civil(2005,2,4,19,30,10).beginning_of_hour
end

def test_end_of_hour
assert_equal DateTime.civil(2005,2,4,19,59,59), DateTime.civil(2005,2,4,19,30,10).end_of_hour
end

def test_beginning_of_month
assert_equal DateTime.civil(2005,2,1,0,0,0), DateTime.civil(2005,2,22,10,10,10).beginning_of_month
end
Expand Down
8 changes: 8 additions & 0 deletions activesupport/test/core_ext/time_ext_test.rb
Expand Up @@ -93,6 +93,10 @@ def test_beginning_of_day
end
end

def test_beginning_of_hour
assert_equal Time.local(2005,2,4,19,0,0), Time.local(2005,2,4,19,30,10).beginning_of_hour
end

def test_beginning_of_month
assert_equal Time.local(2005,2,1,0,0,0), Time.local(2005,2,22,10,10,10).beginning_of_month
end
Expand Down Expand Up @@ -127,6 +131,10 @@ def test_end_of_week
assert_equal Time.local(2007,9,2,23,59,59,999999.999), Time.local(2007,9,02,0,0,0).end_of_week #sunday
end

def test_end_of_hour
assert_equal Time.local(2005,2,4,19,59,59,999999.999), Time.local(2005,2,4,19,30,10).end_of_hour
end

def test_end_of_month
assert_equal Time.local(2005,3,31,23,59,59,999999.999), Time.local(2005,3,20,10,10,10).end_of_month
assert_equal Time.local(2005,2,28,23,59,59,999999.999), Time.local(2005,2,20,10,10,10).end_of_month
Expand Down
14 changes: 14 additions & 0 deletions activesupport/test/core_ext/time_with_zone_test.rb
Expand Up @@ -513,6 +513,20 @@ def end_of_day
assert_equal "Fri, 31 Dec 1999 23:59:59 EST -05:00", @twz.end_of_day.inspect
end

def test_beginning_of_hour
utc = Time.utc(2000, 1, 1, 0, 30)
twz = ActiveSupport::TimeWithZone.new(utc, @time_zone)
assert_equal "Fri, 31 Dec 1999 19:30:00 EST -05:00", twz.inspect
assert_equal "Fri, 31 Dec 1999 19:00:00 EST -05:00", twz.beginning_of_hour.inspect
end

def test_end_of_hour
utc = Time.utc(2000, 1, 1, 0, 30)
twz = ActiveSupport::TimeWithZone.new(utc, @time_zone)
assert_equal "Fri, 31 Dec 1999 19:30:00 EST -05:00", twz.inspect
assert_equal "Fri, 31 Dec 1999 19:59:59 EST -05:00", twz.end_of_hour.inspect
end

def test_since
assert_equal "Fri, 31 Dec 1999 19:00:01 EST -05:00", @twz.since(1).inspect
end
Expand Down

0 comments on commit 145cc69

Please sign in to comment.