From 201f373e7a183f33264ae31e7ca66768dddd3ebe Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Sat, 3 Jul 2010 01:39:54 -0300 Subject: Refactor move some date, time and date_time methods to */zones and fixed some requires --- .../active_support/core_ext/date/calculations.rb | 3 ++- .../lib/active_support/core_ext/date/conversions.rb | 18 ------------------ .../lib/active_support/core_ext/date/zones.rb | 21 +++++++++++++++++++++ .../core_ext/date_time/calculations.rb | 1 + .../core_ext/date_time/conversions.rb | 1 + .../lib/active_support/core_ext/date_time/zones.rb | 2 ++ .../active_support/core_ext/time/calculations.rb | 5 +++++ .../lib/active_support/core_ext/time/zones.rb | 5 ----- activesupport/lib/active_support/time.rb | 1 + 9 files changed, 33 insertions(+), 24 deletions(-) create mode 100644 activesupport/lib/active_support/core_ext/date/zones.rb (limited to 'activesupport/lib/active_support') diff --git a/activesupport/lib/active_support/core_ext/date/calculations.rb b/activesupport/lib/active_support/core_ext/date/calculations.rb index 28fec5394f..e6a213625c 100644 --- a/activesupport/lib/active_support/core_ext/date/calculations.rb +++ b/activesupport/lib/active_support/core_ext/date/calculations.rb @@ -1,7 +1,8 @@ require 'date' require 'active_support/duration' -require 'active_support/core_ext/time/zones' require 'active_support/core_ext/object/acts_like' +require 'active_support/core_ext/date/zones' +require 'active_support/core_ext/time/zones' class Date if RUBY_VERSION < '1.9' diff --git a/activesupport/lib/active_support/core_ext/date/conversions.rb b/activesupport/lib/active_support/core_ext/date/conversions.rb index ba17b0a06b..48d474dd9a 100644 --- a/activesupport/lib/active_support/core_ext/date/conversions.rb +++ b/activesupport/lib/active_support/core_ext/date/conversions.rb @@ -1,6 +1,5 @@ require 'date' require 'active_support/inflector' -require 'active_support/core_ext/time/calculations' class Date DATE_FORMATS = { @@ -15,9 +14,6 @@ class Date # Ruby 1.9 has Date#to_time which converts to localtime only. remove_method :to_time if instance_methods.include?(:to_time) - # Ruby 1.9 has Date#xmlschema which converts to a string without the time component. - remove_method :xmlschema if instance_methods.include?(:xmlschema) - # Convert to a formatted string. See DATE_FORMATS for predefined formats. # # This method is aliased to to_s. @@ -82,16 +78,6 @@ class Date ::Time.send("#{form}_time", year, month, day) end - # Converts Date to a TimeWithZone in the current zone if Time.zone_default is set, - # otherwise converts Date to a Time via Date#to_time - def to_time_in_current_zone - if ::Time.zone_default - ::Time.zone.local(year, month, day) - else - to_time - end - end - # Converts a Date instance to a DateTime, where the time is set to the beginning of the day # and UTC offset is set to 0. # @@ -102,8 +88,4 @@ class Date def to_datetime ::DateTime.civil(year, month, day, 0, 0, 0, 0) end if RUBY_VERSION < '1.9' - - def xmlschema - to_time_in_current_zone.xmlschema - end end diff --git a/activesupport/lib/active_support/core_ext/date/zones.rb b/activesupport/lib/active_support/core_ext/date/zones.rb new file mode 100644 index 0000000000..722f086951 --- /dev/null +++ b/activesupport/lib/active_support/core_ext/date/zones.rb @@ -0,0 +1,21 @@ +require 'date' +require 'active_support/core_ext/time/zones' + +class Date + # Converts Date to a TimeWithZone in the current zone if Time.zone_default is set, + # otherwise converts Date to a Time via Date#to_time + def to_time_in_current_zone + if ::Time.zone_default + ::Time.zone.local(year, month, day) + else + to_time + end + end + + # Ruby 1.9 has Date#xmlschema which converts to a string without the time component. + remove_method :xmlschema if instance_methods.include?(:xmlschema) + + def xmlschema + to_time_in_current_zone.xmlschema + end +end 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 26979aa906..1dc3933e12 100644 --- a/activesupport/lib/active_support/core_ext/date_time/calculations.rb +++ b/activesupport/lib/active_support/core_ext/date_time/calculations.rb @@ -1,5 +1,6 @@ require 'rational' unless RUBY_VERSION >= '1.9.2' require 'active_support/core_ext/object/acts_like' +require 'active_support/core_ext/time/zones' class DateTime class << self diff --git a/activesupport/lib/active_support/core_ext/date_time/conversions.rb b/activesupport/lib/active_support/core_ext/date_time/conversions.rb index c3f0acce25..47b8aa59fb 100644 --- a/activesupport/lib/active_support/core_ext/date_time/conversions.rb +++ b/activesupport/lib/active_support/core_ext/date_time/conversions.rb @@ -1,6 +1,7 @@ require 'active_support/inflector' require 'active_support/core_ext/time/conversions' require 'active_support/core_ext/date_time/calculations' +require 'active_support/values/time_zone' class DateTime # Ruby 1.9 has DateTime#to_time which internally relies on Time. We define our own #to_time which allows diff --git a/activesupport/lib/active_support/core_ext/date_time/zones.rb b/activesupport/lib/active_support/core_ext/date_time/zones.rb index 6002d4ad2a..a7411d54ae 100644 --- a/activesupport/lib/active_support/core_ext/date_time/zones.rb +++ b/activesupport/lib/active_support/core_ext/date_time/zones.rb @@ -1,3 +1,5 @@ +require 'active_support/core_ext/time/zones' + class DateTime # Returns the simultaneous time in Time.zone. # diff --git a/activesupport/lib/active_support/core_ext/time/calculations.rb b/activesupport/lib/active_support/core_ext/time/calculations.rb index 3c218d88e5..90b6cd3685 100644 --- a/activesupport/lib/active_support/core_ext/time/calculations.rb +++ b/activesupport/lib/active_support/core_ext/time/calculations.rb @@ -40,6 +40,11 @@ class Time def local_time(*args) time_with_datetime_fallback(:local, *args) end + + # Returns Time.zone.now when config.time_zone is set, otherwise just returns Time.now. + def current + ::Time.zone_default ? ::Time.zone.now : ::Time.now + end end # Tells whether the Time object's time lies in the past diff --git a/activesupport/lib/active_support/core_ext/time/zones.rb b/activesupport/lib/active_support/core_ext/time/zones.rb index a02402aa3f..6b9ee84d5c 100644 --- a/activesupport/lib/active_support/core_ext/time/zones.rb +++ b/activesupport/lib/active_support/core_ext/time/zones.rb @@ -41,11 +41,6 @@ class Time ::Time.zone = old_zone end - # Returns Time.zone.now when config.time_zone is set, otherwise just returns Time.now. - def current - ::Time.zone_default ? ::Time.zone.now : ::Time.now - end - private def get_zone(time_zone) return time_zone if time_zone.nil? || time_zone.is_a?(ActiveSupport::TimeZone) diff --git a/activesupport/lib/active_support/time.rb b/activesupport/lib/active_support/time.rb index 784c7173a9..86f057d676 100644 --- a/activesupport/lib/active_support/time.rb +++ b/activesupport/lib/active_support/time.rb @@ -24,6 +24,7 @@ require 'active_support/core_ext/date/acts_like' require 'active_support/core_ext/date/freeze' require 'active_support/core_ext/date/calculations' require 'active_support/core_ext/date/conversions' +require 'active_support/core_ext/date/zones' require 'active_support/core_ext/date_time/acts_like' require 'active_support/core_ext/date_time/calculations' -- cgit v1.2.3