Changeset 8854
- Timestamp:
- 02/10/08 22:26:16 (5 months ago)
- Files:
-
- trunk/activesupport/CHANGELOG (modified) (1 diff)
- trunk/activesupport/lib/active_support/time_with_zone.rb (modified) (5 diffs)
- trunk/activesupport/test/core_ext/time_with_zone_test.rb (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/activesupport/CHANGELOG
r8853 r8854 1 1 *SVN* 2 3 * Adding TimeWithZone #to_yaml, #to_datetime, #eql? and method aliases for duck-typing compatibility with Time [Geoff Buesing] 2 4 3 5 * TimeWithZone #in_time_zone returns +self+ if zone argument is the same as #time_zone [Geoff Buesing] trunk/activesupport/lib/active_support/time_with_zone.rb
r8853 r8854 22 22 end 23 23 alias_method :comparable_time, :utc 24 alias_method :getgm, :utc 25 alias_method :getutc, :utc 26 alias_method :gmtime, :utc 24 27 25 28 # Returns the underlying TZInfo::TimezonePeriod for the local time … … 48 51 utc.getlocal 49 52 end 53 alias_method :getlocal, :localtime 50 54 51 55 def dst? 52 56 period.dst? 53 57 end 58 alias_method :isdst, :dst? 54 59 55 60 def utc? 56 61 time_zone.name == 'UTC' 57 62 end 63 alias_method :gmt?, :utc? 58 64 59 65 def utc_offset 60 66 period.utc_total_offset 61 67 end 68 alias_method :gmt_offset, :utc_offset 69 alias_method :gmtoff, :utc_offset 62 70 63 71 def formatted_offset(colon = true, alternate_utc_string = nil) … … 77 85 "#{time.strftime("%Y-%m-%dT%H:%M:%S")}#{formatted_offset(true, 'Z')}" 78 86 end 87 alias_method :iso8601, :xmlschema 79 88 80 89 def to_json(options = nil) 81 90 %("#{time.strftime("%Y/%m/%d %H:%M:%S")} #{formatted_offset(false)}") 91 end 92 93 def to_yaml(options = {}) 94 time.to_yaml(options).gsub('Z', formatted_offset(true, 'Z')) 82 95 end 83 96 … … 113 126 end 114 127 128 def eql?(other) 129 utc == other 130 end 131 115 132 # Need to override #- to intercept situation where a Time or Time With Zone object is passed in 116 133 # Otherwise, just pass on to method missing … … 130 147 utc.to_i 131 148 end 149 alias_method :hash, :to_i 150 alias_method :tv_sec, :to_i 132 151 133 152 # A TimeProxy acts like a Time, so just return self 134 153 def to_time 135 154 self 155 end 156 157 def to_datetime 158 utc.to_datetime.new_offset(Rational(utc_offset, 86_400)) 136 159 end 137 160 trunk/activesupport/test/core_ext/time_with_zone_test.rb
r8853 r8854 87 87 end 88 88 89 def test_to_yaml 90 assert_equal "--- 1999-12-31 19:00:00 -05:00\n", @twz.to_yaml 91 end 92 89 93 def test_httpdate 90 94 assert_equal 'Sat, 01 Jan 2000 00:00:00 GMT', @twz.httpdate … … 112 116 assert_equal(-1, @twz <=> ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1, 0, 0, 1), TimeZone['UTC'] )) 113 117 end 118 119 def test_eql? 120 assert @twz.eql?(Time.utc(2000)) 121 assert @twz.eql?( ActiveSupport::TimeWithZone.new(Time.utc(2000), TimeZone["Hawaii"]) ) 122 end 114 123 115 124 def test_plus … … 158 167 def test_to_time 159 168 assert_equal @twz, @twz.to_time 169 end 170 171 def test_to_datetime 172 assert_equal DateTime.civil(1999, 12, 31, 19, 0, 0, Rational(-18_000, 86_400)), @twz.to_datetime 160 173 end 161 174