mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #10879 from makaroni4/master
Added Time#middle_of_day method
This commit is contained in:
commit
c901056131
6 changed files with 50 additions and 0 deletions
|
@ -69,6 +69,16 @@ class Date
|
||||||
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
|
||||||
|
|
|
@ -99,6 +99,16 @@ class DateTime
|
||||||
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)
|
||||||
|
|
|
@ -142,6 +142,16 @@ class Time
|
||||||
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(
|
||||||
|
|
|
@ -248,6 +248,10 @@ class DateExtCalculationsTest < ActiveSupport::TestCase
|
||||||
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
|
||||||
|
|
|
@ -82,6 +82,10 @@ class DateTimeExtCalculationsTest < ActiveSupport::TestCase
|
||||||
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
|
||||||
|
|
|
@ -117,6 +117,18 @@ class TimeExtCalculationsTest < ActiveSupport::TestCase
|
||||||
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
|
||||||
|
|
Loading…
Reference in a new issue