Changeset 9042
- Timestamp:
- 03/17/08 03:45:32 (4 months ago)
- Files:
-
- trunk/activesupport/CHANGELOG (modified) (1 diff)
- trunk/activesupport/lib/active_support/core_ext/date_time/conversions.rb (modified) (1 diff)
- trunk/activesupport/lib/active_support/time_with_zone.rb (modified) (1 diff)
- trunk/activesupport/lib/active_support/values/time_zone.rb (modified) (3 diffs)
- trunk/activesupport/test/core_ext/date_time_ext_test.rb (modified) (1 diff)
- trunk/activesupport/test/time_zone_test.rb (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/activesupport/CHANGELOG
r9041 r9042 1 1 *SVN* 2 3 * Adding TimeZone#at and DateTime#to_f [Geoff Buesing] 2 4 3 5 * TimeWithZone responds to Ruby 1.9 weekday-named query methods [Geoff Buesing] trunk/activesupport/lib/active_support/core_ext/date_time/conversions.rb
r8705 r9042 79 79 strftime("%Y-%m-%dT%H:%M:%S%Z") 80 80 end if RUBY_VERSION < '1.9' 81 82 # Converts self to a floating-point number of seconds since the Unix epoch 83 def to_f 84 days_since_unix_epoch = self - ::DateTime.civil(1970) 85 (days_since_unix_epoch * 86_400).to_f 86 end 81 87 end 82 88 end trunk/activesupport/lib/active_support/time_with_zone.rb
r9041 r9042 161 161 162 162 def to_a 163 time.to_a[0, 8].push(dst?, zone)163 [time.sec, time.min, time.hour, time.day, time.mon, time.year, time.wday, time.yday, dst?, zone] 164 164 end 165 165 trunk/activesupport/lib/active_support/values/time_zone.rb
r9040 r9042 179 179 require_library_or_gem "tzinfo" unless Object.const_defined?(:TZInfo) 180 180 181 # Method for creating new ActiveSupport::TimeWithZone instance in time zone of +self+ . Example:181 # Method for creating new ActiveSupport::TimeWithZone instance in time zone of +self+ from given values. Example: 182 182 # 183 183 # Time.zone = "Hawaii" # => "Hawaii" … … 186 186 time = Time.utc_time(*args) 187 187 ActiveSupport::TimeWithZone.new(nil, self, time) 188 end 189 190 # Method for creating new ActiveSupport::TimeWithZone instance in time zone of +self+ from number of seconds since the Unix epoch. Example: 191 # 192 # Time.zone = "Hawaii" # => "Hawaii" 193 # Time.utc(2000).to_f # => 946684800.0 194 # Time.zone.at(946684800.0) # => Fri, 31 Dec 1999 14:00:00 HST -10:00 195 def at(secs) 196 utc = Time.at(secs).utc rescue DateTime.civil(1970).since(secs) 197 utc.in_time_zone(self) 188 198 end 189 199 … … 235 245 rescue LoadError # Tzinfo gem is not available 236 246 # re-raise LoadError only when a tzinfo-dependent method is called: 237 %w(local now today utc_to_local local_to_utc period_for_utc period_for_local tzinfo).each do |method|247 %w(local at now today utc_to_local local_to_utc period_for_utc period_for_local tzinfo).each do |method| 238 248 define_method(method) {|*args| raise LoadError, "TZInfo gem is required for TimeZone##{method}. `gem install tzinfo` and try again."} 239 249 end trunk/activesupport/test/core_ext/date_time_ext_test.rb
r8731 r9042 272 272 assert_equal(-1, DateTime.civil(2000) <=> ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1, 0, 0, 1), TimeZone['UTC'] )) 273 273 end 274 275 def test_to_f 276 assert_equal 946684800.0, DateTime.civil(2000).to_f 277 assert_equal 946684800.0, DateTime.civil(1999,12,31,19,0,0,Rational(-5,24)).to_f 278 end 274 279 275 280 protected trunk/activesupport/test/time_zone_test.rb
r9009 r9042 186 186 assert_equal 'EDT', twz.zone 187 187 end 188 189 def test_at 190 zone = TimeZone['Eastern Time (US & Canada)'] 191 secs = 946684800.0 192 twz = zone.at(secs) 193 assert_equal Time.utc(1999,12,31,19), twz.time 194 assert_equal Time.utc(2000), twz.utc 195 assert_equal zone, twz.time_zone 196 assert_equal secs, twz.to_f 197 end 198 199 def test_at_with_old_date 200 zone = TimeZone['UTC'] 201 secs = -3786825600.0 202 twz = zone.at(secs) 203 assert_equal [0,0,0,1,1,1850], twz.to_a[0,6] 204 assert_equal zone, twz.time_zone 205 assert_equal secs, twz.to_f 206 end 188 207 189 208 protected