Permalink
Browse files

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
  • Loading branch information...
1 parent 8ad5a29 commit 6608a3ef37a8f998958b867a1be186d33edbc151 @gbuesing gbuesing committed Feb 10, 2008
@@ -1,5 +1,7 @@
*SVN*
+* Adding TimeWithZone #to_yaml, #to_datetime, #eql? and method aliases for duck-typing compatibility with Time [Geoff Buesing]
+
* TimeWithZone #in_time_zone returns +self+ if zone argument is the same as #time_zone [Geoff Buesing]
* Adding TimeWithZone #to_a, #to_f, #to_i, #httpdate, #rfc2822 [Geoff Buesing]
@@ -21,6 +21,9 @@ def utc
@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 @@ def change_time_zone(new_zone)
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 @@ def inspect
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 @@ def <=>(other)
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 @@ def to_f
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
@@ -86,6 +86,10 @@ def test_xmlschema
assert_equal "1999-12-31T19:00:00-05:00", @twz.xmlschema
end
+ def test_to_yaml
+ assert_equal "--- 1999-12-31 19:00:00 -05:00\n", @twz.to_yaml
+ end
+
def test_httpdate
assert_equal 'Sat, 01 Jan 2000 00:00:00 GMT', @twz.httpdate
end
@@ -111,6 +115,11 @@ def test_compare_with_time_with_zone
assert_equal 0, @twz <=> ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1, 0, 0, 0), TimeZone['UTC'] )
assert_equal(-1, @twz <=> ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1, 0, 0, 1), TimeZone['UTC'] ))
end
+
+ def test_eql?
+ assert @twz.eql?(Time.utc(2000))
+ assert @twz.eql?( ActiveSupport::TimeWithZone.new(Time.utc(2000), TimeZone["Hawaii"]) )
+ end
def test_plus
assert_equal Time.utc(1999, 12, 31, 19, 0 ,5), (@twz + 5).time
@@ -158,6 +167,10 @@ def test_to_i
def test_to_time
assert_equal @twz, @twz.to_time
end
+
+ def test_to_datetime
+ assert_equal DateTime.civil(1999, 12, 31, 19, 0, 0, Rational(-18_000, 86_400)), @twz.to_datetime
+ end
def test_acts_like_time
assert @twz.acts_like?(:time)

0 comments on commit 6608a3e

Please sign in to comment.