aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/core_ext
diff options
context:
space:
mode:
authorGeoff Buesing <gbuesing@gmail.com>2008-01-23 01:56:22 +0000
committerGeoff Buesing <gbuesing@gmail.com>2008-01-23 01:56:22 +0000
commit022d9f7ce6d1237c4103a2aed561220e1c0f4dcc (patch)
tree1d5ab8ddf1d3cde4cae5f6eec5ed07068ca7ce75 /activesupport/lib/active_support/core_ext
parenta91b7f19754d6fabe1cec3658bbaad45771f13c6 (diff)
downloadrails-022d9f7ce6d1237c4103a2aed561220e1c0f4dcc.tar.gz
rails-022d9f7ce6d1237c4103a2aed561220e1c0f4dcc.tar.bz2
rails-022d9f7ce6d1237c4103a2aed561220e1c0f4dcc.zip
Introduce ActiveSupport::TimeWithZone, for wrapping Time instances with a TimeZone. Introduce instance methods to Time for creating TimeWithZone instances, and class methods for managing a global time zone
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8696 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activesupport/lib/active_support/core_ext')
-rw-r--r--activesupport/lib/active_support/core_ext/date_time.rb2
-rw-r--r--activesupport/lib/active_support/core_ext/time.rb2
-rw-r--r--activesupport/lib/active_support/core_ext/time/zones.rb68
3 files changed, 72 insertions, 0 deletions
diff --git a/activesupport/lib/active_support/core_ext/date_time.rb b/activesupport/lib/active_support/core_ext/date_time.rb
index 1d711de70b..66a93eac43 100644
--- a/activesupport/lib/active_support/core_ext/date_time.rb
+++ b/activesupport/lib/active_support/core_ext/date_time.rb
@@ -1,10 +1,12 @@
require 'date'
require 'active_support/core_ext/time/behavior'
+require 'active_support/core_ext/time/zones'
require 'active_support/core_ext/date_time/calculations'
require 'active_support/core_ext/date_time/conversions'
class DateTime
include ActiveSupport::CoreExtensions::Time::Behavior
+ include ActiveSupport::CoreExtensions::Time::Zones
include ActiveSupport::CoreExtensions::DateTime::Calculations
include ActiveSupport::CoreExtensions::DateTime::Conversions
end
diff --git a/activesupport/lib/active_support/core_ext/time.rb b/activesupport/lib/active_support/core_ext/time.rb
index d87c4ec65a..ea50511a96 100644
--- a/activesupport/lib/active_support/core_ext/time.rb
+++ b/activesupport/lib/active_support/core_ext/time.rb
@@ -11,9 +11,11 @@ end
require 'active_support/core_ext/time/behavior'
require 'active_support/core_ext/time/calculations'
require 'active_support/core_ext/time/conversions'
+require 'active_support/core_ext/time/zones'
class Time#:nodoc:
include ActiveSupport::CoreExtensions::Time::Behavior
include ActiveSupport::CoreExtensions::Time::Calculations
include ActiveSupport::CoreExtensions::Time::Conversions
+ include ActiveSupport::CoreExtensions::Time::Zones
end
diff --git a/activesupport/lib/active_support/core_ext/time/zones.rb b/activesupport/lib/active_support/core_ext/time/zones.rb
new file mode 100644
index 0000000000..563d522d9a
--- /dev/null
+++ b/activesupport/lib/active_support/core_ext/time/zones.rb
@@ -0,0 +1,68 @@
+module ActiveSupport #:nodoc:
+ module CoreExtensions #:nodoc:
+ module Time #:nodoc:
+ # Methods for creating TimeWithZone objects from Time instances
+ module Zones
+
+ def self.included(base) #:nodoc:
+ base.extend(ClassMethods) if base == ::Time # i.e., don't include class methods in DateTime
+ end
+
+ module ClassMethods
+ attr_reader :zone
+
+ # Sets a global default time zone, separate from the system time zone in ENV['TZ'].
+ # Accepts either a Rails TimeZone object, a string that identifies a
+ # Rails TimeZone object (e.g., "Central Time (US & Canada)"), or a TZInfo::Timezone object
+ #
+ # Any Time or DateTime object can use this default time zone, via #in_current_time_zone.
+ # Example:
+ #
+ # Time.zone = 'Hawaii' # => 'Hawaii'
+ # Time.utc(2000).in_current_time_zone # => Fri, 31 Dec 1999 14:00:00 HST -10:00
+ def zone=(zone)
+ @zone = get_zone(zone)
+ end
+
+ def zone_reset!
+ @zone = nil
+ end
+
+ def get_zone(zone)
+ ::String === zone || ::Numeric === zone ? TimeZone[zone] : zone
+ end
+ end
+
+ # Gives the corresponding time in the supplied zone. self is assumed to be in UTC regardless of constructor.
+ #
+ # Examples:
+ #
+ # t = Time.utc(2000) # => Sat Jan 01 00:00:00 UTC 2000
+ # t.in_time_zone('Alaska') # => Fri, 31 Dec 1999 15:00:00 AKST -09:00
+ # t.in_time_zone('Hawaii') # => Fri, 31 Dec 1999 14:00:00 HST -10:00
+ def in_time_zone(zone)
+ ActiveSupport::TimeWithZone.new(self, ::Time.get_zone(zone))
+ end
+
+ # Returns the simultaneous time in Time.zone
+ def in_current_time_zone
+ in_time_zone(::Time.zone)
+ end
+
+ # Replaces the existing zone; leaves time value intact. Examples:
+ #
+ # t = Time.utc(2000) # => Sat Jan 01 00:00:00 UTC 2000
+ # t.change_time_zone('Alaska') # => Sat, 01 Jan 2000 00:00:00 AKST -09:00
+ # t.change_time_zone('Hawaii') # => Sat, 01 Jan 2000 00:00:00 HST -10:00
+ def change_time_zone(zone)
+ ActiveSupport::TimeWithZone.new(nil, ::Time.get_zone(zone), self)
+ end
+
+ # Replaces the existing zone to Time.zone; leaves time value intact
+ def change_time_zone_to_current
+ change_time_zone(::Time.zone)
+ end
+ end
+ end
+ end
+end \ No newline at end of file