diff options
author | Geoff Buesing <gbuesing@gmail.com> | 2008-02-10 22:26:16 +0000 |
---|---|---|
committer | Geoff Buesing <gbuesing@gmail.com> | 2008-02-10 22:26:16 +0000 |
commit | 6608a3ef37a8f998958b867a1be186d33edbc151 (patch) | |
tree | 8adea40ad921d031718319c68d0460eb1498fa45 /activesupport/lib/active_support | |
parent | 8ad5a29cef38cd95aac6553357fc32a6e9e7b411 (diff) | |
download | rails-6608a3ef37a8f998958b867a1be186d33edbc151.tar.gz rails-6608a3ef37a8f998958b867a1be186d33edbc151.tar.bz2 rails-6608a3ef37a8f998958b867a1be186d33edbc151.zip |
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
Diffstat (limited to 'activesupport/lib/active_support')
-rw-r--r-- | activesupport/lib/active_support/time_with_zone.rb | 23 |
1 files changed, 23 insertions, 0 deletions
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 |