Skip to content

Commit

Permalink
Merge pull request #10879 from makaroni4/master
Browse files Browse the repository at this point in the history
Added Time#middle_of_day method
  • Loading branch information
pixeltrix committed Jul 29, 2013
2 parents b51786d + 1b6bbb0 commit c901056
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 0 deletions.
10 changes: 10 additions & 0 deletions activesupport/lib/active_support/core_ext/date/calculations.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -69,6 +69,16 @@ def beginning_of_day
alias :at_midnight :beginning_of_day alias :at_midnight :beginning_of_day
alias :at_beginning_of_day :beginning_of_day alias :at_beginning_of_day :beginning_of_day


# Converts Date to a Time (or DateTime if necessary) with the time portion set to the middle of the day (12:00)
def middle_of_day
in_time_zone.middle_of_day
end
alias :midday :middle_of_day
alias :noon :middle_of_day
alias :at_midday :middle_of_day
alias :at_noon :middle_of_day
alias :at_middle_of_day :middle_of_day

# Converts Date to a Time (or DateTime if necessary) with the time portion set to the end of the day (23:59:59) # Converts Date to a Time (or DateTime if necessary) with the time portion set to the end of the day (23:59:59)
def end_of_day def end_of_day
in_time_zone.end_of_day in_time_zone.end_of_day
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -99,6 +99,16 @@ def beginning_of_day
alias :at_midnight :beginning_of_day alias :at_midnight :beginning_of_day
alias :at_beginning_of_day :beginning_of_day alias :at_beginning_of_day :beginning_of_day


# Returns a new DateTime representing the middle of the day (12:00)
def middle_of_day
change(:hour => 12)
end
alias :midday :middle_of_day
alias :noon :middle_of_day
alias :at_midday :middle_of_day
alias :at_noon :middle_of_day
alias :at_middle_of_day :middle_of_day

# Returns a new DateTime representing the end of the day (23:59:59). # Returns a new DateTime representing the end of the day (23:59:59).
def end_of_day def end_of_day
change(:hour => 23, :min => 59, :sec => 59) change(:hour => 23, :min => 59, :sec => 59)
Expand Down
10 changes: 10 additions & 0 deletions activesupport/lib/active_support/core_ext/time/calculations.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -142,6 +142,16 @@ def beginning_of_day
alias :at_midnight :beginning_of_day alias :at_midnight :beginning_of_day
alias :at_beginning_of_day :beginning_of_day alias :at_beginning_of_day :beginning_of_day


# Returns a new Time representing the middle of the day (12:00)
def middle_of_day
change(:hour => 12)
end
alias :midday :middle_of_day
alias :noon :middle_of_day
alias :at_midday :middle_of_day
alias :at_noon :middle_of_day
alias :at_middle_of_day :middle_of_day

# Returns a new Time representing the end of the day, 23:59:59.999999 (.999999999 in ruby1.9) # Returns a new Time representing the end of the day, 23:59:59.999999 (.999999999 in ruby1.9)
def end_of_day def end_of_day
change( change(
Expand Down
4 changes: 4 additions & 0 deletions activesupport/test/core_ext/date_ext_test.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -248,6 +248,10 @@ def test_beginning_of_day
assert_equal Time.local(2005,2,21,0,0,0), Date.new(2005,2,21).beginning_of_day assert_equal Time.local(2005,2,21,0,0,0), Date.new(2005,2,21).beginning_of_day
end end


def test_middle_of_day
assert_equal Time.local(2005,2,21,12,0,0), Date.new(2005,2,21).middle_of_day
end

def test_beginning_of_day_when_zone_is_set def test_beginning_of_day_when_zone_is_set
zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)'] zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
with_env_tz 'UTC' do with_env_tz 'UTC' do
Expand Down
4 changes: 4 additions & 0 deletions activesupport/test/core_ext/date_time_ext_test.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ def test_beginning_of_day
assert_equal DateTime.civil(2005,2,4,0,0,0), DateTime.civil(2005,2,4,10,10,10).beginning_of_day assert_equal DateTime.civil(2005,2,4,0,0,0), DateTime.civil(2005,2,4,10,10,10).beginning_of_day
end end


def test_middle_of_day
assert_equal DateTime.civil(2005,2,4,12,0,0), DateTime.civil(2005,2,4,10,10,10).middle_of_day
end

def test_end_of_day 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 assert_equal DateTime.civil(2005,2,4,23,59,59), DateTime.civil(2005,2,4,10,10,10).end_of_day
end end
Expand Down
12 changes: 12 additions & 0 deletions activesupport/test/core_ext/time_ext_test.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -117,6 +117,18 @@ def test_beginning_of_day
end end
end end


def test_middle_of_day
assert_equal Time.local(2005,2,4,12,0,0), Time.local(2005,2,4,10,10,10).middle_of_day
with_env_tz 'US/Eastern' do
assert_equal Time.local(2006,4,2,12,0,0), Time.local(2006,4,2,10,10,10).middle_of_day, 'start DST'
assert_equal Time.local(2006,10,29,12,0,0), Time.local(2006,10,29,10,10,10).middle_of_day, 'ends DST'
end
with_env_tz 'NZ' do
assert_equal Time.local(2006,3,19,12,0,0), Time.local(2006,3,19,10,10,10).middle_of_day, 'ends DST'
assert_equal Time.local(2006,10,1,12,0,0), Time.local(2006,10,1,10,10,10).middle_of_day, 'start DST'
end
end

def test_beginning_of_hour 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 assert_equal Time.local(2005,2,4,19,0,0), Time.local(2005,2,4,19,30,10).beginning_of_hour
end end
Expand Down

0 comments on commit c901056

Please sign in to comment.