Created
May 14, 2011 02:10
-
-
Save AquaGeek/971614 to your computer and use it in GitHub Desktop.
Rails Lighthouse ticket #2114
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
From 26d9b864009b522e1fa87245d76c0a39a8b148ea Mon Sep 17 00:00:00 2001 | |
From: Matias Flores <mflores@atlanware.com> | |
Date: Sun, 27 Sep 2009 10:59:26 -0300 | |
Subject: [PATCH] Adding beginning_of_our and end_of_hour | |
--- | |
.../core_ext/date_time/calculations.rb | 10 ++++++++ | |
.../active_support/core_ext/time/calculations.rb | 10 ++++++++ | |
activesupport/test/core_ext/date_time_ext_test.rb | 8 ++++++ | |
activesupport/test/core_ext/time_ext_test.rb | 24 ++++++++++++++++++++ | |
activesupport/test/core_ext/time_with_zone_test.rb | 10 ++++++++ | |
5 files changed, 62 insertions(+), 0 deletions(-) | |
diff --git a/activesupport/lib/active_support/core_ext/date_time/calculations.rb b/activesupport/lib/active_support/core_ext/date_time/calculations.rb | |
index 0099431..407317a 100644 | |
--- a/activesupport/lib/active_support/core_ext/date_time/calculations.rb | |
+++ b/activesupport/lib/active_support/core_ext/date_time/calculations.rb | |
@@ -80,6 +80,16 @@ module ActiveSupport #:nodoc: | |
end | |
alias :in :since | |
+ # Returns a new DateTime representing the start of the current hour | |
+ def beginning_of_hour | |
+ change(:min => 0, :sec => 0) | |
+ end | |
+ | |
+ # Returns a new DateTime representing the end of the current hour | |
+ def end_of_hour | |
+ change(:min => 59, :sec => 59) | |
+ end | |
+ | |
# Returns a new DateTime representing the start of the day (0:00) | |
def beginning_of_day | |
change(:hour => 0) | |
diff --git a/activesupport/lib/active_support/core_ext/time/calculations.rb b/activesupport/lib/active_support/core_ext/time/calculations.rb | |
index 380922d..946f302 100644 | |
--- a/activesupport/lib/active_support/core_ext/time/calculations.rb | |
+++ b/activesupport/lib/active_support/core_ext/time/calculations.rb | |
@@ -199,6 +199,16 @@ module ActiveSupport #:nodoc: | |
since(1.week).beginning_of_week.since(days_into_week[day].day).change(:hour => 0) | |
end | |
+ # Returns a new Time representing the start of the current hour | |
+ def beginning_of_hour | |
+ change(:min => 0, :sec => 0, :usec => 0) | |
+ end | |
+ | |
+ # Returns a new Time representing the end of the current hour | |
+ def end_of_hour | |
+ change(:min => 59, :sec => 59, :usec => 999999.999) | |
+ end | |
+ | |
# Returns a new Time representing the start of the day (0:00) | |
def beginning_of_day | |
#(self - seconds_since_midnight).change(:usec => 0) | |
diff --git a/activesupport/test/core_ext/date_time_ext_test.rb b/activesupport/test/core_ext/date_time_ext_test.rb | |
index 45eb52c..cf14dda 100644 | |
--- a/activesupport/test/core_ext/date_time_ext_test.rb | |
+++ b/activesupport/test/core_ext/date_time_ext_test.rb | |
@@ -57,6 +57,14 @@ class DateTimeExtCalculationsTest < Test::Unit::TestCase | |
assert_equal DateTime.civil(2005,11,28), DateTime.civil(2005,12,04,0,0,0).beginning_of_week #sunday | |
end | |
+ def test_beginning_of_hour | |
+ assert_equal DateTime.civil(2005,2,4,10,0,0), DateTime.civil(2005,2,4,10,10,10).beginning_of_hour | |
+ end | |
+ | |
+ def test_end_of_hour | |
+ assert_equal DateTime.civil(2005,2,4,10,59,59), DateTime.civil(2005,2,4,10,10,10).end_of_hour | |
+ end | |
+ | |
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 | |
diff --git a/activesupport/test/core_ext/time_ext_test.rb b/activesupport/test/core_ext/time_ext_test.rb | |
index 62c2772..180cda5 100644 | |
--- a/activesupport/test/core_ext/time_ext_test.rb | |
+++ b/activesupport/test/core_ext/time_ext_test.rb | |
@@ -49,6 +49,18 @@ class TimeExtCalculationsTest < Test::Unit::TestCase | |
end | |
end | |
+ def test_beginning_of_hour | |
+ assert_equal Time.local(2005,2,4,10,0,0), Time.local(2005,2,4,10,10,10).beginning_of_hour | |
+ with_env_tz 'US/Eastern' do | |
+ assert_equal Time.local(2006,4,2,10,0,0), Time.local(2006,4,2,10,10,10).beginning_of_hour, 'start DST' | |
+ assert_equal Time.local(2006,10,29,10,0,0), Time.local(2006,10,29,10,10,10).beginning_of_hour, 'ends DST' | |
+ end | |
+ with_env_tz 'NZ' do | |
+ assert_equal Time.local(2006,3,19,10,0,0), Time.local(2006,3,19,10,10,10).beginning_of_hour, 'ends DST' | |
+ assert_equal Time.local(2006,10,1,10,0,0), Time.local(2006,10,1,10,10,10).beginning_of_hour, 'start DST' | |
+ end | |
+ end | |
+ | |
def test_beginning_of_week | |
assert_equal Time.local(2005,1,31), Time.local(2005,2,4,10,10,10).beginning_of_week | |
assert_equal Time.local(2005,11,28), Time.local(2005,11,28,0,0,0).beginning_of_week #monday | |
@@ -83,6 +95,18 @@ class TimeExtCalculationsTest < Test::Unit::TestCase | |
assert_equal Time.local(2005,4,1,0,0,0), Time.local(2005,6,30,23,59,59).beginning_of_quarter | |
end | |
+ def test_end_of_hour | |
+ assert_equal Time.local(2007,8,12,10,59,59,999999.999), Time.local(2007,8,12,10,10,10).end_of_hour | |
+ with_env_tz 'US/Eastern' do | |
+ assert_equal Time.local(2007,4,2,10,59,59,999999.999), Time.local(2007,4,2,10,10,10).end_of_hour, 'start DST' | |
+ assert_equal Time.local(2007,10,29,10,59,59,999999.999), Time.local(2007,10,29,10,10,10).end_of_hour, 'ends DST' | |
+ end | |
+ with_env_tz 'NZ' do | |
+ assert_equal Time.local(2006,3,19,10,59,59,999999.999), Time.local(2006,3,19,10,10,10).end_of_hour, 'ends DST' | |
+ assert_equal Time.local(2006,10,1,10,59,59,999999.999), Time.local(2006,10,1,10,10,10).end_of_hour, 'start DST' | |
+ end | |
+ end | |
+ | |
def test_end_of_day | |
assert_equal Time.local(2007,8,12,23,59,59,999999.999), Time.local(2007,8,12,10,10,10).end_of_day | |
with_env_tz 'US/Eastern' do | |
diff --git a/activesupport/test/core_ext/time_with_zone_test.rb b/activesupport/test/core_ext/time_with_zone_test.rb | |
index 03ed783..b0e9015 100644 | |
--- a/activesupport/test/core_ext/time_with_zone_test.rb | |
+++ b/activesupport/test/core_ext/time_with_zone_test.rb | |
@@ -474,6 +474,16 @@ class TimeWithZoneTest < Test::Unit::TestCase | |
assert_equal "Fri, 31 Dec 1999 23:59:59 EST -05:00", @twz.end_of_day.inspect | |
end | |
+ def beginning_of_hour | |
+ assert_equal "Fri, 31 Dec 1999 19:00: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 end_of_hour | |
+ assert_equal "Fri, 31 Dec 1999 19:00: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 | |
-- | |
1.6.0.4 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
From c3d7b8813a50e4c574f4b03076cc813050cdf30f Mon Sep 17 00:00:00 2001 | |
From: Matias Flores <mflores@atlanware.com> | |
Date: Sun, 27 Sep 2009 10:44:03 -0300 | |
Subject: [PATCH] Adding beginning_of_hour and end_of_hour | |
--- | |
.../core_ext/date_time/calculations.rb | 10 ++++++++ | |
.../active_support/core_ext/time/calculations.rb | 10 ++++++++ | |
activesupport/test/core_ext/date_time_ext_test.rb | 8 ++++++ | |
activesupport/test/core_ext/time_ext_test.rb | 24 ++++++++++++++++++++ | |
activesupport/test/core_ext/time_with_zone_test.rb | 10 ++++++++ | |
5 files changed, 62 insertions(+), 0 deletions(-) | |
diff --git a/activesupport/lib/active_support/core_ext/date_time/calculations.rb b/activesupport/lib/active_support/core_ext/date_time/calculations.rb | |
index 41cf020..8305861 100644 | |
--- a/activesupport/lib/active_support/core_ext/date_time/calculations.rb | |
+++ b/activesupport/lib/active_support/core_ext/date_time/calculations.rb | |
@@ -67,6 +67,16 @@ class DateTime | |
end | |
alias :in :since | |
+ # Returns a new DateTime representing the start of the current hour | |
+ def beginning_of_hour | |
+ change(:min => 0, :sec => 0) | |
+ end | |
+ | |
+ # Returns a new DateTime representing the end of the current hour | |
+ def end_of_hour | |
+ change(:min => 59, :sec => 59) | |
+ end | |
+ | |
# Returns a new DateTime representing the start of the day (0:00) | |
def beginning_of_day | |
change(:hour => 0) | |
diff --git a/activesupport/lib/active_support/core_ext/time/calculations.rb b/activesupport/lib/active_support/core_ext/time/calculations.rb | |
index 4f4492f..cb4327a 100644 | |
--- a/activesupport/lib/active_support/core_ext/time/calculations.rb | |
+++ b/activesupport/lib/active_support/core_ext/time/calculations.rb | |
@@ -170,6 +170,16 @@ class Time | |
since(1.week).beginning_of_week.since(DAYS_INTO_WEEK[day].day).change(:hour => 0) | |
end | |
+ # Returns a new Time representing the start of the current hour | |
+ def beginning_of_hour | |
+ change(:min => 0, :sec => 0, :usec => 0) | |
+ end | |
+ | |
+ # Returns a new Time representing the end of the current hour | |
+ def end_of_hour | |
+ change(:min => 59, :sec => 59, :usec => 999999.999) | |
+ end | |
+ | |
# Returns a new Time representing the start of the day (0:00) | |
def beginning_of_day | |
#(self - seconds_since_midnight).change(:usec => 0) | |
diff --git a/activesupport/test/core_ext/date_time_ext_test.rb b/activesupport/test/core_ext/date_time_ext_test.rb | |
index a7b179b..5a3d45d 100644 | |
--- a/activesupport/test/core_ext/date_time_ext_test.rb | |
+++ b/activesupport/test/core_ext/date_time_ext_test.rb | |
@@ -58,6 +58,14 @@ class DateTimeExtCalculationsTest < Test::Unit::TestCase | |
assert_equal DateTime.civil(2005,11,28), DateTime.civil(2005,12,04,0,0,0).beginning_of_week #sunday | |
end | |
+ def test_beginning_of_hour | |
+ assert_equal DateTime.civil(2005,2,4,10,0,0), DateTime.civil(2005,2,4,10,10,10).beginning_of_hour | |
+ end | |
+ | |
+ def test_end_of_hour | |
+ assert_equal DateTime.civil(2005,2,4,10,59,59), DateTime.civil(2005,2,4,10,10,10).end_of_hour | |
+ end | |
+ | |
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 | |
diff --git a/activesupport/test/core_ext/time_ext_test.rb b/activesupport/test/core_ext/time_ext_test.rb | |
index f6003bc..a52fa8a 100644 | |
--- a/activesupport/test/core_ext/time_ext_test.rb | |
+++ b/activesupport/test/core_ext/time_ext_test.rb | |
@@ -50,6 +50,18 @@ class TimeExtCalculationsTest < Test::Unit::TestCase | |
end | |
end | |
+ def test_beginning_of_hour | |
+ assert_equal Time.local(2005,2,4,10,0,0), Time.local(2005,2,4,10,10,10).beginning_of_hour | |
+ with_env_tz 'US/Eastern' do | |
+ assert_equal Time.local(2006,4,2,10,0,0), Time.local(2006,4,2,10,10,10).beginning_of_hour, 'start DST' | |
+ assert_equal Time.local(2006,10,29,10,0,0), Time.local(2006,10,29,10,10,10).beginning_of_hour, 'ends DST' | |
+ end | |
+ with_env_tz 'NZ' do | |
+ assert_equal Time.local(2006,3,19,10,0,0), Time.local(2006,3,19,10,10,10).beginning_of_hour, 'ends DST' | |
+ assert_equal Time.local(2006,10,1,10,0,0), Time.local(2006,10,1,10,10,10).beginning_of_hour, 'start DST' | |
+ end | |
+ end | |
+ | |
def test_beginning_of_week | |
assert_equal Time.local(2005,1,31), Time.local(2005,2,4,10,10,10).beginning_of_week | |
assert_equal Time.local(2005,11,28), Time.local(2005,11,28,0,0,0).beginning_of_week #monday | |
@@ -84,6 +96,18 @@ class TimeExtCalculationsTest < Test::Unit::TestCase | |
assert_equal Time.local(2005,4,1,0,0,0), Time.local(2005,6,30,23,59,59).beginning_of_quarter | |
end | |
+ def test_end_of_hour | |
+ assert_equal Time.local(2007,8,12,10,59,59,999999.999), Time.local(2007,8,12,10,10,10).end_of_hour | |
+ with_env_tz 'US/Eastern' do | |
+ assert_equal Time.local(2007,4,2,10,59,59,999999.999), Time.local(2007,4,2,10,10,10).end_of_hour, 'start DST' | |
+ assert_equal Time.local(2007,10,29,10,59,59,999999.999), Time.local(2007,10,29,10,10,10).end_of_hour, 'ends DST' | |
+ end | |
+ with_env_tz 'NZ' do | |
+ assert_equal Time.local(2006,3,19,10,59,59,999999.999), Time.local(2006,3,19,10,10,10).end_of_hour, 'ends DST' | |
+ assert_equal Time.local(2006,10,1,10,59,59,999999.999), Time.local(2006,10,1,10,10,10).end_of_hour, 'start DST' | |
+ end | |
+ end | |
+ | |
def test_end_of_day | |
assert_equal Time.local(2007,8,12,23,59,59,999999.999), Time.local(2007,8,12,10,10,10).end_of_day | |
with_env_tz 'US/Eastern' do | |
diff --git a/activesupport/test/core_ext/time_with_zone_test.rb b/activesupport/test/core_ext/time_with_zone_test.rb | |
index bb60968..556ff62 100644 | |
--- a/activesupport/test/core_ext/time_with_zone_test.rb | |
+++ b/activesupport/test/core_ext/time_with_zone_test.rb | |
@@ -480,6 +480,16 @@ class TimeWithZoneTest < Test::Unit::TestCase | |
assert_equal "Fri, 31 Dec 1999 23:59:59 EST -05:00", @twz.end_of_day.inspect | |
end | |
+ def beginning_of_hour | |
+ assert_equal "Fri, 31 Dec 1999 19:00: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 end_of_hour | |
+ assert_equal "Fri, 31 Dec 1999 19:00: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 | |
-- | |
1.6.0.4 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment