Skip to content

Commit

Permalink
Added Time#middle_of_day
Browse files Browse the repository at this point in the history
Added middle_of_day method to Date and DateTime
  • Loading branch information
makaroni4 committed Jul 28, 2013
1 parent 94725b8 commit 1b6bbb0
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 1 deletion.
12 changes: 11 additions & 1 deletion activesupport/lib/active_support/core_ext/date/calculations.rb
Expand Up @@ -69,6 +69,16 @@ def beginning_of_day
alias :at_midnight :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)
def end_of_day
in_time_zone.end_of_day
Expand Down Expand Up @@ -119,7 +129,7 @@ def change(options)
options.fetch(:day, day)
)
end

# Allow Date to be compared with Time by converting to DateTime and relying on the <=> from there.
def compare_with_coercion(other)
if other.is_a?(Time)
Expand Down
Expand Up @@ -107,6 +107,16 @@ def beginning_of_day
alias :at_midnight :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).
def end_of_day
change(:hour => 23, :min => 59, :sec => 59)
Expand Down
10 changes: 10 additions & 0 deletions activesupport/lib/active_support/core_ext/time/calculations.rb
Expand Up @@ -161,6 +161,16 @@ def beginning_of_day
alias :at_midnight :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)
def end_of_day
change(
Expand Down
4 changes: 4 additions & 0 deletions activesupport/test/core_ext/date_ext_test.rb
Expand Up @@ -247,6 +247,10 @@ def test_beginning_of_day
assert_equal Time.local(2005,2,21,0,0,0), Date.new(2005,2,21).beginning_of_day
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
zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
with_env_tz 'UTC' do
Expand Down
4 changes: 4 additions & 0 deletions activesupport/test/core_ext/date_time_ext_test.rb
Expand Up @@ -76,6 +76,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
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
assert_equal DateTime.civil(2005,2,4,23,59,59), DateTime.civil(2005,2,4,10,10,10).end_of_day
end
Expand Down
12 changes: 12 additions & 0 deletions activesupport/test/core_ext/time_ext_test.rb
Expand Up @@ -117,6 +117,18 @@ def test_beginning_of_day
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
assert_equal Time.local(2005,2,4,19,0,0), Time.local(2005,2,4,19,30,10).beginning_of_hour
end
Expand Down

0 comments on commit 1b6bbb0

Please sign in to comment.