From 6608a3ef37a8f998958b867a1be186d33edbc151 Mon Sep 17 00:00:00 2001 From: Geoff Buesing Date: Sun, 10 Feb 2008 22:26:16 +0000 Subject: Adding TimeWithZone #to_yaml, #to_datetime, #eql? and method aliases for duck-typing compatibility with Time git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8854 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activesupport/lib/active_support/time_with_zone.rb | 23 ++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'activesupport/lib/active_support/time_with_zone.rb') diff --git a/activesupport/lib/active_support/time_with_zone.rb b/activesupport/lib/active_support/time_with_zone.rb index 13f968386a..15cd95772c 100644 --- a/activesupport/lib/active_support/time_with_zone.rb +++ b/activesupport/lib/active_support/time_with_zone.rb @@ -21,6 +21,9 @@ module ActiveSupport @utc ||= time_zone.local_to_utc(@time) end alias_method :comparable_time, :utc + alias_method :getgm, :utc + alias_method :getutc, :utc + alias_method :gmtime, :utc # Returns the underlying TZInfo::TimezonePeriod for the local time def period @@ -47,18 +50,23 @@ module ActiveSupport def localtime utc.getlocal end + alias_method :getlocal, :localtime def dst? period.dst? end + alias_method :isdst, :dst? def utc? time_zone.name == 'UTC' end + alias_method :gmt?, :utc? def utc_offset period.utc_total_offset end + alias_method :gmt_offset, :utc_offset + alias_method :gmtoff, :utc_offset def formatted_offset(colon = true, alternate_utc_string = nil) utc? && alternate_utc_string || utc_offset.to_utc_offset_s(colon) @@ -76,11 +84,16 @@ module ActiveSupport def xmlschema "#{time.strftime("%Y-%m-%dT%H:%M:%S")}#{formatted_offset(true, 'Z')}" end + alias_method :iso8601, :xmlschema def to_json(options = nil) %("#{time.strftime("%Y/%m/%d %H:%M:%S")} #{formatted_offset(false)}") end + def to_yaml(options = {}) + time.to_yaml(options).gsub('Z', formatted_offset(true, 'Z')) + end + def httpdate utc.httpdate end @@ -112,6 +125,10 @@ module ActiveSupport utc <=> other end + def eql?(other) + utc == other + end + # Need to override #- to intercept situation where a Time or Time With Zone object is passed in # Otherwise, just pass on to method missing def -(other) @@ -129,12 +146,18 @@ module ActiveSupport def to_i utc.to_i end + alias_method :hash, :to_i + alias_method :tv_sec, :to_i # A TimeProxy acts like a Time, so just return self def to_time self end + def to_datetime + utc.to_datetime.new_offset(Rational(utc_offset, 86_400)) + end + # so that self acts_like?(:time) def acts_like_time? true -- cgit v1.2.3