Merge pull request #6156 from mjtko/feature-beginning_of_hour

Beginning and end of hour support for Time and DateTime
This commit is contained in:
Jeremy Kemper 2012-05-04 15:34:11 -07:00
commit fecfd61c22
5 changed files with 56 additions and 0 deletions

@ -91,6 +91,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
# Adjusts DateTime to UTC by adding its offset value; offset is set to 0
#
# Example:

@ -257,6 +257,21 @@ def end_of_day
)
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)

@ -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

@ -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
@ -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

@ -512,6 +512,20 @@ def test_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